@fayz-ai/plugin-forms 0.10.2 → 0.10.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
export declare const MIGRATION_000_PLG_RENAME = "-- 000_plg_rename.sql \u2014 rename legacy forms tables to plg_forms_* for pools\n-- provisioned before the industry-pool rename. Guarded: fires only when the legacy\n-- name exists and the target does not, so fresh pools skip every branch.\n-- The core `documents` archetype table is NOT renamed \u2014 it stays public.documents.\nDO $$\nBEGIN\n IF to_regclass('public.frm_documents') IS NOT NULL AND to_regclass('public.plg_forms_documents') IS NULL THEN\n ALTER TABLE public.frm_documents RENAME TO plg_forms_documents;\n END IF;\n IF to_regclass('public.frm_templates') IS NOT NULL AND to_regclass('public.plg_forms_templates') IS NULL THEN\n ALTER TABLE public.frm_templates RENAME TO plg_forms_templates;\n END IF;\n IF to_regclass('public.frm_document_files') IS NOT NULL AND to_regclass('public.plg_forms_document_files') IS NULL THEN\n ALTER TABLE public.frm_document_files RENAME TO plg_forms_document_files;\n END IF;\n -- frm_categories: registry-declared (form-template categories); no base-table\n -- DDL ships in this plugin, but rename in place if a pool created one.\n IF to_regclass('public.frm_categories') IS NOT NULL AND to_regclass('public.plg_forms_categories') IS NULL THEN\n ALTER TABLE public.frm_categories RENAME TO plg_forms_categories;\n END IF;\nEND $$;\n";
|
|
2
|
-
export declare const MIGRATION_001_FRM_BASE = "-- ============================================================\n-- Custom Forms Plugin \u2014 Base Tables\n-- ============================================================\n\n-- plg_forms_templates: form template definitions (versioned)\nCREATE TABLE IF NOT EXISTS public.plg_forms_templates (\n id uuid PRIMARY KEY DEFAULT gen_random_uuid(),\n tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,\n name text NOT NULL,\n description text,\n category text NOT NULL DEFAULT 'general',\n version integer NOT NULL DEFAULT 1,\n is_current boolean NOT NULL DEFAULT true,\n parent_id uuid REFERENCES public.plg_forms_templates(id),\n schema jsonb NOT NULL DEFAULT '{\"fields\":[],\"layout\":{\"columns\":12}}',\n specialty text,\n tags text[] DEFAULT '{}',\n metadata jsonb DEFAULT '{}',\n is_active boolean NOT NULL DEFAULT true,\n is_deleted boolean NOT NULL DEFAULT false,\n created_by uuid,\n updated_by uuid,\n created_at timestamptz NOT NULL DEFAULT now(),\n updated_at timestamptz NOT NULL DEFAULT now()\n);\n\nALTER TABLE public.plg_forms_templates ENABLE ROW LEVEL SECURITY;\n\nCREATE INDEX IF NOT EXISTS idx_plg_forms_templates_tenant\n ON public.plg_forms_templates(tenant_id);\nCREATE INDEX IF NOT EXISTS idx_plg_forms_templates_parent\n ON public.plg_forms_templates(parent_id);\nCREATE INDEX IF NOT EXISTS idx_plg_forms_templates_category\n ON public.plg_forms_templates(tenant_id, category);\nCREATE INDEX IF NOT EXISTS idx_plg_forms_templates_current\n ON public.plg_forms_templates(tenant_id, is_current, is_active)\n WHERE is_current = true AND is_active = true AND is_deleted = false;\n\nDROP POLICY IF EXISTS \"plg_forms_templates_select\" ON public.plg_forms_templates;\nCREATE POLICY \"plg_forms_templates_select\" ON public.plg_forms_templates\n FOR SELECT TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_templates_insert\" ON public.plg_forms_templates;\nCREATE POLICY \"plg_forms_templates_insert\" ON public.plg_forms_templates\n FOR INSERT TO authenticated\n WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_templates_update\" ON public.plg_forms_templates;\nCREATE POLICY \"plg_forms_templates_update\" ON public.plg_forms_templates\n FOR UPDATE TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_templates_delete\" ON public.plg_forms_templates;\nCREATE POLICY \"plg_forms_templates_delete\" ON public.plg_forms_templates\n FOR DELETE TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\n\n-- plg_forms_documents: filled form instances\nCREATE TABLE IF NOT EXISTS public.plg_forms_documents (\n id uuid PRIMARY KEY DEFAULT gen_random_uuid(),\n tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,\n template_id uuid NOT NULL REFERENCES public.plg_forms_templates(id),\n person_id uuid REFERENCES public.people(id) ON DELETE SET NULL,\n title text,\n data jsonb NOT NULL DEFAULT '{}',\n status text NOT NULL DEFAULT 'draft',\n signed_at timestamptz,\n signed_by uuid,\n notes text,\n metadata jsonb DEFAULT '{}',\n is_deleted boolean NOT NULL DEFAULT false,\n created_by uuid,\n updated_by uuid,\n created_at timestamptz NOT NULL DEFAULT now(),\n updated_at timestamptz NOT NULL DEFAULT now()\n);\n\nALTER TABLE public.plg_forms_documents ENABLE ROW LEVEL SECURITY;\n\nCREATE INDEX IF NOT EXISTS idx_plg_forms_documents_tenant\n ON public.plg_forms_documents(tenant_id);\n-- person_id/status only exist in the pre-archetype shape this file creates;\n-- converted pools (salon) already carry the archetype extension shape\n-- (document_id PK, no person_id) \u2014 002 owns that shape, so guard these.\nDO $$\nBEGIN\n IF EXISTS (\n SELECT 1 FROM information_schema.columns\n WHERE table_schema = 'public' AND table_name = 'plg_forms_documents'\n AND column_name = 'person_id'\n ) THEN\n EXECUTE 'CREATE INDEX IF NOT EXISTS idx_plg_forms_documents_person\n ON public.plg_forms_documents(person_id)';\n EXECUTE 'CREATE INDEX IF NOT EXISTS idx_plg_forms_documents_status\n ON public.plg_forms_documents(tenant_id, status)';\n END IF;\nEND $$;\nCREATE INDEX IF NOT EXISTS idx_plg_forms_documents_template\n ON public.plg_forms_documents(template_id);\n\nDROP POLICY IF EXISTS \"plg_forms_documents_select\" ON public.plg_forms_documents;\nCREATE POLICY \"plg_forms_documents_select\" ON public.plg_forms_documents\n FOR SELECT TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_documents_insert\" ON public.plg_forms_documents;\nCREATE POLICY \"plg_forms_documents_insert\" ON public.plg_forms_documents\n FOR INSERT TO authenticated\n WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_documents_update\" ON public.plg_forms_documents;\nCREATE POLICY \"plg_forms_documents_update\" ON public.plg_forms_documents\n FOR UPDATE TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_documents_delete\" ON public.plg_forms_documents;\nCREATE POLICY \"plg_forms_documents_delete\" ON public.plg_forms_documents\n FOR DELETE TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\n\n-- plg_forms_document_files: file attachments for image/gallery/drawing fields\nCREATE TABLE IF NOT EXISTS public.plg_forms_document_files (\n id uuid PRIMARY KEY DEFAULT gen_random_uuid(),\n tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,\n document_id uuid NOT NULL REFERENCES public.plg_forms_documents(id) ON DELETE CASCADE,\n field_key text NOT NULL,\n file_url text NOT NULL,\n file_name text,\n file_size integer,\n mime_type text,\n sort_order integer DEFAULT 0,\n metadata jsonb DEFAULT '{}',\n created_at timestamptz NOT NULL DEFAULT now()\n);\n\nALTER TABLE public.plg_forms_document_files ENABLE ROW LEVEL SECURITY;\n\nCREATE INDEX IF NOT EXISTS idx_plg_forms_document_files_document\n ON public.plg_forms_document_files(document_id);\n\nDROP POLICY IF EXISTS \"plg_forms_document_files_select\" ON public.plg_forms_document_files;\nCREATE POLICY \"plg_forms_document_files_select\" ON public.plg_forms_document_files\n FOR SELECT TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_document_files_insert\" ON public.plg_forms_document_files;\nCREATE POLICY \"plg_forms_document_files_insert\" ON public.plg_forms_document_files\n FOR INSERT TO authenticated\n WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_document_files_update\" ON public.plg_forms_document_files;\nCREATE POLICY \"plg_forms_document_files_update\" ON public.plg_forms_document_files\n FOR UPDATE TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_document_files_delete\" ON public.plg_forms_document_files;\nCREATE POLICY \"plg_forms_document_files_delete\" ON public.plg_forms_document_files\n FOR DELETE TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\n\n-- View: pre-archetype read model (needs person_id; archetype pools get\n-- v_documents from 002 instead, which also drops this view when migrating).\nDO $$\nBEGIN\n IF EXISTS (\n SELECT 1 FROM information_schema.columns\n WHERE table_schema = 'public' AND table_name = 'plg_forms_documents'\n AND column_name = 'person_id'\n ) THEN\n EXECUTE $v$\n CREATE OR REPLACE VIEW public.v_frm_documents AS\n SELECT\n d.*,\n t.name AS template_name,\n t.category AS template_category,\n p.name AS person_name\n FROM public.plg_forms_documents d\n LEFT JOIN public.plg_forms_templates t ON t.id = d.template_id\n LEFT JOIN public.people p ON p.id = d.person_id\n $v$;\n END IF;\nEND $$;\n";
|
|
3
|
-
export declare const MIGRATION_002_DOCUMENT_ARCHETYPE = "-- ============================================================\n-- Document Archetype \u2014 public.documents\n-- A document is any record associated with a person (or standalone):\n-- forms, images, attachments, prescriptions, contracts, etc.\n-- The `kind` column discriminates the type.\n--\n-- DATA SAFETY (converted pools): plg_forms_documents may already hold REAL\n-- rows in its PRE-archetype shape (its own `id` PK, no `document_id` column \u2014\n-- see 001_frm_base.sql). This migration MIGRATES those rows into the new\n-- document archetype instead of dropping them:\n-- * pre-archetype + rows -> copy each legacy row into public.documents\n-- (reusing its id) + into the new extension\n-- table, migrate its files, then drop legacy.\n-- * pre-archetype + empty -> plain drop + create (fresh shape).\n-- * already new shape -> no-op.\n-- ============================================================\n\nCREATE TABLE IF NOT EXISTS public.documents (\n id uuid PRIMARY KEY DEFAULT gen_random_uuid(),\n tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,\n kind text NOT NULL DEFAULT 'attachment',\n -- kind: 'form', 'image', 'attachment', 'prescription', 'contract', etc.\n person_id uuid REFERENCES public.people(id) ON DELETE SET NULL,\n title text,\n description text,\n status text NOT NULL DEFAULT 'draft',\n -- status: 'draft', 'completed', 'signed', 'archived'\n file_url text,\n file_name text,\n file_size integer,\n mime_type text,\n tags text[] DEFAULT '{}',\n notes text,\n is_active boolean NOT NULL DEFAULT true,\n metadata jsonb DEFAULT '{}',\n created_by uuid,\n updated_by uuid,\n created_at timestamptz NOT NULL DEFAULT now(),\n updated_at timestamptz NOT NULL DEFAULT now()\n);\n\nALTER TABLE public.documents ENABLE ROW LEVEL SECURITY;\n\nCREATE INDEX IF NOT EXISTS idx_documents_tenant ON public.documents(tenant_id, kind);\nCREATE INDEX IF NOT EXISTS idx_documents_person ON public.documents(person_id);\nCREATE INDEX IF NOT EXISTS idx_documents_status ON public.documents(tenant_id, status);\n\nDROP POLICY IF EXISTS \"documents_select\" ON public.documents;\nCREATE POLICY \"documents_select\" ON public.documents\n FOR SELECT TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"documents_insert\" ON public.documents;\nCREATE POLICY \"documents_insert\" ON public.documents\n FOR INSERT TO authenticated\n WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"documents_update\" ON public.documents;\nCREATE POLICY \"documents_update\" ON public.documents\n FOR UPDATE TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"documents_delete\" ON public.documents;\nCREATE POLICY \"documents_delete\" ON public.documents\n FOR DELETE TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\n\n-- ============================================================\n-- Reconcile plg_forms_documents / plg_forms_document_files with the archetype.\n-- ============================================================\n\n-- Step 1: if a PRE-archetype plg_forms_documents is present (no document_id\n-- column), rename it (and its files table + dependent view) out of the way so\n-- the new archetype-shaped tables can be created and \u2014 when it holds rows \u2014\n-- its data copied across. Fully guarded, so already-migrated pools skip this.\nDO $$\nBEGIN\n IF to_regclass('public.plg_forms_documents') IS NOT NULL\n AND NOT EXISTS (\n SELECT 1 FROM information_schema.columns\n WHERE table_schema = 'public'\n AND table_name = 'plg_forms_documents'\n AND column_name = 'document_id'\n ) THEN\n DROP VIEW IF EXISTS public.v_frm_documents;\n ALTER TABLE public.plg_forms_documents RENAME TO plg_forms_documents_legacy;\n IF to_regclass('public.plg_forms_document_files') IS NOT NULL THEN\n ALTER TABLE public.plg_forms_document_files RENAME TO plg_forms_document_files_legacy;\n END IF;\n END IF;\nEND $$;\n\n-- Step 2: create the new archetype-shaped extension tables. IF NOT EXISTS makes\n-- this a no-op on pools already migrated to the new shape; after Step 1 the\n-- names are free on pre-archetype pools.\n\n-- plg_forms_documents: extension table for form-type documents\nCREATE TABLE IF NOT EXISTS public.plg_forms_documents (\n document_id uuid PRIMARY KEY REFERENCES public.documents(id) ON DELETE CASCADE,\n tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,\n template_id uuid NOT NULL REFERENCES public.plg_forms_templates(id),\n data jsonb NOT NULL DEFAULT '{}',\n signed_at timestamptz,\n signed_by uuid,\n created_at timestamptz NOT NULL DEFAULT now(),\n updated_at timestamptz NOT NULL DEFAULT now()\n);\n\nALTER TABLE public.plg_forms_documents ENABLE ROW LEVEL SECURITY;\n\nCREATE INDEX IF NOT EXISTS idx_plg_forms_documents_template ON public.plg_forms_documents(template_id);\n\nDROP POLICY IF EXISTS \"plg_forms_documents_select\" ON public.plg_forms_documents;\nCREATE POLICY \"plg_forms_documents_select\" ON public.plg_forms_documents\n FOR SELECT TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_documents_insert\" ON public.plg_forms_documents;\nCREATE POLICY \"plg_forms_documents_insert\" ON public.plg_forms_documents\n FOR INSERT TO authenticated\n WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_documents_update\" ON public.plg_forms_documents;\nCREATE POLICY \"plg_forms_documents_update\" ON public.plg_forms_documents\n FOR UPDATE TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_documents_delete\" ON public.plg_forms_documents;\nCREATE POLICY \"plg_forms_documents_delete\" ON public.plg_forms_documents\n FOR DELETE TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\n\n-- plg_forms_document_files: file attachments for form fields\nCREATE TABLE IF NOT EXISTS public.plg_forms_document_files (\n id uuid PRIMARY KEY DEFAULT gen_random_uuid(),\n tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,\n document_id uuid NOT NULL REFERENCES public.documents(id) ON DELETE CASCADE,\n field_key text NOT NULL,\n file_url text NOT NULL,\n file_name text,\n file_size integer,\n mime_type text,\n sort_order integer DEFAULT 0,\n metadata jsonb DEFAULT '{}',\n created_at timestamptz NOT NULL DEFAULT now()\n);\n\nALTER TABLE public.plg_forms_document_files ENABLE ROW LEVEL SECURITY;\n\nCREATE INDEX IF NOT EXISTS idx_plg_forms_document_files_document ON public.plg_forms_document_files(document_id);\n\nDROP POLICY IF EXISTS \"plg_forms_document_files_select\" ON public.plg_forms_document_files;\nCREATE POLICY \"plg_forms_document_files_select\" ON public.plg_forms_document_files\n FOR SELECT TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_document_files_insert\" ON public.plg_forms_document_files;\nCREATE POLICY \"plg_forms_document_files_insert\" ON public.plg_forms_document_files\n FOR INSERT TO authenticated\n WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_document_files_update\" ON public.plg_forms_document_files;\nCREATE POLICY \"plg_forms_document_files_update\" ON public.plg_forms_document_files\n FOR UPDATE TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\nDROP POLICY IF EXISTS \"plg_forms_document_files_delete\" ON public.plg_forms_document_files;\nCREATE POLICY \"plg_forms_document_files_delete\" ON public.plg_forms_document_files\n FOR DELETE TO authenticated\n USING (tenant_id IN (SELECT public.user_tenant_ids()));\n\n-- Step 3: if legacy rows exist, migrate them, then drop the quarantined legacy\n-- tables. Reusing each legacy id as the documents.id keeps every legacy file's\n-- document_id FK valid after the copy. ON CONFLICT DO NOTHING makes a partial\n-- re-run safe.\nDO $$\nBEGIN\n IF to_regclass('public.plg_forms_documents_legacy') IS NOT NULL THEN\n -- 3a. base document (one per legacy form document; is_active mirrors NOT is_deleted)\n INSERT INTO public.documents (\n id, tenant_id, kind, person_id, title, status, notes, metadata,\n is_active, created_by, updated_by, created_at, updated_at\n )\n SELECT\n id, tenant_id, 'form', person_id, title, status, notes, metadata,\n NOT COALESCE(is_deleted, false), created_by, updated_by, created_at, updated_at\n FROM public.plg_forms_documents_legacy\n ON CONFLICT (id) DO NOTHING;\n\n -- 3b. form extension row (keyed by the reused document id)\n INSERT INTO public.plg_forms_documents (\n document_id, tenant_id, template_id, data, signed_at, signed_by, created_at, updated_at\n )\n SELECT\n id, tenant_id, template_id, data, signed_at, signed_by, created_at, updated_at\n FROM public.plg_forms_documents_legacy\n ON CONFLICT (document_id) DO NOTHING;\n\n -- 3c. file attachments (document_id already points at the reused id)\n IF to_regclass('public.plg_forms_document_files_legacy') IS NOT NULL THEN\n INSERT INTO public.plg_forms_document_files (\n id, tenant_id, document_id, field_key, file_url, file_name,\n file_size, mime_type, sort_order, metadata, created_at\n )\n SELECT\n id, tenant_id, document_id, field_key, file_url, file_name,\n file_size, mime_type, sort_order, metadata, created_at\n FROM public.plg_forms_document_files_legacy\n ON CONFLICT (id) DO NOTHING;\n END IF;\n END IF;\n\n -- Drop legacy remnants now that any data has been copied across.\n DROP TABLE IF EXISTS public.plg_forms_document_files_legacy;\n DROP TABLE IF EXISTS public.plg_forms_documents_legacy;\nEND $$;\n\n-- View: all documents for a person with form data joined when applicable\nCREATE OR REPLACE VIEW public.v_documents AS\nSELECT\n d.*,\n f.template_id,\n f.data AS form_data,\n f.signed_at,\n f.signed_by,\n t.name AS template_name,\n t.category AS template_category,\n p.name AS person_name\nFROM public.documents d\nLEFT JOIN public.plg_forms_documents f ON f.document_id = d.id\nLEFT JOIN public.plg_forms_templates t ON t.id = f.template_id\nLEFT JOIN public.people p ON p.id = d.person_id;\n";
|
|
4
|
-
export declare const MIGRATION_003_AGENT_RPCS = "-- ============================================================================\n-- plugin-forms 003: server-plane agent write RPC.\n--\n-- public.agent_forms_upsert_template \u2014 the assistant BUILDS or EDITS a form\n-- template from any surface or channel (in-app FAB, WhatsApp, MCP). Same\n-- contract as every agent_* RPC (agenda 005, financial 009):\n-- (p_tenant_id, p_actor_user_id, p_payload jsonb) \u2192 jsonb\n-- {ok:true, id, record:{...}} | {ok:false, denial:{...}} | {ok:false, error}\n--\n-- The grid layout is computed HERE (col 0, sequential rows, full-width span) so\n-- the model only ever sends {type,label,required?,options?} \u2014 never positions.\n--\n-- Payload:\n-- template_id? uuid \u2014 omit to CREATE, provide to EDIT\n-- name? text \u2014 required on create\n-- category? text \u2014 anamnesis|evolution|report|contract|general\n-- description? text\n-- fields? [{type,label,required?,placeholder?,options?[]}] \u2014 REPLACE all\n-- append_fields? [{...}] \u2014 APPEND to existing\n--\n-- GRANT authenticated+service_role \u2014 NEVER anon (the broker calls with the pool\n-- service key and injects tenant/actor; in-browser the signed-in user calls it\n-- directly and spine RLS/actor guards bind them to their own tenant).\n-- ============================================================================\n\nCREATE OR REPLACE FUNCTION public.agent_forms_upsert_template(\n p_tenant_id uuid,\n p_actor_user_id uuid,\n p_payload jsonb\n) RETURNS jsonb\nLANGUAGE plpgsql SECURITY DEFINER\nSET search_path = public\nAS $$\nDECLARE\n v_denial jsonb;\n v_used int;\n v_template_id uuid;\n v_is_update boolean;\n v_name text;\n v_category text;\n v_description text;\n v_existing jsonb;\n v_input jsonb;\n v_merged jsonb;\n v_fields jsonb;\n v_schema jsonb;\n v_final_id uuid;\nBEGIN\n -- \u2500\u2500 payload \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n BEGIN\n v_template_id := NULLIF(p_payload->>'template_id','')::uuid;\n EXCEPTION WHEN OTHERS THEN\n RETURN jsonb_build_object('ok', false, 'error', 'invalid template_id');\n END;\n v_is_update := v_template_id IS NOT NULL;\n v_name := btrim(p_payload->>'name');\n v_category := lower(COALESCE(NULLIF(p_payload->>'category',''), 'general'));\n v_description := left(p_payload->>'description', 1000);\n IF v_category NOT IN ('anamnesis','evolution','report','contract','general') THEN\n v_category := 'general';\n END IF;\n\n IF NOT v_is_update AND (v_name IS NULL OR v_name = '') THEN\n RETURN jsonb_build_object('ok', false, 'error', 'name is required to create a form');\n END IF;\n\n -- \u2500\u2500 authorization: role \u2192 plan \u2192 form_templates cap \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n SELECT count(*) INTO v_used FROM plg_forms_templates\n WHERE tenant_id = p_tenant_id AND is_deleted = false;\n v_denial := agent_guard(p_tenant_id, p_actor_user_id, 'custom_forms',\n CASE WHEN v_is_update THEN 'update' ELSE 'create' END,\n 'form_templates', v_used, CASE WHEN v_is_update THEN 0 ELSE 1 END);\n IF v_denial IS NOT NULL THEN\n RETURN jsonb_build_object('ok', false, 'denial', v_denial);\n END IF;\n\n -- \u2500\u2500 current fields (for APPEND / to reject an unknown id) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n IF v_is_update THEN\n SELECT COALESCE(schema->'fields','[]'::jsonb) INTO v_existing\n FROM plg_forms_templates\n WHERE id = v_template_id AND tenant_id = p_tenant_id AND is_deleted = false;\n IF v_existing IS NULL THEN\n RETURN jsonb_build_object('ok', false, 'error', 'unknown template for this tenant');\n END IF;\n ELSE\n v_existing := '[]'::jsonb;\n END IF;\n\n -- \u2500\u2500 normalize the incoming fields to the builder's FormFieldDef shape \u2500\u2500\u2500\u2500\n -- (id, type, label, required, placeholder, col, colSpan, options) \u2014 row is\n -- assigned last, across the merged list.\n WITH src AS (\n SELECT CASE\n WHEN p_payload ? 'fields' THEN COALESCE(p_payload->'fields','[]'::jsonb)\n WHEN p_payload ? 'append_fields' THEN COALESCE(p_payload->'append_fields','[]'::jsonb)\n ELSE '[]'::jsonb\n END AS arr\n ),\n norm AS (\n SELECT f.ord, jsonb_strip_nulls(jsonb_build_object(\n 'id', COALESCE(NULLIF(f.value->>'id',''), gen_random_uuid()::text),\n 'type', COALESCE(NULLIF(f.value->>'type',''), 'text'),\n 'label', COALESCE(NULLIF(f.value->>'label',''), 'Campo'),\n 'required', COALESCE((f.value->>'required')::boolean, false),\n 'placeholder', NULLIF(f.value->>'placeholder',''),\n 'col', 0,\n 'colSpan', 12,\n 'options', CASE\n WHEN jsonb_typeof(f.value->'options') = 'array' AND jsonb_array_length(f.value->'options') > 0\n THEN (SELECT jsonb_agg(jsonb_build_object(\n 'label', opt,\n 'value', lower(regexp_replace(btrim(opt), '\\s+', '_', 'g'))))\n FROM jsonb_array_elements_text(f.value->'options') opt)\n ELSE NULL END\n )) AS field\n FROM src, jsonb_array_elements(src.arr) WITH ORDINALITY AS f(value, ord)\n )\n SELECT COALESCE(jsonb_agg(field ORDER BY ord), '[]'::jsonb) INTO v_input FROM norm;\n\n -- REPLACE with `fields`; otherwise APPEND onto what's already there.\n IF p_payload ? 'fields' THEN\n v_merged := v_input;\n ELSE\n v_merged := v_existing || v_input;\n END IF;\n\n -- sequential rows across the final list\n SELECT COALESCE(jsonb_agg(jsonb_set(e.value, '{row}', to_jsonb((e.ord - 1))) ORDER BY e.ord), '[]'::jsonb)\n INTO v_fields\n FROM jsonb_array_elements(v_merged) WITH ORDINALITY AS e(value, ord);\n\n v_schema := jsonb_build_object('layout', jsonb_build_object('columns', 12), 'fields', v_fields);\n\n -- \u2500\u2500 write + audit \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n IF v_is_update THEN\n UPDATE plg_forms_templates SET\n name = COALESCE(NULLIF(v_name,''), name),\n category = v_category,\n description = COALESCE(v_description, description),\n schema = v_schema,\n updated_by = p_actor_user_id,\n updated_at = now()\n WHERE id = v_template_id AND tenant_id = p_tenant_id AND is_deleted = false\n RETURNING id INTO v_final_id;\n ELSE\n INSERT INTO plg_forms_templates (tenant_id, name, category, description, schema, created_by, updated_by)\n VALUES (p_tenant_id, v_name, v_category, v_description, v_schema, p_actor_user_id, p_actor_user_id)\n RETURNING id INTO v_final_id;\n END IF;\n\n INSERT INTO audit_logs (tenant_id, user_id, action, entity_type, entity_id, metadata)\n VALUES (p_tenant_id, p_actor_user_id,\n CASE WHEN v_is_update THEN 'agent.updateFormTemplate' ELSE 'agent.createFormTemplate' END,\n 'form_template', v_final_id::text,\n jsonb_build_object('payload', p_payload));\n\n RETURN jsonb_build_object(\n 'ok', true,\n 'id', v_final_id,\n 'record', jsonb_build_object(\n 'ref', jsonb_build_object('id', v_final_id, 'resource', 'plg_forms_templates',\n 'archetype', 'custom_forms:template'),\n 'name', COALESCE(NULLIF(v_name,''), (SELECT name FROM plg_forms_templates WHERE id = v_final_id)),\n 'category', v_category,\n 'fieldCount', jsonb_array_length(v_fields)\n )\n );\nEND;\n$$;\n\nREVOKE ALL ON FUNCTION public.agent_forms_upsert_template(uuid, uuid, jsonb) FROM public;\nREVOKE EXECUTE ON FUNCTION public.agent_forms_upsert_template(uuid, uuid, jsonb) FROM anon;\nGRANT EXECUTE ON FUNCTION public.agent_forms_upsert_template(uuid, uuid, jsonb)\n TO authenticated, service_role;\n";
|
|
5
|
-
export declare const MIGRATION_004_TEMPLATE_USAGE = "-- ============================================================================\n-- 004_template_usage.sql \u2014 quanto cada modelo foi usado.\n--\n-- A lista de modelos mostrava nome, categoria e \"33 campos\". Nada disso diz o\n-- que a pessoa quer saber ao abrir a tela: quais modelos o sal\u00E3o realmente usa.\n-- Um formul\u00E1rio com 38 campos e zero preenchimentos e outro com 3 campos e 400\n-- preenchimentos apareciam do mesmo jeito \u2014 e \u00E9 o segundo que n\u00E3o se mexe sem\n-- pensar duas vezes.\n--\n-- Uma view agregada e n\u00E3o uma contagem por modelo na interface: treze modelos\n-- seriam treze consultas de contagem mais treze de \"qual a \u00FAltima\", a cada vez\n-- que a tela abre. Aqui \u00E9 uma leitura s\u00F3, e o Postgres agrega onde os dados j\u00E1\n-- est\u00E3o.\n--\n-- `security_invoker` para a view herdar a RLS de `documents` /\n-- `plg_forms_documents`: sem isso ela contaria, para qualquer usu\u00E1rio, os\n-- documentos de todos os tenants do pool.\n--\n-- `is_active = false` (arquivado) n\u00E3o conta: a pergunta \u00E9 quantos documentos o\n-- modelo tem VIVOS, e um arquivo morto inflaria o n\u00FAmero que decide se o modelo\n-- pode ser mexido.\n-- ============================================================================\n\nCREATE OR REPLACE VIEW public.v_forms_template_usage\nWITH (security_invoker = true) AS\nSELECT\n f.template_id,\n f.tenant_id,\n count(*)::bigint AS document_count,\n max(d.created_at) AS last_document_at\nFROM public.plg_forms_documents f\nJOIN public.documents d\n ON d.id = f.document_id\n AND d.is_active = true\nGROUP BY f.template_id, f.tenant_id;\n\nGRANT SELECT ON public.v_forms_template_usage TO authenticated;\n";
|
|
6
|
-
export declare const MIGRATION_005_V_DOCUMENTS_INVOKER = "-- 005_v_documents_invoker.sql \u2014 the forms plugin carries the fix forward.\n--\n-- Core `043_view_invoker` swept nine owner-rights views, `public.v_documents`\n-- among them, because on a shared pool any signed-in user could read another\n-- tenant's patient documents. Its own header asked the plugins to carry the fix\n-- into their next migration, and this is that migration.\n--\n-- It matters more than tidiness: plugin migrations run AFTER the core chain, so\n-- `002_document_archetype` re-creates this view every time and hands the hole\n-- straight back. The core sweep can never win that race \u2014 only the file that\n-- owns the view can.\n--\n-- 900_verification/001 N16 is what caught it: green on the core chain, red the\n-- moment the optional plugins joined.\n--\n-- Idempotent: setting the option twice is a no-op.\nDO $$\nBEGIN\n IF to_regclass('public.v_documents') IS NOT NULL THEN\n ALTER VIEW public.v_documents SET (security_invoker = true);\n END IF;\nEND $$;\n\nCOMMENT ON VIEW public.v_documents IS\n 'Documents with their template joined. security_invoker: the reader''s own RLS decides, not the migration role''s (core 043, carried forward here so re-creating 002 cannot undo it).';\n";
|
|
7
|
-
export declare const MIGRATION_006_FORMS_ANALYTICS_READ_MODELS = "-- ============================================================================\n-- Forms \u2014 read model for the analytics engine (spine 024_analytics_engine)\n--\n-- A form submission had nowhere to be READ in bulk: it lives on the person it\n-- belongs to, one document at a time, so \"todo mundo que preencheu a ficha de\n-- matr\u00EDcula em agosto\" was a question the product could not answer. This view\n-- is that answer, and because it is a registered read model it is at the same\n-- time the KPI, the breakdown by template and the exportable list behind them.\n--\n-- WHY THE INNER JOIN. public.documents holds every attachment a person has \u2014\n-- a photo, a PDF, a contract. A row only counts as a SUBMISSION when it has a\n-- form behind it (plg_forms_documents), so the join is inner on purpose: the\n-- report would otherwise report uploads as answers.\n--\n-- WHY FLAG COLUMNS. analytics_run has no conditional aggregate (a free-form\n-- expression would be an injection point), so \"assinadas\" has to be a column.\n--\n-- Idempotent.\n-- ============================================================================\n\nCREATE OR REPLACE VIEW public.plg_forms_rep_submissions\nWITH (security_invoker = true) AS\nSELECT\n d.id AS submission_id,\n d.tenant_id,\n d.created_at,\n d.updated_at,\n d.title,\n d.status,\n d.kind,\n d.person_id,\n p.name AS person_name,\n p.email AS person_email,\n p.phone AS person_phone,\n f.template_id,\n COALESCE(NULLIF(t.name, ''), 'Sem modelo') AS template_name,\n COALESCE(NULLIF(t.category, ''), 'Sem categoria') AS template_category,\n f.signed_at,\n f.data AS form_data,\n 1 AS submissions,\n CASE WHEN f.signed_at IS NOT NULL THEN 1 ELSE 0 END AS is_signed,\n CASE WHEN d.status = 'completed' THEN 1 ELSE 0 END AS is_completed,\n CASE WHEN d.status = 'draft' THEN 1 ELSE 0 END AS is_draft\nFROM public.documents d\nJOIN public.plg_forms_documents f ON f.document_id = d.id\nLEFT JOIN public.plg_forms_templates t ON t.id = f.template_id\nLEFT JOIN public.people p ON p.id = d.person_id\nWHERE d.is_active;\n\nGRANT SELECT ON public.plg_forms_rep_submissions TO authenticated;\n\nCOMMENT ON VIEW public.plg_forms_rep_submissions IS\n 'One row per form submission (a document WITH a form behind it), joined to its template and the person who filled it.';\n\n-- Register. A view outside the allowlist cannot be reached by analytics_run at\n-- all. Guarded: a pool without the spine's 024 skips instead of failing.\nDO $$\nBEGIN\n IF to_regclass('public.plg_analytics_read_models') IS NULL THEN\n RAISE NOTICE 'forms: analytics engine not installed \u2014 read model not registered';\n RETURN;\n END IF;\n\n INSERT INTO public.plg_analytics_read_models (name, date_column, tenant_column, description) VALUES\n ('plg_forms_rep_submissions', 'created_at', 'tenant_id',\n 'One row per form submission, with template and the person who filled it')\n ON CONFLICT (name) DO UPDATE\n SET date_column = EXCLUDED.date_column,\n tenant_column = EXCLUDED.tenant_column,\n description = EXCLUDED.description;\nEND $$;\n";
|
|
1
|
+
export declare const MIGRATION_000_BASELINE = "-- ============================================================================\n-- plugins/plugin-forms/src/migrations/000_baseline.sql \u2014 what installing this provisions, as one file.\n--\n-- GENERATED by scripts/emit-unit-baselines.mjs (#338). Do not edit by hand:\n-- change the schema, regenerate, and let scripts/check-chain-equivalence.mjs\n-- prove the result still matches.\n--\n-- This replaced 7 migration files. They are kept, unapplied, in\n-- migrations.archive/ \u2014 a year of decisions is worth reading even when it is no\n-- longer worth replaying.\n--\n-- Object order is fixed rather than dependency-sorted: schemas, types,\n-- sequences, tables, defaults, functions, constraints, views, indexes, foreign\n-- keys, triggers, row security, policies, grants. Dependencies BETWEEN units\n-- are carried by the order packages/db/chain.json declares.\n-- ============================================================================\n\n-- A LANGUAGE sql function binds its body at CREATE time, and 570 functions\n-- sorted by name are not sorted by who calls whom. This is what lets them load\n-- in any order; every body is still compiled on first call, so a genuinely\n-- broken reference surfaces then rather than never. Session-scoped.\nSET check_function_bodies = false;\n\n\n\n-- \u2500\u2500 default privileges: reset \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n-- Cleared BEFORE anything is created, so every object below gets the ACL the\n-- dump describes rather than the image's defaults on top of it. Restored at\n-- the end of this file.\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE ALL ON SEQUENCES FROM anon, authenticated, service_role, fayz_connector$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE ALL ON SEQUENCES FROM anon, authenticated, service_role, fayz_connector$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public REVOKE ALL ON SEQUENCES FROM anon, authenticated, service_role, fayz_connector$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public REVOKE ALL ON SEQUENCES FROM anon, authenticated, service_role, fayz_connector$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE ALL ON FUNCTIONS FROM anon, authenticated, service_role, fayz_connector$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE ALL ON FUNCTIONS FROM anon, authenticated, service_role, fayz_connector$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public REVOKE ALL ON FUNCTIONS FROM anon, authenticated, service_role, fayz_connector$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public REVOKE ALL ON FUNCTIONS FROM anon, authenticated, service_role, fayz_connector$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE ALL ON TABLES FROM anon, authenticated, service_role, fayz_connector$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE ALL ON TABLES FROM anon, authenticated, service_role, fayz_connector$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public REVOKE ALL ON TABLES FROM anon, authenticated, service_role, fayz_connector$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public REVOKE ALL ON TABLES FROM anon, authenticated, service_role, fayz_connector$stmt$;\nEND $dp$;\n\n\n-- \u2500\u2500 table \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nCREATE TABLE public.plg_forms_document_files (\n id uuid DEFAULT gen_random_uuid() NOT NULL,\n tenant_id uuid NOT NULL,\n document_id uuid NOT NULL,\n field_key text NOT NULL,\n file_url text NOT NULL,\n file_name text,\n file_size integer,\n mime_type text,\n sort_order integer DEFAULT 0,\n metadata jsonb DEFAULT '{}'::jsonb,\n created_at timestamp with time zone DEFAULT now() NOT NULL\n);\n\nCREATE TABLE public.plg_forms_documents (\n document_id uuid NOT NULL,\n tenant_id uuid NOT NULL,\n template_id uuid NOT NULL,\n data jsonb DEFAULT '{}'::jsonb NOT NULL,\n signed_at timestamp with time zone,\n signed_by uuid,\n created_at timestamp with time zone DEFAULT now() NOT NULL,\n updated_at timestamp with time zone DEFAULT now() NOT NULL\n);\n\nCREATE TABLE public.plg_forms_templates (\n id uuid DEFAULT gen_random_uuid() NOT NULL,\n tenant_id uuid NOT NULL,\n name text NOT NULL,\n description text,\n category text DEFAULT 'general'::text NOT NULL,\n version integer DEFAULT 1 NOT NULL,\n is_current boolean DEFAULT true NOT NULL,\n parent_id uuid,\n schema jsonb DEFAULT '{\"fields\": [], \"layout\": {\"columns\": 12}}'::jsonb NOT NULL,\n specialty text,\n tags text[] DEFAULT '{}'::text[],\n metadata jsonb DEFAULT '{}'::jsonb,\n is_active boolean DEFAULT true NOT NULL,\n is_deleted boolean DEFAULT false NOT NULL,\n created_by uuid,\n updated_by uuid,\n created_at timestamp with time zone DEFAULT now() NOT NULL,\n updated_at timestamp with time zone DEFAULT now() NOT NULL\n);\n\n\n-- \u2500\u2500 function \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nCREATE FUNCTION public.agent_forms_upsert_template(p_tenant_id uuid, p_actor_user_id uuid, p_payload jsonb) RETURNS jsonb\n LANGUAGE plpgsql SECURITY DEFINER\n SET search_path TO 'public'\n AS $$\nDECLARE\n v_denial jsonb;\n v_used int;\n v_template_id uuid;\n v_is_update boolean;\n v_name text;\n v_category text;\n v_description text;\n v_existing jsonb;\n v_input jsonb;\n v_merged jsonb;\n v_fields jsonb;\n v_schema jsonb;\n v_final_id uuid;\nBEGIN\n -- \u2500\u2500 payload \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n BEGIN\n v_template_id := NULLIF(p_payload->>'template_id','')::uuid;\n EXCEPTION WHEN OTHERS THEN\n RETURN jsonb_build_object('ok', false, 'error', 'invalid template_id');\n END;\n v_is_update := v_template_id IS NOT NULL;\n v_name := btrim(p_payload->>'name');\n v_category := lower(COALESCE(NULLIF(p_payload->>'category',''), 'general'));\n v_description := left(p_payload->>'description', 1000);\n IF v_category NOT IN ('anamnesis','evolution','report','contract','general') THEN\n v_category := 'general';\n END IF;\n\n IF NOT v_is_update AND (v_name IS NULL OR v_name = '') THEN\n RETURN jsonb_build_object('ok', false, 'error', 'name is required to create a form');\n END IF;\n\n -- \u2500\u2500 authorization: role \u2192 plan \u2192 form_templates cap \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n SELECT count(*) INTO v_used FROM plg_forms_templates\n WHERE tenant_id = p_tenant_id AND is_deleted = false;\n v_denial := agent_guard(p_tenant_id, p_actor_user_id, 'custom_forms',\n CASE WHEN v_is_update THEN 'update' ELSE 'create' END,\n 'form_templates', v_used, CASE WHEN v_is_update THEN 0 ELSE 1 END);\n IF v_denial IS NOT NULL THEN\n RETURN jsonb_build_object('ok', false, 'denial', v_denial);\n END IF;\n\n -- \u2500\u2500 current fields (for APPEND / to reject an unknown id) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n IF v_is_update THEN\n SELECT COALESCE(schema->'fields','[]'::jsonb) INTO v_existing\n FROM plg_forms_templates\n WHERE id = v_template_id AND tenant_id = p_tenant_id AND is_deleted = false;\n IF v_existing IS NULL THEN\n RETURN jsonb_build_object('ok', false, 'error', 'unknown template for this tenant');\n END IF;\n ELSE\n v_existing := '[]'::jsonb;\n END IF;\n\n -- \u2500\u2500 normalize the incoming fields to the builder's FormFieldDef shape \u2500\u2500\u2500\u2500\n -- (id, type, label, required, placeholder, col, colSpan, options) \u2014 row is\n -- assigned last, across the merged list.\n WITH src AS (\n SELECT CASE\n WHEN p_payload ? 'fields' THEN COALESCE(p_payload->'fields','[]'::jsonb)\n WHEN p_payload ? 'append_fields' THEN COALESCE(p_payload->'append_fields','[]'::jsonb)\n ELSE '[]'::jsonb\n END AS arr\n ),\n norm AS (\n SELECT f.ord, jsonb_strip_nulls(jsonb_build_object(\n 'id', COALESCE(NULLIF(f.value->>'id',''), gen_random_uuid()::text),\n 'type', COALESCE(NULLIF(f.value->>'type',''), 'text'),\n 'label', COALESCE(NULLIF(f.value->>'label',''), 'Campo'),\n 'required', COALESCE((f.value->>'required')::boolean, false),\n 'placeholder', NULLIF(f.value->>'placeholder',''),\n 'col', 0,\n 'colSpan', 12,\n 'options', CASE\n WHEN jsonb_typeof(f.value->'options') = 'array' AND jsonb_array_length(f.value->'options') > 0\n THEN (SELECT jsonb_agg(jsonb_build_object(\n 'label', opt,\n 'value', lower(regexp_replace(btrim(opt), '\\s+', '_', 'g'))))\n FROM jsonb_array_elements_text(f.value->'options') opt)\n ELSE NULL END\n )) AS field\n FROM src, jsonb_array_elements(src.arr) WITH ORDINALITY AS f(value, ord)\n )\n SELECT COALESCE(jsonb_agg(field ORDER BY ord), '[]'::jsonb) INTO v_input FROM norm;\n\n -- REPLACE with `fields`; otherwise APPEND onto what's already there.\n IF p_payload ? 'fields' THEN\n v_merged := v_input;\n ELSE\n v_merged := v_existing || v_input;\n END IF;\n\n -- sequential rows across the final list\n SELECT COALESCE(jsonb_agg(jsonb_set(e.value, '{row}', to_jsonb((e.ord - 1))) ORDER BY e.ord), '[]'::jsonb)\n INTO v_fields\n FROM jsonb_array_elements(v_merged) WITH ORDINALITY AS e(value, ord);\n\n v_schema := jsonb_build_object('layout', jsonb_build_object('columns', 12), 'fields', v_fields);\n\n -- \u2500\u2500 write + audit \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n IF v_is_update THEN\n UPDATE plg_forms_templates SET\n name = COALESCE(NULLIF(v_name,''), name),\n category = v_category,\n description = COALESCE(v_description, description),\n schema = v_schema,\n updated_by = p_actor_user_id,\n updated_at = now()\n WHERE id = v_template_id AND tenant_id = p_tenant_id AND is_deleted = false\n RETURNING id INTO v_final_id;\n ELSE\n INSERT INTO plg_forms_templates (tenant_id, name, category, description, schema, created_by, updated_by)\n VALUES (p_tenant_id, v_name, v_category, v_description, v_schema, p_actor_user_id, p_actor_user_id)\n RETURNING id INTO v_final_id;\n END IF;\n\n INSERT INTO audit_logs (tenant_id, user_id, action, entity_type, entity_id, metadata)\n VALUES (p_tenant_id, p_actor_user_id,\n CASE WHEN v_is_update THEN 'agent.updateFormTemplate' ELSE 'agent.createFormTemplate' END,\n 'form_template', v_final_id::text,\n jsonb_build_object('payload', p_payload));\n\n RETURN jsonb_build_object(\n 'ok', true,\n 'id', v_final_id,\n 'record', jsonb_build_object(\n 'ref', jsonb_build_object('id', v_final_id, 'resource', 'plg_forms_templates',\n 'archetype', 'custom_forms:template'),\n 'name', COALESCE(NULLIF(v_name,''), (SELECT name FROM plg_forms_templates WHERE id = v_final_id)),\n 'category', v_category,\n 'fieldCount', jsonb_array_length(v_fields)\n )\n );\nEND;\n$$;\n\n\n-- \u2500\u2500 constraint \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nALTER TABLE ONLY public.plg_forms_document_files\n ADD CONSTRAINT plg_forms_document_files_pkey1 PRIMARY KEY (id);\n\nALTER TABLE ONLY public.plg_forms_documents\n ADD CONSTRAINT plg_forms_documents_pkey1 PRIMARY KEY (document_id);\n\nALTER TABLE ONLY public.plg_forms_templates\n ADD CONSTRAINT plg_forms_templates_pkey PRIMARY KEY (id);\n\n\n-- \u2500\u2500 view \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nCREATE VIEW public.plg_forms_rep_submissions WITH (security_invoker='true') AS\n SELECT d.id AS submission_id,\n d.tenant_id,\n d.created_at,\n d.updated_at,\n d.title,\n d.status,\n d.kind,\n d.person_id,\n p.name AS person_name,\n p.email AS person_email,\n p.phone AS person_phone,\n f.template_id,\n COALESCE(NULLIF(t.name, ''::text), 'Sem modelo'::text) AS template_name,\n COALESCE(NULLIF(t.category, ''::text), 'Sem categoria'::text) AS template_category,\n f.signed_at,\n f.data AS form_data,\n 1 AS submissions,\n CASE\n WHEN (f.signed_at IS NOT NULL) THEN 1\n ELSE 0\n END AS is_signed,\n CASE\n WHEN (d.status = 'completed'::text) THEN 1\n ELSE 0\n END AS is_completed,\n CASE\n WHEN (d.status = 'draft'::text) THEN 1\n ELSE 0\n END AS is_draft\n FROM (((public.documents d\n JOIN public.plg_forms_documents f ON ((f.document_id = d.id)))\n LEFT JOIN public.plg_forms_templates t ON ((t.id = f.template_id)))\n LEFT JOIN public.people p ON ((p.id = d.person_id)))\n WHERE d.is_active;\n\nCREATE VIEW public.v_documents WITH (security_invoker='true') AS\n SELECT d.id,\n d.tenant_id,\n d.kind,\n d.person_id,\n d.title,\n d.description,\n d.status,\n d.file_url,\n d.file_name,\n d.file_size,\n d.mime_type,\n d.tags,\n d.notes,\n d.is_active,\n d.metadata,\n d.created_by,\n d.updated_by,\n d.created_at,\n d.updated_at,\n d.storage_provider,\n d.storage_bucket,\n d.storage_path,\n d.checksum,\n d.unit_id,\n d.subject_type,\n d.subject_id,\n f.template_id,\n f.data AS form_data,\n f.signed_at,\n f.signed_by,\n t.name AS template_name,\n t.category AS template_category,\n p.name AS person_name\n FROM (((public.documents d\n LEFT JOIN public.plg_forms_documents f ON ((f.document_id = d.id)))\n LEFT JOIN public.plg_forms_templates t ON ((t.id = f.template_id)))\n LEFT JOIN public.people p ON ((p.id = d.person_id)));\n\nCREATE VIEW public.v_forms_template_usage WITH (security_invoker='true') AS\n SELECT f.template_id,\n f.tenant_id,\n count(*) AS document_count,\n max(d.created_at) AS last_document_at\n FROM (public.plg_forms_documents f\n JOIN public.documents d ON (((d.id = f.document_id) AND (d.is_active = true))))\n GROUP BY f.template_id, f.tenant_id;\n\n\n-- \u2500\u2500 index \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nCREATE INDEX idx_plg_forms_templates_category ON public.plg_forms_templates USING btree (tenant_id, category);\n\nCREATE INDEX idx_plg_forms_templates_current ON public.plg_forms_templates USING btree (tenant_id, is_current, is_active) WHERE ((is_current = true) AND (is_active = true) AND (is_deleted = false));\n\nCREATE INDEX idx_plg_forms_templates_parent ON public.plg_forms_templates USING btree (parent_id);\n\nCREATE INDEX idx_plg_forms_templates_tenant ON public.plg_forms_templates USING btree (tenant_id);\n\n\n-- \u2500\u2500 fk constraint \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nALTER TABLE ONLY public.plg_forms_document_files\n ADD CONSTRAINT plg_forms_document_files_document_id_fkey1 FOREIGN KEY (document_id) REFERENCES public.documents(id) ON DELETE CASCADE;\n\nALTER TABLE ONLY public.plg_forms_document_files\n ADD CONSTRAINT plg_forms_document_files_tenant_id_fkey1 FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;\n\nALTER TABLE ONLY public.plg_forms_documents\n ADD CONSTRAINT plg_forms_documents_document_id_fkey FOREIGN KEY (document_id) REFERENCES public.documents(id) ON DELETE CASCADE;\n\nALTER TABLE ONLY public.plg_forms_documents\n ADD CONSTRAINT plg_forms_documents_template_id_fkey1 FOREIGN KEY (template_id) REFERENCES public.plg_forms_templates(id);\n\nALTER TABLE ONLY public.plg_forms_documents\n ADD CONSTRAINT plg_forms_documents_tenant_id_fkey1 FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;\n\nALTER TABLE ONLY public.plg_forms_templates\n ADD CONSTRAINT plg_forms_templates_parent_id_fkey FOREIGN KEY (parent_id) REFERENCES public.plg_forms_templates(id);\n\nALTER TABLE ONLY public.plg_forms_templates\n ADD CONSTRAINT plg_forms_templates_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;\n\n\n-- \u2500\u2500 row security \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nALTER TABLE public.plg_forms_document_files ENABLE ROW LEVEL SECURITY;\n\nALTER TABLE public.plg_forms_documents ENABLE ROW LEVEL SECURITY;\n\nALTER TABLE public.plg_forms_templates ENABLE ROW LEVEL SECURITY;\n\n\n-- \u2500\u2500 policy \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nCREATE POLICY plg_forms_document_files_delete ON public.plg_forms_document_files FOR DELETE TO authenticated USING ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));\n\nCREATE POLICY plg_forms_document_files_insert ON public.plg_forms_document_files FOR INSERT TO authenticated WITH CHECK ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));\n\nCREATE POLICY plg_forms_document_files_select ON public.plg_forms_document_files FOR SELECT TO authenticated USING ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));\n\nCREATE POLICY plg_forms_document_files_update ON public.plg_forms_document_files FOR UPDATE TO authenticated USING ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));\n\nCREATE POLICY plg_forms_documents_delete ON public.plg_forms_documents FOR DELETE TO authenticated USING ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));\n\nCREATE POLICY plg_forms_documents_insert ON public.plg_forms_documents FOR INSERT TO authenticated WITH CHECK ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));\n\nCREATE POLICY plg_forms_documents_select ON public.plg_forms_documents FOR SELECT TO authenticated USING ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));\n\nCREATE POLICY plg_forms_documents_update ON public.plg_forms_documents FOR UPDATE TO authenticated USING ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));\n\nCREATE POLICY plg_forms_templates_delete ON public.plg_forms_templates FOR DELETE TO authenticated USING ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));\n\nCREATE POLICY plg_forms_templates_insert ON public.plg_forms_templates FOR INSERT TO authenticated WITH CHECK ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));\n\nCREATE POLICY plg_forms_templates_select ON public.plg_forms_templates FOR SELECT TO authenticated USING ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));\n\nCREATE POLICY plg_forms_templates_update ON public.plg_forms_templates FOR UPDATE TO authenticated USING ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));\n\n\n-- \u2500\u2500 acl \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nREVOKE ALL ON FUNCTION public.agent_forms_upsert_template(p_tenant_id uuid, p_actor_user_id uuid, p_payload jsonb) FROM PUBLIC;\nGRANT ALL ON FUNCTION public.agent_forms_upsert_template(p_tenant_id uuid, p_actor_user_id uuid, p_payload jsonb) TO authenticated;\nGRANT ALL ON FUNCTION public.agent_forms_upsert_template(p_tenant_id uuid, p_actor_user_id uuid, p_payload jsonb) TO service_role;\n\nGRANT MAINTAIN ON TABLE public.plg_forms_document_files TO anon;\nGRANT ALL ON TABLE public.plg_forms_document_files TO authenticated;\nGRANT ALL ON TABLE public.plg_forms_document_files TO service_role;\n\nGRANT MAINTAIN ON TABLE public.plg_forms_documents TO anon;\nGRANT ALL ON TABLE public.plg_forms_documents TO authenticated;\nGRANT ALL ON TABLE public.plg_forms_documents TO service_role;\n\nGRANT MAINTAIN ON TABLE public.plg_forms_templates TO anon;\nGRANT ALL ON TABLE public.plg_forms_templates TO authenticated;\nGRANT ALL ON TABLE public.plg_forms_templates TO service_role;\n\nGRANT MAINTAIN ON TABLE public.plg_forms_rep_submissions TO anon;\nGRANT ALL ON TABLE public.plg_forms_rep_submissions TO authenticated;\nGRANT ALL ON TABLE public.plg_forms_rep_submissions TO service_role;\n\nGRANT MAINTAIN ON TABLE public.v_documents TO anon;\nGRANT ALL ON TABLE public.v_documents TO authenticated;\nGRANT ALL ON TABLE public.v_documents TO service_role;\n\nGRANT MAINTAIN ON TABLE public.v_forms_template_usage TO anon;\nGRANT ALL ON TABLE public.v_forms_template_usage TO authenticated;\nGRANT ALL ON TABLE public.v_forms_template_usage TO service_role;\n\n\n-- \u2500\u2500 comment \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nCOMMENT ON VIEW public.plg_forms_rep_submissions IS 'One row per form submission (a document WITH a form behind it), joined to its template and the person who filled it.';\n\nCOMMENT ON VIEW public.v_documents IS 'Documents with their template joined. security_invoker: the reader''s own RLS decides, not the migration role''s (core 043, carried forward here so re-creating 002 cannot undo it).';\n\n\n-- \u2500\u2500 default privileges: restore \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n-- Put back, so the NEXT thing installed inherits what the chain had.\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO postgres$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO postgres$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO anon$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO anon$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO authenticated$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO authenticated$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO service_role$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO service_role$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO postgres$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO postgres$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO anon$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO anon$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO authenticated$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO authenticated$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO service_role$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO service_role$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO postgres$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO postgres$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO authenticated$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO authenticated$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO service_role$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO service_role$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO postgres$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO postgres$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO anon$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO anon$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO authenticated$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO authenticated$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO service_role$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO service_role$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO postgres$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO postgres$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT MAINTAIN ON TABLES TO anon$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT MAINTAIN ON TABLES TO anon$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO authenticated$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO authenticated$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO service_role$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO service_role$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO postgres$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO postgres$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO anon$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO anon$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO authenticated$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO authenticated$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO service_role$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO service_role$stmt$;\nEND $dp$;\n\nDO $dp$ BEGIN\n EXECUTE $stmt$--\n-- PostgreSQL database dump complete\n--$stmt$;\nEXCEPTION WHEN insufficient_privilege OR undefined_object THEN\n RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$--\n-- PostgreSQL database dump complete\n--$stmt$;\nEND $dp$;\n";
|
|
8
2
|
export declare const MIGRATIONS: Array<{
|
|
9
3
|
id: string;
|
|
10
4
|
sql: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/migrations/index.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/migrations/index.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,sBAAsB,0+kCA0mBlC,CAAA;AAED,eAAO,MAAM,UAAU,EAAE,KAAK,CAAC;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAEzD,CAAA"}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"status": "beta",
|
|
5
5
|
"dependencies": []
|
|
6
6
|
},
|
|
7
|
-
"version": "0.10.
|
|
7
|
+
"version": "0.10.4",
|
|
8
8
|
"description": "Fayz SDK — plugin-forms plugin",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"sideEffects": false,
|
|
@@ -31,9 +31,9 @@
|
|
|
31
31
|
"@dnd-kit/modifiers": "^9.0.0",
|
|
32
32
|
"@dnd-kit/sortable": "^10.0.0",
|
|
33
33
|
"@dnd-kit/utilities": "^3.2.2",
|
|
34
|
-
"@fayz-ai/core": "^0.
|
|
35
|
-
"@fayz-ai/admin": "^0.
|
|
36
|
-
"@fayz-ai/ui": "^0.
|
|
34
|
+
"@fayz-ai/core": "^0.19.0",
|
|
35
|
+
"@fayz-ai/admin": "^0.19.0",
|
|
36
|
+
"@fayz-ai/ui": "^0.19.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@types/react": "^18.3.0",
|