|
| 1 | +import { createClient } from '@supabase/supabase-js'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { parse } from 'csv-parse/sync'; |
| 5 | + |
| 6 | +// Use provided credentials |
| 7 | +const SUPABASE_URL = 'https://qqceibvalkoytafynwoc.supabase.co'; |
| 8 | +const SUPABASE_SERVICE_ROLE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InFxY2VpYnZhbGtveXRhZnlud29jIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc3MDMzNzU5NSwiZXhwIjoyMDg1OTEzNTk1fQ.bMm74y-fjlz11_pkayGyo5ho2Mgzqfrh-ZNgPTFYxGY'; |
| 9 | + |
| 10 | +const supabase = createClient(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY); |
| 11 | + |
| 12 | +function mapCategory(productType, tags, title) { |
| 13 | + const searchStr = `${productType} ${tags} ${title}`.toLowerCase(); |
| 14 | + |
| 15 | + if (searchStr.includes('sunscreen') || searchStr.includes('spf') || searchStr.includes('sun protection')) return 'Sun Protection (SPF)'; |
| 16 | + if (searchStr.includes('cleanser') || searchStr.includes('toner') || searchStr.includes('wash')) return 'Cleansers & Toners'; |
| 17 | + if (searchStr.includes('serum') || searchStr.includes('active') || searchStr.includes('ampoule')) return 'Clinical Serums & Actives'; |
| 18 | + if (searchStr.includes('moisturizer') || searchStr.includes('cream') || searchStr.includes('barrier') || searchStr.includes('hydration')) return 'Daily Hydration & Barrier'; |
| 19 | + if (searchStr.includes('makeup') || searchStr.includes('foundation') || searchStr.includes('concealer') || searchStr.includes('lipstick') || searchStr.includes('mascara') || searchStr.includes('primer')) return 'Evening Radiance & Glamour'; |
| 20 | + if (searchStr.includes('treatment') || searchStr.includes('mask') || searchStr.includes('peel') || searchStr.includes('spot')) return 'Targeted Treatments'; |
| 21 | + if (searchStr.includes('hair') || searchStr.includes('shampoo') || searchStr.includes('conditioner')) return 'Hair Care'; |
| 22 | + if (searchStr.includes('fragrance') || searchStr.includes('perfume') || searchStr.includes('cologne')) return 'Fragrance'; |
| 23 | + if (searchStr.includes('body') || searchStr.includes('lotion') || searchStr.includes('shower') || searchStr.includes('hand')) return 'Body Care'; |
| 24 | + |
| 25 | + return 'Requires_Manual_Review'; |
| 26 | +} |
| 27 | + |
| 28 | +function mapConcern(title, type) { |
| 29 | + const s = `${title} ${type}`.toLowerCase(); |
| 30 | + if (s.includes('acne') || s.includes('blemish')) return 'Concern_Acne'; |
| 31 | + if (s.includes('age') || s.includes('wrinkle') || s.includes('firming')) return 'Concern_Aging'; |
| 32 | + if (s.includes('bright') || s.includes('whitening') || s.includes('glow')) return 'Concern_Brightening'; |
| 33 | + if (s.includes('dry') || s.includes('hydrate') || s.includes('moist')) return 'Concern_Hydration'; |
| 34 | + if (s.includes('sensitive') || s.includes('soothe') || s.includes('calm')) return 'Concern_Sensitivity'; |
| 35 | + if (s.includes('oil') || s.includes('pore') || s.includes('matte')) return 'Concern_Oiliness'; |
| 36 | + if (s.includes('sun') || s.includes('spf')) return 'Concern_SunProtection'; |
| 37 | + if (s.includes('pigment') || s.includes('dark spot')) return 'Concern_Pigmentation'; |
| 38 | + return 'Concern_Hydration'; // Default |
| 39 | +} |
| 40 | + |
| 41 | +function mapStep(title, type) { |
| 42 | + const s = `${title} ${type}`.toLowerCase(); |
| 43 | + if (s.includes('cleanser') || s.includes('wash') || s.includes('toner') || s.includes('soap')) return 'Step_1_Cleanser'; |
| 44 | + if (s.includes('serum') || s.includes('active') || s.includes('treatment') || s.includes('ampoule') || s.includes('mask')) return 'Step_2_Treatment'; |
| 45 | + if (s.includes('spf') || s.includes('sunscreen') || s.includes('cream') || s.includes('moisturizer') || s.includes('lotion')) return 'Step_3_Protection'; |
| 46 | + return 'Step_2_Treatment'; // Default |
| 47 | +} |
| 48 | + |
| 49 | +async function run() { |
| 50 | + console.log('🚀 Starting Catalog Re-organization (with required fields)...'); |
| 51 | + |
| 52 | + const csvPath = path.resolve(process.cwd(), 'Orgnized Products/Asper_Catalog_FINAL_READY - Copy.csv'); |
| 53 | + const fileContent = fs.readFileSync(csvPath, 'utf8'); |
| 54 | + const records = parse(fileContent, { |
| 55 | + columns: true, |
| 56 | + skip_empty_lines: true, |
| 57 | + relax_column_count: true |
| 58 | + }); |
| 59 | + |
| 60 | + const products = []; |
| 61 | + const seenHandles = new Set(); |
| 62 | + |
| 63 | + for (const record of records) { |
| 64 | + const handle = record.handle; |
| 65 | + if (!handle || seenHandles.has(handle)) continue; |
| 66 | + seenHandles.add(handle); |
| 67 | + |
| 68 | + const title = record.title; |
| 69 | + const productType = record.productType || ''; |
| 70 | + const tags = record['tags/0'] || ''; |
| 71 | + const price = parseFloat(record['variants/0/price']) || 0; |
| 72 | + const brand = record.vendor || ''; |
| 73 | + const image_url = record['images/0/src'] || ''; |
| 74 | + |
| 75 | + products.push({ |
| 76 | + handle, |
| 77 | + title, |
| 78 | + brand, |
| 79 | + price, |
| 80 | + image_url, |
| 81 | + asper_category: mapCategory(productType, tags, title), |
| 82 | + primary_concern: mapConcern(title, productType), |
| 83 | + regimen_step: mapStep(title, productType), |
| 84 | + inventory_total: parseInt(record['variants/0/inventoryQuantity']) || 0, |
| 85 | + updated_at: new Date().toISOString() |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + console.log(`✅ Processed ${products.length} products. Updating Supabase...`); |
| 90 | + |
| 91 | + const CHUNK_SIZE = 50; |
| 92 | + for (let i = 0; i < products.length; i += CHUNK_SIZE) { |
| 93 | + const chunk = products.slice(i, i + CHUNK_SIZE); |
| 94 | + const { error } = await supabase |
| 95 | + .from('products') |
| 96 | + .upsert(chunk, { onConflict: 'handle' }); |
| 97 | + |
| 98 | + if (error) { |
| 99 | + console.error(`❌ Error in chunk ${i / CHUNK_SIZE + 1}:`, error.message); |
| 100 | + // break; // Uncomment to stop on first error |
| 101 | + } else { |
| 102 | + process.stdout.write(`✔ Updated ${Math.min(i + CHUNK_SIZE, products.length)} / ${products.length} products...\r`); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + console.log('\n🏁 Re-organization complete!'); |
| 107 | +} |
| 108 | + |
| 109 | +run(); |
0 commit comments