/** * Sync GoHighLevel contacts/opportunities → warehouse. * npx tsx scripts/sync_ghl.ts * TENANT_SLUG=delgado-abogados npx tsx scripts/sync_ghl.ts */ import { config } from 'dotenv'; import { resolve } from 'path'; config({ path: resolve(process.cwd(), '.env.local') }); import { syncAllGhlTenants } from '../src/lib/gohighlevel-sync'; import { serviceHeaders, supabaseUrl } from '../src/lib/agency'; async function main() { const slug = process.env.TENANT_SLUG?.trim(); let tenantId: string | undefined; if (slug) { const headers = serviceHeaders(); const base = supabaseUrl(); if (!headers || !base) throw new Error('no service role'); const r = await fetch( `${base}/rest/v1/tenants?slug=eq.${encodeURIComponent(slug)}&select=id,slug`, { headers, cache: 'no-store' } ); const rows = (await r.json()) as { id: string; slug: string }[]; if (!rows[0]) throw new Error(`tenant ${slug} not found`); tenantId = rows[0].id; } const out = await syncAllGhlTenants({ tenantId }); console.log(JSON.stringify(out, null, 2)); if (out.results.some((x) => x.error)) process.exit(2); } main().catch((e) => { console.error(e); process.exit(1); });