Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-20 00:27:43 +00:00
co-authored by renee-png
parent 1c06b930a0
commit b456315b0f
@@ -0,0 +1,30 @@
-- Function: ensure a case with the Collections practice area has at least one collection row.
CREATE OR REPLACE FUNCTION public.tg_ensure_collection_for_collections_case()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
BEGIN
IF NEW.practice_area = 'Community Ass''n Law - Collections' THEN
IF NOT EXISTS (SELECT 1 FROM public.collections WHERE case_id = NEW.id) THEN
INSERT INTO public.collections (case_id, status, opened_at, created_by)
VALUES (NEW.id, 'late_notice', COALESCE(NEW.opened_at, CURRENT_DATE), NEW.created_by);
END IF;
END IF;
RETURN NEW;
END;
$$;
DROP TRIGGER IF EXISTS trg_ensure_collection_for_collections_case ON public.cases;
CREATE TRIGGER trg_ensure_collection_for_collections_case
AFTER INSERT OR UPDATE OF practice_area ON public.cases
FOR EACH ROW
EXECUTE FUNCTION public.tg_ensure_collection_for_collections_case();
-- One-time backfill for existing cases.
INSERT INTO public.collections (case_id, status, opened_at, created_by)
SELECT c.id, 'late_notice', COALESCE(c.opened_at, CURRENT_DATE), c.created_by
FROM public.cases c
WHERE c.practice_area = 'Community Ass''n Law - Collections'
AND NOT EXISTS (SELECT 1 FROM public.collections col WHERE col.case_id = c.id);