From 247eeb959755db0c7cc28e9750525193a5f68a7c Mon Sep 17 00:00:00 2001 From: root Date: Tue, 24 Feb 2026 11:37:20 +0000 Subject: [PATCH] Fix leads API null handling --- app/api/leads/route.ts | 10 +++++----- supabase-leads-fix.sql | 9 +++++++++ supabase-leads.sql | 23 +++++++++-------------- 3 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 supabase-leads-fix.sql diff --git a/app/api/leads/route.ts b/app/api/leads/route.ts index e5d15b9..11011e6 100644 --- a/app/api/leads/route.ts +++ b/app/api/leads/route.ts @@ -23,12 +23,12 @@ export async function POST(request: NextRequest) { const lead = { name: body.name, - business_name: body.businessName || body.business_name, - phone: body.phone, - email: body.email, - source: body.source, + business_name: body.businessName || body.business_name || null, + phone: body.phone || null, + email: body.email || null, + source: body.source || null, status: body.status || 'new', - notes: body.notes, + notes: body.notes || null, } const { data, error } = await supabase diff --git a/supabase-leads-fix.sql b/supabase-leads-fix.sql new file mode 100644 index 0000000..885e430 --- /dev/null +++ b/supabase-leads-fix.sql @@ -0,0 +1,9 @@ +-- Add missing columns to existing leads table +ALTER TABLE leads ADD COLUMN IF NOT EXISTS source TEXT; +ALTER TABLE leads ADD COLUMN IF NOT EXISTS email TEXT; + +-- Add sample leads +INSERT INTO leads (name, business_name, phone, status, source) VALUES +('Juan', 'Restaurante La Niña', '+34 952 449 193', 'new', 'cold_call'), +('Maria', 'Restaurante Trocadero', '+34 681 142 944', 'new', 'cold_call'), +('Carlos', 'Restaurant No7', '+34 655 036 827', 'new', 'cold_call'); diff --git a/supabase-leads.sql b/supabase-leads.sql index 6e91eb7..258d01c 100644 --- a/supabase-leads.sql +++ b/supabase-leads.sql @@ -1,6 +1,4 @@ --- Leads Table for Supabase --- Run in SQL Editor - +-- Create leads table CREATE TABLE IF NOT EXISTS leads ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), user_id TEXT DEFAULT 'default', @@ -9,19 +7,16 @@ CREATE TABLE IF NOT EXISTS leads ( phone TEXT, email TEXT, source TEXT, - status TEXT DEFAULT 'new' CHECK (status IN ('new', 'contacted', 'qualified', 'won', 'lost')), + status TEXT DEFAULT 'new', notes TEXT, created_at TIMESTAMPTZ DEFAULT NOW() ); -CREATE INDEX IF NOT EXISTS idx_leads_user ON leads(user_id); -CREATE INDEX IF NOT EXISTS idx_leads_status ON leads(status); - --- Sample leads -INSERT INTO leads (name, business_name, phone, status, source) VALUES -('Juan García', 'Restaurante La Niña', '+34 952 449 193', 'new', 'cold_call'), -('María López', 'Clínica Dental Málaga', '+34 951 123 456', 'contacted', 'website'), -('Carlos Ruiz', 'Inmobiliaria Costa', '+34 600 123 456', 'qualified', 'referral'); - --- Disable RLS for now +-- Disable RLS ALTER TABLE leads DISABLE ROW LEVEL SECURITY; + +-- Add sample leads +INSERT INTO leads (name, business_name, phone, status, source) VALUES +('Juan', 'Restaurante La Niña', '+34 952 449 193', 'new', 'cold_call'), +('Maria', 'Restaurante Trocadero', '+34 681 142 944', 'new', 'cold_call'), +('Carlos', 'Restaurant No7', '+34 655 036 827', 'new', 'cold_call');