@marktiderman/genesis-cli 1.19.0 → 1.21.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.
Files changed (42) hide show
  1. package/README.md +98 -1
  2. package/dist/cli.js +682 -74
  3. package/dist/cli.js.map +1 -1
  4. package/dist/doctor.d.ts +1 -1
  5. package/dist/doctor.js +270 -8
  6. package/dist/doctor.js.map +1 -1
  7. package/dist/eslint.cjs +99 -0
  8. package/dist/eslint.cjs.map +1 -0
  9. package/dist/eslint.d.cts +68 -0
  10. package/dist/eslint.d.ts +68 -0
  11. package/dist/eslint.js +72 -0
  12. package/dist/eslint.js.map +1 -0
  13. package/dist/genesis-catalog.json +86 -14
  14. package/dist/index.js +278 -16
  15. package/dist/index.js.map +1 -1
  16. package/dist/starter/README.md +17 -5
  17. package/dist/starter/__tests__/components/setup.ts +11 -0
  18. package/dist/starter/__tests__/starter.test.ts +19 -0
  19. package/dist/starter/eslint.config.js +8 -1
  20. package/dist/starter/nativewind-env.d.ts +1 -0
  21. package/dist/starter/package.json +5 -3
  22. package/dist/starter/src/lib/app.ts +13 -5
  23. package/dist/starter/src/lib/define-starter.ts +1 -1
  24. package/dist/starter/src/lib/devtools-overlay.ts +3 -4
  25. package/dist/starter/src/lib/native-modules.ts +5 -1
  26. package/dist/starter/src/lib/offline.ts +14 -0
  27. package/dist/starter/supabase/migrations/20261024000750_support_default_tenant.sql +18 -0
  28. package/dist/starter/supabase/migrations/20261024000800_genesis_support_tickets.sql +800 -0
  29. package/dist/starter/supabase/migrations/20261025000070_starter_tenancy.sql +26 -0
  30. package/dist/starter/supabase/migrations/20261025000080_genesis_chat_tenants.sql +716 -0
  31. package/dist/starter/supabase/tests/database/chat_people.test.sql +1 -1
  32. package/dist/starter/supabase/tests/database/genesis_chat.test.sql +10 -2
  33. package/dist/starter/supabase/tests/database/genesis_chat_coaching.test.sql +8 -0
  34. package/dist/starter/supabase/tests/database/genesis_chat_search_threads.test.sql +8 -0
  35. package/dist/starter/supabase/tests/database/genesis_chat_tenants.test.sql +259 -0
  36. package/dist/starter/supabase/tests/database/genesis_support.test.sql +15 -3
  37. package/dist/starter/supabase/tests/database/genesis_support_findings.test.sql +10 -0
  38. package/dist/starter/supabase/tests/database/genesis_support_tickets.test.sql +327 -0
  39. package/dist/starter/supabase/tests/database/starter_tenancy.test.sql +19 -0
  40. package/dist/vite.js +270 -8
  41. package/dist/vite.js.map +1 -1
  42. package/package.json +18 -4
@@ -0,0 +1,800 @@
1
+ -- @feat:support-feedback
2
+ -- Genesis support 0004: tickets, and tenancy (after 0006_genesis_support.sql and 0003).
3
+ --
4
+ -- A support report becomes a ticket, and every ticket belongs to a tenant, so each business on
5
+ -- the app can run its own help desk while the app runs its own support the same way.
6
+ --
7
+ -- 1. public.support_reports is renamed public.tickets, and public.support_attachments
8
+ -- public.ticket_attachments. For one minor the old names stay as views over the new tables
9
+ -- (security_invoker, same column grants), so installed clients, the intake function and
10
+ -- app SQL keep working. Foreign-key columns keep `report_id` for now.
11
+ -- 2. tickets.tenant_id and support_ideas.tenant_id: the tenant is whatever the app's
12
+ -- private.genesis_viewer_people hook returns (0016): one fixed id in a single-tenant app,
13
+ -- one per organization in a multi-tenant one. A new row takes the tenant the client sends
14
+ -- (checked: the sender is in it and its help desk is on), else the app's default tenant;
15
+ -- nothing is guessed from the sender's memberships. Existing rows are filled in
16
+ -- from the app's optional private.genesis_support_tenant_for(report) hook, else the default.
17
+ -- 3. Agents: public.ticket_agents (tenant, person). An agent of a tenant, a tenant admin, or,
18
+ -- in the default tenant only, whoever the app's staff hook names, handles that tenant's
19
+ -- tickets. Platform staff therefore see the app's own tickets and no one else's.
20
+ -- 4. Reading and handling stay two checks: a ticket's requester reads it but never handles it.
21
+ -- The app's report-handler hook (0003) answers only for the app's own tickets.
22
+ -- 5. The help desk is opt-in per tenant: a business takes tickets once `support-helpdesk` is
23
+ -- enabled for it in genesis-supabase 0004's tenant_features. The default tenant always is.
24
+ -- 6. Screenshots: who may read one is decided by its ticket row (its tenant and handlers), never
25
+ -- by the storage path, so paths keep their `<user>/<ticket>/<file>` shape.
26
+ --
27
+ -- Your app answers through these hooks (Genesis never defines them):
28
+ --
29
+ -- private.genesis_support_default_tenant() returns uuid (required, before 0004)
30
+ -- The app's own tenant: where its own support lives, and where a ticket goes when the
31
+ -- client names none. A single-tenant app returns its one tenant id.
32
+ -- private.genesis_support_tenant_for(report uuid) returns uuid (optional)
33
+ -- For existing rows only: which tenant an old report belongs to.
34
+ -- private.genesis_viewer_people(viewer uuid) returns table (tenant_id uuid, person_id uuid)
35
+ -- From 0016_genesis_viewer_people.sql: the tenants and person records a login has.
36
+ -- private.genesis_tenant_admin(tenant uuid, person uuid) returns boolean (optional, 0004)
37
+ -- Tenant admins handle tickets and manage agents.
38
+ --
39
+ -- Without the default tenant hook this migration stops and says so: define it first.
40
+ --
41
+ -- Use: copy this file into your app's `supabase/migrations/` under a new timestamped name
42
+ -- (`genesis migrate` does it). Never edit it once applied; change it with a new migration.
43
+ --
44
+ -- Verify: the pgTAP test beside this file (`tests/0004_genesis_support_tickets.test.sql`).
45
+
46
+ create schema if not exists private;
47
+
48
+ -- 1. Rename ------------------------------------------------------------------------------
49
+
50
+ alter table public.support_reports rename to tickets;
51
+ alter table public.support_attachments rename to ticket_attachments;
52
+
53
+ comment on table public.tickets is
54
+ 'Support tickets: a request from a person, with the app''s context, in a tenant. Genesis support.';
55
+ comment on table public.ticket_attachments is
56
+ 'Screenshots on a ticket, in the private genesis-support bucket. Genesis support.';
57
+
58
+ -- 2. Tenant hooks -------------------------------------------------------------------------
59
+
60
+ -- The app's default tenant, or null without the hook. A hook that errors raises (never a silent fallback).
61
+ create or replace function private.genesis_support_default_tenant_id()
62
+ returns uuid
63
+ language plpgsql
64
+ stable
65
+ security definer
66
+ set search_path = ''
67
+ as $$
68
+ begin
69
+ if to_regprocedure('private.genesis_support_default_tenant()') is null then
70
+ return null;
71
+ end if;
72
+ return private.genesis_support_default_tenant();
73
+ end;
74
+ $$;
75
+
76
+ -- The tenants a login belongs to, from the app's viewer-people hook (0016). Empty without it; a
77
+ -- hook that errors raises, so a ticket is never routed to the wrong tenant by accident.
78
+ create or replace function private.genesis_support_tenants_of(person uuid)
79
+ returns table (tenant_id uuid, person_id uuid)
80
+ language plpgsql
81
+ stable
82
+ security definer
83
+ set search_path = ''
84
+ as $$
85
+ begin
86
+ if person is null or to_regprocedure('private.genesis_viewer_people(uuid)') is null then
87
+ return;
88
+ end if;
89
+ return query
90
+ select distinct v.tenant_id, v.person_id
91
+ from private.genesis_viewer_people(person) v
92
+ where v.tenant_id is not null;
93
+ end;
94
+ $$;
95
+
96
+ -- Is this login a tenant admin there (0004's hook)? False without it.
97
+ create or replace function private.genesis_support_tenant_admin(tenant uuid, person uuid)
98
+ returns boolean
99
+ language plpgsql
100
+ stable
101
+ security definer
102
+ set search_path = ''
103
+ as $$
104
+ begin
105
+ if tenant is null or person is null or to_regprocedure('private.genesis_tenant_admin(uuid,uuid)') is null then
106
+ return false;
107
+ end if;
108
+ return coalesce(private.genesis_tenant_admin(tenant, person), false);
109
+ end;
110
+ $$;
111
+
112
+ -- Is this login named by the app's staff hook (0006)? False without it.
113
+ create or replace function private.genesis_support_staff_hook(person uuid)
114
+ returns boolean
115
+ language plpgsql
116
+ stable
117
+ security definer
118
+ set search_path = ''
119
+ as $$
120
+ begin
121
+ if person is null or to_regprocedure('private.genesis_support_staff(uuid)') is null then
122
+ return false;
123
+ end if;
124
+ return coalesce(private.genesis_support_staff(person), false);
125
+ end;
126
+ $$;
127
+
128
+ -- 3. Agents -------------------------------------------------------------------------------
129
+
130
+ create table public.ticket_agents (
131
+ tenant_id uuid not null,
132
+ person_id uuid not null,
133
+ role text not null default 'agent',
134
+ constraint ticket_agents_role check (role in ('agent', 'admin')),
135
+ created_by uuid references auth.users (id) on delete set null,
136
+ created_at timestamptz not null default now(),
137
+ primary key (tenant_id, person_id)
138
+ );
139
+
140
+ comment on table public.ticket_agents is
141
+ 'Who handles a tenant''s tickets, by the tenant''s person record. Written only through support_set_agent. Genesis support.';
142
+
143
+ alter table public.ticket_agents enable row level security;
144
+
145
+ -- Does this login handle tickets in this tenant? An agent row for one of the login's person
146
+ -- records there, a tenant admin, or, in the default tenant, the app's staff hook.
147
+ create or replace function private.genesis_support_agent(tenant uuid, person uuid)
148
+ returns boolean
149
+ language plpgsql
150
+ stable
151
+ security definer
152
+ set search_path = ''
153
+ as $$
154
+ begin
155
+ if tenant is null or person is null then
156
+ return false;
157
+ end if;
158
+ if exists (select 1 from public.ticket_agents a
159
+ join private.genesis_support_tenants_of(person) v
160
+ on v.tenant_id = a.tenant_id and v.person_id = a.person_id
161
+ where a.tenant_id = tenant) then
162
+ return true;
163
+ end if;
164
+ if private.genesis_support_tenant_admin(tenant, person) then
165
+ return true;
166
+ end if;
167
+ return tenant = private.genesis_support_default_tenant_id()
168
+ and private.genesis_support_staff_hook(person);
169
+ end;
170
+ $$;
171
+
172
+ -- The signed-in person, for policies and the client.
173
+ create or replace function public.support_is_agent(tenant uuid)
174
+ returns boolean
175
+ language sql
176
+ stable
177
+ security definer
178
+ set search_path = ''
179
+ as $$
180
+ select private.genesis_support_agent(tenant, auth.uid())
181
+ $$;
182
+
183
+ comment on function public.support_is_agent(uuid) is
184
+ 'Whether the signed-in person handles tickets in this tenant. Genesis support.';
185
+
186
+ -- Staff now means: an agent of the app's own tenant. Kept for callers of 0006.
187
+ create or replace function public.support_is_staff()
188
+ returns boolean
189
+ language sql
190
+ stable
191
+ security definer
192
+ set search_path = ''
193
+ as $$
194
+ select private.genesis_support_agent(private.genesis_support_default_tenant_id(), auth.uid())
195
+ $$;
196
+
197
+ comment on function public.support_is_staff() is
198
+ 'True when the signed-in person handles the app''s own tickets (its default tenant). Genesis support.';
199
+
200
+ -- Add, change or remove an agent (role null removes). Tenant admins and the tenant's agent
201
+ -- admins may; in the default tenant, staff too. An agent row counts only while the app's hook still
202
+ -- lists that person in the tenant (genesis_support_agent), so a stray row grants nothing.
203
+ create or replace function public.support_set_agent(tenant uuid, person uuid, role text)
204
+ returns void
205
+ language plpgsql
206
+ security definer
207
+ set search_path = ''
208
+ as $$
209
+ declare
210
+ me uuid := auth.uid();
211
+ begin
212
+ if me is null or tenant is null or person is null then
213
+ raise exception 'support_set_agent: not allowed' using errcode = '42501';
214
+ end if;
215
+ if not (
216
+ private.genesis_support_tenant_admin(tenant, me)
217
+ or exists (select 1 from public.ticket_agents a
218
+ join private.genesis_support_tenants_of(me) v
219
+ on v.tenant_id = a.tenant_id and v.person_id = a.person_id
220
+ where a.tenant_id = tenant and a.role = 'admin')
221
+ or (tenant = private.genesis_support_default_tenant_id() and private.genesis_support_staff_hook(me))
222
+ ) then
223
+ raise exception 'support_set_agent: not allowed' using errcode = '42501';
224
+ end if;
225
+ if role is null then
226
+ delete from public.ticket_agents a where a.tenant_id = tenant and a.person_id = person;
227
+ return;
228
+ end if;
229
+ if role not in ('agent', 'admin') then
230
+ raise exception 'support_set_agent: role is agent or admin' using errcode = '22023';
231
+ end if;
232
+ insert into public.ticket_agents (tenant_id, person_id, role, created_by)
233
+ values (tenant, person, role, me)
234
+ on conflict (tenant_id, person_id) do update set role = excluded.role;
235
+ end;
236
+ $$;
237
+
238
+ comment on function public.support_set_agent(uuid, uuid, text) is
239
+ 'Makes a tenant''s person an agent or agent admin, or removes them (role null). Genesis support.';
240
+
241
+ create policy ticket_agents_read on public.ticket_agents
242
+ for select to authenticated
243
+ using ((select public.support_is_agent(tenant_id)));
244
+
245
+ -- 4. Tenant columns -----------------------------------------------------------------------
246
+
247
+ alter table public.tickets add column tenant_id uuid;
248
+ alter table public.support_ideas add column tenant_id uuid;
249
+
250
+ create index tickets_tenant_idx on public.tickets (tenant_id, updated_at desc);
251
+ create index support_ideas_tenant_idx on public.support_ideas (tenant_id, status);
252
+
253
+ -- A new ticket's or idea's tenant: the one the client sends (the viewer's current workspace),
254
+ -- checked: the sender must be in it and its help desk on; nothing sent means the app's own
255
+ -- tenant. Nothing is guessed from the sender's memberships. Server code (no sender) keeps what it
256
+ -- sends, or the app's own. A business takes tickets only once it turns the help desk on.
257
+ create or replace function private.genesis_support_helpdesk_on(tenant uuid)
258
+ returns boolean
259
+ language plpgsql
260
+ stable
261
+ security definer
262
+ set search_path = ''
263
+ as $$
264
+ begin
265
+ if tenant is null then
266
+ return false;
267
+ end if;
268
+ if tenant = private.genesis_support_default_tenant_id() then
269
+ return true;
270
+ end if;
271
+ -- Opt in: a business takes tickets only once it turns the help desk on (an enabled row in
272
+ -- genesis-supabase 0004's tenant_features). Without that table, only the app's own tenant.
273
+ if to_regclass('public.tenant_features') is null then
274
+ return false;
275
+ end if;
276
+ return exists (select 1 from public.tenant_features f
277
+ where f.tenant_id = tenant and f.feature_id = 'support-helpdesk' and f.enabled);
278
+ end;
279
+ $$;
280
+
281
+ revoke execute on function private.genesis_support_helpdesk_on(uuid) from public, anon, authenticated;
282
+
283
+ create or replace function private.genesis_support_ticket_tenant()
284
+ returns trigger
285
+ language plpgsql
286
+ security definer
287
+ set search_path = ''
288
+ as $$
289
+ declare
290
+ me uuid := auth.uid();
291
+ dflt uuid := private.genesis_support_default_tenant_id();
292
+ begin
293
+ new.tenant_id := coalesce(new.tenant_id, dflt);
294
+ if new.tenant_id is null then
295
+ raise exception 'support: no tenant (the app defines private.genesis_support_default_tenant)'
296
+ using errcode = '23502';
297
+ end if;
298
+ if me is not null and new.tenant_id is distinct from dflt then
299
+ if not exists (select 1 from private.genesis_support_tenants_of(me) v where v.tenant_id = new.tenant_id) then
300
+ raise exception 'support: not your tenant' using errcode = '42501';
301
+ end if;
302
+ if not private.genesis_support_helpdesk_on(new.tenant_id) then
303
+ raise exception 'support: the help desk is off here' using errcode = '42501';
304
+ end if;
305
+ end if;
306
+ return new;
307
+ end;
308
+ $$;
309
+
310
+ revoke execute on function private.genesis_support_ticket_tenant() from public, anon, authenticated;
311
+
312
+ create trigger genesis_support_ticket_tenant
313
+ before insert on public.tickets
314
+ for each row execute function private.genesis_support_ticket_tenant();
315
+
316
+ create trigger genesis_support_ticket_tenant
317
+ before insert on public.support_ideas
318
+ for each row execute function private.genesis_support_ticket_tenant();
319
+
320
+ -- A ticket or idea never changes tenant.
321
+ create or replace function private.genesis_support_tenant_fixed()
322
+ returns trigger
323
+ language plpgsql
324
+ security definer
325
+ set search_path = ''
326
+ as $$
327
+ begin
328
+ if new.tenant_id is distinct from old.tenant_id and old.tenant_id is not null then
329
+ raise exception 'support: a ticket keeps its tenant' using errcode = '42501';
330
+ end if;
331
+ return new;
332
+ end;
333
+ $$;
334
+
335
+ revoke execute on function private.genesis_support_tenant_fixed() from public, anon, authenticated;
336
+
337
+ create trigger genesis_support_tenant_fixed
338
+ before update of tenant_id on public.tickets
339
+ for each row execute function private.genesis_support_tenant_fixed();
340
+
341
+ create trigger genesis_support_tenant_fixed
342
+ before update of tenant_id on public.support_ideas
343
+ for each row execute function private.genesis_support_tenant_fixed();
344
+
345
+ -- Existing rows: the app's mapping hook, else the default tenant. The app must name its default
346
+ -- tenant before this migration, rows or not: without it every new ticket would be refused.
347
+ -- Filled with user triggers off, so updated_at and the app's own update triggers are untouched.
348
+ do $$
349
+ declare
350
+ dflt uuid := private.genesis_support_default_tenant_id();
351
+ mapped boolean := to_regprocedure('private.genesis_support_tenant_for(uuid)') is not null;
352
+ begin
353
+ if dflt is null then
354
+ raise exception 'genesis support 0004: define private.genesis_support_default_tenant() (your own tenant) before this migration; see its header.';
355
+ end if;
356
+ alter table public.tickets disable trigger user;
357
+ alter table public.support_ideas disable trigger user;
358
+ if mapped then
359
+ execute 'update public.tickets t set tenant_id = private.genesis_support_tenant_for(t.id) where t.tenant_id is null';
360
+ end if;
361
+ update public.tickets set tenant_id = dflt where tenant_id is null;
362
+ update public.support_ideas set tenant_id = dflt where tenant_id is null;
363
+ alter table public.tickets enable trigger user;
364
+ alter table public.support_ideas enable trigger user;
365
+ end;
366
+ $$;
367
+
368
+ -- Every row has a tenant from here on.
369
+ alter table public.tickets add constraint tickets_tenant_required check (tenant_id is not null) not valid;
370
+ alter table public.tickets validate constraint tickets_tenant_required;
371
+ alter table public.support_ideas add constraint support_ideas_tenant_required check (tenant_id is not null) not valid;
372
+ alter table public.support_ideas validate constraint support_ideas_tenant_required;
373
+
374
+ -- 5. Handling: an agent of the ticket's tenant, or whom the app's hook names ---------------
375
+
376
+ -- The report hook (0003) is consulted only for the app's own tickets (the default tenant): an
377
+ -- app's hook can't hand a business's tickets to anyone; those are its agents'.
378
+ create or replace function private.genesis_support_handler(report uuid, person uuid)
379
+ returns boolean
380
+ language plpgsql
381
+ stable
382
+ security definer
383
+ set search_path = ''
384
+ as $$
385
+ declare
386
+ tenant uuid;
387
+ requester uuid;
388
+ begin
389
+ if person is null or report is null then
390
+ return false;
391
+ end if;
392
+ select t.tenant_id, t.user_id into tenant, requester from public.tickets t where t.id = report;
393
+ -- The requester never handles their own ticket, even when they are an agent of its tenant:
394
+ -- another agent answers it.
395
+ if tenant is null or person = requester then
396
+ return false;
397
+ end if;
398
+ if private.genesis_support_agent(tenant, person) then
399
+ return true;
400
+ end if;
401
+ if to_regprocedure('private.genesis_support_report_handler(uuid, uuid)') is null then
402
+ return false;
403
+ end if;
404
+ -- The app's hook names handlers for the app's own tickets only; a business's are its agents'.
405
+ if tenant is distinct from private.genesis_support_default_tenant_id() then
406
+ return false;
407
+ end if;
408
+ return coalesce(private.genesis_support_report_handler(report, person), false);
409
+ end;
410
+ $$;
411
+
412
+ revoke execute on function private.genesis_support_handler(uuid, uuid) from public, anon, authenticated;
413
+
414
+ -- Reading: the requester, or a handler. No app-wide staff bypass.
415
+ drop policy if exists support_reports_read on public.tickets;
416
+ create policy support_reports_read on public.tickets
417
+ for select to authenticated
418
+ using (user_id = (select auth.uid()) or public.support_handles(id));
419
+
420
+ drop policy if exists support_attachments_read on public.ticket_attachments;
421
+ create policy support_attachments_read on public.ticket_attachments
422
+ for select to authenticated
423
+ using (user_id = (select auth.uid()) or public.support_handles(report_id));
424
+
425
+ drop policy if exists support_replies_read on public.support_replies;
426
+ create policy support_replies_read on public.support_replies
427
+ for select to authenticated
428
+ using (
429
+ public.support_handles(report_id)
430
+ or exists (select 1 from public.tickets r where r.id = report_id and r.user_id = (select auth.uid()))
431
+ );
432
+ drop policy if exists support_replies_send on public.support_replies;
433
+ create policy support_replies_send on public.support_replies
434
+ for insert to authenticated
435
+ with check (
436
+ public.support_handles(report_id)
437
+ or exists (select 1 from public.tickets r where r.id = report_id and r.user_id = (select auth.uid()))
438
+ );
439
+
440
+ -- Is this the app's own tenant, or one the signed-in person belongs to?
441
+ create or replace function public.support_in_tenant(tenant uuid)
442
+ returns boolean
443
+ language sql
444
+ stable
445
+ security definer
446
+ set search_path = ''
447
+ as $$
448
+ select auth.uid() is not null and tenant is not null and (
449
+ tenant = private.genesis_support_default_tenant_id()
450
+ or exists (select 1 from private.genesis_support_tenants_of(auth.uid()) v where v.tenant_id = tenant)
451
+ )
452
+ $$;
453
+
454
+ comment on function public.support_in_tenant(uuid) is
455
+ 'Whether a tenant is the app''s own or one the signed-in person belongs to. Genesis support.';
456
+
457
+ -- The app's own tenant, for clients that send a ticket or idea to the app itself.
458
+ create or replace function public.support_default_tenant()
459
+ returns uuid
460
+ language sql
461
+ stable
462
+ security definer
463
+ set search_path = ''
464
+ as $$
465
+ select private.genesis_support_default_tenant_id()
466
+ $$;
467
+
468
+ comment on function public.support_default_tenant() is
469
+ 'The app''s own tenant (where its own support and idea board live). Genesis support.';
470
+
471
+ -- Ideas: the app's own board is read by everyone signed in, as before; a business's board by
472
+ -- its members; hidden ideas by their author and the tenant's agents.
473
+ drop policy if exists support_ideas_read on public.support_ideas;
474
+ create policy support_ideas_read on public.support_ideas
475
+ for select to authenticated
476
+ using (
477
+ (status <> 'hidden' and public.support_in_tenant(tenant_id))
478
+ or author_id = (select auth.uid())
479
+ or (select public.support_is_agent(tenant_id))
480
+ );
481
+
482
+ -- Screenshots: the requester's own while attached (or within an hour of uploading), or a
483
+ -- handler of the ticket the attachment belongs to.
484
+ create or replace function public.support_can_read(object_name text, uploaded_at timestamptz)
485
+ returns boolean
486
+ language sql
487
+ stable
488
+ security definer
489
+ set search_path = ''
490
+ as $$
491
+ select auth.uid() is not null and (
492
+ exists (select 1 from public.ticket_attachments a
493
+ where a.path = object_name
494
+ and (split_part(object_name, '/', 1) = auth.uid()::text
495
+ or private.genesis_support_handler(a.report_id, auth.uid())))
496
+ or (split_part(object_name, '/', 1) = auth.uid()::text and uploaded_at > now() - interval '1 hour')
497
+ )
498
+ $$;
499
+
500
+ comment on function public.support_can_read(text, timestamptz) is
501
+ 'Whether the signed-in person may read this support screenshot (their own, or as a handler of its ticket). Genesis support.';
502
+
503
+ -- Marking a thread read: the requester, or a handler.
504
+ create or replace function public.support_mark_read(report uuid)
505
+ returns void
506
+ language plpgsql
507
+ security definer
508
+ set search_path = ''
509
+ as $$
510
+ declare
511
+ me uuid := auth.uid();
512
+ owner uuid;
513
+ exists_ticket boolean;
514
+ begin
515
+ select r.user_id, true into owner, exists_ticket from public.tickets r where r.id = report;
516
+ if me is null or exists_ticket is null then
517
+ raise exception 'support_mark_read: no such report' using errcode = '42501';
518
+ end if;
519
+ if owner = me then
520
+ update public.tickets set user_read_at = clock_timestamp() where id = report;
521
+ elsif private.genesis_support_handler(report, me) then
522
+ update public.tickets set staff_read_at = clock_timestamp() where id = report;
523
+ else
524
+ raise exception 'support_mark_read: no such report' using errcode = '42501';
525
+ end if;
526
+ end;
527
+ $$;
528
+
529
+ comment on function public.support_mark_read(uuid) is
530
+ 'Marks a ticket''s thread read for its requester, or for a handler. Genesis support.';
531
+
532
+ -- Idea status: the idea's tenant's agents.
533
+ create or replace function public.support_set_idea_status(idea uuid, status text, feature text default null)
534
+ returns void
535
+ language plpgsql
536
+ security definer
537
+ set search_path = ''
538
+ as $$
539
+ declare
540
+ tenant uuid;
541
+ begin
542
+ select i.tenant_id into tenant from public.support_ideas i where i.id = idea;
543
+ if tenant is null or not private.genesis_support_agent(tenant, auth.uid()) then
544
+ raise exception 'support_set_idea_status: only staff' using errcode = '42501';
545
+ end if;
546
+ update public.support_ideas i
547
+ set status = support_set_idea_status.status,
548
+ feature_id = case when feature = '' then null else coalesce(feature, i.feature_id) end,
549
+ shipped_at = case when support_set_idea_status.status = 'shipped' then coalesce(i.shipped_at, now()) end
550
+ where i.id = idea;
551
+ if not found then
552
+ raise exception 'support_set_idea_status: no such idea' using errcode = 'P0002';
553
+ end if;
554
+ end;
555
+ $$;
556
+
557
+ -- Replies (0003's rule, on the renamed table): a handler writes as support on someone else's
558
+ -- ticket; server code only on an email ticket (support-email).
559
+ create or replace function private.genesis_support_new_reply()
560
+ returns trigger
561
+ language plpgsql
562
+ security definer
563
+ set search_path = ''
564
+ as $$
565
+ declare
566
+ me uuid := auth.uid();
567
+ report public.tickets;
568
+ handles boolean;
569
+ begin
570
+ select * into report from public.tickets r where r.id = new.report_id;
571
+ if report.id is null then
572
+ raise exception 'support: not your report' using errcode = '42501';
573
+ end if;
574
+ if me is null then
575
+ if coalesce(to_jsonb(report) ->> 'channel', '') <> 'email' then
576
+ raise exception 'support: not your report' using errcode = '42501';
577
+ end if;
578
+ new.author_id := null;
579
+ new.created_at := clock_timestamp();
580
+ return new;
581
+ end if;
582
+ handles := private.genesis_support_handler(new.report_id, me);
583
+ if report.user_id is null then
584
+ if not handles then
585
+ raise exception 'support: not your report' using errcode = '42501';
586
+ end if;
587
+ new.author_id := me;
588
+ new.from_staff := true;
589
+ else
590
+ if report.user_id <> me and not handles then
591
+ raise exception 'support: not your report' using errcode = '42501';
592
+ end if;
593
+ new.author_id := me;
594
+ new.from_staff := handles and report.user_id <> me;
595
+ end if;
596
+ new.created_at := clock_timestamp();
597
+ if new.prompt is null then
598
+ perform private.genesis_support_rate_limit(me, 'reply', 60, interval '1 hour');
599
+ end if;
600
+ return new;
601
+ end;
602
+ $$;
603
+
604
+ revoke execute on function private.genesis_support_new_reply() from public, anon, authenticated;
605
+
606
+ -- 6. Grants -------------------------------------------------------------------------------
607
+
608
+ -- A sender may name the tenant (checked by the trigger above).
609
+ grant insert (tenant_id) on table public.tickets to authenticated;
610
+ grant insert (tenant_id) on table public.support_ideas to authenticated;
611
+
612
+ revoke all on table public.ticket_agents from public, anon, authenticated;
613
+ grant select on table public.ticket_agents to authenticated;
614
+ grant select, insert, update, delete on table public.ticket_agents to service_role;
615
+
616
+ revoke execute on function private.genesis_support_default_tenant_id() from public, anon, authenticated;
617
+ revoke execute on function private.genesis_support_tenants_of(uuid) from public, anon, authenticated;
618
+ revoke execute on function private.genesis_support_tenant_admin(uuid, uuid) from public, anon, authenticated;
619
+ revoke execute on function private.genesis_support_staff_hook(uuid) from public, anon, authenticated;
620
+ revoke execute on function private.genesis_support_agent(uuid, uuid) from public, anon, authenticated;
621
+ revoke execute on function public.support_is_agent(uuid) from public, anon;
622
+ grant execute on function public.support_is_agent(uuid) to authenticated, service_role;
623
+ revoke execute on function public.support_set_agent(uuid, uuid, text) from public, anon;
624
+ grant execute on function public.support_set_agent(uuid, uuid, text) to authenticated;
625
+ revoke execute on function public.support_is_staff() from public, anon;
626
+ grant execute on function public.support_is_staff() to authenticated, service_role;
627
+
628
+ revoke execute on function public.support_default_tenant() from public, anon;
629
+ grant execute on function public.support_default_tenant() to authenticated, service_role;
630
+ revoke execute on function public.support_in_tenant(uuid) from public, anon;
631
+ grant execute on function public.support_in_tenant(uuid) to authenticated, service_role;
632
+ grant select (tenant_id) on table public.support_ideas to authenticated;
633
+
634
+ -- 8. The rest of support's functions, on the new names -------------------------------------
635
+ -- (The views above keep older callers working; support's own code reads the tables.)
636
+
637
+ -- May the signed-in person upload `object_name`? Only `<own id>/<own open report>/<name>.<image>`,
638
+ -- within an hour of sending the report, and at most 5 objects in its folder. A function, so the
639
+ -- policy can count the folder.
640
+ create or replace function public.support_can_upload(object_name text)
641
+ returns boolean
642
+ language plpgsql
643
+ stable
644
+ security definer
645
+ set search_path = ''
646
+ as $$
647
+ declare
648
+ me uuid := auth.uid();
649
+ folder text;
650
+ begin
651
+ if me is null or object_name is null
652
+ or object_name !~ '^[0-9a-f-]{36}/[0-9a-f-]{36}/[A-Za-z0-9_-]{1,80}\.(png|jpg|jpeg|webp)$'
653
+ or split_part(object_name, '/', 1) <> me::text then
654
+ return false;
655
+ end if;
656
+ if not exists (select 1 from public.tickets r
657
+ where r.id::text = split_part(object_name, '/', 2) and r.user_id = me and r.status = 'open'
658
+ and r.created_at > now() - interval '1 hour') then
659
+ return false;
660
+ end if;
661
+ folder := split_part(object_name, '/', 1) || '/' || split_part(object_name, '/', 2) || '/';
662
+ return (select count(*) from storage.objects o where o.bucket_id = 'genesis-support' and left(o.name, length(folder)) = folder) < 5;
663
+ end;
664
+ $$;
665
+
666
+ -- For the cleanup function: objects in the bucket no attachment names, older than
667
+ -- `older_than` (an upload whose attachment row never landed). Service role only.
668
+ create or replace function public.support_orphan_objects(older_than interval default interval '1 day', max_rows integer default 500)
669
+ returns table (path text)
670
+ language sql
671
+ stable
672
+ security definer
673
+ set search_path = ''
674
+ as $$
675
+ select o.name from storage.objects o
676
+ where o.bucket_id = 'genesis-support'
677
+ and o.created_at <= now() - older_than
678
+ and not exists (select 1 from public.ticket_attachments a where a.path = o.name)
679
+ order by o.created_at
680
+ limit greatest(1, least(coalesce(max_rows, 500), 1000))
681
+ $$;
682
+
683
+ -- An attachment names an object the person already uploaded under their own report's folder.
684
+ create or replace function private.genesis_support_new_attachment()
685
+ returns trigger
686
+ language plpgsql
687
+ security definer
688
+ set search_path = ''
689
+ as $$
690
+ begin
691
+ new.bucket := 'genesis-support';
692
+ new.created_at := now();
693
+ if new.path is null or split_part(new.path, '/', 1) <> new.user_id::text or split_part(new.path, '/', 2) <> new.report_id::text then
694
+ raise exception 'support: an attachment lives under <user id>/<report id>/' using errcode = '42501';
695
+ end if;
696
+ if not exists (select 1 from public.tickets r where r.id = new.report_id and r.user_id = new.user_id) then
697
+ raise exception 'support: not your report' using errcode = '42501';
698
+ end if;
699
+ if not exists (select 1 from storage.objects o where o.bucket_id = 'genesis-support' and o.name = new.path) then
700
+ raise exception 'support: upload the file before recording it' using errcode = '23503';
701
+ end if;
702
+ if (select count(*) from public.ticket_attachments a where a.report_id = new.report_id) >= 5 then
703
+ raise exception 'support: at most 5 screenshots per report' using errcode = 'GS429';
704
+ end if;
705
+ return new;
706
+ end;
707
+ $$;
708
+
709
+ -- A reply moves the thread on: the writer's side has read it, the other side has something new.
710
+ create or replace function private.genesis_support_reply_posted()
711
+ returns trigger
712
+ language plpgsql
713
+ security definer
714
+ set search_path = ''
715
+ as $$
716
+ begin
717
+ if new.from_staff then
718
+ update public.tickets
719
+ set last_staff_reply_at = new.created_at, staff_read_at = new.created_at
720
+ where id = new.report_id;
721
+ else
722
+ update public.tickets
723
+ set last_user_reply_at = new.created_at, user_read_at = new.created_at
724
+ where id = new.report_id;
725
+ end if;
726
+ return null;
727
+ end;
728
+ $$;
729
+
730
+ -- Status: staff, or a handler of the report.
731
+ create or replace function public.support_set_report_status(report uuid, status text, feature text default null)
732
+ returns void
733
+ language plpgsql
734
+ security definer
735
+ set search_path = ''
736
+ as $$
737
+ begin
738
+ if not private.genesis_support_handler(report, auth.uid()) then
739
+ raise exception 'support_set_report_status: only staff' using errcode = '42501';
740
+ end if;
741
+ update public.tickets r
742
+ set status = support_set_report_status.status,
743
+ feature_id = case when feature = '' then null else coalesce(feature, r.feature_id) end
744
+ where r.id = report;
745
+ if not found then
746
+ raise exception 'support_set_report_status: no such report' using errcode = 'P0002';
747
+ end if;
748
+ end;
749
+ $$;
750
+
751
+ -- The person confirms the fix, or reopens the report (with what's still wrong, in the thread).
752
+ create or replace function public.support_verify_report(report uuid, fixed boolean, note text default null)
753
+ returns void
754
+ language plpgsql
755
+ security definer
756
+ set search_path = ''
757
+ as $$
758
+ declare
759
+ _r public.tickets;
760
+ begin
761
+ select * into _r from public.tickets where id = report;
762
+ if _r.id is null or _r.user_id is distinct from auth.uid() then
763
+ raise exception 'support_verify_report: not your report' using errcode = '42501';
764
+ end if;
765
+ if _r.status <> 'addressed' then
766
+ raise exception 'support_verify_report: it isn''t marked fixed' using errcode = '22023';
767
+ end if;
768
+ if nullif(btrim(note), '') is not null then
769
+ insert into public.support_replies (report_id, body) values (report, btrim(note));
770
+ end if;
771
+ if fixed then
772
+ update public.tickets set verified_at = now() where id = report;
773
+ else
774
+ update public.tickets set status = 'in_progress', reopened_at = now() where id = report;
775
+ end if;
776
+ end;
777
+ $$;
778
+
779
+ -- 7. The old names, for one minor ----------------------------------------------------------
780
+
781
+ create view public.support_reports with (security_invoker = true) as select * from public.tickets;
782
+ create view public.support_attachments with (security_invoker = true) as select * from public.ticket_attachments;
783
+
784
+ comment on view public.support_reports is
785
+ 'Deprecated: public.tickets under its old name, for one minor. Genesis support.';
786
+ comment on view public.support_attachments is
787
+ 'Deprecated: public.ticket_attachments under its old name, for one minor. Genesis support.';
788
+
789
+ revoke all on public.support_reports from public, anon, authenticated;
790
+ grant select, delete on public.support_reports to authenticated;
791
+ grant insert (type, title, message, route, app_version, platform, device, context, severity, tenant_id)
792
+ on public.support_reports to authenticated;
793
+ grant update (type, title, message) on public.support_reports to authenticated;
794
+ grant select, insert, update, delete on public.support_reports to service_role;
795
+
796
+ revoke all on public.support_attachments from public, anon, authenticated;
797
+ grant select on public.support_attachments to authenticated;
798
+ grant insert (report_id, path, content_type, byte_size, width, height)
799
+ on public.support_attachments to authenticated;
800
+ grant select, insert, update, delete on public.support_attachments to service_role;