Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-22 17:50:31 +00:00
co-authored by renee-png
parent 36b653fd06
commit f478e98755
2 changed files with 98 additions and 0 deletions
@@ -0,0 +1,50 @@
-- Per-client overrides for fee schedule expense items
CREATE TABLE public.client_fee_overrides (
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
client_id UUID NOT NULL REFERENCES public.clients(id) ON DELETE CASCADE,
fee_item_id UUID NOT NULL REFERENCES public.fee_schedule_items(id) ON DELETE CASCADE,
amount NUMERIC NOT NULL DEFAULT 0,
enabled BOOLEAN NOT NULL DEFAULT true,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
created_by UUID,
UNIQUE (client_id, fee_item_id)
);
CREATE INDEX idx_client_fee_overrides_client ON public.client_fee_overrides(client_id);
ALTER TABLE public.client_fee_overrides ENABLE ROW LEVEL SECURITY;
CREATE POLICY "cfo_select_auth" ON public.client_fee_overrides
FOR SELECT TO authenticated USING (true);
CREATE POLICY "cfo_insert_auth" ON public.client_fee_overrides
FOR INSERT TO authenticated
WITH CHECK (
is_admin(auth.uid()) OR EXISTS (
SELECT 1 FROM public.clients c
WHERE c.id = client_fee_overrides.client_id AND c.created_by = auth.uid()
)
);
CREATE POLICY "cfo_update_auth" ON public.client_fee_overrides
FOR UPDATE TO authenticated
USING (
is_admin(auth.uid()) OR EXISTS (
SELECT 1 FROM public.clients c
WHERE c.id = client_fee_overrides.client_id AND c.created_by = auth.uid()
)
);
CREATE POLICY "cfo_delete_auth" ON public.client_fee_overrides
FOR DELETE TO authenticated
USING (
is_admin(auth.uid()) OR EXISTS (
SELECT 1 FROM public.clients c
WHERE c.id = client_fee_overrides.client_id AND c.created_by = auth.uid()
)
);
CREATE TRIGGER trg_cfo_updated_at
BEFORE UPDATE ON public.client_fee_overrides
FOR EACH ROW EXECUTE FUNCTION public.tg_set_updated_at();