Changes
Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
co-authored by
renee-png
parent
aa24b0c843
commit
6041e3ca9c
@@ -0,0 +1,82 @@
|
||||
-- Documents templates and generated docs
|
||||
|
||||
CREATE TABLE public.document_templates (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name text NOT NULL,
|
||||
description text,
|
||||
kind text NOT NULL DEFAULT 'general', -- 'general' | 'pleading'
|
||||
body text NOT NULL DEFAULT '',
|
||||
fields jsonb NOT NULL DEFAULT '[]'::jsonb, -- [{key, label, type}]
|
||||
font_family text NOT NULL DEFAULT 'Bookman Old Style',
|
||||
font_size_pt integer NOT NULL DEFAULT 12,
|
||||
created_by uuid,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
ALTER TABLE public.document_templates ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
CREATE POLICY "doc_templates_select_auth" ON public.document_templates
|
||||
FOR SELECT TO authenticated USING (true);
|
||||
CREATE POLICY "doc_templates_insert_auth" ON public.document_templates
|
||||
FOR INSERT TO authenticated WITH CHECK (auth.uid() IS NOT NULL);
|
||||
CREATE POLICY "doc_templates_update_owner" ON public.document_templates
|
||||
FOR UPDATE TO authenticated USING (is_admin(auth.uid()) OR created_by = auth.uid());
|
||||
CREATE POLICY "doc_templates_delete_owner" ON public.document_templates
|
||||
FOR DELETE TO authenticated USING (is_admin(auth.uid()) OR created_by = auth.uid());
|
||||
|
||||
CREATE TRIGGER trg_doc_templates_updated
|
||||
BEFORE UPDATE ON public.document_templates
|
||||
FOR EACH ROW EXECUTE FUNCTION public.tg_set_updated_at();
|
||||
|
||||
CREATE TABLE public.generated_documents (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name text NOT NULL,
|
||||
template_id uuid REFERENCES public.document_templates(id) ON DELETE SET NULL,
|
||||
kind text NOT NULL DEFAULT 'general',
|
||||
payload jsonb NOT NULL DEFAULT '{}'::jsonb,
|
||||
body text,
|
||||
storage_path text,
|
||||
created_by uuid,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
ALTER TABLE public.generated_documents ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
CREATE POLICY "gen_docs_select_owner" ON public.generated_documents
|
||||
FOR SELECT TO authenticated USING (is_admin(auth.uid()) OR created_by = auth.uid());
|
||||
CREATE POLICY "gen_docs_insert_auth" ON public.generated_documents
|
||||
FOR INSERT TO authenticated WITH CHECK (auth.uid() IS NOT NULL);
|
||||
CREATE POLICY "gen_docs_update_owner" ON public.generated_documents
|
||||
FOR UPDATE TO authenticated USING (is_admin(auth.uid()) OR created_by = auth.uid());
|
||||
CREATE POLICY "gen_docs_delete_owner" ON public.generated_documents
|
||||
FOR DELETE TO authenticated USING (is_admin(auth.uid()) OR created_by = auth.uid());
|
||||
|
||||
CREATE TRIGGER trg_gen_docs_updated
|
||||
BEFORE UPDATE ON public.generated_documents
|
||||
FOR EACH ROW EXECUTE FUNCTION public.tg_set_updated_at();
|
||||
|
||||
-- Storage bucket for generated docs
|
||||
INSERT INTO storage.buckets (id, name, public) VALUES ('generated-documents', 'generated-documents', false)
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
CREATE POLICY "gen_docs_storage_select" ON storage.objects
|
||||
FOR SELECT TO authenticated
|
||||
USING (bucket_id = 'generated-documents' AND auth.uid()::text = (storage.foldername(name))[1]);
|
||||
CREATE POLICY "gen_docs_storage_insert" ON storage.objects
|
||||
FOR INSERT TO authenticated
|
||||
WITH CHECK (bucket_id = 'generated-documents' AND auth.uid()::text = (storage.foldername(name))[1]);
|
||||
CREATE POLICY "gen_docs_storage_delete" ON storage.objects
|
||||
FOR DELETE TO authenticated
|
||||
USING (bucket_id = 'generated-documents' AND auth.uid()::text = (storage.foldername(name))[1]);
|
||||
|
||||
-- Seed default Pleading template
|
||||
INSERT INTO public.document_templates (name, kind, description, body, fields)
|
||||
VALUES (
|
||||
'Florida Pleading',
|
||||
'pleading',
|
||||
'Standard Florida court pleading with caption header.',
|
||||
'',
|
||||
'[]'::jsonb
|
||||
);
|
||||
Reference in New Issue
Block a user