@fayz-ai/plugin-forms 0.9.0 → 0.9.1

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.
Files changed (59) hide show
  1. package/README.md +3 -3
  2. package/dist/{FormBuilder-7SWZQKFN.js → FormBuilder-JCNHXQXT.js} +4 -4
  3. package/dist/{FormBuilder-7SWZQKFN.js.map → FormBuilder-JCNHXQXT.js.map} +1 -1
  4. package/dist/{chunk-W2ZNJGQC.js → chunk-JCAWSDX6.js} +3 -3
  5. package/dist/chunk-JCAWSDX6.js.map +1 -0
  6. package/dist/components/AddDocumentDropdown.d.ts +1 -1
  7. package/dist/components/AddDocumentDropdown.d.ts.map +1 -1
  8. package/dist/{CustomFormsContext.d.ts → context.d.ts} +1 -1
  9. package/dist/context.d.ts.map +1 -0
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +4 -4
  13. package/dist/index.js.map +1 -1
  14. package/dist/lib/agent-tools.d.ts.map +1 -0
  15. package/dist/lib/document-types.d.ts.map +1 -0
  16. package/package.json +5 -7
  17. package/dist/CustomFormsContext.d.ts.map +0 -1
  18. package/dist/agent-tools.d.ts.map +0 -1
  19. package/dist/chunk-W2ZNJGQC.js.map +0 -1
  20. package/dist/document-types.d.ts.map +0 -1
  21. package/src/CustomFormsContext.tsx +0 -28
  22. package/src/agent-tools.ts +0 -73
  23. package/src/components/AddDocumentDropdown.tsx +0 -92
  24. package/src/components/BuilderCanvas.tsx +0 -87
  25. package/src/components/BuilderField.tsx +0 -81
  26. package/src/components/DocumentFormDialog.tsx +0 -155
  27. package/src/components/DocumentList.tsx +0 -254
  28. package/src/components/FieldConfigDialog.tsx +0 -243
  29. package/src/components/FieldPalette.tsx +0 -81
  30. package/src/components/FormBuilder.tsx +0 -427
  31. package/src/components/FormRenderer.tsx +0 -256
  32. package/src/components/FormViewer.tsx +0 -94
  33. package/src/components/PersonDocumentsWidget.tsx +0 -229
  34. package/src/components/TemplateSelector.tsx +0 -91
  35. package/src/config.ts +0 -43
  36. package/src/data/index.ts +0 -3
  37. package/src/data/mock.ts +0 -193
  38. package/src/data/supabase.ts +0 -408
  39. package/src/data/tables.ts +0 -7
  40. package/src/data/types.ts +0 -33
  41. package/src/document-types.ts +0 -51
  42. package/src/index.ts +0 -234
  43. package/src/lib/person-fields.ts +0 -67
  44. package/src/lib/print.ts +0 -207
  45. package/src/lib/tenant.ts +0 -9
  46. package/src/locales/en.ts +0 -95
  47. package/src/locales/index.ts +0 -7
  48. package/src/locales/pt-BR.ts +0 -95
  49. package/src/migrations/000_plg_rename.sql +0 -21
  50. package/src/migrations/001_frm_base.sql +0 -174
  51. package/src/migrations/002_document_archetype.sql +0 -223
  52. package/src/migrations/003_agent_rpcs.sql +0 -174
  53. package/src/migrations/index.ts +0 -610
  54. package/src/store.ts +0 -167
  55. package/src/types.ts +0 -217
  56. package/src/views/CustomFormsSettingsTab.tsx +0 -105
  57. package/src/views/TemplateListView.tsx +0 -174
  58. /package/dist/{agent-tools.d.ts → lib/agent-tools.d.ts} +0 -0
  59. /package/dist/{document-types.d.ts → lib/document-types.d.ts} +0 -0
@@ -1,174 +0,0 @@
1
- -- ============================================================================
2
- -- plugin-forms 003: server-plane agent write RPC.
3
- --
4
- -- public.agent_forms_upsert_template — the assistant BUILDS or EDITS a form
5
- -- template from any surface or channel (in-app FAB, WhatsApp, MCP). Same
6
- -- contract as every agent_* RPC (agenda 005, financial 009):
7
- -- (p_tenant_id, p_actor_user_id, p_payload jsonb) → jsonb
8
- -- {ok:true, id, record:{...}} | {ok:false, denial:{...}} | {ok:false, error}
9
- --
10
- -- The grid layout is computed HERE (col 0, sequential rows, full-width span) so
11
- -- the model only ever sends {type,label,required?,options?} — never positions.
12
- --
13
- -- Payload:
14
- -- template_id? uuid — omit to CREATE, provide to EDIT
15
- -- name? text — required on create
16
- -- category? text — anamnesis|evolution|report|contract|general
17
- -- description? text
18
- -- fields? [{type,label,required?,placeholder?,options?[]}] — REPLACE all
19
- -- append_fields? [{...}] — APPEND to existing
20
- --
21
- -- GRANT authenticated+service_role — NEVER anon (the broker calls with the pool
22
- -- service key and injects tenant/actor; in-browser the signed-in user calls it
23
- -- directly and spine RLS/actor guards bind them to their own tenant).
24
- -- ============================================================================
25
-
26
- CREATE OR REPLACE FUNCTION public.agent_forms_upsert_template(
27
- p_tenant_id uuid,
28
- p_actor_user_id uuid,
29
- p_payload jsonb
30
- ) RETURNS jsonb
31
- LANGUAGE plpgsql SECURITY DEFINER
32
- SET search_path = public
33
- AS $$
34
- DECLARE
35
- v_denial jsonb;
36
- v_used int;
37
- v_template_id uuid;
38
- v_is_update boolean;
39
- v_name text;
40
- v_category text;
41
- v_description text;
42
- v_existing jsonb;
43
- v_input jsonb;
44
- v_merged jsonb;
45
- v_fields jsonb;
46
- v_schema jsonb;
47
- v_final_id uuid;
48
- BEGIN
49
- -- ── payload ──────────────────────────────────────────────────────────────
50
- BEGIN
51
- v_template_id := NULLIF(p_payload->>'template_id','')::uuid;
52
- EXCEPTION WHEN OTHERS THEN
53
- RETURN jsonb_build_object('ok', false, 'error', 'invalid template_id');
54
- END;
55
- v_is_update := v_template_id IS NOT NULL;
56
- v_name := btrim(p_payload->>'name');
57
- v_category := lower(COALESCE(NULLIF(p_payload->>'category',''), 'general'));
58
- v_description := left(p_payload->>'description', 1000);
59
- IF v_category NOT IN ('anamnesis','evolution','report','contract','general') THEN
60
- v_category := 'general';
61
- END IF;
62
-
63
- IF NOT v_is_update AND (v_name IS NULL OR v_name = '') THEN
64
- RETURN jsonb_build_object('ok', false, 'error', 'name is required to create a form');
65
- END IF;
66
-
67
- -- ── authorization: role → plan → form_templates cap ──────────────────────
68
- SELECT count(*) INTO v_used FROM plg_forms_templates
69
- WHERE tenant_id = p_tenant_id AND is_deleted = false;
70
- v_denial := agent_guard(p_tenant_id, p_actor_user_id, 'custom_forms',
71
- CASE WHEN v_is_update THEN 'update' ELSE 'create' END,
72
- 'form_templates', v_used, CASE WHEN v_is_update THEN 0 ELSE 1 END);
73
- IF v_denial IS NOT NULL THEN
74
- RETURN jsonb_build_object('ok', false, 'denial', v_denial);
75
- END IF;
76
-
77
- -- ── current fields (for APPEND / to reject an unknown id) ────────────────
78
- IF v_is_update THEN
79
- SELECT COALESCE(schema->'fields','[]'::jsonb) INTO v_existing
80
- FROM plg_forms_templates
81
- WHERE id = v_template_id AND tenant_id = p_tenant_id AND is_deleted = false;
82
- IF v_existing IS NULL THEN
83
- RETURN jsonb_build_object('ok', false, 'error', 'unknown template for this tenant');
84
- END IF;
85
- ELSE
86
- v_existing := '[]'::jsonb;
87
- END IF;
88
-
89
- -- ── normalize the incoming fields to the builder's FormFieldDef shape ────
90
- -- (id, type, label, required, placeholder, col, colSpan, options) — row is
91
- -- assigned last, across the merged list.
92
- WITH src AS (
93
- SELECT CASE
94
- WHEN p_payload ? 'fields' THEN COALESCE(p_payload->'fields','[]'::jsonb)
95
- WHEN p_payload ? 'append_fields' THEN COALESCE(p_payload->'append_fields','[]'::jsonb)
96
- ELSE '[]'::jsonb
97
- END AS arr
98
- ),
99
- norm AS (
100
- SELECT f.ord, jsonb_strip_nulls(jsonb_build_object(
101
- 'id', COALESCE(NULLIF(f.value->>'id',''), gen_random_uuid()::text),
102
- 'type', COALESCE(NULLIF(f.value->>'type',''), 'text'),
103
- 'label', COALESCE(NULLIF(f.value->>'label',''), 'Campo'),
104
- 'required', COALESCE((f.value->>'required')::boolean, false),
105
- 'placeholder', NULLIF(f.value->>'placeholder',''),
106
- 'col', 0,
107
- 'colSpan', 12,
108
- 'options', CASE
109
- WHEN jsonb_typeof(f.value->'options') = 'array' AND jsonb_array_length(f.value->'options') > 0
110
- THEN (SELECT jsonb_agg(jsonb_build_object(
111
- 'label', opt,
112
- 'value', lower(regexp_replace(btrim(opt), '\s+', '_', 'g'))))
113
- FROM jsonb_array_elements_text(f.value->'options') opt)
114
- ELSE NULL END
115
- )) AS field
116
- FROM src, jsonb_array_elements(src.arr) WITH ORDINALITY AS f(value, ord)
117
- )
118
- SELECT COALESCE(jsonb_agg(field ORDER BY ord), '[]'::jsonb) INTO v_input FROM norm;
119
-
120
- -- REPLACE with `fields`; otherwise APPEND onto what's already there.
121
- IF p_payload ? 'fields' THEN
122
- v_merged := v_input;
123
- ELSE
124
- v_merged := v_existing || v_input;
125
- END IF;
126
-
127
- -- sequential rows across the final list
128
- SELECT COALESCE(jsonb_agg(jsonb_set(e.value, '{row}', to_jsonb((e.ord - 1))) ORDER BY e.ord), '[]'::jsonb)
129
- INTO v_fields
130
- FROM jsonb_array_elements(v_merged) WITH ORDINALITY AS e(value, ord);
131
-
132
- v_schema := jsonb_build_object('layout', jsonb_build_object('columns', 12), 'fields', v_fields);
133
-
134
- -- ── write + audit ────────────────────────────────────────────────────────
135
- IF v_is_update THEN
136
- UPDATE plg_forms_templates SET
137
- name = COALESCE(NULLIF(v_name,''), name),
138
- category = v_category,
139
- description = COALESCE(v_description, description),
140
- schema = v_schema,
141
- updated_by = p_actor_user_id,
142
- updated_at = now()
143
- WHERE id = v_template_id AND tenant_id = p_tenant_id AND is_deleted = false
144
- RETURNING id INTO v_final_id;
145
- ELSE
146
- INSERT INTO plg_forms_templates (tenant_id, name, category, description, schema, created_by, updated_by)
147
- VALUES (p_tenant_id, v_name, v_category, v_description, v_schema, p_actor_user_id, p_actor_user_id)
148
- RETURNING id INTO v_final_id;
149
- END IF;
150
-
151
- INSERT INTO audit_logs (tenant_id, user_id, action, entity_type, entity_id, metadata)
152
- VALUES (p_tenant_id, p_actor_user_id,
153
- CASE WHEN v_is_update THEN 'agent.updateFormTemplate' ELSE 'agent.createFormTemplate' END,
154
- 'form_template', v_final_id::text,
155
- jsonb_build_object('payload', p_payload));
156
-
157
- RETURN jsonb_build_object(
158
- 'ok', true,
159
- 'id', v_final_id,
160
- 'record', jsonb_build_object(
161
- 'ref', jsonb_build_object('id', v_final_id, 'resource', 'plg_forms_templates',
162
- 'archetype', 'custom_forms:template'),
163
- 'name', COALESCE(NULLIF(v_name,''), (SELECT name FROM plg_forms_templates WHERE id = v_final_id)),
164
- 'category', v_category,
165
- 'fieldCount', jsonb_array_length(v_fields)
166
- )
167
- );
168
- END;
169
- $$;
170
-
171
- REVOKE ALL ON FUNCTION public.agent_forms_upsert_template(uuid, uuid, jsonb) FROM public;
172
- REVOKE EXECUTE ON FUNCTION public.agent_forms_upsert_template(uuid, uuid, jsonb) FROM anon;
173
- GRANT EXECUTE ON FUNCTION public.agent_forms_upsert_template(uuid, uuid, jsonb)
174
- TO authenticated, service_role;