/** * Supabase Lead Submission * * Helper function to submit lead data to Supabase. * Requires @supabase/supabase-js as peer dependency. */ import type { SupabaseClient } from '@supabase/supabase-js'; import type { GatingLeadData, StorageConfig } from '../types/gating'; /** * Submit lead data to Supabase * * @param supabaseClient - Initialized Supabase client * @param leadData - Lead data from gating form * @param storageConfig - Storage configuration * @throws Error if submission fails * * @example * ```typescript * import { createClient } from '@supabase/supabase-js'; * import { submitLeadToSupabase } from '@buzztrail-ai/gating'; * * const supabase = createClient(url, key); * await submitLeadToSupabase(supabase, leadData, storageConfig); * ``` */ export async function submitLeadToSupabase( supabaseClient: SupabaseClient, leadData: GatingLeadData, storageConfig: StorageConfig ): Promise { const tableName = storageConfig.supabaseTable || 'buzztrail_leads'; // Insert lead data const { error } = await supabaseClient.from(tableName).insert({ customer_id: leadData.customerId, name: leadData.name || null, email: leadData.email || null, phone: leadData.phone || null, company: leadData.company || null, language: leadData.language, consented: leadData.consented, submitted_at: leadData.submittedAt, created_at: new Date().toISOString(), }); if (error) { throw new Error(`Failed to submit lead to Supabase: ${error.message}`); } } /** * Submit lead data to webhook * * @param webhookUrl - Webhook URL to POST to * @param leadData - Lead data from gating form * @throws Error if submission fails * * @example * ```typescript * await submitLeadToWebhook('https://api.example.com/leads', leadData); * ``` */ export async function submitLeadToWebhook( webhookUrl: string, leadData: GatingLeadData ): Promise { const response = await fetch(webhookUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(leadData), }); if (!response.ok) { const errorText = await response.text(); throw new Error( `Failed to submit lead to webhook: ${response.status} ${errorText}` ); } } /** * Submit lead data according to storage configuration * * Handles supabase, webhook, or both destinations. * * @param supabaseClient - Initialized Supabase client (required if destination includes 'supabase') * @param leadData - Lead data from gating form * @param storageConfig - Storage configuration * @throws Error if submission fails */ export async function submitLead( supabaseClient: SupabaseClient | null, leadData: GatingLeadData, storageConfig: StorageConfig ): Promise { const { destination } = storageConfig; // Submit to Supabase if (destination === 'supabase' || destination === 'both') { if (!supabaseClient) { throw new Error('Supabase client is required when destination includes "supabase"'); } await submitLeadToSupabase(supabaseClient, leadData, storageConfig); } // Submit to webhook if (destination === 'webhook' || destination === 'both') { if (!storageConfig.webhookUrl) { throw new Error('Webhook URL is required when destination includes "webhook"'); } await submitLeadToWebhook(storageConfig.webhookUrl, leadData); } }