@ai-matrx/records 0.11.0 → 0.17.0

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.
@@ -72,6 +72,9 @@ __export(core_exports, {
72
72
  asWriteConflict: () => asWriteConflict,
73
73
  atLeast: () => atLeast,
74
74
  cellText: () => cellText,
75
+ choiceSlug: () => choiceSlug,
76
+ choiceValuesOf: () => choiceValuesOf,
77
+ choicesOf: () => choicesOf,
75
78
  createRecordsClient: () => createRecordsClient,
76
79
  doorAbsent: () => doorAbsent,
77
80
  doorCensus: () => doorCensus,
@@ -80,15 +83,21 @@ __export(core_exports, {
80
83
  exportCsv: () => exportCsv,
81
84
  exportXlsx: () => exportXlsx,
82
85
  highestLevel: () => highestLevel,
86
+ holdsARetiredChoice: () => holdsARetiredChoice,
83
87
  importCsv: () => importCsv,
84
88
  importXlsx: () => importXlsx,
85
89
  intersectIds: () => intersectIds,
86
90
  isRecordsErr: () => isRecordsErr,
91
+ isTheChosen: () => isTheChosen,
92
+ looksLikeAnId: () => looksLikeAnId,
87
93
  mapPgError: () => mapPgError,
88
94
  measureKey: () => measureKey,
89
95
  ok: () => ok,
96
+ optionKey: () => optionKey,
97
+ optionLabel: () => optionLabel,
90
98
  parseCsv: () => parseCsv,
91
99
  planQuery: () => planQuery,
100
+ portalPath: () => portalPath,
92
101
  predictEnvelopeRefusal: () => predictEnvelopeRefusal,
93
102
  predictSizeRefusal: () => predictSizeRefusal,
94
103
  predictValueRefusals: () => predictValueRefusals,
@@ -183,11 +192,59 @@ function highestLevel(left, right) {
183
192
  return ORDINAL[left] >= ORDINAL[right] ? left : right;
184
193
  }
185
194
 
195
+ // src/choice.ts
196
+ var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
197
+ function optionKey(option) {
198
+ const stored = option.metadata?.["option_key"];
199
+ if (typeof stored === "string" && stored.trim() !== "") return stored;
200
+ return choiceSlug(optionLabel(option));
201
+ }
202
+ function optionLabel(option) {
203
+ const doc = option.data;
204
+ const title = doc?.["title"] ?? doc?.["name"];
205
+ if (typeof title === "string" && title.trim() !== "") return title;
206
+ return "Unnamed choice";
207
+ }
208
+ function choiceSlug(word) {
209
+ const slug = word.trim().toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "").slice(0, 55);
210
+ return slug === "" ? "choice" : slug;
211
+ }
212
+ function isTheChosen(option, value) {
213
+ if (typeof value !== "string") return false;
214
+ const asked = value.trim().toLowerCase();
215
+ if (asked === "") return false;
216
+ return asked === optionKey(option).toLowerCase() || asked === optionLabel(option).toLowerCase() || asked === option.id.toLowerCase();
217
+ }
218
+ function choiceValuesOf(value) {
219
+ if (Array.isArray(value)) return value.filter((v) => typeof v === "string");
220
+ if (typeof value === "string" && value !== "") return [value];
221
+ return [];
222
+ }
223
+ function choicesOf(document, fieldKeyOrId) {
224
+ const block = document?.["_choices"];
225
+ if (!block || typeof block !== "object") return [];
226
+ const entry = block[fieldKeyOrId];
227
+ if (!entry) return [];
228
+ const list = Array.isArray(entry) ? entry : [entry];
229
+ return list.filter((x) => !!x && typeof x === "object" && typeof x.key === "string");
230
+ }
231
+ function holdsARetiredChoice(document, fieldKeyOrId) {
232
+ return choicesOf(document, fieldKeyOrId).some((c) => c.retired === true);
233
+ }
234
+ function looksLikeAnId(value) {
235
+ return typeof value === "string" && UUID.test(value);
236
+ }
237
+
186
238
  // src/rule.ts
187
239
  function publicFormPath(formId) {
188
240
  return `/f/${formId}`;
189
241
  }
190
242
 
243
+ // src/portal.ts
244
+ function portalPath(slug) {
245
+ return `/portal/c/${slug}`;
246
+ }
247
+
191
248
  // src/aggregate.ts
192
249
  function measureKey(measure) {
193
250
  return measure.op === "count" ? "count" : `${measure.op}_${measure.key ?? ""}`;
@@ -302,7 +359,24 @@ var STORE_DOORS = [
302
359
  { name: "caller_role", args: "", returns: "name", security: "invoker" },
303
360
  { name: "carrying_edges_in", args: "p_organization_id uuid", returns: "TABLE(container_type text, container_id uuid, item_type text, item_id uuid, conveys_max permission_level)", security: "definer" },
304
361
  { name: "carrying_edges_of", args: "p_item_type text, p_item_id uuid", returns: "TABLE(container_type text, container_id uuid, conveys_max permission_level)", security: "definer" },
362
+ { name: "choice_census", args: "p_organization_id uuid, p_table_id uuid DEFAULT NULL::uuid", returns: "jsonb", security: "definer" },
363
+ { name: "choice_field_map", args: "p_organization_id uuid, p_table_id uuid", returns: "jsonb", security: "invoker" },
364
+ { name: "choice_filter_normalize", args: "p_map jsonb, p_filter jsonb", returns: "jsonb", security: "invoker" },
365
+ { name: "choice_key_for", args: "p_organization_id uuid, p_options_table_id uuid, p_title text, p_exclude uuid DEFAULT NULL::uuid", returns: "text", security: "invoker" },
366
+ { name: "choice_key_of", args: "p_field jsonb, p_token text", returns: "text", security: "invoker" },
367
+ { name: "choice_options", args: "p_organization_id uuid, p_options_table_id uuid", returns: "jsonb", security: "invoker" },
368
+ { name: "choice_render", args: "p_organization_id uuid, p_table_id uuid, p_doc jsonb", returns: "jsonb", security: "invoker" },
369
+ { name: "choice_render_groups", args: "p_map jsonb, p_groups jsonb", returns: "jsonb", security: "invoker" },
370
+ { name: "choice_render_note", args: "p_field jsonb, p_value jsonb", returns: "jsonb", security: "invoker" },
371
+ { name: "choice_render_value", args: "p_field jsonb, p_value jsonb", returns: "jsonb", security: "invoker" },
372
+ { name: "choice_slug", args: "p_word text", returns: "text", security: "invoker" },
373
+ { name: "choice_synonyms", args: "p_organization_id uuid, p_table_id uuid, p_token text", returns: "text[]", security: "invoker" },
374
+ { name: "choice_synonyms_in", args: "p_map jsonb, p_token text", returns: "text[]", security: "invoker" },
375
+ { name: "choice_words", args: "p_field jsonb", returns: "text", security: "invoker" },
305
376
  { name: "client_write_grants", args: "", returns: "TABLE(role_name text, object_name text, privilege text)", security: "invoker" },
377
+ { name: "comment_mention_deliver", args: "p_organization_id uuid, p_record_id uuid, p_table_id uuid, p_comment_id uuid, p_recipient uuid, p_author_name text, p_record_title text, p_body text", returns: "uuid", security: "definer" },
378
+ { name: "comment_thread", args: "p_organization_id uuid, p_record_id uuid, p_include_resolved boolean DEFAULT false", returns: "jsonb", security: "definer" },
379
+ { name: "comment_write", args: "p_organization_id uuid, p_record_id uuid, p_body text, p_anchor jsonb DEFAULT '{}'::jsonb, p_parent_comment_id uuid DEFAULT NULL::uuid, p_mentions uuid[] DEFAULT NULL::uuid[]", returns: "jsonb", security: "definer" },
306
380
  { name: "computed_provenance", args: "p_organization_id uuid, p_record_id uuid", returns: "TABLE(field_key text, field_id uuid, value jsonb, rule_id uuid, rule_version integer, computed_at timestamp with time zone)", security: "invoker" },
307
381
  { name: "containment_chain", args: "p_organization_id uuid, p_record_id uuid", returns: "TABLE(ancestor_id uuid, depth integer)", security: "invoker" },
308
382
  { name: "containment_depth_ceiling", args: "p_organization_id uuid DEFAULT NULL::uuid", returns: "integer", security: "invoker" },
@@ -310,6 +384,17 @@ var STORE_DOORS = [
310
384
  { name: "containment_parent", args: "p_data jsonb", returns: "uuid", security: "invoker" },
311
385
  { name: "cross_organization_links_open", args: "p_source_organization_id uuid, p_target_organization_id uuid", returns: "boolean", security: "invoker" },
312
386
  { name: "custom_fields_tables", args: "", returns: "TABLE(token text, schema_name text, table_name text)", security: "invoker" },
387
+ { name: "dashboard_block_normalize", args: "p_organization_id uuid, p_subject_table_id uuid, p_block jsonb", returns: "jsonb", security: "definer" },
388
+ { name: "dashboard_class", args: "", returns: "text", security: "invoker" },
389
+ { name: "dashboard_declare", args: "p_organization_id uuid, p_table_id uuid, p_name text, p_blocks jsonb DEFAULT '[]'::jsonb, p_presentation jsonb DEFAULT '{}'::jsonb, p_dashboard_id uuid DEFAULT NULL::uuid", returns: "uuid", security: "definer" },
390
+ { name: "dashboard_delete", args: "p_organization_id uuid, p_dashboard_id uuid", returns: "boolean", security: "definer" },
391
+ { name: "dashboard_field_keys", args: "p_organization_id uuid, p_table_id uuid", returns: "text[]", security: "definer" },
392
+ { name: "dashboard_kinds", args: "", returns: "text[]", security: "invoker" },
393
+ { name: "dashboard_moment_sql", args: "p_key text", returns: "text", security: "invoker" },
394
+ { name: "dashboard_run", args: "p_organization_id uuid, p_dashboard_id uuid, p_filter jsonb DEFAULT '{}'::jsonb", returns: "jsonb", security: "definer" },
395
+ { name: "dashboard_stuck", args: "p_organization_id uuid, p_table_id uuid, p_state_key text, p_days integer DEFAULT 14, p_filter jsonb DEFAULT '{}'::jsonb, p_limit integer DEFAULT 50, p_required text DEFAULT 'viewer'::text", returns: "TABLE(record_id uuid, title text, state text, last_changed_at timestamp with time zone, days_unchanged numeric, measured_from text)", security: "definer" },
396
+ { name: "dashboard_window_sql", args: "p_key text, p_window jsonb", returns: "text", security: "invoker" },
397
+ { name: "dashboards", args: "p_organization_id uuid, p_table_id uuid DEFAULT NULL::uuid", returns: "TABLE(dashboard_id uuid, table_id uuid, name text, blocks jsonb, presentation jsonb, block_count integer, version integer, created_at timestamp with time zone, updated_at timestamp with time zone)", security: "definer" },
313
398
  { name: "delete_cascade_closure", args: "p_organization_id uuid, p_record_id uuid", returns: "uuid[]", security: "invoker" },
314
399
  { name: "delete_preview", args: "p_organization_id uuid, p_record_id uuid", returns: "jsonb", security: "definer" },
315
400
  { name: "delete_rule", args: "p_organization_id uuid, p_record_id uuid, p_apply boolean DEFAULT true", returns: "jsonb", security: "definer" },
@@ -324,13 +409,17 @@ var STORE_DOORS = [
324
409
  { name: "doc_render_document", args: "p_organization_id uuid, p_template_id uuid, p_record_id uuid", returns: "uuid", security: "definer" },
325
410
  { name: "doc_render_read", args: "p_organization_id uuid, p_render_id uuid", returns: "jsonb", security: "definer" },
326
411
  { name: "doc_render_write", args: "p_organization_id uuid, p_template_id uuid, p_record_id uuid, p_table_id uuid, p_template_version integer, p_body text, p_content_hash text", returns: "uuid", security: "definer" },
412
+ { name: "doc_renders", args: "p_organization_id uuid, p_record_id uuid", returns: "TABLE(render_id uuid, template_id uuid, record_id uuid, table_id uuid, template_version integer, body text, content_hash text, rendered_at timestamp with time zone)", security: "definer" },
327
413
  { name: "doc_sign", args: "p_organization_id uuid, p_render_id uuid, p_field_key text, p_signer_name text, p_signer_user_id uuid DEFAULT NULL::uuid", returns: "uuid", security: "definer" },
328
414
  { name: "doc_signature_field_ok", args: "p_field_data jsonb", returns: "boolean", security: "invoker" },
329
415
  { name: "doc_signature_intact", args: "p_organization_id uuid, p_signature_id uuid", returns: "jsonb", security: "invoker" },
330
416
  { name: "doc_signature_read", args: "p_organization_id uuid, p_signature_id uuid", returns: "jsonb", security: "definer" },
331
417
  { name: "doc_signature_write", args: "p_organization_id uuid, p_render_id uuid, p_record_id uuid, p_field_key text, p_signer_name text, p_signer_user_id uuid, p_document_hash text, p_document_version integer", returns: "uuid", security: "definer" },
418
+ { name: "doc_signatures", args: "p_organization_id uuid, p_record_id uuid", returns: "TABLE(signature_id uuid, render_id uuid, record_id uuid, field_key text, signer_name text, signer_user_id uuid, signed_at timestamp with time zone, document_hash text, document_version integer)", security: "definer" },
419
+ { name: "doc_template_delete", args: "p_organization_id uuid, p_template_id uuid", returns: "boolean", security: "definer" },
332
420
  { name: "doc_template_read", args: "p_organization_id uuid, p_template_id uuid", returns: "jsonb", security: "definer" },
333
421
  { name: "doc_template_save", args: "p_organization_id uuid, p_table_id uuid, p_name text, p_body text, p_template_id uuid DEFAULT NULL::uuid", returns: "uuid", security: "definer" },
422
+ { name: "doc_templates", args: "p_organization_id uuid, p_table_id uuid", returns: "TABLE(template_id uuid, renders_table_id uuid, name text, body text, template_version integer, token_count bigint, updated_at timestamp with time zone)", security: "definer" },
334
423
  { name: "doc_token_pattern", args: "", returns: "text", security: "invoker" },
335
424
  { name: "doc_tokens", args: "p_body text", returns: "TABLE(ordinal integer, raw text, field_id uuid)", security: "invoker" },
336
425
  { name: "doc_unresolved_tokens", args: "p_organization_id uuid, p_table_id uuid, p_body text", returns: "TABLE(raw text, field_id uuid, why text)", security: "invoker" },
@@ -359,6 +448,7 @@ var STORE_DOORS = [
359
448
  { name: "field_behaviour", args: "p_field_data jsonb", returns: "text", security: "invoker" },
360
449
  { name: "field_declare", args: "p_organization_id uuid, p_table_id uuid, p_spec jsonb", returns: "uuid", security: "definer" },
361
450
  { name: "field_dependants", args: "p_organization_id uuid, p_field_id uuid", returns: "TABLE(kind text, dependant_id uuid, label text, how text)", security: "definer" },
451
+ { name: "field_history", args: "p_organization_id uuid, p_table_id uuid, p_field_key text, p_limit integer DEFAULT 100, p_offset integer DEFAULT 0, p_record_id uuid DEFAULT NULL::uuid", returns: "TABLE(record_id uuid, record_title text, version integer, occurred_at timestamp with time zone, operation_label text, actor jsonb, before jsonb, after jsonb)", security: "definer" },
362
452
  { name: "field_kernel_id", args: "", returns: "uuid", security: "invoker" },
363
453
  { name: "field_options", args: "p_organization_id uuid, p_field_id uuid", returns: "SETOF custom.record", security: "definer" },
364
454
  { name: "field_retire", args: "p_organization_id uuid, p_field_id uuid", returns: "boolean", security: "definer" },
@@ -377,7 +467,11 @@ var STORE_DOORS = [
377
467
  { name: "has_visibility", args: "p_user_id uuid, p_type text, p_id uuid, p_required permission_level DEFAULT 'viewer'::permission_level", returns: "boolean", security: "definer" },
378
468
  { name: "has_visibility_at", args: "p_user_id uuid, p_type text, p_id uuid, p_required permission_level DEFAULT 'viewer'::permission_level, p_organization_id uuid DEFAULT NULL::uuid, p_min_version bigint DEFAULT NULL::bigint", returns: "boolean", security: "definer" },
379
469
  { name: "hidden_field_notice", args: "p_field custom.record, p_action text DEFAULT 'read'::text", returns: "jsonb", security: "invoker" },
470
+ { name: "history_actor", args: "p_tier text, p_document jsonb, p_actor_id uuid, p_people jsonb", returns: "jsonb", security: "invoker" },
471
+ { name: "history_changes", args: "p_organization_id uuid, p_table_id uuid, p_old jsonb, p_new jsonb", returns: "jsonb", security: "invoker" },
472
+ { name: "history_people", args: "p_organization_id uuid, p_ids uuid[]", returns: "jsonb", security: "definer" },
380
473
  { name: "history_prune", args: "p_organization_id uuid, p_scope text DEFAULT 'values'::text, p_table_id uuid DEFAULT NULL::uuid, p_dry_run boolean DEFAULT true", returns: "jsonb", security: "definer" },
474
+ { name: "history_restore_body", args: "p_current jsonb, p_target jsonb, p_field_key text DEFAULT NULL::text", returns: "jsonb", security: "invoker" },
381
475
  { name: "history_retention", args: "p_organization_id uuid, p_table_id uuid DEFAULT NULL::uuid", returns: "jsonb", security: "definer" },
382
476
  { name: "history_retention_floor_raise", args: "p_organization_id uuid, p_days integer", returns: "integer", security: "definer" },
383
477
  { name: "history_retention_set", args: "p_organization_id uuid, p_table_id uuid, p_days integer", returns: "integer", security: "definer" },
@@ -406,16 +500,20 @@ var STORE_DOORS = [
406
500
  { name: "io_restore", args: "p_organization_id uuid, p_record_id uuid, p_version integer", returns: "integer", security: "definer" },
407
501
  { name: "io_revisions", args: "p_organization_id uuid, p_record_id uuid", returns: "TABLE(version integer, changed_at timestamp with time zone, changed_by uuid, summary text, operation text, changed_fields jsonb)", security: "definer" },
408
502
  { name: "is_a_retirement", args: "p_old_deleted timestamp with time zone, p_new_deleted timestamp with time zone, p_old_data jsonb, p_new_data jsonb, p_old_table uuid, p_new_table uuid, p_old_org uuid, p_new_org uuid, p_old_class text, p_new_class text", returns: "boolean", security: "invoker" },
503
+ { name: "list_door_disagreements", args: "p_pretend text DEFAULT NULL::text, p_only_organization uuid DEFAULT NULL::uuid, p_sample integer DEFAULT 200", returns: "TABLE(organization_id uuid, organization_name text, member_id uuid, table_id uuid, record_id uuid, door text, why text)", security: "invoker" },
504
+ { name: "list_door_disagreements", args: "p_pretend text, p_only_organization uuid, p_sample integer, p_exhaustive boolean", returns: "TABLE(organization_id uuid, organization_name text, member_id uuid, table_id uuid, record_id uuid, door text, why text)", security: "invoker" },
409
505
  { name: "lookup_value", args: "p_organization_id uuid, p_record_id uuid, p_field_data jsonb", returns: "jsonb", security: "invoker" },
410
506
  { name: "mask_document", args: "p_document jsonb, p_visible_keys text[], p_notices jsonb, p_by_id boolean DEFAULT false, p_key_ids jsonb DEFAULT '{}'::jsonb", returns: "jsonb", security: "invoker" },
411
507
  { name: "mask_document", args: "p_document jsonb, p_visible_keys text[], p_notices jsonb, p_by_id boolean DEFAULT false, p_key_ids jsonb DEFAULT '{}'::jsonb, p_declared_keys text[] DEFAULT NULL::text[]", returns: "jsonb", security: "invoker" },
412
508
  { name: "merge_field_kernel_id", args: "", returns: "uuid", security: "invoker" },
509
+ { name: "migrate_choice_keys", args: "p_organization_id uuid, p_table_id uuid, p_dry_run boolean DEFAULT false", returns: "jsonb", security: "definer" },
413
510
  { name: "migrate_delete", args: "p_organization_id uuid, p_record_id uuid, p_note text DEFAULT NULL::text", returns: "jsonb", security: "definer" },
414
511
  { name: "migrate_demote", args: "p_organization_id uuid, p_table_id uuid, p_note text DEFAULT NULL::text", returns: "jsonb", security: "definer" },
415
512
  { name: "migrate_extract_parent", args: "p_organization_id uuid, p_id uuid, p_parent_table_id uuid, p_moved_keys text[], p_note text DEFAULT NULL::text", returns: "jsonb", security: "definer" },
416
513
  { name: "migrate_merge", args: "p_organization_id uuid, p_winner_id uuid, p_loser_id uuid, p_note text DEFAULT NULL::text", returns: "jsonb", security: "definer" },
417
514
  { name: "migrate_promote", args: "p_organization_id uuid, p_table_id uuid, p_note text DEFAULT NULL::text", returns: "jsonb", security: "definer" },
418
515
  { name: "migrate_purge", args: "p_organization_id uuid, p_table_id uuid DEFAULT NULL::uuid, p_dry_run boolean DEFAULT true", returns: "jsonb", security: "definer" },
516
+ { name: "migrate_reclass", args: "p_organization_id uuid, p_id uuid, p_to text DEFAULT 'field'::text, p_note text DEFAULT NULL::text", returns: "jsonb", security: "definer" },
419
517
  { name: "migrate_rename", args: "p_organization_id uuid, p_id uuid, p_to text, p_note text DEFAULT NULL::text", returns: "jsonb", security: "definer" },
420
518
  { name: "migrate_reparent", args: "p_organization_id uuid, p_id uuid, p_parent_id uuid, p_note text DEFAULT NULL::text", returns: "jsonb", security: "definer" },
421
519
  { name: "migrate_retype", args: "p_organization_id uuid, p_id uuid, p_to text, p_note text DEFAULT NULL::text", returns: "jsonb", security: "definer" },
@@ -435,6 +533,20 @@ var STORE_DOORS = [
435
533
  { name: "parity_values", args: "p_organization_id uuid, p_record_id uuid", returns: "TABLE(field_key text, field_id uuid, parity_type text, behavior text, unit text, format text, value jsonb, value_version integer, actor text, written_at timestamp with time zone)", security: "invoker" },
436
534
  { name: "per_value_access_words", args: "", returns: "text[]", security: "invoker" },
437
535
  { name: "person_kernel_id", args: "", returns: "uuid", security: "invoker" },
536
+ { name: "portal_admits", args: "p_organization_id uuid, p_user_id uuid DEFAULT NULL::uuid", returns: "boolean", security: "definer" },
537
+ { name: "portal_card", args: "p_organization_id uuid, p_portal_id uuid", returns: "jsonb", security: "definer" },
538
+ { name: "portal_declare", args: "p_organization_id uuid, p_title text, p_client_table_id uuid, p_tables jsonb, p_portal_id uuid DEFAULT NULL::uuid, p_slug text DEFAULT NULL::text, p_sign_in_method text DEFAULT 'magic_link'::text", returns: "uuid", security: "definer" },
539
+ { name: "portal_field_map", args: "p_organization_id uuid, p_table_id uuid", returns: "TABLE(field_id uuid, field_key text, field_type text, points_at uuid)", security: "definer" },
540
+ { name: "portal_invitation", args: "p_slug text, p_email text", returns: "jsonb", security: "definer" },
541
+ { name: "portal_invite", args: "p_organization_id uuid, p_portal_id uuid, p_client_record_id uuid, p_email text, p_user_id uuid DEFAULT NULL::uuid", returns: "jsonb", security: "definer" },
542
+ { name: "portal_me", args: "", returns: "jsonb", security: "definer" },
543
+ { name: "portal_preview", args: "p_organization_id uuid, p_portal_id uuid, p_principal_id uuid, p_table_id uuid", returns: "TABLE(record_id uuid, title text)", security: "definer" },
544
+ { name: "portal_principal_bind", args: "p_organization_id uuid, p_principal_id uuid, p_user_id uuid", returns: "jsonb", security: "definer" },
545
+ { name: "portal_public", args: "p_slug text", returns: "jsonb", security: "definer" },
546
+ { name: "portal_record_title", args: "p_organization_id uuid, p_record_id uuid", returns: "text", security: "definer" },
547
+ { name: "portal_revoke", args: "p_organization_id uuid, p_portal_id uuid, p_principal_id uuid", returns: "jsonb", security: "definer" },
548
+ { name: "portal_slug", args: "p_organization_id uuid, p_title text, p_portal_id uuid DEFAULT NULL::uuid", returns: "text", security: "definer" },
549
+ { name: "portals", args: "p_organization_id uuid", returns: "TABLE(portal_id uuid, title text, slug text, client_table_id uuid, client_table text, is_active boolean, tables integer, invited integer, signed_in integer, sign_in_method text, opened_at timestamp with time zone)", security: "definer" },
438
550
  { name: "presentation_kernel_id", args: "", returns: "uuid", security: "invoker" },
439
551
  { name: "promote_field", args: "p_organization_id uuid, p_table_id uuid, p_field_id uuid", returns: "jsonb", security: "invoker" },
440
552
  { name: "promote_table", args: "p_organization_id uuid, p_table_id uuid", returns: "jsonb", security: "definer" },
@@ -480,15 +592,20 @@ var STORE_DOORS = [
480
592
  { name: "record_card", args: "p_viewer uuid, p_organization_id uuid, p_record_id uuid", returns: "jsonb", security: "definer" },
481
593
  { name: "record_carrying_edges", args: "p_id uuid, p_data_class text, p_data jsonb, p_deleted_at timestamp with time zone", returns: "TABLE(container_id uuid, item_id uuid, edge_role text)", security: "invoker" },
482
594
  { name: "record_delete", args: "p_organization_id uuid, p_record_id uuid", returns: "timestamp with time zone", security: "definer" },
595
+ { name: "record_history", args: "p_organization_id uuid, p_record_id uuid, p_limit integer DEFAULT 200, p_offset integer DEFAULT 0", returns: "TABLE(version integer, occurred_at timestamp with time zone, operation text, operation_label text, actor jsonb, changes jsonb, migration_id uuid, undoable boolean)", security: "definer" },
483
596
  { name: "record_relation_edges", args: "p_organization_id uuid, p_id uuid, p_table_id uuid, p_data_class text, p_data jsonb, p_deleted_at timestamp with time zone", returns: "TABLE(target_id uuid, edge_role text, field_id uuid, ord integer)", security: "invoker" },
484
597
  { name: "record_reparent", args: "p_organization_id uuid, p_record_id uuid, p_parent_id uuid", returns: "void", security: "definer" },
485
598
  { name: "record_resolve", args: "p_organization_id uuid, p_id uuid", returns: "jsonb", security: "definer" },
486
599
  { name: "record_restore", args: "p_organization_id uuid, p_record_id uuid", returns: "void", security: "definer" },
600
+ { name: "record_restore_preview", args: "p_organization_id uuid, p_record_id uuid, p_version integer, p_field_key text DEFAULT NULL::text", returns: "jsonb", security: "definer" },
601
+ { name: "record_restore_version", args: "p_organization_id uuid, p_record_id uuid, p_version integer", returns: "jsonb", security: "definer" },
487
602
  { name: "record_state_as_of", args: "p_record_id uuid, p_at timestamp with time zone", returns: "TABLE(state jsonb, replayed boolean)", security: "definer" },
603
+ { name: "record_table", args: "p_organization_id uuid, p_record_id uuid", returns: "uuid", security: "definer" },
488
604
  { name: "record_update", args: "p_organization_id uuid, p_record_id uuid, p_patch jsonb, p_expected_version integer DEFAULT NULL::integer", returns: "integer", security: "definer" },
489
605
  { name: "record_values", args: "p_organization_id uuid, p_record_id uuid", returns: "jsonb", security: "invoker" },
490
606
  { name: "record_values_versioned", args: "p_organization_id uuid, p_record_id uuid", returns: "TABLE(field_key text, field_id uuid, value jsonb, value_version integer, source jsonb, absent_reason text, actor text, on_behalf_of text, written_at timestamp with time zone, alternates jsonb)", security: "definer" },
491
607
  { name: "record_write", args: "p_organization_id uuid, p_table_id uuid, p_data jsonb", returns: "uuid", security: "definer" },
608
+ { name: "refusals_claiming_a_level_never_asked", args: "", returns: "TABLE(function_name text, identity_args text, why text)", security: "invoker" },
492
609
  { name: "relation_carry", args: "p_organization_id uuid, p_container_id uuid, p_item_id uuid", returns: "uuid", security: "definer" },
493
610
  { name: "relation_edges_withdraw", args: "p_organization_id uuid, p_field_ids uuid[], p_why text", returns: "integer", security: "definer" },
494
611
  { name: "relation_own", args: "p_organization_id uuid, p_owner_id uuid, p_target_id uuid", returns: "uuid", security: "definer" },
@@ -534,7 +651,11 @@ var STORE_DOORS = [
534
651
  { name: "stamp_value_envelopes", args: "p_data jsonb, p_actor text, p_on_behalf_of text, p_at timestamp with time zone", returns: "jsonb", security: "invoker" },
535
652
  { name: "storage_modes", args: "", returns: "text[]", security: "invoker" },
536
653
  { name: "store_is_open", args: "p_organization_id uuid DEFAULT NULL::uuid", returns: "boolean", security: "invoker" },
654
+ { name: "subscription_cadences", args: "p_organization_id uuid", returns: "text[]", security: "definer" },
655
+ { name: "subscription_mute", args: "p_organization_id uuid, p_rule_id uuid, p_muted boolean DEFAULT true", returns: "boolean", security: "definer" },
656
+ { name: "subscriptions", args: "p_organization_id uuid, p_table_id uuid DEFAULT NULL::uuid", returns: "TABLE(rule_id uuid, name text, table_id uuid, saved_view_id uuid, cadence text, schedule text, channel text, recipient_user_id uuid, event_key text, muted boolean, mine boolean, i_may_mute boolean)", security: "definer" },
537
657
  { name: "table_capacity", args: "p_organization_id uuid, p_table_id uuid", returns: "jsonb", security: "definer" },
658
+ { name: "table_carries_its_rows", args: "p_user_id uuid, p_table_id uuid, p_required permission_level DEFAULT 'viewer'::permission_level", returns: "boolean", security: "definer" },
538
659
  { name: "table_contents", args: "p_organization_id uuid, p_table_id uuid", returns: "TABLE(record_id uuid, kind text, goes_at integer)", security: "invoker" },
539
660
  { name: "table_declare", args: "p_organization_id uuid, p_spec jsonb", returns: "uuid", security: "definer" },
540
661
  { name: "table_has_a_visible_record", args: "p_user uuid, p_organization_id uuid, p_table_id uuid", returns: "boolean", security: "definer" },
@@ -556,6 +677,7 @@ var STORE_DOORS = [
556
677
  { name: "value_envelope_ok", args: "p_data jsonb", returns: "boolean", security: "invoker" },
557
678
  { name: "value_envelope_refusal", args: "p_data jsonb", returns: "text", security: "invoker" },
558
679
  { name: "value_read", args: "p_organization_id uuid, p_record_id uuid, p_key text", returns: "TABLE(field_key text, field_id uuid, value jsonb, value_version integer, source jsonb, absent_reason text, actor text, on_behalf_of text, written_at timestamp with time zone, alternates jsonb)", security: "definer" },
680
+ { name: "value_restore", args: "p_organization_id uuid, p_record_id uuid, p_field_key text, p_version integer", returns: "jsonb", security: "definer" },
559
681
  { name: "value_versions", args: "p_old jsonb, p_new jsonb", returns: "jsonb", security: "invoker" },
560
682
  { name: "visibility_ancestors", args: "p_item_type text, p_item_id uuid", returns: "TABLE(container_type text, container_id uuid, depth integer, max_level permission_level)", security: "definer" },
561
683
  { name: "visibility_as_of", args: "p_organization_id uuid, p_record_id uuid, p_at timestamp with time zone", returns: "TABLE(principal_kind text, principal_id uuid, level permission_level, through_kind text, through_id uuid, reason text, replayed boolean, held_from timestamp with time zone, held_to timestamp with time zone)", security: "definer" },
@@ -568,6 +690,7 @@ var STORE_DOORS = [
568
690
  { name: "widget_kernel_id", args: "", returns: "uuid", security: "invoker" },
569
691
  { name: "work_approval_approvers", args: "p_organization_id uuid, p_subject_id uuid, p_approver_id uuid DEFAULT NULL::uuid", returns: "TABLE(user_id uuid, name text, why text)", security: "definer" },
570
692
  { name: "work_approval_decide", args: "p_organization_id uuid, p_approval_id uuid, p_approve boolean, p_note text DEFAULT NULL::text", returns: "jsonb", security: "definer" },
693
+ { name: "work_approval_kinds", args: "", returns: "text[]", security: "invoker" },
571
694
  { name: "work_approval_may_decide", args: "p_organization_id uuid, p_approval_id uuid", returns: "boolean", security: "definer" },
572
695
  { name: "work_approval_read", args: "p_organization_id uuid, p_approval_id uuid", returns: "jsonb", security: "definer" },
573
696
  { name: "work_approval_request", args: "p_organization_id uuid, p_subject_id uuid, p_change jsonb, p_note text DEFAULT NULL::text, p_approver_id uuid DEFAULT NULL::uuid, p_origin text DEFAULT 'person'::text, p_conversation_id uuid DEFAULT NULL::uuid", returns: "jsonb", security: "definer" },
@@ -587,6 +710,7 @@ var STORE_DOORS = [
587
710
  { name: "work_slot_index_name", args: "p_table_id uuid", returns: "text", security: "invoker" },
588
711
  { name: "work_slot_release", args: "p_organization_id uuid, p_hold_id uuid", returns: "boolean", security: "definer" },
589
712
  { name: "work_slots_declare", args: "p_organization_id uuid, p_name text, p_slug text, p_home_id uuid DEFAULT NULL::uuid", returns: "jsonb", security: "definer" },
713
+ { name: "work_state_id", args: "p_organization_id uuid, p_table_id uuid, p_value text", returns: "uuid", security: "invoker" },
590
714
  { name: "work_states", args: "", returns: "TABLE(sort integer, name text, terminal boolean, next text[])", security: "invoker" },
591
715
  { name: "work_take_assignment", args: "p_organization_id uuid, p_table_id uuid", returns: "jsonb", security: "definer" },
592
716
  { name: "work_template_declare", args: "p_organization_id uuid, p_name text, p_graph jsonb", returns: "uuid", security: "definer" },
@@ -604,6 +728,7 @@ var STORE_KNOBS = [
604
728
  { key: "code_paths_enabled", value: "false", type: "boolean" },
605
729
  { key: "consumer_chat_seeding_enabled", value: "false", type: "boolean" },
606
730
  { key: "consumer_checkout_enabled", value: "false", type: "boolean" },
731
+ { key: "consumer_context_enabled", value: "false", type: "boolean" },
607
732
  { key: "consumer_education_enabled", value: "false", type: "boolean" },
608
733
  { key: "consumer_extension_enabled", value: "false", type: "boolean" },
609
734
  { key: "consumer_grid_enabled", value: "false", type: "boolean" },
@@ -635,17 +760,18 @@ var STORE_KNOBS = [
635
760
  { key: "world_publish_enabled", value: "false", type: "boolean" }
636
761
  ];
637
762
  var STORE_REFUSAL_CODES = [
638
- { code: "02000", raised_by: "doc_render_body,doc_render_document,doc_render_read,doc_sign,doc_signature_intact,doc_signature_read,doc_template_read,doc_template_save,entity_field_retire,entity_field_update,entity_record_read,entity_value_write,external_history_event,external_rows,external_stub_upsert,external_writes_set,migrate_delete,migrate_extract_parent,migrate_merge,migrate_rename,migrate_reparent,migrate_retype,migrate_split,migrate_undo,organization_clear,read_record,record_delete,record_restore,record_update,relation_uncarry,share_access,share_grant,share_lane_set,share_people,share_revoke,visibility_as_of,work_approval_decide,work_approval_read,work_approval_request,work_assign,work_record_states,work_set_state" },
639
- { code: "0A000", raised_by: "external_rows,external_source_declare,external_write_through,promote_field,promoted_index_expr,promoted_query_sql,promoted_read,rule_eval,work_assign,work_slots_declare" },
640
- { code: "22004", raised_by: "_entity_custom_fields_guard,_value_envelope,actor_word,agg_assert_key,agg_sql,anon_capture,anon_token_issue,anon_write,assert_client_may_reach,doc_render_write,doc_sign,doc_signature_write,doc_template_save,external_history_event,external_rows,external_source_declare,external_stub_upsert,external_write_through,external_writes_set,form_declare,form_submit,home_add,io_comment_write,io_import_open,io_outbox_drain,io_restore,migrate_purge,migrate_split,migrate_undo,organization_clear,organization_contents,query_across_homes,query_by_coordinates,query_rollup,record_delete,record_reparent,record_resolve,record_restore,record_update,record_write,relation_carry,relation_own,rule_declare,share_grant,table_declare,work_approval_decide,work_approval_request,work_template_declare" },
763
+ { code: "02000", raised_by: "dashboard_delete,dashboard_run,doc_render_body,doc_render_document,doc_render_read,doc_sign,doc_signature_intact,doc_signature_read,doc_template_delete,doc_template_read,doc_template_save,entity_field_retire,entity_field_update,entity_record_read,entity_value_write,external_history_event,external_rows,external_stub_upsert,external_writes_set,io_restore,migrate_delete,migrate_extract_parent,migrate_merge,migrate_reclass,migrate_rename,migrate_reparent,migrate_retype,migrate_split,migrate_undo,organization_clear,portal_card,portal_declare,portal_invite,portal_preview,portal_principal_bind,portal_revoke,read_record,record_delete,record_restore,record_restore_preview,record_table,record_update,relation_uncarry,share_access,share_grant,share_lane_set,share_people,share_revoke,visibility_as_of,work_approval_decide,work_approval_read,work_approval_request,work_assign,work_record_states,work_set_state" },
764
+ { code: "0A000", raised_by: "external_rows,external_source_declare,external_write_through,promote_field,promoted_index_expr,promoted_query_sql,promoted_read,rule_eval,work_assign" },
765
+ { code: "22004", raised_by: "_entity_custom_fields_guard,_value_envelope,actor_word,agg_assert_key,agg_sql,anon_capture,anon_token_issue,anon_write,assert_client_may_reach,dashboard_block_normalize,dashboard_declare,dashboard_window_sql,doc_render_write,doc_sign,doc_signature_write,doc_template_save,external_history_event,external_rows,external_source_declare,external_stub_upsert,external_write_through,external_writes_set,field_history,form_declare,form_submit,home_add,io_comment_write,io_import_open,io_outbox_drain,io_restore,migrate_choice_keys,migrate_purge,migrate_reclass,migrate_split,migrate_undo,organization_clear,organization_contents,portal_declare,portal_invite,portal_principal_bind,query_across_homes,query_by_coordinates,query_rollup,record_delete,record_reparent,record_resolve,record_restore,record_update,record_write,relation_carry,relation_own,rule_declare,share_grant,table_declare,value_restore,work_approval_decide,work_approval_request,work_template_declare" },
766
+ { code: "22007", raised_by: "dashboard_window_sql" },
641
767
  { code: "22012", raised_by: "rule_eval" },
642
- { code: "22023", raised_by: "_entity_custom_fields_guard,_value_envelope,_work_shape_guard,actor_word,agg_assert_key,agg_sql,anon_token_issue,containment_parent,doc_render_body,doc_render_write,doc_signature_write,entity_value_write,external_history_event,external_source_declare,external_stub_upsert,external_write_through,io_import_open,io_proposal_accept,migrate_merge,my_levels,organization_clear,query_by_coordinates,query_rollup,record_update,relation_carry,rule_eval,rule_members,share_grant,share_lane_set,table_rules,visibility_as_of,work_approval_request,work_list,work_slot_hold" },
643
- { code: "23503", raised_by: "_containment_guard,_field_document_for,_record_rule_uses,anon_publish,anon_rate_take,anon_token_issue,anon_write,assert_organization_wall,delete_rule,doc_sign,doc_template_save,form_declare,form_submit,home_add,io_comment_write,io_import_rows,io_proposal_accept,migrate_demote,migrate_promote,promote_field,promote_table,record_applicability,record_reparent,relation_carry,relation_own,resolve_first_match,rule_applies,rule_declare,rule_eval,rule_members,rule_run,work_set_state,work_take_assignment,work_template_instantiate" },
768
+ { code: "22023", raised_by: "_entity_custom_fields_guard,_value_envelope,_work_shape_guard,actor_word,agg_assert_key,agg_sql,anon_token_issue,containment_parent,dashboard_block_normalize,dashboard_window_sql,doc_render_body,doc_render_write,doc_signature_write,entity_value_write,external_history_event,external_source_declare,external_stub_upsert,external_write_through,field_history,io_import_open,io_proposal_accept,migrate_merge,migrate_reclass,my_levels,organization_clear,portal_declare,query_by_coordinates,query_rollup,record_update,relation_carry,rule_eval,rule_members,share_grant,share_lane_set,table_rules,visibility_as_of,work_approval_request,work_list,work_slot_hold" },
769
+ { code: "23503", raised_by: "_containment_guard,_field_document_for,_record_rule_uses,anon_publish,anon_rate_take,anon_token_issue,anon_write,assert_organization_wall,comment_write,dashboard_declare,delete_rule,doc_sign,doc_template_save,form_declare,form_submit,home_add,io_comment_write,io_import_rows,io_proposal_accept,migrate_demote,migrate_promote,promote_field,promote_table,record_applicability,record_reparent,relation_carry,relation_own,resolve_first_match,rule_applies,rule_declare,rule_eval,rule_members,rule_run,subscription_mute,work_set_state,work_take_assignment,work_template_instantiate" },
644
770
  { code: "23505", raised_by: "_unique_rule_holds,doc_sign,doc_signature_write,entity_field_declare,field_declare,home_add,io_proposal_accept,relation_carry,relation_own,share_grant,work_approval_decide,work_slot_hold" },
645
- { code: "23514", raised_by: "_containment_guard,_dated_values_guard,_derived_fields,_entity_custom_fields_guard,_field_document_for,_field_shape_guard,_field_type_parity_guard,_merge_field_shape_guard,_merge_field_temporal_guard,_promoted_field_cap_guard,_record_rule_uses,_resolve_choice_words,_rule_shape_guard,_rule_topology_guard,_store_relation_edge_names_its_field,_table_shape_guard,_value_envelope,_work_shape_guard,_workdoors_approval_guard,assert_entity_is_organization_scoped,doc_sign,doc_signature_write,doc_template_save,entity_field_declare,entity_field_retire,entity_field_update,entity_records_find,entity_table,field_declare,field_retire,field_update,home_add,migrate_merge,relation_carry,relation_own,validate_value_envelope,validate_values,work_set_state,work_slots_declare,work_take_assignment,work_template_instantiate" },
771
+ { code: "23514", raised_by: "_containment_guard,_dated_values_guard,_derived_fields,_entity_custom_fields_guard,_field_class_guard,_field_document_for,_field_shape_guard,_field_type_parity_guard,_merge_field_shape_guard,_merge_field_temporal_guard,_promoted_field_cap_guard,_record_rule_uses,_resolve_choice_words,_rule_shape_guard,_rule_topology_guard,_store_relation_edge_names_its_field,_table_shape_guard,_value_envelope,_work_shape_guard,_workdoors_approval_guard,assert_entity_is_organization_scoped,doc_sign,doc_signature_write,doc_template_save,entity_field_declare,entity_field_retire,entity_field_update,entity_records_find,entity_table,field_declare,field_retire,field_update,home_add,migrate_merge,relation_carry,relation_own,validate_value_envelope,validate_values,work_set_state,work_slots_declare,work_take_assignment,work_template_instantiate" },
646
772
  { code: "40001", raised_by: "has_visibility_at" },
647
- { code: "42501", raised_by: "_doc_render_immutable,_doc_signature_immutable,_field_definition_write,_field_write_door,_rule_definition_write,_store_door,anon_capture,anon_inbound_land,anon_publish,anon_token_issue,anon_token_verify,anon_write,assert_client_may_change,assert_client_may_open,assert_client_may_reach,assert_may_know_table,assert_organization_admin,assert_store_door,entity_field_declare,entity_field_retire,entity_field_update,entity_value_write,external_write_through,form_declare,form_submit,io_comment_resolve,io_comment_write,io_restore,migrate_purge,my_levels,organization_clear,promote_table,query_visibility_parity,read_record,read_records,share_grant,visibility_as_of,work_approval_decide,work_approval_read,work_approval_request,work_person" },
648
- { code: "42703", raised_by: "entity_table" },
773
+ { code: "42501", raised_by: "_doc_render_immutable,_doc_signature_immutable,_field_definition_write,_field_write_door,_rule_definition_write,_store_door,anon_capture,anon_inbound_land,anon_publish,anon_token_issue,anon_token_verify,anon_write,assert_client_may_change,assert_client_may_open,assert_client_may_reach,assert_may_know_table,assert_organization_admin,assert_store_door,comment_write,entity_field_declare,entity_field_retire,entity_field_update,entity_value_write,external_write_through,form_declare,form_submit,io_comment_resolve,io_comment_write,io_restore,migrate_purge,migrate_reclass,my_levels,organization_clear,portal_declare,portal_invitation,portal_invite,portal_principal_bind,promote_table,query_visibility_parity,read_record,read_records,record_table,share_grant,subscription_mute,visibility_as_of,work_approval_decide,work_approval_read,work_approval_request,work_person" },
774
+ { code: "42703", raised_by: "entity_table,portal_declare" },
649
775
  { code: "53400", raised_by: "anon_rate_take,delete_cascade_closure,promote_field" },
650
776
  { code: "54001", raised_by: "record_delete" },
651
777
  { code: "PT409", raised_by: "record_update" }
@@ -1821,6 +1947,32 @@ var DOORS = {
1821
1947
  // are server-lane doors and a browser never calls them.
1822
1948
  forms: "forms",
1823
1949
  formDeclare: "form_declare",
1950
+ // THE CLIENT PORTAL, OWNER'S SIDE (VIS-31 / PORTAL). A portal is how somebody
1951
+ // with NO membership is let in to see the records that name them. `portals`
1952
+ // lists them, `portalCard` opens one, `portalDeclare` states what it exposes,
1953
+ // `portalInvite` and `portalRevoke` are the two acts on a person, and
1954
+ // `portalPreview` is "view as this client" — it asks `custom.visible_set` for
1955
+ // HER user id, the same call `custom.read_records` makes when she opens the
1956
+ // page, so the preview cannot disagree with what she sees. The CLIENT-side
1957
+ // pair (`custom.portal_public`, `custom.portal_invitation`, `custom.portal_me`)
1958
+ // is deliberately absent here: those belong to the outsider's own shell.
1959
+ portals: "portals",
1960
+ portalCard: "portal_card",
1961
+ portalDeclare: "portal_declare",
1962
+ portalInvite: "portal_invite",
1963
+ portalRevoke: "portal_revoke",
1964
+ portalPreview: "portal_preview",
1965
+ // SCR-16 — the canvas. A dashboard is a record in the presentation kernel
1966
+ // whose blocks are saved SPECS for the eighth verb; `dashboardRun` walks them
1967
+ // under the CALLER'S principal, so nothing here is precomputed and nothing is
1968
+ // summed in a browser. `dashboardStuck` is the seventh block kind on its own,
1969
+ // because "what has not moved in fourteen days" is a question a screen asks
1970
+ // outside a canvas too.
1971
+ dashboards: "dashboards",
1972
+ dashboardDeclare: "dashboard_declare",
1973
+ dashboardRun: "dashboard_run",
1974
+ dashboardStuck: "dashboard_stuck",
1975
+ dashboardDelete: "dashboard_delete",
1824
1976
  // The eighth verb (AGT-N-8): a grouped, bucketed, filtered count/sum/avg/
1825
1977
  // min/max computed INSIDE the read door's own query, so a chart shows exactly
1826
1978
  // the rows this person may see and never fetches the ones they may not.
@@ -1831,6 +1983,16 @@ var DOORS = {
1831
1983
  // over a saved view. There is no subscription TABLE and no second notifier.
1832
1984
  aggSubscriptions: "agg_subscriptions",
1833
1985
  aggSubscriptionCadences: "agg_subscription_cadences",
1986
+ // The OWNER's side of DOOR-18. `agg_subscriptions` is the notifier's reader
1987
+ // and is not client-callable; these two are what a person reaches: what am I
1988
+ // being told about, and switch this one off. A form's notify Rule is one of
1989
+ // these rows, so "tell me when a patient arrives" can also be un-told.
1990
+ subscriptions: "subscriptions",
1991
+ subscriptionMute: "subscription_mute",
1992
+ // The cadence CATALOGUE a person's screen may ask for. `agg_subscription_cadences`
1993
+ // above is the notifier's own and holds no client grant; this door delegates to
1994
+ // it, so a picker and the digest runner can never offer different words.
1995
+ subscriptionCadences: "subscription_cadences",
1834
1996
  // The doc doors: a template is saved, a render is written once and frozen,
1835
1997
  // and a signature is checked against the exact bytes that were signed.
1836
1998
  docTemplateSave: "doc_template_save",
@@ -1840,6 +2002,15 @@ var DOORS = {
1840
2002
  docUnresolvedTokens: "doc_unresolved_tokens",
1841
2003
  docSign: "doc_sign",
1842
2004
  docSignatureIntact: "doc_signature_intact",
2005
+ // THE PLURAL ACTS. Until 2026-09-20 this package read `custom.doc_template`,
2006
+ // `custom.doc_render` and `custom.doc_signature` with a direct PostgREST
2007
+ // `.from()`, and all three hold NO client SELECT — so every list in product #5
2008
+ // opened on "permission denied for view doc_template". A list is a DOOR, never
2009
+ // a grant on the thing behind it (DOORS-TWO).
2010
+ docTemplates: "doc_templates",
2011
+ docTemplateDelete: "doc_template_delete",
2012
+ docRenders: "doc_renders",
2013
+ docSignatures: "doc_signatures",
1843
2014
  // THE ANONYMOUS LANE (W4-ANON). It landed, and it landed as SEVEN doors
1844
2015
  // rather than the one this package had guessed at: a token is issued and
1845
2016
  // verified separately from the write it authorises, a form is published
@@ -1935,6 +2106,14 @@ var BY_SQLSTATE = {
1935
2106
  // caught it raised by a door this package did not know about — which is the
1936
2107
  // suite working, and is why the map is a census rather than a guess.
1937
2108
  "42703": "invalid_argument",
2109
+ // 22007 — a date or a time the store could not read as one. It is raised by
2110
+ // `custom.dashboard_window_sql`, which is handed a window a caller wrote
2111
+ // ("last 30 days", a literal date), so the class is again "the call named
2112
+ // something the shape does not accept" and the store's own sentence names the
2113
+ // text it could not read. Added 2026-09-20 (lane SCREEN-BREAK) after the
2114
+ // live-door census caught it — the same way 42703 arrived, which is the
2115
+ // census working rather than a guess.
2116
+ "22007": "invalid_argument",
1938
2117
  "02000": "not_found",
1939
2118
  "23503": "missing_reference",
1940
2119
  "23505": "already_exists",
@@ -2304,9 +2483,11 @@ function createRecordsClient(config) {
2304
2483
  );
2305
2484
  if (!applicable.ok) return err(applicable.error);
2306
2485
  const rows = applicable.data ?? [];
2307
- return ok(
2308
- rows.map((row) => ({ id: row.id, ...row.data }))
2486
+ const fields = rows.map((row) => ({ id: row.id, ...row.data }));
2487
+ fields.sort(
2488
+ (a, b) => (a.sort ?? 0) - (b.sort ?? 0) || (a.label ?? "").localeCompare(b.label ?? "")
2309
2489
  );
2490
+ return ok(fields);
2310
2491
  },
2311
2492
  async fieldOptions({ field_id }) {
2312
2493
  return callDoor(
@@ -2480,6 +2661,65 @@ function createRecordsClient(config) {
2480
2661
  "formDeclare"
2481
2662
  );
2482
2663
  },
2664
+ async portals() {
2665
+ return callDoor(DOORS.portals, { p_organization_id: org }, "portals");
2666
+ },
2667
+ async portalCard({ portal_id }) {
2668
+ return callDoor(
2669
+ DOORS.portalCard,
2670
+ { p_organization_id: org, p_portal_id: portal_id },
2671
+ "portalCard"
2672
+ );
2673
+ },
2674
+ async portalDeclare(args) {
2675
+ return callDoor(
2676
+ DOORS.portalDeclare,
2677
+ {
2678
+ p_organization_id: org,
2679
+ p_title: args.title,
2680
+ p_client_table_id: args.client_table_id,
2681
+ p_tables: args.tables,
2682
+ p_portal_id: args.portal_id ?? null,
2683
+ p_slug: args.slug ?? null,
2684
+ p_sign_in_method: args.sign_in_method ?? "magic_link"
2685
+ },
2686
+ "portalDeclare"
2687
+ );
2688
+ },
2689
+ async portalInvite(args) {
2690
+ return callDoor(
2691
+ DOORS.portalInvite,
2692
+ {
2693
+ p_organization_id: org,
2694
+ p_portal_id: args.portal_id,
2695
+ p_client_record_id: args.client_record_id,
2696
+ p_email: args.email,
2697
+ // ALWAYS null from a client. See the interface comment: an invitation
2698
+ // confers nothing until the person follows their own sign-in link.
2699
+ p_user_id: null
2700
+ },
2701
+ "portalInvite"
2702
+ );
2703
+ },
2704
+ async portalRevoke({ portal_id, principal_id }) {
2705
+ return callDoor(
2706
+ DOORS.portalRevoke,
2707
+ { p_organization_id: org, p_portal_id: portal_id, p_principal_id: principal_id },
2708
+ "portalRevoke"
2709
+ );
2710
+ },
2711
+ async portalPreview({ portal_id, principal_id, table_id }) {
2712
+ return callDoor(
2713
+ DOORS.portalPreview,
2714
+ {
2715
+ p_organization_id: org,
2716
+ p_portal_id: portal_id,
2717
+ p_principal_id: principal_id,
2718
+ p_table_id: table_id
2719
+ },
2720
+ "portalPreview"
2721
+ );
2722
+ },
2483
2723
  async ruleRun({ rule_id, values, context }) {
2484
2724
  return callDoor(
2485
2725
  DOORS.ruleRun,
@@ -2504,6 +2744,55 @@ function createRecordsClient(config) {
2504
2744
  "ruleEval"
2505
2745
  );
2506
2746
  },
2747
+ async dashboards(args) {
2748
+ return callDoor(
2749
+ DOORS.dashboards,
2750
+ { p_organization_id: org, p_table_id: args?.table_id ?? null },
2751
+ "dashboards"
2752
+ );
2753
+ },
2754
+ async dashboardDeclare(args) {
2755
+ return callDoor(
2756
+ DOORS.dashboardDeclare,
2757
+ {
2758
+ p_organization_id: org,
2759
+ p_table_id: args.table_id,
2760
+ p_name: args.name,
2761
+ p_blocks: args.blocks ?? [],
2762
+ p_presentation: args.presentation ?? {},
2763
+ p_dashboard_id: args.dashboard_id ?? null
2764
+ },
2765
+ "dashboardDeclare"
2766
+ );
2767
+ },
2768
+ async dashboardRun({ dashboard_id, filter }) {
2769
+ return callDoor(
2770
+ DOORS.dashboardRun,
2771
+ { p_organization_id: org, p_dashboard_id: dashboard_id, p_filter: filter ?? {} },
2772
+ "dashboardRun"
2773
+ );
2774
+ },
2775
+ async dashboardStuck({ table_id, state_key, days, filter, limit }) {
2776
+ return callDoor(
2777
+ DOORS.dashboardStuck,
2778
+ {
2779
+ p_organization_id: org,
2780
+ p_table_id: table_id,
2781
+ p_state_key: state_key,
2782
+ p_days: days ?? 14,
2783
+ p_filter: filter ?? {},
2784
+ p_limit: limit ?? 50
2785
+ },
2786
+ "dashboardStuck"
2787
+ );
2788
+ },
2789
+ async dashboardDelete({ dashboard_id }) {
2790
+ return callDoor(
2791
+ DOORS.dashboardDelete,
2792
+ { p_organization_id: org, p_dashboard_id: dashboard_id },
2793
+ "dashboardDelete"
2794
+ );
2795
+ },
2507
2796
  async recordAggregate({ table_id, groupBy, measures, bucket, filter, limit }) {
2508
2797
  return callDoor(
2509
2798
  DOORS.recordAggregate,
@@ -2539,6 +2828,27 @@ function createRecordsClient(config) {
2539
2828
  async aggSubscriptionCadences() {
2540
2829
  return callDoor(DOORS.aggSubscriptionCadences, {}, "aggSubscriptionCadences");
2541
2830
  },
2831
+ async subscriptionCadences() {
2832
+ return callDoor(
2833
+ DOORS.subscriptionCadences,
2834
+ { p_organization_id: org },
2835
+ "subscriptionCadences"
2836
+ );
2837
+ },
2838
+ async subscriptions(args) {
2839
+ return callDoor(
2840
+ DOORS.subscriptions,
2841
+ { p_organization_id: org, p_table_id: args?.table_id ?? null },
2842
+ "subscriptions"
2843
+ );
2844
+ },
2845
+ async subscriptionMute({ rule_id, muted }) {
2846
+ return callDoor(
2847
+ DOORS.subscriptionMute,
2848
+ { p_organization_id: org, p_rule_id: rule_id, p_muted: muted },
2849
+ "subscriptionMute"
2850
+ );
2851
+ },
2542
2852
  async docTemplateSave({ table_id, name, body, template_id }) {
2543
2853
  return callDoor(
2544
2854
  DOORS.docTemplateSave,
@@ -2589,32 +2899,52 @@ function createRecordsClient(config) {
2589
2899
  "docSign"
2590
2900
  );
2591
2901
  },
2902
+ // A LIST IS A DOOR, NEVER A GRANT ON THE THING BEHIND IT. These three used to
2903
+ // read `custom.doc_template`, `custom.doc_render` and `custom.doc_signature`
2904
+ // with a direct PostgREST `.from()`, and none of the three holds a client
2905
+ // SELECT — so a person could save a template they could never list and
2906
+ // render a document they could never find again, and every document screen
2907
+ // opened on "permission denied for view doc_template". The doors below
2908
+ // narrow inside the definer, after the ladder has spoken (DOORS-TWO).
2592
2909
  async docTemplates({ table_id }) {
2593
- const response = await config.dataSource.schema(schema).from("doc_template").select("*").eq("organization_id", org).eq("renders_table_id", table_id).order("name");
2594
- if (response.error) {
2595
- const refusal = mapPgError(response.error, "docTemplates");
2596
- scream({ code: refusal.code, message: refusal.message, ...refusal.hint ? { hint: refusal.hint } : {}, where: "docTemplates" });
2597
- return err(refusal);
2598
- }
2599
- return ok(response.data ?? []);
2910
+ const answered = await callDoor(
2911
+ DOORS.docTemplates,
2912
+ { p_organization_id: org, p_table_id: table_id },
2913
+ "docTemplates"
2914
+ );
2915
+ if (!answered.ok) return answered;
2916
+ return ok(
2917
+ answered.data.map((row) => ({ ...row, id: row["template_id"] }))
2918
+ );
2919
+ },
2920
+ async docTemplateDelete({ template_id }) {
2921
+ return callDoor(
2922
+ DOORS.docTemplateDelete,
2923
+ { p_organization_id: org, p_template_id: template_id },
2924
+ "docTemplateDelete"
2925
+ );
2600
2926
  },
2601
2927
  async docRenders({ record_id }) {
2602
- const response = await config.dataSource.schema(schema).from("doc_render").select("*").eq("organization_id", org).eq("record_id", record_id).is("deleted_at", null).order("rendered_at", { ascending: false });
2603
- if (response.error) {
2604
- const refusal = mapPgError(response.error, "docRenders");
2605
- scream({ code: refusal.code, message: refusal.message, ...refusal.hint ? { hint: refusal.hint } : {}, where: "docRenders" });
2606
- return err(refusal);
2607
- }
2608
- return ok(response.data ?? []);
2928
+ const answered = await callDoor(
2929
+ DOORS.docRenders,
2930
+ { p_organization_id: org, p_record_id: record_id },
2931
+ "docRenders"
2932
+ );
2933
+ if (!answered.ok) return answered;
2934
+ return ok(
2935
+ answered.data.map((row) => ({ ...row, id: row["render_id"] }))
2936
+ );
2609
2937
  },
2610
2938
  async docSignatures({ record_id }) {
2611
- const response = await config.dataSource.schema(schema).from("doc_signature").select("*").eq("organization_id", org).eq("record_id", record_id).is("deleted_at", null).order("signed_at", { ascending: false });
2612
- if (response.error) {
2613
- const refusal = mapPgError(response.error, "docSignatures");
2614
- scream({ code: refusal.code, message: refusal.message, ...refusal.hint ? { hint: refusal.hint } : {}, where: "docSignatures" });
2615
- return err(refusal);
2616
- }
2617
- return ok(response.data ?? []);
2939
+ const answered = await callDoor(
2940
+ DOORS.docSignatures,
2941
+ { p_organization_id: org, p_record_id: record_id },
2942
+ "docSignatures"
2943
+ );
2944
+ if (!answered.ok) return answered;
2945
+ return ok(
2946
+ answered.data.map((row) => ({ ...row, id: row["signature_id"] }))
2947
+ );
2618
2948
  },
2619
2949
  async recordValuesVersioned({ record_id }) {
2620
2950
  return readValues(record_id);
@@ -3142,7 +3472,7 @@ function pgJsonbText(value) {
3142
3472
  return JSON.stringify(value);
3143
3473
  }
3144
3474
  var jsonBytes = (value) => new TextEncoder().encode(pgJsonbText(value)).length;
3145
- var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
3475
+ var UUID2 = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
3146
3476
  function labelOf(field) {
3147
3477
  return field.label && field.label.length > 0 ? field.label : field.key;
3148
3478
  }
@@ -3252,7 +3582,7 @@ function predictValueRefusals(fields, values, recordType) {
3252
3582
  message: `${label} takes one of its choices, and it was given a ${jsonType(item)}`,
3253
3583
  hint: "FLD-5 / FLD-6: a list field stores the id of the option RECORD, because every pick-list is already a Table."
3254
3584
  });
3255
- } else if (!UUID.test(item)) {
3585
+ } else if (!UUID2.test(item)) {
3256
3586
  found.push({
3257
3587
  ...base,
3258
3588
  message: `${label} was given a choice that is not one of its choices`,
@@ -3262,7 +3592,7 @@ function predictValueRefusals(fields, values, recordType) {
3262
3592
  } else if (field.type === "relation") {
3263
3593
  if (typeof item !== "string") {
3264
3594
  found.push({ ...base, message: `${label} points at a record, and it was given a ${jsonType(item)}`, hint: "FLD-1: relation." });
3265
- } else if (!UUID.test(item)) {
3595
+ } else if (!UUID2.test(item)) {
3266
3596
  found.push({
3267
3597
  ...base,
3268
3598
  message: `${label} points at something that is not there`,