Add Lead CRM API with Supabase
This commit is contained in:
@@ -0,0 +1,68 @@
|
|||||||
|
import { NextRequest, NextResponse } from 'next/server'
|
||||||
|
import { supabase } from '@/lib/supabase'
|
||||||
|
|
||||||
|
export async function GET() {
|
||||||
|
try {
|
||||||
|
const { data: leads, error } = await supabase
|
||||||
|
.from('leads')
|
||||||
|
.select('*')
|
||||||
|
.order('created_at', { ascending: false })
|
||||||
|
|
||||||
|
if (error) throw error
|
||||||
|
|
||||||
|
return NextResponse.json({ leads: leads || [] })
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Supabase error:', error)
|
||||||
|
return NextResponse.json({ leads: [], error: 'Failed to fetch' }, { status: 500 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function POST(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
const body = await request.json()
|
||||||
|
|
||||||
|
const lead = {
|
||||||
|
name: body.name,
|
||||||
|
business_name: body.businessName || body.business_name,
|
||||||
|
phone: body.phone,
|
||||||
|
email: body.email,
|
||||||
|
source: body.source,
|
||||||
|
status: body.status || 'new',
|
||||||
|
notes: body.notes,
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from('leads')
|
||||||
|
.insert([lead])
|
||||||
|
.select()
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
console.error('Supabase insert error:', error)
|
||||||
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true, lead: data?.[0] })
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error:', error)
|
||||||
|
return NextResponse.json({ error: 'Failed to save' }, { status: 500 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function PATCH(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
const body = await request.json()
|
||||||
|
const { id, ...updates } = body
|
||||||
|
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from('leads')
|
||||||
|
.update(updates)
|
||||||
|
.eq('id', id)
|
||||||
|
.select()
|
||||||
|
|
||||||
|
if (error) throw error
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true, lead: data?.[0] })
|
||||||
|
} catch (error) {
|
||||||
|
return NextResponse.json({ error: 'Failed to update' }, { status: 500 })
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
-- Leads Table for Supabase
|
||||||
|
-- Run in SQL Editor
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS leads (
|
||||||
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
|
user_id TEXT DEFAULT 'default',
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
business_name TEXT,
|
||||||
|
phone TEXT,
|
||||||
|
email TEXT,
|
||||||
|
source TEXT,
|
||||||
|
status TEXT DEFAULT 'new' CHECK (status IN ('new', 'contacted', 'qualified', 'won', 'lost')),
|
||||||
|
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
|
||||||
|
ALTER TABLE leads DISABLE ROW LEVEL SECURITY;
|
||||||
Reference in New Issue
Block a user