@composable-os/cli 0.1.0-beta.11 → 0.1.0-beta.14

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 (166) hide show
  1. package/dist/{accounts-UHYLCLEY.js → accounts-GOCNY6WI.js} +17 -13
  2. package/dist/app-OSPZHKOI.js +43 -0
  3. package/dist/app-upload-LH4PXKI4.js +198 -0
  4. package/dist/{blocks-5IFU25J6.js → blocks-6DZIP47W.js} +207 -25
  5. package/dist/{chunk-XIDLW3EP.js → chunk-2B6YUYZV.js} +23499 -11766
  6. package/dist/{run-7P4RQALH.js → chunk-2CCHB37H.js} +9 -146
  7. package/dist/{chunk-DU6OIFOU.js → chunk-3OGDWKJ3.js} +530 -434
  8. package/dist/{chunk-DHS2CV2M.js → chunk-5D7BDF4Y.js} +15 -13
  9. package/dist/{chunk-MBIKVRAC.js → chunk-5YDYQM6V.js} +830 -47
  10. package/dist/{chunk-ZAWES2UD.js → chunk-6CM3WZG5.js} +13 -2483
  11. package/dist/{chunk-7D4GKZOX.js → chunk-CIBJGB5H.js} +422 -75
  12. package/dist/chunk-EPDTPGZJ.js +330 -0
  13. package/dist/{chunk-LAAL4YGR.js → chunk-HAYF4G5V.js} +66 -23
  14. package/dist/chunk-HCG6T53D.js +61904 -0
  15. package/dist/{chunk-K34UKWEI.js → chunk-JX4IFWNT.js} +64 -32
  16. package/dist/{chunk-6XTU2MTP.js → chunk-KFHKNXGH.js} +632 -418
  17. package/dist/chunk-KMFMD52Z.js +13276 -0
  18. package/dist/{chunk-E4WCYTCT.js → chunk-LX2TTLVY.js} +174 -55
  19. package/dist/{chunk-PDD2ETVF.js → chunk-O37YUZ52.js} +172 -16
  20. package/dist/{chunk-GYTEW66E.js → chunk-O3R4FS2J.js} +1 -1
  21. package/dist/{chunk-E64SG24J.js → chunk-Q6Y45DY6.js} +25 -2
  22. package/dist/chunk-RGKJKC5T.js +876 -0
  23. package/dist/{chunk-YFTLIEWU.js → chunk-RSEWEFAV.js} +12 -2
  24. package/dist/{chunk-2ABSVMGB.js → chunk-SMJZIK2O.js} +1 -1
  25. package/dist/{chunk-LO6WGU5Y.js → chunk-U72NEGCT.js} +8089 -3464
  26. package/dist/{chunk-P6UWZ6QQ.js → chunk-Z4IKF23A.js} +11 -7
  27. package/dist/{chunk-2IVEVPMY.js → chunk-Z7OVQ6BC.js} +105 -61
  28. package/dist/{chunk-6UWRT4VM.js → chunk-ZJSFX4RD.js} +175 -3
  29. package/dist/{cli-5UNY5G5L.js → cli-OKQHHSOE.js} +17 -25
  30. package/dist/{cli-UYZMVQMR.js → cli-VYSD2S4Z.js} +227 -81
  31. package/dist/cli.js +5 -5
  32. package/dist/{client-repo-SE4EKG6W.js → client-repo-MPCPTXLF.js} +7 -5
  33. package/dist/create.js +5 -5
  34. package/dist/{fields-FTWWMD7N.js → fields-GTHE6TTS.js} +27 -3
  35. package/dist/gateway-DPPEQZT4.js +25 -0
  36. package/dist/grants-6JY4XZO7.js +27 -0
  37. package/dist/invoke-MRONRZ5Q.js +33 -0
  38. package/dist/moves-UAUCMWL5.js +36 -0
  39. package/dist/{order-U4KZLLSJ.js → order-RKLGT74L.js} +2 -2
  40. package/dist/{person-UHLVBVLR.js → person-YOZTFV4Q.js} +11 -10
  41. package/dist/plan-on-sale-H4F26YIN.js +26 -0
  42. package/dist/platform/migrations/0730_practitioners.sql +79 -0
  43. package/dist/platform/migrations/0750_routine_mail.sql +98 -0
  44. package/dist/platform/migrations/0770_clear_fixed_role_grants.sql +64 -0
  45. package/dist/platform/migrations/0810_recurrence_exceptions.sql +66 -0
  46. package/dist/platform/migrations/0811_staff_time.sql +109 -0
  47. package/dist/platform/migrations/0812_staff_offers.sql +112 -0
  48. package/dist/platform/migrations/0813_pay_periods.sql +241 -0
  49. package/dist/platform/migrations/0814_staff_feed.sql +47 -0
  50. package/dist/platform/migrations/0815_checklists.sql +237 -0
  51. package/dist/platform/migrations/0816_checklist_schedules.sql +121 -0
  52. package/dist/platform/migrations/0817_checklist_day_window.sql +35 -0
  53. package/dist/platform/migrations/0850_agreements.sql +271 -0
  54. package/dist/platform/migrations/0851_agreement_acceptances.sql +387 -0
  55. package/dist/platform/migrations/0853_agreement_enforcement.sql +58 -0
  56. package/dist/platform/migrations/0854_agreement_copy.sql +149 -0
  57. package/dist/platform/migrations/0855_agreement_retention.sql +279 -0
  58. package/dist/platform/migrations/0856_subscription_cooling_off.sql +159 -0
  59. package/dist/platform/migrations/0857_agreement_copy_guardian.sql +337 -0
  60. package/dist/platform/migrations/0858_agreement_desk.sql +175 -0
  61. package/dist/platform/migrations/0910_routine_messages.sql +99 -0
  62. package/dist/platform/migrations/0911_texts.sql +60 -0
  63. package/dist/platform/migrations/0913_contact_consent.sql +93 -0
  64. package/dist/platform/migrations/0916_contact_mailbox_index.sql +29 -0
  65. package/dist/platform/migrations/0930_stock_movements.sql +95 -0
  66. package/dist/platform/migrations/0931_stock_low.sql +25 -0
  67. package/dist/platform/migrations/0932_order_delivery.sql +78 -0
  68. package/dist/platform/migrations/0950_order_adjustments.sql +111 -0
  69. package/dist/platform/migrations/0951_order_price_guards.sql +33 -0
  70. package/dist/platform/migrations/0952_order_paid_another_way.sql +194 -0
  71. package/dist/platform/migrations/0953_discount_uses.sql +183 -0
  72. package/dist/platform/migrations/0955_folded_addresses.sql +92 -0
  73. package/dist/platform/migrations/0970_published_text.sql +59 -0
  74. package/dist/platform/migrations/0971_publish_acks.sql +68 -0
  75. package/dist/platform/migrations/0990_customer_email_change.sql +65 -0
  76. package/dist/platform/migrations/1010_intake_links.sql +148 -0
  77. package/dist/platform/migrations/1030_invoices.sql +747 -0
  78. package/dist/platform/migrations/1031_invoice_orders.sql +327 -0
  79. package/dist/platform/migrations/1050_inbox_conversations.sql +83 -0
  80. package/dist/platform/migrations/1090_staff_hours.sql +43 -0
  81. package/dist/platform/migrations/1091_staff_time_margins.sql +12 -0
  82. package/dist/platform/migrations/1092_client_guard.sql +26 -0
  83. package/dist/platform/migrations/1095_thing_time.sql +70 -0
  84. package/dist/platform/migrations/1130_devices.sql +162 -0
  85. package/dist/platform/migrations/1135_person_codes.sql +88 -0
  86. package/dist/platform/migrations/1150_finance_periods.sql +316 -0
  87. package/dist/platform/migrations/1151_payouts.sql +202 -0
  88. package/dist/platform/migrations/1153_finance_money_dates.sql +18 -0
  89. package/dist/platform/migrations/1170_journeys.sql +318 -0
  90. package/dist/platform/migrations/1171_person_birthday.sql +52 -0
  91. package/dist/platform/migrations/1175_person_signals.sql +29 -0
  92. package/dist/platform/migrations/1190_restricted_values.sql +25 -0
  93. package/dist/platform/migrations/1230_note_levels.sql +113 -0
  94. package/dist/platform/migrations/1231_person_carers.sql +33 -0
  95. package/dist/platform/migrations/1232_note_kinds.sql +122 -0
  96. package/dist/platform/migrations/1270_time_entries.sql +145 -0
  97. package/dist/platform/migrations/1271_leave.sql +249 -0
  98. package/dist/platform/migrations/1273_time_entry_reopened.sql +21 -0
  99. package/dist/platform/migrations/1330_accounting.sql +260 -0
  100. package/dist/platform/migrations/1350_customer_feed.sql +24 -0
  101. package/dist/platform/migrations/1390_locations.sql +77 -0
  102. package/dist/platform/migrations/1410_outside_copy.sql +213 -0
  103. package/dist/platform/migrations/1430_inbox_activities.sql +231 -0
  104. package/dist/platform/migrations/1450_wrong_guesses.sql +23 -0
  105. package/dist/platform/sdk/acknowledge.ts +100 -0
  106. package/dist/platform/sdk/agreements.ts +185 -0
  107. package/dist/platform/sdk/allowance.ts +98 -8
  108. package/dist/platform/sdk/automation-check.ts +120 -9
  109. package/dist/platform/sdk/automation.ts +78 -10
  110. package/dist/platform/sdk/behaviour.ts +26 -6
  111. package/dist/platform/sdk/calendar-entry.ts +258 -0
  112. package/dist/platform/sdk/capacity.ts +1 -2
  113. package/dist/platform/sdk/checklists.ts +118 -0
  114. package/dist/platform/sdk/contract.ts +489 -27
  115. package/dist/platform/sdk/discount.ts +408 -0
  116. package/dist/platform/sdk/document.ts +4 -2
  117. package/dist/platform/sdk/field-types.ts +5 -0
  118. package/dist/platform/sdk/formula/compile.ts +10 -0
  119. package/dist/platform/sdk/index.ts +17 -0
  120. package/dist/platform/sdk/intake.ts +380 -0
  121. package/dist/platform/sdk/journey-signals.ts +215 -0
  122. package/dist/platform/sdk/manifest.ts +1236 -65
  123. package/dist/platform/sdk/mine.ts +161 -0
  124. package/dist/platform/sdk/occupies.ts +192 -0
  125. package/dist/platform/sdk/questions.ts +681 -0
  126. package/dist/platform/sdk/recurring.ts +90 -9
  127. package/dist/platform/sdk/restricted-fields.ts +129 -0
  128. package/dist/platform/sdk/rich-text.ts +112 -0
  129. package/dist/platform/sdk/routine-mail.ts +1272 -0
  130. package/dist/platform/sdk/runtime.ts +25 -12
  131. package/dist/platform/sdk/screens.ts +91 -0
  132. package/dist/platform/sdk/sellable.ts +344 -0
  133. package/dist/platform/sdk/slug.ts +7 -2
  134. package/dist/platform/sdk/staffed.ts +474 -0
  135. package/dist/platform/sdk/states.ts +68 -26
  136. package/dist/platform/sdk/stock.ts +332 -0
  137. package/dist/platform/sdk/team.ts +83 -0
  138. package/dist/platform/sdk/testing.ts +70 -6
  139. package/dist/platform/sdk/worked-on.ts +134 -0
  140. package/dist/{products-2H5DR232.js → products-GCQLSZ3U.js} +2 -2
  141. package/dist/run-7ID64RA6.js +65 -0
  142. package/dist/run-FH7OGGTV.js +131 -0
  143. package/dist/run-OYZIAP6M.js +3363 -0
  144. package/dist/{run-ZCGFMEZF.js → run-QOFRRSZV.js} +41 -60
  145. package/dist/{run-PO3W5BFA.js → run-TJAEWR5U.js} +21 -51
  146. package/dist/{src-I3IJ4NKC.js → src-52VERUPK.js} +306 -4
  147. package/dist/src-PHRELWN5.js +40 -0
  148. package/dist/{visibility-TVXZ67FD.js → visibility-SZCINPP4.js} +21 -4
  149. package/package.json +2 -2
  150. package/dist/chunk-2TWV3SVB.js +0 -349
  151. package/dist/chunk-2XC5JLBO.js +0 -1931
  152. package/dist/chunk-324WJXNP.js +0 -320
  153. package/dist/chunk-6ELZRNUU.js +0 -116
  154. package/dist/chunk-6WGFA2IV.js +0 -643
  155. package/dist/chunk-7CQGMVDO.js +0 -63
  156. package/dist/chunk-7OGCMQXG.js +0 -284
  157. package/dist/chunk-FJIZGS5Q.js +0 -698
  158. package/dist/chunk-UB4I2QKR.js +0 -31575
  159. package/dist/chunk-WHKJGF36.js +0 -2418
  160. package/dist/chunk-YNUZRTPH.js +0 -182
  161. package/dist/chunk-ZZFWMVJO.js +0 -704
  162. package/dist/grants-LOW6TSWD.js +0 -32
  163. package/dist/invoke-6KVT6WJI.js +0 -41
  164. package/dist/moves-E2HKYCTK.js +0 -41
  165. package/dist/plan-on-sale-TJPFCNL7.js +0 -26
  166. package/dist/src-CRO35DYY.js +0 -46
@@ -0,0 +1,337 @@
1
+ -- Migration 0857: a child's paper form copy goes to the parent or guardian staff pick (stage 44,
2
+ -- ADR 0648, amending ADR 0645). Nothing here touches core_write_guard. (0856 is claimed by another
3
+ -- branch of block AX, docs/parallel-work.md.)
4
+ --
5
+ -- Until now `agreement.copy.send` refused a minor's paper form, and the database allowed a paper
6
+ -- copy only to the Person's own linked sign-in address or one of their addresses. Now staff pick a
7
+ -- live guardian of the child, and the copy row records whom (`guardian_id`): its address must be
8
+ -- that guardian's own linked sign-in address or one of their addresses, the guardian must have a
9
+ -- live link to the child at the insert, and only for a paper acceptance. A paper copy with no
10
+ -- guardian is now refused for a Person who is a minor (marked, or with a live guardian link): it
11
+ -- never goes to the child, in the database too. Otherwise migration 0854's rule holds unchanged.
12
+ --
13
+ -- The row belongs to the child's acceptance but carries the guardian's address, so the address
14
+ -- goes with either of them: erasing the child erases the acceptance (migration 0855's trigger
15
+ -- clears the address and owes the scrub); erasing the guardian clears the address, the guardian's
16
+ -- id and any send not yet gone, and owes the scrub too, recorded as `guardian_erased_at`
17
+ -- (`agreements/people.ts`). A merge moves `guardian_id` to the survivor only.
18
+ --
19
+ -- A copy not yet gone goes only while the link is live (the review of PR #301). The moment the
20
+ -- link ends, or is deleted, or the guardian or the child goes to the Trash, every such copy still
21
+ -- open (queued, sending, or waiting for a person's approval) is cancelled: the address cleared,
22
+ -- recorded as `guardian_ended_at`, its pending approval turned down and redacted (so nobody can
23
+ -- release it), and the scrub owed if it ever reached the email gatekeeper. The sender checks the
24
+ -- link again before every send or re-ask (`agreements/copy.ts`).
25
+ --
26
+ -- A cancelled guardian copy that never reached the gatekeeper (no try made, nothing sent, no scrub
27
+ -- owed) may be asked for again, to a live guardian (`core_agreement_copy_reopen`): the same row,
28
+ -- the same first key, under the insert's rules. Nothing was ever sent under that key, so the
29
+ -- gatekeeper's first-send answer can only be for this one: exactly once still holds.
30
+ --
31
+ -- Every other guarantee is unchanged: no insert over an existing row (INSERT OR REPLACE and
32
+ -- upserts are refused, D1 runs with recursive_triggers off), the address never changes once
33
+ -- written except to nothing, a sent copy stays sent, the scrub rules of migration 0855.
34
+
35
+ -- The parent or guardian a child's paper form was sent to, picked by staff; null for every other
36
+ -- copy, and once that guardian is erased.
37
+ ALTER TABLE core_agreement_copy ADD COLUMN guardian_id TEXT;
38
+ -- When that guardian was erased: their address left the row then.
39
+ ALTER TABLE core_agreement_copy ADD COLUMN guardian_erased_at TEXT;
40
+ -- When the guardian's link to the child stopped being live before the copy went: it was cancelled
41
+ -- and its address cleared then.
42
+ ALTER TABLE core_agreement_copy ADD COLUMN guardian_ended_at TEXT;
43
+ CREATE INDEX core_agreement_copy_guardian_idx
44
+ ON core_agreement_copy (guardian_id) WHERE guardian_id IS NOT NULL;
45
+
46
+ DROP TRIGGER core_agreement_copy_new;
47
+ CREATE TRIGGER core_agreement_copy_new
48
+ BEFORE INSERT ON core_agreement_copy
49
+ BEGIN
50
+ SELECT CASE
51
+ WHEN EXISTS (SELECT 1 FROM core_agreement_copy c WHERE c.acceptance_id = NEW.acceptance_id)
52
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
53
+ WHEN NEW.state != 'queued' OR NEW.attempts != 0 OR NEW.sent_at IS NOT NULL
54
+ OR NEW.next_at IS NULL OR NEW.to_address IS NULL
55
+ OR NEW.send_key != 'agreement-copy:' || NEW.acceptance_id
56
+ OR NEW.guardian_erased_at IS NOT NULL OR NEW.guardian_ended_at IS NOT NULL
57
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
58
+ -- Migration 0854's rule: the acceptance's own address, or for paper the Person's, and for
59
+ -- paper never a minor's (marked, or with a live guardian link: as the host reads a minor).
60
+ WHEN NEW.guardian_id IS NULL AND NOT EXISTS (
61
+ SELECT 1 FROM core_agreement_acceptance a
62
+ WHERE a.id = NEW.acceptance_id AND a.erased_at IS NULL AND a.person_id IS NOT NULL
63
+ AND (
64
+ (a.by != 'paper' AND a.email IS NOT NULL AND lower(a.email) = lower(NEW.to_address))
65
+ OR (a.by = 'paper'
66
+ AND NOT EXISTS (SELECT 1 FROM core_agreement_minor m WHERE m.person_id = a.person_id)
67
+ AND NOT EXISTS (SELECT 1 FROM core_person_link l
68
+ JOIN core_person g ON g.id = l.guardian_id AND g.deleted_at IS NULL
69
+ JOIN core_person d ON d.id = l.dependant_id AND d.deleted_at IS NULL
70
+ WHERE l.dependant_id = a.person_id AND l.kind = 'guardian' AND l.ended_at IS NULL)
71
+ AND (
72
+ EXISTS (SELECT 1 FROM core_customer c WHERE c.person_id = a.person_id
73
+ AND c.status = 'active' AND c.link_state = 'linked'
74
+ AND lower(c.email) = lower(NEW.to_address))
75
+ OR EXISTS (SELECT 1 FROM core_person_emails e WHERE e.person_id = a.person_id
76
+ AND lower(e.email) = lower(NEW.to_address))))
77
+ )
78
+ )
79
+ THEN RAISE(ABORT, 'core_agreement_copy_address')
80
+ -- A child's paper form: to a live guardian of the Person it is for (the link not ended, both
81
+ -- out of the Trash, as the seam reads it), at that guardian's own linked sign-in address or
82
+ -- one of their addresses.
83
+ WHEN NEW.guardian_id IS NOT NULL AND NOT EXISTS (
84
+ SELECT 1 FROM core_agreement_acceptance a
85
+ JOIN core_person d ON d.id = a.person_id AND d.deleted_at IS NULL
86
+ JOIN core_person_link l ON l.dependant_id = a.person_id AND l.guardian_id = NEW.guardian_id
87
+ AND l.kind = 'guardian' AND l.ended_at IS NULL
88
+ JOIN core_person g ON g.id = l.guardian_id AND g.deleted_at IS NULL
89
+ WHERE a.id = NEW.acceptance_id AND a.erased_at IS NULL AND a.by = 'paper'
90
+ AND (
91
+ EXISTS (SELECT 1 FROM core_customer c WHERE c.person_id = g.id
92
+ AND c.status = 'active' AND c.link_state = 'linked'
93
+ AND lower(c.email) = lower(NEW.to_address))
94
+ OR EXISTS (SELECT 1 FROM core_person_emails e WHERE e.person_id = g.id
95
+ AND lower(e.email) = lower(NEW.to_address)))
96
+ )
97
+ THEN RAISE(ABORT, 'core_agreement_copy_address')
98
+ END;
99
+ END;
100
+
101
+ -- Migration 0855's rules, plus: whom a child's paper copy went to changes only to a merge's
102
+ -- survivor, or to nothing once that Person is gone (erased), recorded once in guardian_erased_at
103
+ -- with the address cleared; an open copy whose guardian's link is no longer live is cancelled,
104
+ -- recorded once in guardian_ended_at with the address cleared; in either case the address may be
105
+ -- cleared, and a scrub owed, as for an erased acceptance. A cancelled copy coming back is the
106
+ -- reopen trigger's alone.
107
+ DROP TRIGGER core_agreement_copy_no_update;
108
+ CREATE TRIGGER core_agreement_copy_no_update
109
+ BEFORE UPDATE ON core_agreement_copy
110
+ WHEN NOT (OLD.state = 'cancelled' AND NEW.state != 'cancelled')
111
+ BEGIN
112
+ SELECT CASE
113
+ WHEN NEW.acceptance_id IS NOT OLD.acceptance_id
114
+ OR NEW.queued_at IS NOT OLD.queued_at OR NEW.requested_by IS NOT OLD.requested_by
115
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
116
+ WHEN NEW.guardian_id IS NOT OLD.guardian_id AND NOT (
117
+ (OLD.guardian_id IS NOT NULL AND NEW.guardian_id IS NOT NULL
118
+ AND NEW.guardian_id IS (
119
+ SELECT p.merged_into_id FROM core_person p WHERE p.id = OLD.guardian_id))
120
+ OR (OLD.guardian_id IS NOT NULL AND NEW.guardian_id IS NULL
121
+ AND NEW.guardian_erased_at IS NOT NULL AND NEW.to_address IS NULL
122
+ AND NOT EXISTS (SELECT 1 FROM core_person p WHERE p.id = OLD.guardian_id)))
123
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
124
+ WHEN NEW.guardian_erased_at IS NOT OLD.guardian_erased_at AND NOT (
125
+ OLD.guardian_erased_at IS NULL AND OLD.guardian_id IS NOT NULL AND NEW.guardian_id IS NULL)
126
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
127
+ -- Set once, on a copy not yet gone, cancelled with its address cleared, only when the
128
+ -- guardian's link to the child is no longer live.
129
+ WHEN NEW.guardian_ended_at IS NOT OLD.guardian_ended_at AND NOT (
130
+ OLD.guardian_ended_at IS NULL AND OLD.guardian_id IS NOT NULL
131
+ AND OLD.state IN ('queued', 'sending', 'waiting') AND NEW.state = 'cancelled'
132
+ AND NEW.to_address IS NULL
133
+ AND NOT EXISTS (
134
+ SELECT 1 FROM core_agreement_acceptance a
135
+ JOIN core_person d ON d.id = a.person_id AND d.deleted_at IS NULL
136
+ JOIN core_person_link l ON l.dependant_id = a.person_id
137
+ AND l.guardian_id = OLD.guardian_id AND l.kind = 'guardian' AND l.ended_at IS NULL
138
+ JOIN core_person g ON g.id = l.guardian_id AND g.deleted_at IS NULL
139
+ WHERE a.id = OLD.acceptance_id))
140
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
141
+ WHEN NEW.to_address IS NOT OLD.to_address AND NOT (NEW.to_address IS NULL
142
+ AND (NEW.guardian_erased_at IS NOT NULL OR NEW.guardian_ended_at IS NOT NULL
143
+ OR EXISTS (SELECT 1 FROM core_agreement_acceptance a
144
+ WHERE a.id = OLD.acceptance_id AND a.erased_at IS NOT NULL)))
145
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
146
+ WHEN OLD.state = 'sent' AND NEW.state != 'sent'
147
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
148
+ WHEN NEW.scrub_due_at IS NOT NULL AND NEW.guardian_erased_at IS NULL
149
+ AND NEW.guardian_ended_at IS NULL
150
+ AND NOT EXISTS (SELECT 1 FROM core_agreement_acceptance a
151
+ WHERE a.id = OLD.acceptance_id AND a.erased_at IS NOT NULL)
152
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
153
+ WHEN OLD.scrub_due_at IS NOT NULL AND NEW.scrub_due_at IS NULL
154
+ AND NEW.scrubbed_at IS OLD.scrubbed_at
155
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
156
+ WHEN OLD.scrub_owed_since IS NOT NULL AND NEW.scrub_owed_since IS NOT OLD.scrub_owed_since
157
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
158
+ WHEN NEW.scrub_due_at IS NOT NULL AND NEW.scrub_owed_since IS NULL
159
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
160
+ WHEN OLD.via IS NOT NULL AND (NEW.via IS NULL OR EXISTS (
161
+ SELECT 1 FROM json_each(OLD.via) o
162
+ WHERE NOT EXISTS (SELECT 1 FROM json_each(NEW.via) n WHERE n.value = o.value)))
163
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
164
+ END;
165
+ END;
166
+
167
+ -- A cancelled copy asked for again (`agreement.copy.send`): only a guardian copy cancelled because
168
+ -- its guardian was erased or their link ended, that never reached the email gatekeeper (no try,
169
+ -- nothing sent, no scrub owed or done) and whose acceptance is not erased; back to its first
170
+ -- state, with its first key, to a live guardian's own address under the insert's rule. Any other
171
+ -- move out of `cancelled` is refused.
172
+ CREATE TRIGGER core_agreement_copy_reopen
173
+ BEFORE UPDATE ON core_agreement_copy
174
+ WHEN OLD.state = 'cancelled' AND NEW.state != 'cancelled'
175
+ BEGIN
176
+ SELECT CASE
177
+ WHEN NOT (OLD.attempts = 0 AND OLD.sent_at IS NULL AND OLD.to_address IS NULL
178
+ AND OLD.scrub_due_at IS NULL AND OLD.scrub_owed_since IS NULL AND OLD.scrubbed_at IS NULL
179
+ AND OLD.pending_action_id IS NULL
180
+ AND (OLD.guardian_ended_at IS NOT NULL OR OLD.guardian_erased_at IS NOT NULL))
181
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
182
+ WHEN NEW.acceptance_id IS NOT OLD.acceptance_id OR NEW.state != 'queued'
183
+ OR NEW.attempts != 0 OR NEW.sent_at IS NOT NULL OR NEW.next_at IS NULL
184
+ OR NEW.claimed_at IS NOT NULL OR NEW.to_address IS NULL OR NEW.requested_by IS NULL
185
+ OR NEW.send_key != 'agreement-copy:' || OLD.acceptance_id
186
+ OR NEW.guardian_id IS NULL OR NEW.guardian_ended_at IS NOT NULL
187
+ OR NEW.guardian_erased_at IS NOT NULL OR NEW.pending_action_id IS NOT NULL
188
+ OR NEW.scrub_due_at IS NOT NULL OR NEW.scrub_owed_since IS NOT NULL
189
+ OR NEW.scrub_error IS NOT NULL OR NEW.scrub_done IS NOT NULL
190
+ OR NEW.scrub_gone_since IS NOT NULL OR NEW.scrubbed_at IS NOT NULL
191
+ OR NEW.via IS NOT OLD.via
192
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
193
+ WHEN NOT EXISTS (
194
+ SELECT 1 FROM core_agreement_acceptance a
195
+ JOIN core_person d ON d.id = a.person_id AND d.deleted_at IS NULL
196
+ JOIN core_person_link l ON l.dependant_id = a.person_id AND l.guardian_id = NEW.guardian_id
197
+ AND l.kind = 'guardian' AND l.ended_at IS NULL
198
+ JOIN core_person g ON g.id = l.guardian_id AND g.deleted_at IS NULL
199
+ WHERE a.id = OLD.acceptance_id AND a.erased_at IS NULL AND a.by = 'paper'
200
+ AND (
201
+ EXISTS (SELECT 1 FROM core_customer c WHERE c.person_id = g.id
202
+ AND c.status = 'active' AND c.link_state = 'linked'
203
+ AND lower(c.email) = lower(NEW.to_address))
204
+ OR EXISTS (SELECT 1 FROM core_person_emails e WHERE e.person_id = g.id
205
+ AND lower(e.email) = lower(NEW.to_address)))
206
+ )
207
+ THEN RAISE(ABORT, 'core_agreement_copy_address')
208
+ END;
209
+ END;
210
+
211
+ -- The moment a guardian's link stops being live (ended, deleted, or either Person to the Trash):
212
+ -- every guardian copy still open whose link is no longer live is cancelled, its pending approval
213
+ -- turned down and redacted first (so nobody can release the held send), and the scrub owed if it
214
+ -- reached the email gatekeeper. The same two statements in each trigger, and in the sender
215
+ -- (`cancelUnlinkedGuardianCopies`).
216
+ CREATE TRIGGER core_agreement_copy_link_end
217
+ AFTER UPDATE OF ended_at ON core_person_link
218
+ WHEN OLD.ended_at IS NULL AND NEW.ended_at IS NOT NULL
219
+ BEGIN
220
+ UPDATE core_pending_action SET
221
+ input = '"[erased]"', reason = '[erased]', preview = '[]',
222
+ simulated = '{"output":null,"records":[]}',
223
+ status_reason = CASE WHEN status = 'pending'
224
+ THEN 'Rejected: the parent or guardian it was for is no longer linked to the child'
225
+ ELSE status_reason END,
226
+ decided_at = CASE WHEN status = 'pending' THEN strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
227
+ ELSE decided_at END,
228
+ status = CASE WHEN status = 'pending' THEN 'rejected' ELSE status END
229
+ WHERE id IN (SELECT c.pending_action_id FROM core_agreement_copy c
230
+ JOIN core_agreement_acceptance a ON a.id = c.acceptance_id
231
+ WHERE c.guardian_id IS NOT NULL AND c.pending_action_id IS NOT NULL
232
+ AND c.state IN ('queued', 'sending', 'waiting')
233
+ AND NOT EXISTS (SELECT 1 FROM core_person_link l
234
+ JOIN core_person g ON g.id = l.guardian_id AND g.deleted_at IS NULL
235
+ JOIN core_person d ON d.id = l.dependant_id AND d.deleted_at IS NULL
236
+ WHERE l.guardian_id = c.guardian_id AND l.dependant_id = a.person_id
237
+ AND l.kind = 'guardian' AND l.ended_at IS NULL));
238
+ UPDATE core_agreement_copy SET
239
+ scrub_due_at = CASE WHEN attempts > 0 THEN strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
240
+ ELSE scrub_due_at END,
241
+ scrub_owed_since = CASE WHEN attempts > 0
242
+ THEN coalesce(scrub_owed_since, strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
243
+ ELSE scrub_owed_since END,
244
+ to_address = NULL, next_at = NULL, error = NULL, pending_action_id = NULL,
245
+ state = 'cancelled', guardian_ended_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
246
+ WHERE guardian_id IS NOT NULL AND state IN ('queued', 'sending', 'waiting')
247
+ AND NOT EXISTS (SELECT 1 FROM core_agreement_acceptance a
248
+ JOIN core_person_link l ON l.dependant_id = a.person_id
249
+ AND l.guardian_id = core_agreement_copy.guardian_id
250
+ AND l.kind = 'guardian' AND l.ended_at IS NULL
251
+ JOIN core_person g ON g.id = l.guardian_id AND g.deleted_at IS NULL
252
+ JOIN core_person d ON d.id = l.dependant_id AND d.deleted_at IS NULL
253
+ WHERE a.id = core_agreement_copy.acceptance_id);
254
+ END;
255
+
256
+ CREATE TRIGGER core_agreement_copy_link_delete
257
+ AFTER DELETE ON core_person_link
258
+ BEGIN
259
+ UPDATE core_pending_action SET
260
+ input = '"[erased]"', reason = '[erased]', preview = '[]',
261
+ simulated = '{"output":null,"records":[]}',
262
+ status_reason = CASE WHEN status = 'pending'
263
+ THEN 'Rejected: the parent or guardian it was for is no longer linked to the child'
264
+ ELSE status_reason END,
265
+ decided_at = CASE WHEN status = 'pending' THEN strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
266
+ ELSE decided_at END,
267
+ status = CASE WHEN status = 'pending' THEN 'rejected' ELSE status END
268
+ WHERE id IN (SELECT c.pending_action_id FROM core_agreement_copy c
269
+ JOIN core_agreement_acceptance a ON a.id = c.acceptance_id
270
+ WHERE c.guardian_id IS NOT NULL AND c.pending_action_id IS NOT NULL
271
+ AND c.state IN ('queued', 'sending', 'waiting')
272
+ AND NOT EXISTS (SELECT 1 FROM core_person_link l
273
+ JOIN core_person g ON g.id = l.guardian_id AND g.deleted_at IS NULL
274
+ JOIN core_person d ON d.id = l.dependant_id AND d.deleted_at IS NULL
275
+ WHERE l.guardian_id = c.guardian_id AND l.dependant_id = a.person_id
276
+ AND l.kind = 'guardian' AND l.ended_at IS NULL));
277
+ UPDATE core_agreement_copy SET
278
+ scrub_due_at = CASE WHEN attempts > 0 THEN strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
279
+ ELSE scrub_due_at END,
280
+ scrub_owed_since = CASE WHEN attempts > 0
281
+ THEN coalesce(scrub_owed_since, strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
282
+ ELSE scrub_owed_since END,
283
+ to_address = NULL, next_at = NULL, error = NULL, pending_action_id = NULL,
284
+ state = 'cancelled', guardian_ended_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
285
+ WHERE guardian_id IS NOT NULL AND state IN ('queued', 'sending', 'waiting')
286
+ AND NOT EXISTS (SELECT 1 FROM core_agreement_acceptance a
287
+ JOIN core_person_link l ON l.dependant_id = a.person_id
288
+ AND l.guardian_id = core_agreement_copy.guardian_id
289
+ AND l.kind = 'guardian' AND l.ended_at IS NULL
290
+ JOIN core_person g ON g.id = l.guardian_id AND g.deleted_at IS NULL
291
+ JOIN core_person d ON d.id = l.dependant_id AND d.deleted_at IS NULL
292
+ WHERE a.id = core_agreement_copy.acceptance_id);
293
+ END;
294
+
295
+ CREATE TRIGGER core_agreement_copy_person_trash
296
+ AFTER UPDATE OF deleted_at ON core_person
297
+ WHEN OLD.deleted_at IS NULL AND NEW.deleted_at IS NOT NULL
298
+ BEGIN
299
+ UPDATE core_pending_action SET
300
+ input = '"[erased]"', reason = '[erased]', preview = '[]',
301
+ simulated = '{"output":null,"records":[]}',
302
+ status_reason = CASE WHEN status = 'pending'
303
+ THEN 'Rejected: the parent or guardian it was for is no longer linked to the child'
304
+ ELSE status_reason END,
305
+ decided_at = CASE WHEN status = 'pending' THEN strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
306
+ ELSE decided_at END,
307
+ status = CASE WHEN status = 'pending' THEN 'rejected' ELSE status END
308
+ WHERE id IN (SELECT c.pending_action_id FROM core_agreement_copy c
309
+ JOIN core_agreement_acceptance a ON a.id = c.acceptance_id
310
+ WHERE c.guardian_id IS NOT NULL AND c.pending_action_id IS NOT NULL
311
+ AND c.state IN ('queued', 'sending', 'waiting')
312
+ AND NOT EXISTS (SELECT 1 FROM core_person_link l
313
+ JOIN core_person g ON g.id = l.guardian_id AND g.deleted_at IS NULL
314
+ JOIN core_person d ON d.id = l.dependant_id AND d.deleted_at IS NULL
315
+ WHERE l.guardian_id = c.guardian_id AND l.dependant_id = a.person_id
316
+ AND l.kind = 'guardian' AND l.ended_at IS NULL));
317
+ UPDATE core_agreement_copy SET
318
+ scrub_due_at = CASE WHEN attempts > 0 THEN strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
319
+ ELSE scrub_due_at END,
320
+ scrub_owed_since = CASE WHEN attempts > 0
321
+ THEN coalesce(scrub_owed_since, strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
322
+ ELSE scrub_owed_since END,
323
+ to_address = NULL, next_at = NULL, error = NULL, pending_action_id = NULL,
324
+ state = 'cancelled', guardian_ended_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
325
+ WHERE guardian_id IS NOT NULL AND state IN ('queued', 'sending', 'waiting')
326
+ AND NOT EXISTS (SELECT 1 FROM core_agreement_acceptance a
327
+ JOIN core_person_link l ON l.dependant_id = a.person_id
328
+ AND l.guardian_id = core_agreement_copy.guardian_id
329
+ AND l.kind = 'guardian' AND l.ended_at IS NULL
330
+ JOIN core_person g ON g.id = l.guardian_id AND g.deleted_at IS NULL
331
+ JOIN core_person d ON d.id = l.dependant_id AND d.deleted_at IS NULL
332
+ WHERE a.id = core_agreement_copy.acceptance_id);
333
+ END;
334
+
335
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
336
+ UPDATE core_meta SET value = '857'
337
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 857;
@@ -0,0 +1,175 @@
1
+ -- Migration 0858: signing at the desk (stage 44 step 6, ADR 0649). Nothing here touches
2
+ -- core_write_guard.
3
+ --
4
+ -- A desk signing session is one Person signing on the business's own enrolled tablet (core_device,
5
+ -- migration 1130, stage 51's shared piece): a person on the staff starts it for a named Person,
6
+ -- the device they chose and, for a parent, the live dependants they will sign for; the host
7
+ -- answers a short one-time code, typed on that tablet. The tablet holds no sign-in: the session
8
+ -- token it gets at the claim reaches only the desk calls of this one session, with the device's
9
+ -- own credential (ADR 0649).
10
+ --
11
+ -- What the session row holds is the evidence an acceptance made there points at: who started it
12
+ -- and when, the device it was meant for and the one that claimed it, and the address the copies
13
+ -- go to, pinned at the start. None of it changes once written, except: the claim (once), the
14
+ -- activity times, the end (once, and never undone), and erase clearing who it names. The code and
15
+ -- the token are kept only as HMACs under an env secret (DESK_SECRET), and cleared when the session
16
+ -- ends.
17
+ --
18
+ -- This table is not a record: no rec_* view reads it, so query.sql, dashboards, exports, search
19
+ -- and the assistant's bulk reads never see it.
20
+ --
21
+ -- core_device is made by migration 1130 (block BM), which runs after this one in name order;
22
+ -- nothing here names it, and 1130's triggers (which end a device's sessions when it is revoked)
23
+ -- name this table, which exists by then.
24
+
25
+ CREATE TABLE core_agreement_desk_session (
26
+ id TEXT PRIMARY KEY,
27
+ -- Who signs, and the live dependants (a JSON list of Person ids) they sign for. Cleared to null
28
+ -- (and the list emptied of them) when the Person is erased; moved to the survivor on a merge.
29
+ person_id TEXT,
30
+ dependant_ids TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(dependant_ids)
31
+ AND json_type(dependant_ids) = 'array'),
32
+ -- The enrolled device staff chose, and the device a claim came from (the same, or one that tried
33
+ -- and ended the session: `wrong_device`).
34
+ device_id TEXT NOT NULL,
35
+ claimed_device_id TEXT,
36
+ started_by TEXT NOT NULL,
37
+ started_at TEXT NOT NULL,
38
+ -- Where the copies go (ADR 0645): the sign-in address of the Person's account, else their
39
+ -- primary address, pinned at the start. Cleared on erase.
40
+ email TEXT CHECK (email IS NULL OR length(email) BETWEEN 3 AND 320),
41
+ -- The code (8 characters, 2 minutes) and the session token (256 bits), as HMACs only.
42
+ code_hash TEXT CHECK (code_hash IS NULL OR length(code_hash) = 64),
43
+ code_expires_at TEXT NOT NULL,
44
+ token_hash TEXT CHECK (token_hash IS NULL OR length(token_hash) = 64),
45
+ claimed_at TEXT,
46
+ -- The host's limits (decision 22): 3 minutes since the last activity, 20 since the claim. A
47
+ -- heartbeat moves `last_activity_at` only 20 seconds or more after the last one.
48
+ last_activity_at TEXT,
49
+ last_heartbeat_at TEXT,
50
+ ended_at TEXT,
51
+ end_reason TEXT CHECK (end_reason IN ('finished', 'staff', 'unclaimed', 'idle', 'limit',
52
+ 'replaced', 'wrong_device', 'failed_claim', 'revoked', 'device_changed', 'erased')),
53
+ erased_at TEXT,
54
+ CHECK ((ended_at IS NULL) = (end_reason IS NULL)),
55
+ CHECK (claimed_at IS NULL OR claimed_device_id IS NOT NULL),
56
+ CHECK (ended_at IS NOT NULL OR person_id IS NOT NULL)
57
+ );
58
+ -- The device's open session (one at most: a claim ends the others in its batch), and the code's.
59
+ CREATE INDEX core_agreement_desk_session_device_idx
60
+ ON core_agreement_desk_session (device_id, ended_at);
61
+ CREATE INDEX core_agreement_desk_session_code_idx
62
+ ON core_agreement_desk_session (code_hash) WHERE code_hash IS NOT NULL;
63
+ CREATE INDEX core_agreement_desk_session_token_idx
64
+ ON core_agreement_desk_session (token_hash) WHERE token_hash IS NOT NULL;
65
+ CREATE INDEX core_agreement_desk_session_person_idx
66
+ ON core_agreement_desk_session (person_id) WHERE person_id IS NOT NULL;
67
+ CREATE INDEX core_agreement_desk_session_started_idx
68
+ ON core_agreement_desk_session (started_by, started_at);
69
+
70
+ -- What may be inserted: never over an existing id, never already claimed or ended, always with a
71
+ -- code and for a Person who is there.
72
+ CREATE TRIGGER core_agreement_desk_session_new
73
+ BEFORE INSERT ON core_agreement_desk_session
74
+ BEGIN
75
+ SELECT CASE
76
+ WHEN EXISTS (SELECT 1 FROM core_agreement_desk_session s WHERE s.id = NEW.id)
77
+ THEN RAISE(ABORT, 'core_agreement_desk_session_fixed')
78
+ WHEN NEW.claimed_at IS NOT NULL OR NEW.ended_at IS NOT NULL OR NEW.token_hash IS NOT NULL
79
+ OR NEW.code_hash IS NULL OR NEW.erased_at IS NOT NULL OR NEW.claimed_device_id IS NOT NULL
80
+ THEN RAISE(ABORT, 'core_agreement_desk_session_fixed')
81
+ WHEN NOT EXISTS (SELECT 1 FROM core_person p WHERE p.id = NEW.person_id
82
+ AND p.deleted_at IS NULL)
83
+ THEN RAISE(ABORT, 'core_agreement_desk_session_person')
84
+ END;
85
+ END;
86
+
87
+ -- What never changes: who started it, when, for which device and Person (but erase and merge),
88
+ -- the dependants (but erase and merge), when the code ran out, the pinned address (but erase).
89
+ -- A claim is made once; an end is final; an ended session answers nothing, so its code and token
90
+ -- may only be cleared.
91
+ CREATE TRIGGER core_agreement_desk_session_no_update
92
+ BEFORE UPDATE ON core_agreement_desk_session
93
+ BEGIN
94
+ SELECT CASE
95
+ WHEN NEW.id IS NOT OLD.id OR NEW.device_id IS NOT OLD.device_id
96
+ OR NEW.started_by IS NOT OLD.started_by OR NEW.started_at IS NOT OLD.started_at
97
+ OR NEW.code_expires_at IS NOT OLD.code_expires_at
98
+ THEN RAISE(ABORT, 'core_agreement_desk_session_fixed')
99
+ WHEN NOT (NEW.person_id IS OLD.person_id
100
+ OR (NEW.person_id IS NULL AND NEW.erased_at IS NOT NULL
101
+ AND NOT EXISTS (SELECT 1 FROM core_person p WHERE p.id = OLD.person_id))
102
+ OR (NEW.person_id IS NOT NULL AND NEW.person_id IS (
103
+ SELECT p.merged_into_id FROM core_person p WHERE p.id = OLD.person_id)))
104
+ THEN RAISE(ABORT, 'core_agreement_desk_session_fixed')
105
+ WHEN NEW.email IS NOT OLD.email AND NOT (NEW.email IS NULL AND NEW.erased_at IS NOT NULL)
106
+ THEN RAISE(ABORT, 'core_agreement_desk_session_fixed')
107
+ WHEN NEW.dependant_ids IS NOT OLD.dependant_ids
108
+ AND json_array_length(NEW.dependant_ids) > json_array_length(OLD.dependant_ids)
109
+ THEN RAISE(ABORT, 'core_agreement_desk_session_fixed')
110
+ WHEN OLD.claimed_at IS NOT NULL AND (NEW.claimed_at IS NOT OLD.claimed_at
111
+ OR NEW.claimed_device_id IS NOT OLD.claimed_device_id)
112
+ THEN RAISE(ABORT, 'core_agreement_desk_session_fixed')
113
+ WHEN OLD.ended_at IS NOT NULL AND (NEW.ended_at IS NOT OLD.ended_at
114
+ OR NEW.end_reason IS NOT OLD.end_reason OR NEW.claimed_at IS NOT OLD.claimed_at
115
+ OR NEW.last_activity_at IS NOT OLD.last_activity_at
116
+ OR (NEW.token_hash IS NOT NULL AND NEW.token_hash IS NOT OLD.token_hash)
117
+ OR (NEW.code_hash IS NOT NULL AND NEW.code_hash IS NOT OLD.code_hash))
118
+ THEN RAISE(ABORT, 'core_agreement_desk_session_fixed')
119
+ WHEN NEW.erased_at IS NOT OLD.erased_at AND OLD.erased_at IS NOT NULL
120
+ THEN RAISE(ABORT, 'core_agreement_desk_session_fixed')
121
+ END;
122
+ END;
123
+
124
+ -- A session is evidence for the acceptances it made: it is never deleted.
125
+ CREATE TRIGGER core_agreement_desk_session_no_delete
126
+ BEFORE DELETE ON core_agreement_desk_session
127
+ BEGIN
128
+ SELECT RAISE(ABORT, 'core_agreement_desk_session_fixed');
129
+ END;
130
+
131
+ -- An acceptance made at the desk names its session, and only one: claimed, not ended, and for
132
+ -- the Person signing (`self`) or one of the dependants listed (`guardian`, the signer the agent).
133
+ -- Any other channel names none. Every writer is held to it, host code included.
134
+ CREATE TRIGGER core_agreement_acceptance_desk
135
+ BEFORE INSERT ON core_agreement_acceptance
136
+ BEGIN
137
+ SELECT CASE
138
+ WHEN NEW.channel != 'desk' AND NEW.desk_session_id IS NOT NULL
139
+ THEN RAISE(ABORT, 'core_agreement_acceptance_desk')
140
+ WHEN NEW.channel = 'desk' AND NOT EXISTS (
141
+ SELECT 1 FROM core_agreement_desk_session s
142
+ WHERE s.id = NEW.desk_session_id AND s.claimed_at IS NOT NULL AND s.ended_at IS NULL
143
+ AND s.person_id IS NOT NULL AND NEW.account_id IS NULL
144
+ AND ((NEW.by = 'self' AND NEW.person_id = s.person_id)
145
+ OR (NEW.by = 'guardian' AND NEW.agent_person_id = s.person_id
146
+ AND EXISTS (SELECT 1 FROM json_each(s.dependant_ids) d WHERE d.value = NEW.person_id)))
147
+ )
148
+ THEN RAISE(ABORT, 'core_agreement_acceptance_desk')
149
+ END;
150
+ END;
151
+
152
+ -- A claim's guard (after the review of PR #390): the claim's batch carries one row here, and fails
153
+ -- unless the session it claimed is now claimed by that device and still open. Of two claims made
154
+ -- at once (or a claim racing the session's end) the one that lost is refused, never answered with
155
+ -- a token that matches nothing. Stores nothing.
156
+ CREATE TABLE core_agreement_desk_guard (
157
+ session_id TEXT NOT NULL,
158
+ device_id TEXT NOT NULL
159
+ );
160
+
161
+ CREATE TRIGGER core_agreement_desk_guard_check
162
+ BEFORE INSERT ON core_agreement_desk_guard
163
+ BEGIN
164
+ SELECT CASE
165
+ WHEN NOT EXISTS (SELECT 1 FROM core_agreement_desk_session s
166
+ WHERE s.id = NEW.session_id AND s.claimed_device_id = NEW.device_id
167
+ AND s.claimed_at IS NOT NULL AND s.ended_at IS NULL)
168
+ THEN RAISE(ABORT, 'core_agreement_desk_claim_lost')
169
+ ELSE RAISE(IGNORE)
170
+ END;
171
+ END;
172
+
173
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
174
+ UPDATE core_meta SET value = '858'
175
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 858;
@@ -0,0 +1,99 @@
1
+ -- Migration 0910: routine messages (stage 30, ADRs 0670 and 0676; grows ADR 0590's routine mail).
2
+ -- A row of core_routine_mail is now one message to one recipient on one channel: `email` (as
3
+ -- before), `text` or `push`. A kind the host declares itself (a host kind, ADR 0670) is asked for
4
+ -- by host code, not an automation: its rows have no automation or run, and `extension_id` is
5
+ -- `core`, an id no extension can take.
6
+ --
7
+ -- New columns:
8
+ -- - `channel`: `email`, `text` or `push`. Every row written before this is an email.
9
+ -- - `segments`, `cost_minor`: a text's segments and its cost, estimated then reported (the text
10
+ -- gatekeeper, a later step, fills the cost).
11
+ -- - `provider_ref`, `delivered_at`: the provider's id for the message, and when it reported it
12
+ -- delivered (texts; a push service's acceptance is `sent`).
13
+ -- - `wait_until`: quiet hours (ADR 0676). A text or push prepared during them waits, `waiting`
14
+ -- with no pending action, until this moment; then it is sent, or `expired` when `until_at`
15
+ -- has passed.
16
+ -- - `until_at`: the kind's moment for this record (`until`, a class's start): a text or push not
17
+ -- sent by then is dropped, its email having gone.
18
+ --
19
+ -- New states: `held` (a text or push held for a person: never sent, nor released by the clock,
20
+ -- until a person decides), `delivered` (the provider said so), `stopped` (the person's choice or a STOP),
21
+ -- `expired` (its moment passed while it waited), `over_limit` (the month's text limit),
22
+ -- `no_channel` (no address, no textable number, no device).
23
+ --
24
+ -- For a text or push, `recipient` holds the number or device while the row waits and is cleared
25
+ -- once it is decided, like the words; `recipient_key` (a hash for a text) is what the caps count.
26
+ -- `subject` is a push's title and `body` its line, or a text's whole words.
27
+ --
28
+ -- SQLite cannot change a CHECK, so the table is remade with its rows and indexes.
29
+ CREATE TABLE core_routine_mail_new (
30
+ id TEXT PRIMARY KEY,
31
+ kind TEXT NOT NULL,
32
+ message TEXT NOT NULL,
33
+ extension_id TEXT NOT NULL,
34
+ record_object TEXT NOT NULL,
35
+ record_id TEXT NOT NULL,
36
+ channel TEXT NOT NULL DEFAULT 'email' CHECK (channel IN ('email', 'text', 'push')),
37
+ recipient TEXT NOT NULL,
38
+ recipient_rule TEXT NOT NULL CHECK (recipient_rule IN ('person', 'guardian', 'order')),
39
+ recipient_person_id TEXT,
40
+ recipient_key TEXT NOT NULL,
41
+ about_person_id TEXT,
42
+ -- Null for a host kind's row: host code asked for it, not an automation.
43
+ automation_id TEXT,
44
+ run_id TEXT,
45
+ trigger_type TEXT NOT NULL,
46
+ triggered_by TEXT,
47
+ idempotency_key TEXT NOT NULL UNIQUE,
48
+ routine INTEGER NOT NULL CHECK (routine IN (0, 1)),
49
+ ordinary INTEGER NOT NULL DEFAULT 0 CHECK (ordinary IN (0, 1)),
50
+ claimed_at TEXT,
51
+ held_reason TEXT,
52
+ status TEXT NOT NULL
53
+ CHECK (status IN ('off', 'ready', 'sending', 'sent', 'delivered', 'waiting', 'held',
54
+ 'denied', 'failed', 'stopped', 'expired', 'over_limit', 'no_channel')),
55
+ connection_id TEXT,
56
+ subject TEXT,
57
+ body TEXT,
58
+ calendar TEXT,
59
+ related TEXT,
60
+ sources TEXT,
61
+ pending_action_id TEXT,
62
+ error TEXT,
63
+ segments INTEGER,
64
+ cost_minor INTEGER,
65
+ provider_ref TEXT,
66
+ delivered_at TEXT,
67
+ wait_until TEXT,
68
+ until_at TEXT,
69
+ created_at TEXT NOT NULL,
70
+ updated_at TEXT NOT NULL
71
+ );
72
+ INSERT INTO core_routine_mail_new (id, kind, message, extension_id, record_object, record_id,
73
+ channel, recipient, recipient_rule, recipient_person_id, recipient_key, about_person_id,
74
+ automation_id, run_id, trigger_type, triggered_by, idempotency_key, routine, ordinary,
75
+ claimed_at, held_reason, status, connection_id, subject, body, calendar, related, sources,
76
+ pending_action_id, error, created_at, updated_at)
77
+ SELECT id, kind, message, extension_id, record_object, record_id, 'email', recipient,
78
+ recipient_rule, recipient_person_id, recipient_key, about_person_id, automation_id, run_id,
79
+ trigger_type, triggered_by, idempotency_key, routine, ordinary, claimed_at, held_reason, status,
80
+ connection_id, subject, body, calendar, related, sources, pending_action_id, error, created_at,
81
+ updated_at
82
+ FROM core_routine_mail;
83
+ DROP TABLE core_routine_mail;
84
+ ALTER TABLE core_routine_mail_new RENAME TO core_routine_mail;
85
+
86
+ -- The indexes of migration 0750, again.
87
+ CREATE INDEX core_routine_mail_recipient_idx ON core_routine_mail (recipient_key, claimed_at);
88
+ CREATE INDEX core_routine_mail_claimed_idx ON core_routine_mail (claimed_at)
89
+ WHERE claimed_at IS NOT NULL;
90
+ CREATE INDEX core_routine_mail_about_idx ON core_routine_mail (about_person_id)
91
+ WHERE about_person_id IS NOT NULL;
92
+ CREATE INDEX core_routine_mail_record_idx ON core_routine_mail (record_object, record_id);
93
+ CREATE INDEX core_routine_mail_kind_idx ON core_routine_mail (kind, created_at);
94
+ CREATE INDEX core_routine_mail_created_idx ON core_routine_mail (created_at);
95
+ CREATE INDEX core_routine_mail_person_idx ON core_routine_mail (recipient_person_id)
96
+ WHERE recipient_person_id IS NOT NULL;
97
+ -- The end of quiet hours (ADR 0676): the rows waiting for it, soonest first.
98
+ CREATE INDEX core_routine_mail_wait_idx ON core_routine_mail (wait_until)
99
+ WHERE status = 'waiting' AND wait_until IS NOT NULL;