Files
flux/supabase/migrations/20261202_customer_orders.sql
T
brontomark 6d2c563ce2
Build and Release FLUX (Multi-Platform) / build-web (push) Successful in 1m37s
Build and Release FLUX (Multi-Platform) / build-linux (push) Successful in 1m38s
Build and Release FLUX (Multi-Platform) / build-macos (push) Successful in 2m24s
Build and Release FLUX (Multi-Platform) / build-windows (push) Successful in 10m50s
feat: add customer orders feature with management, forms, and PDF export
2026-12-02 18:26:45 +01:00

93 lines
3.4 KiB
PL/PgSQL

-- =========================================================================
-- MIGRAZIONE ORDINI CLIENTI, RECAPITO ALTERNATIVO & MODALITÀ AVVISO
-- =========================================================================
-- 1. Aggiunta recapito alternativo ai Clienti
ALTER TABLE public.customers
ADD COLUMN IF NOT EXISTS alternative_phone_number text DEFAULT '';
-- 2. Aggiunta modalità di avviso preferita ai Ticket di Assistenza
ALTER TABLE public.tickets
ADD COLUMN IF NOT EXISTS contact_preference text DEFAULT 'whatsapp_primary';
-- 3. Creazione tabella Ordini Clienti (customer_orders)
CREATE TABLE IF NOT EXISTS public.customer_orders (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
order_date timestamptz NOT NULL DEFAULT now(),
company_id uuid NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE,
store_id uuid REFERENCES public.stores(id) ON DELETE SET NULL,
created_by_id uuid REFERENCES public.staff_members(id) ON DELETE SET NULL,
customer_id uuid REFERENCES public.customers(id) ON DELETE SET NULL,
product_description text NOT NULL DEFAULT '',
quantity integer NOT NULL DEFAULT 1,
notes text NOT NULL DEFAULT '',
price numeric(10, 2) NOT NULL DEFAULT 0,
deposit numeric(10, 2) NOT NULL DEFAULT 0,
external_code text NOT NULL DEFAULT '',
status text NOT NULL DEFAULT 'to_order',
tags text[] NOT NULL DEFAULT '{}',
contact_preference text NOT NULL DEFAULT 'whatsapp_primary',
alternative_phone_number text,
customer_notified boolean NOT NULL DEFAULT false,
customer_notified_at timestamptz
);
-- Indici per filtri rapidi (azienda, stato, date, etichette GIN)
CREATE INDEX IF NOT EXISTS idx_customer_orders_company_status
ON public.customer_orders(company_id, status);
CREATE INDEX IF NOT EXISTS idx_customer_orders_order_date
ON public.customer_orders(company_id, order_date DESC);
CREATE INDEX IF NOT EXISTS idx_customer_orders_tags_gin
ON public.customer_orders USING GIN (tags);
-- 4. Collegamento allegati (attachments) agli Ordini Clienti
ALTER TABLE public.attachments
ADD COLUMN IF NOT EXISTS customer_order_id uuid REFERENCES public.customer_orders(id) ON DELETE CASCADE;
CREATE INDEX IF NOT EXISTS idx_attachments_customer_order_id
ON public.attachments(customer_order_id);
-- 5. Trigger automatico per updated_at su customer_orders
CREATE OR REPLACE FUNCTION public.set_customer_orders_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS trg_customer_orders_updated_at ON public.customer_orders;
CREATE TRIGGER trg_customer_orders_updated_at
BEFORE UPDATE ON public.customer_orders
FOR EACH ROW
EXECUTE FUNCTION public.set_customer_orders_updated_at();
-- 6. Row Level Security (RLS) per multi-tenancy aziendale
ALTER TABLE public.customer_orders ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "Accesso ordini per membri azienda" ON public.customer_orders;
CREATE POLICY "Accesso ordini per membri azienda"
ON public.customer_orders
FOR ALL
TO authenticated
USING (
company_id IN (
SELECT sm.company_id FROM public.staff_members sm WHERE sm.user_id = auth.uid()
UNION
SELECT c.id FROM public.companies c WHERE c.user_id = auth.uid()
)
)
WITH CHECK (
company_id IN (
SELECT sm.company_id FROM public.staff_members sm WHERE sm.user_id = auth.uid()
UNION
SELECT c.id FROM public.companies c WHERE c.user_id = auth.uid()
)
);