38913-vm/app-9xzmfic2e4g1/supabase/migrations/00007_lead_marketplace.sql
2026-03-04 12:43:13 +00:00

99 lines
3.4 KiB
PL/PgSQL

-- Lead Categories
CREATE TYPE public.lead_category AS ENUM ('balloon', 'atv', 'horse', 'hotel', 'restaurant', 'transfer', 'other');
-- Lead Status
CREATE TYPE public.lead_status AS ENUM ('new', 'assigned', 'contacted', 'converted', 'lost');
-- Operators Table
CREATE TABLE public.operators (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
name text NOT NULL,
category public.lead_category NOT NULL,
whatsapp text,
email text,
max_daily_leads int DEFAULT 10,
priority_score int DEFAULT 0,
active boolean DEFAULT true,
created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- Leads Table
CREATE TABLE public.leads (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
user_id uuid REFERENCES public.profiles(id) ON DELETE SET NULL,
trip_id uuid REFERENCES public.trips(id) ON DELETE SET NULL,
category public.lead_category NOT NULL,
budget_range text,
participants int DEFAULT 1,
preferred_date date,
status public.lead_status DEFAULT 'new'::public.lead_status,
score int DEFAULT 0,
assigned_operator_id uuid REFERENCES public.operators(id) ON DELETE SET NULL,
-- User details captured at lead point
full_name text,
phone text,
whatsapp_available boolean DEFAULT false,
email text,
metadata jsonb DEFAULT '{}'::jsonb,
created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- Lead Events Table (for Analytics)
CREATE TABLE public.lead_events (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
lead_id uuid REFERENCES public.leads(id) ON DELETE CASCADE,
event_type text NOT NULL, -- e.g., 'created', 'scored', 'assigned', 'notified', 'status_changed'
metadata jsonb DEFAULT '{}'::jsonb,
created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- Enable RLS
ALTER TABLE public.operators ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.leads ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.lead_events ENABLE ROW LEVEL SECURITY;
-- Operators Policies (Admins only for management)
CREATE POLICY "Admins can manage operators" ON public.operators
FOR ALL TO authenticated USING (is_admin(auth.uid()));
CREATE POLICY "Public can view active operators (for assignment logic check)" ON public.operators
FOR SELECT TO authenticated USING (active = true);
-- Leads Policies
CREATE POLICY "Users can view their own leads" ON public.leads
FOR SELECT TO authenticated USING (auth.uid() = user_id);
CREATE POLICY "Users can insert their own leads" ON public.leads
FOR INSERT TO authenticated WITH CHECK (auth.uid() = user_id OR auth.uid() IS NULL);
CREATE POLICY "Admins have full access to leads" ON public.leads
FOR ALL TO authenticated USING (is_admin(auth.uid()));
-- Lead Events Policies
CREATE POLICY "Admins have full access to lead events" ON public.lead_events
FOR ALL TO authenticated USING (is_admin(auth.uid()));
-- Functions
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_operators_updated_at
BEFORE UPDATE ON operators
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_leads_updated_at
BEFORE UPDATE ON leads
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();