Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-19 17:54:53 +00:00
co-authored by renee-png
parent 5fc84b98c1
commit eaa036ab98
2 changed files with 136 additions and 0 deletions
@@ -0,0 +1,62 @@
-- Categories: list of archive types (cases, time, expenses, plus user-added)
CREATE TABLE public.archive_categories (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
key text NOT NULL UNIQUE,
label text NOT NULL,
sort_order integer NOT NULL DEFAULT 0,
active boolean NOT NULL DEFAULT true,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
created_by uuid
);
ALTER TABLE public.archive_categories ENABLE ROW LEVEL SECURITY;
CREATE POLICY "ac_select_auth" ON public.archive_categories
FOR SELECT TO authenticated USING (true);
CREATE POLICY "ac_insert_admin" ON public.archive_categories
FOR INSERT TO authenticated WITH CHECK (public.is_admin(auth.uid()));
CREATE POLICY "ac_update_admin" ON public.archive_categories
FOR UPDATE TO authenticated USING (public.is_admin(auth.uid()));
CREATE POLICY "ac_delete_admin" ON public.archive_categories
FOR DELETE TO authenticated USING (public.is_admin(auth.uid()));
CREATE TRIGGER trg_ac_updated BEFORE UPDATE ON public.archive_categories
FOR EACH ROW EXECUTE FUNCTION public.tg_set_updated_at();
-- Records: one row per CSV row, columns preserved verbatim in `data` JSONB
CREATE TABLE public.archive_records (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
category_id uuid NOT NULL REFERENCES public.archive_categories(id) ON DELETE CASCADE,
data jsonb NOT NULL DEFAULT '{}'::jsonb,
source_filename text,
batch_id uuid,
row_index integer,
created_at timestamptz NOT NULL DEFAULT now(),
created_by uuid
);
CREATE INDEX idx_archive_records_category ON public.archive_records(category_id, created_at DESC);
CREATE INDEX idx_archive_records_batch ON public.archive_records(batch_id);
ALTER TABLE public.archive_records ENABLE ROW LEVEL SECURITY;
CREATE POLICY "ar_select_auth" ON public.archive_records
FOR SELECT TO authenticated USING (true);
CREATE POLICY "ar_insert_admin" ON public.archive_records
FOR INSERT TO authenticated WITH CHECK (public.is_admin(auth.uid()));
CREATE POLICY "ar_delete_admin" ON public.archive_records
FOR DELETE TO authenticated USING (public.is_admin(auth.uid()));
-- Seed default categories
INSERT INTO public.archive_categories (key, label, sort_order) VALUES
('cases', 'Cases', 10),
('time_entries', 'Time Entries', 20),
('expenses', 'Expenses', 30),
('invoices', 'Invoices', 40),
('clients', 'Clients', 50),
('status_updates', 'Status Updates', 60),
('contacts', 'Contacts', 70),
('homeowners', 'Homeowners', 80),
('call_logs', 'Call Logs', 90),
('documents', 'Documents', 100);