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

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-RPRZMLU5.js +43 -0
  3. package/dist/app-upload-YJVEKZC6.js +198 -0
  4. package/dist/{blocks-5IFU25J6.js → blocks-DE4RRWPJ.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-ZAWES2UD.js → chunk-6CM3WZG5.js} +13 -2483
  10. package/dist/{chunk-7D4GKZOX.js → chunk-CIBJGB5H.js} +422 -75
  11. package/dist/chunk-EPDTPGZJ.js +330 -0
  12. package/dist/{chunk-LAAL4YGR.js → chunk-HAYF4G5V.js} +66 -23
  13. package/dist/chunk-HCG6T53D.js +61904 -0
  14. package/dist/{chunk-K34UKWEI.js → chunk-JX4IFWNT.js} +64 -32
  15. package/dist/{chunk-6XTU2MTP.js → chunk-KFHKNXGH.js} +632 -418
  16. package/dist/chunk-KMFMD52Z.js +13276 -0
  17. package/dist/{chunk-E4WCYTCT.js → chunk-LX2TTLVY.js} +174 -55
  18. package/dist/{chunk-GYTEW66E.js → chunk-O3R4FS2J.js} +1 -1
  19. package/dist/{chunk-E64SG24J.js → chunk-Q6Y45DY6.js} +25 -2
  20. package/dist/{chunk-MBIKVRAC.js → chunk-QJZGYJVV.js} +823 -44
  21. package/dist/{chunk-PDD2ETVF.js → chunk-QQ3YSL5F.js} +172 -16
  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-NNUCHQ2Y.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,387 @@
1
+ -- Migration 0851: acceptances, signatures, health answers, the minor mark, and the write guard's
2
+ -- `agreements` kind (stage 44 step 2, ADRs 0642 and 0643).
3
+ --
4
+ -- An acceptance is one record that a person agreed to one published version: by themselves, by a
5
+ -- guardian for a dependant, by a guest with an order (step 3), or recorded from paper by staff.
6
+ -- Nobody changes what it says. The database refuses every update except setting its Person
7
+ -- (a guest's at settlement, to its order's Person; a merged Person's survivor) and, once the
8
+ -- Person or the guardian who agreed is erased (gone from core_person), clearing who it names; for
9
+ -- every statement shape: D1 runs with recursive triggers off, so the rows an INSERT OR REPLACE or
10
+ -- an UPDATE OR REPLACE deletes fire no delete trigger. So every table here refuses an insert over
11
+ -- an existing key (a REPLACE and an upsert are inserts first), refuses any change of key, and has
12
+ -- no unique index but its key (an UPDATE OR REPLACE of an allowed column can then delete nothing).
13
+ --
14
+ -- None of these tables is a record: no rec_* view reads them, so query.sql, dashboards, exports,
15
+ -- search, embeddings and the assistant's bulk reads never see them. Health answers have their own
16
+ -- table, read by one operation for a person holding agreements:health, every read audited.
17
+
18
+ CREATE TABLE core_agreement_acceptance (
19
+ id TEXT PRIMARY KEY,
20
+ -- What was shown: the version, and the hash of what the screen showed (the version's hash; a
21
+ -- care consent's filled-in values are stage 43's, step 9: until then `context` is always null).
22
+ document_id TEXT NOT NULL,
23
+ version_id TEXT NOT NULL,
24
+ shown_hash TEXT NOT NULL CHECK (length(shown_hash) = 64),
25
+ context TEXT,
26
+ -- Whom it is for. Null only for a guest's acceptance before settlement, or once erased.
27
+ person_id TEXT,
28
+ by TEXT NOT NULL CHECK (by IN ('self', 'guardian', 'guest', 'paper')),
29
+ -- Who agreed: a guardian's Person; the customer account that agreed (their own or the
30
+ -- guardian's). Null for a guest and for paper.
31
+ agent_person_id TEXT,
32
+ account_id TEXT,
33
+ -- What a guest's acceptance was taken with (step 3).
34
+ order_id TEXT,
35
+ subscription_checkout TEXT,
36
+ -- Where the copy goes (step 4): the account's sign-in address, or null. Erase keeps it with the
37
+ -- rest of the evidence (decision 15); step 8's purge deletes the row after the retention period.
38
+ email TEXT,
39
+ how TEXT NOT NULL CHECK (how IN ('typed', 'drawn', 'paper')),
40
+ -- The name typed. Cleared (null) only when the guardian who typed it for a child is erased.
41
+ typed_name TEXT CHECK (typed_name IS NULL OR length(typed_name) BETWEEN 1 AND 200),
42
+ channel TEXT NOT NULL CHECK (channel IN ('web', 'app', 'paper', 'desk')),
43
+ desk_session_id TEXT,
44
+ -- The host's clock at commit.
45
+ accepted_at TEXT NOT NULL,
46
+ -- Paper only: the date on the form, the staff user who recorded it, who signed it (a parent
47
+ -- for a child), and an optional note.
48
+ signed_on TEXT,
49
+ recorded_by TEXT,
50
+ signed_by TEXT,
51
+ note TEXT,
52
+ -- Set once when the Person it is for, or the guardian who made it, is erased: it no longer
53
+ -- counts, and step 8's purge deletes it after the retention period.
54
+ erased_at TEXT,
55
+ CHECK ((by = 'paper') = (how = 'paper')),
56
+ CHECK ((by = 'paper') = (channel = 'paper')),
57
+ CHECK (by != 'paper' OR (signed_on IS NOT NULL AND recorded_by IS NOT NULL)),
58
+ CHECK (by = 'paper' OR (signed_on IS NULL AND recorded_by IS NULL AND signed_by IS NULL)),
59
+ -- Only a guardian's names an agent (the insert trigger checks the link; an erased guardian is
60
+ -- cleared to null later).
61
+ CHECK (by = 'guardian' OR agent_person_id IS NULL),
62
+ CHECK (by != 'guest' OR order_id IS NOT NULL OR subscription_checkout IS NOT NULL)
63
+ );
64
+ CREATE INDEX core_agreement_acceptance_person_idx
65
+ ON core_agreement_acceptance (person_id, document_id, accepted_at);
66
+ CREATE INDEX core_agreement_acceptance_document_idx
67
+ ON core_agreement_acceptance (document_id, accepted_at);
68
+ CREATE INDEX core_agreement_acceptance_agent_idx
69
+ ON core_agreement_acceptance (agent_person_id) WHERE agent_person_id IS NOT NULL;
70
+
71
+ -- What may be inserted: never over an existing id; never erased, never with context, always with
72
+ -- a name; only of a version of a live document that is current or scheduled at commit, showing
73
+ -- that version's hash; a guest's with no Person and an order or checkout that exists, every other
74
+ -- with a Person; for a minor (a Person staff marked, or a live dependant) never their own, and a
75
+ -- minor never agrees for anyone; a guardian's only over a live link.
76
+ CREATE TRIGGER core_agreement_acceptance_new
77
+ BEFORE INSERT ON core_agreement_acceptance
78
+ BEGIN
79
+ SELECT CASE
80
+ WHEN EXISTS (SELECT 1 FROM core_agreement_acceptance a WHERE a.id = NEW.id)
81
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
82
+ WHEN NEW.erased_at IS NOT NULL OR NEW.context IS NOT NULL OR NEW.typed_name IS NULL
83
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
84
+ WHEN (NEW.person_id IS NULL) != (NEW.by = 'guest')
85
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
86
+ WHEN NEW.by = 'guest' AND NOT EXISTS (SELECT 1 FROM core_order o WHERE o.id = NEW.order_id)
87
+ AND NOT EXISTS (SELECT 1 FROM core_subscription_checkout c
88
+ WHERE c.id = NEW.subscription_checkout)
89
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
90
+ WHEN NOT EXISTS (
91
+ SELECT 1 FROM core_agreement_version v
92
+ JOIN core_agreement_document d ON d.id = v.document_id
93
+ WHERE v.id = NEW.version_id AND v.document_id = NEW.document_id
94
+ AND v.hash = NEW.shown_hash AND d.state = 'live' AND d.current_version_id IS NOT NULL
95
+ AND v.number >= (SELECT c.number FROM core_agreement_version c
96
+ WHERE c.id = d.current_version_id)
97
+ )
98
+ THEN RAISE(ABORT, 'core_agreement_version_not_open')
99
+ WHEN NEW.by IN ('self', 'guardian') AND (
100
+ EXISTS (SELECT 1 FROM core_agreement_minor m
101
+ WHERE m.person_id = CASE NEW.by WHEN 'self' THEN NEW.person_id ELSE NEW.agent_person_id END)
102
+ OR EXISTS (
103
+ SELECT 1 FROM core_person_link l
104
+ JOIN core_person g ON g.id = l.guardian_id AND g.deleted_at IS NULL
105
+ JOIN core_person p ON p.id = l.dependant_id AND p.deleted_at IS NULL
106
+ WHERE l.dependant_id = CASE NEW.by WHEN 'self' THEN NEW.person_id
107
+ ELSE NEW.agent_person_id END
108
+ AND l.kind = 'guardian' AND l.ended_at IS NULL
109
+ )
110
+ )
111
+ THEN RAISE(ABORT, 'core_agreement_minor_self')
112
+ WHEN NEW.by = 'guardian' AND NOT EXISTS (
113
+ SELECT 1 FROM core_person_link l
114
+ JOIN core_person g ON g.id = l.guardian_id AND g.deleted_at IS NULL
115
+ JOIN core_person p ON p.id = l.dependant_id AND p.deleted_at IS NULL
116
+ WHERE l.guardian_id = NEW.agent_person_id AND l.dependant_id = NEW.person_id
117
+ AND l.kind = 'guardian' AND l.ended_at IS NULL
118
+ )
119
+ THEN RAISE(ABORT, 'core_person_link_not_live')
120
+ END;
121
+ END;
122
+
123
+ -- Nothing an acceptance says ever changes. The only updates, each tied to what makes it true:
124
+ -- settlement: a guest's Person, null until then, set to its order's Person (or its checkout's
125
+ -- payer or holder), and never on an erased one;
126
+ -- merge: its Person, or the guardian who made it, moved to that Person's `merged_into_id`;
127
+ -- erase of the Person it is for, once that Person is gone (core_person has no row for them):
128
+ -- the Person and the account cleared, `erased_at` set once;
129
+ -- erase of the guardian who made it, once they are gone: the guardian, the account and the
130
+ -- typed name cleared, `erased_at` set once (the child's record that a guardian agreed stays
131
+ -- until the purge).
132
+ -- Nothing is set back, and an erased one is nobody's again.
133
+ CREATE TRIGGER core_agreement_acceptance_no_update
134
+ BEFORE UPDATE ON core_agreement_acceptance
135
+ BEGIN
136
+ SELECT CASE
137
+ WHEN NEW.id IS NOT OLD.id OR NEW.document_id IS NOT OLD.document_id
138
+ OR NEW.version_id IS NOT OLD.version_id OR NEW.shown_hash IS NOT OLD.shown_hash
139
+ OR NEW.context IS NOT OLD.context OR NEW.by IS NOT OLD.by
140
+ OR NEW.order_id IS NOT OLD.order_id
141
+ OR NEW.subscription_checkout IS NOT OLD.subscription_checkout
142
+ OR NEW.how IS NOT OLD.how OR NEW.email IS NOT OLD.email
143
+ OR NEW.channel IS NOT OLD.channel OR NEW.desk_session_id IS NOT OLD.desk_session_id
144
+ OR NEW.accepted_at IS NOT OLD.accepted_at OR NEW.signed_on IS NOT OLD.signed_on
145
+ OR NEW.recorded_by IS NOT OLD.recorded_by OR NEW.signed_by IS NOT OLD.signed_by
146
+ OR NEW.note IS NOT OLD.note
147
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
148
+ -- erased_at: set once, and only as part of one of the two erasures.
149
+ WHEN NOT (NEW.erased_at IS OLD.erased_at
150
+ OR (OLD.erased_at IS NULL AND NEW.erased_at IS NOT NULL AND (
151
+ (OLD.person_id IS NOT NULL AND NEW.person_id IS NULL
152
+ AND NOT EXISTS (SELECT 1 FROM core_person p WHERE p.id = OLD.person_id))
153
+ OR (OLD.by = 'guardian' AND OLD.agent_person_id IS NOT NULL
154
+ AND NEW.agent_person_id IS NULL
155
+ AND NOT EXISTS (SELECT 1 FROM core_person p WHERE p.id = OLD.agent_person_id)))))
156
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
157
+ WHEN NOT (NEW.person_id IS OLD.person_id
158
+ OR (OLD.person_id IS NULL AND NEW.person_id IS NOT NULL AND OLD.by = 'guest'
159
+ AND OLD.erased_at IS NULL AND NEW.erased_at IS NULL AND (
160
+ NEW.person_id IS (SELECT o.person_id FROM core_order o WHERE o.id = OLD.order_id)
161
+ OR EXISTS (SELECT 1 FROM core_subscription_checkout c
162
+ WHERE c.id = OLD.subscription_checkout
163
+ AND NEW.person_id IN (c.payer_person_id, c.holder_person_id))))
164
+ OR (OLD.person_id IS NOT NULL AND NEW.person_id IS NULL AND NEW.erased_at IS NOT NULL
165
+ AND NOT EXISTS (SELECT 1 FROM core_person p WHERE p.id = OLD.person_id))
166
+ OR (OLD.person_id IS NOT NULL AND NEW.person_id IS NOT NULL AND NEW.person_id IS (
167
+ SELECT p.merged_into_id FROM core_person p WHERE p.id = OLD.person_id)))
168
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
169
+ WHEN NOT (NEW.agent_person_id IS OLD.agent_person_id
170
+ OR (OLD.agent_person_id IS NOT NULL AND NEW.agent_person_id IS NULL
171
+ AND NOT EXISTS (SELECT 1 FROM core_person p WHERE p.id = OLD.agent_person_id))
172
+ OR (OLD.agent_person_id IS NOT NULL AND NEW.agent_person_id IS NOT NULL
173
+ AND NEW.agent_person_id IS (
174
+ SELECT p.merged_into_id FROM core_person p WHERE p.id = OLD.agent_person_id)))
175
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
176
+ WHEN NOT (NEW.account_id IS OLD.account_id
177
+ OR (NEW.account_id IS NULL AND NEW.erased_at IS NOT NULL))
178
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
179
+ WHEN NOT (NEW.typed_name IS OLD.typed_name
180
+ OR (NEW.typed_name IS NULL AND OLD.by = 'guardian' AND NEW.agent_person_id IS NULL
181
+ AND NEW.erased_at IS NOT NULL))
182
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
183
+ END;
184
+ END;
185
+
186
+ -- Deleted only with a guest's unpaid order (step 3). An erased one is kept: step 8 adds the purge
187
+ -- after the retention period (and remakes this trigger to allow it). No operation deletes one.
188
+ CREATE TRIGGER core_agreement_acceptance_no_delete
189
+ BEFORE DELETE ON core_agreement_acceptance
190
+ WHEN NOT (OLD.by = 'guest' AND OLD.person_id IS NULL AND OLD.erased_at IS NULL)
191
+ BEGIN
192
+ SELECT RAISE(ABORT, 'core_agreement_acceptance_immutable');
193
+ END;
194
+
195
+ -- A drawn signature, as its strokes (a small checked JSON document, never an image), kept beside
196
+ -- its acceptance so it is read only where it is shown. Personal data: never to an AI actor.
197
+ CREATE TABLE core_agreement_signature (
198
+ acceptance_id TEXT PRIMARY KEY,
199
+ strokes TEXT NOT NULL CHECK (length(strokes) <= 32768)
200
+ );
201
+
202
+ CREATE TRIGGER core_agreement_signature_new
203
+ BEFORE INSERT ON core_agreement_signature
204
+ BEGIN
205
+ SELECT CASE
206
+ WHEN EXISTS (SELECT 1 FROM core_agreement_signature s WHERE s.acceptance_id = NEW.acceptance_id)
207
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
208
+ WHEN NOT EXISTS (SELECT 1 FROM core_agreement_acceptance a
209
+ WHERE a.id = NEW.acceptance_id AND a.how = 'drawn' AND a.erased_at IS NULL)
210
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
211
+ END;
212
+ END;
213
+
214
+ CREATE TRIGGER core_agreement_signature_no_update
215
+ BEFORE UPDATE ON core_agreement_signature
216
+ BEGIN
217
+ SELECT RAISE(ABORT, 'core_agreement_acceptance_immutable');
218
+ END;
219
+
220
+ -- Kept with its acceptance, erased or not (decision 15), except the strokes of a guardian who is
221
+ -- erased (the typed name goes with them) and a guest's unpaid one.
222
+ CREATE TRIGGER core_agreement_signature_no_delete
223
+ BEFORE DELETE ON core_agreement_signature
224
+ WHEN EXISTS (SELECT 1 FROM core_agreement_acceptance a WHERE a.id = OLD.acceptance_id
225
+ AND NOT (a.by = 'guardian' AND a.erased_at IS NOT NULL AND a.agent_person_id IS NULL)
226
+ AND NOT (a.by = 'guest' AND a.person_id IS NULL AND a.erased_at IS NULL))
227
+ BEGIN
228
+ SELECT RAISE(ABORT, 'core_agreement_acceptance_immutable');
229
+ END;
230
+
231
+ -- A health form's answers (decision 5): health information. One row per acceptance of a health
232
+ -- document; `any_yes` is the one fact the coach's list shows (a mark, no content). `entered_by`
233
+ -- is the staff user who typed in a paper form's answers (null when the person answered).
234
+ CREATE TABLE core_agreement_answer (
235
+ acceptance_id TEXT PRIMARY KEY,
236
+ answers TEXT NOT NULL CHECK (length(answers) <= 65536),
237
+ any_yes INTEGER NOT NULL CHECK (any_yes IN (0, 1)),
238
+ entered_by TEXT
239
+ );
240
+
241
+ CREATE TRIGGER core_agreement_answer_new
242
+ BEFORE INSERT ON core_agreement_answer
243
+ BEGIN
244
+ SELECT CASE
245
+ WHEN EXISTS (SELECT 1 FROM core_agreement_answer s WHERE s.acceptance_id = NEW.acceptance_id)
246
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
247
+ WHEN NOT EXISTS (SELECT 1 FROM core_agreement_acceptance a
248
+ JOIN core_agreement_document d ON d.id = a.document_id
249
+ WHERE a.id = NEW.acceptance_id AND d.kind = 'health' AND a.erased_at IS NULL)
250
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
251
+ END;
252
+ END;
253
+
254
+ CREATE TRIGGER core_agreement_answer_no_update
255
+ BEFORE UPDATE ON core_agreement_answer
256
+ BEGIN
257
+ SELECT RAISE(ABORT, 'core_agreement_acceptance_immutable');
258
+ END;
259
+
260
+ -- Health answers are deleted at once when the Person they are about is erased (decision 15):
261
+ -- the erase detaches the acceptance first (which needs the Person gone), in the same batch.
262
+ CREATE TRIGGER core_agreement_answer_no_delete
263
+ BEFORE DELETE ON core_agreement_answer
264
+ WHEN EXISTS (SELECT 1 FROM core_agreement_acceptance a WHERE a.id = OLD.acceptance_id
265
+ AND NOT (a.erased_at IS NOT NULL AND a.person_id IS NULL)
266
+ AND NOT (a.by = 'guest' AND a.person_id IS NULL AND a.erased_at IS NULL))
267
+ BEGIN
268
+ SELECT RAISE(ABORT, 'core_agreement_acceptance_immutable');
269
+ END;
270
+
271
+ -- The minor mark (decision 20): staff with people:link mark a Person whom a parent or guardian
272
+ -- agrees for. The host holds no date of birth and never works out an age; staff clear the mark.
273
+ CREATE TABLE core_agreement_minor (
274
+ person_id TEXT PRIMARY KEY,
275
+ set_by TEXT NOT NULL,
276
+ set_at TEXT NOT NULL
277
+ );
278
+
279
+ -- What is due changes when someone becomes or stops being a minor, a family link starts or ends,
280
+ -- or a Person goes to or from the Trash, is merged or erased: each bumps the counter the
281
+ -- `agreements` guard reads, for every writer (review of PR #274), so a batch planned from a read
282
+ -- before the change fails.
283
+ CREATE TRIGGER core_agreement_rev_minor_insert AFTER INSERT ON core_agreement_minor
284
+ BEGIN
285
+ UPDATE core_agreement_rev SET rev = rev + 1 WHERE id = 1;
286
+ END;
287
+ CREATE TRIGGER core_agreement_rev_minor_delete AFTER DELETE ON core_agreement_minor
288
+ BEGIN
289
+ UPDATE core_agreement_rev SET rev = rev + 1 WHERE id = 1;
290
+ END;
291
+ CREATE TRIGGER core_agreement_rev_link_insert AFTER INSERT ON core_person_link
292
+ BEGIN
293
+ UPDATE core_agreement_rev SET rev = rev + 1 WHERE id = 1;
294
+ END;
295
+ CREATE TRIGGER core_agreement_rev_link_update
296
+ AFTER UPDATE OF guardian_id, dependant_id, ended_at, kind ON core_person_link
297
+ BEGIN
298
+ UPDATE core_agreement_rev SET rev = rev + 1 WHERE id = 1;
299
+ END;
300
+ CREATE TRIGGER core_agreement_rev_link_delete AFTER DELETE ON core_person_link
301
+ BEGIN
302
+ UPDATE core_agreement_rev SET rev = rev + 1 WHERE id = 1;
303
+ END;
304
+ CREATE TRIGGER core_agreement_rev_person_trash
305
+ AFTER UPDATE OF deleted_at, merged_into_id ON core_person
306
+ WHEN NEW.deleted_at IS NOT OLD.deleted_at OR NEW.merged_into_id IS NOT OLD.merged_into_id
307
+ BEGIN
308
+ UPDATE core_agreement_rev SET rev = rev + 1 WHERE id = 1;
309
+ END;
310
+ CREATE TRIGGER core_agreement_rev_person_delete AFTER DELETE ON core_person
311
+ BEGIN
312
+ UPDATE core_agreement_rev SET rev = rev + 1 WHERE id = 1;
313
+ END;
314
+
315
+ -- Shapes that would change what is due without a bump (the second review of PR #274): a minor mark
316
+ -- is set or cleared, never rewritten to another Person; and a Person is never inserted over one
317
+ -- that exists (D1 runs without recursive triggers, so an INSERT OR REPLACE would delete the old
318
+ -- row, Trash and all, without the delete trigger above). The host only ever inserts a new Person
319
+ -- (person.create, with a new id) and updates one in place.
320
+ CREATE TRIGGER core_agreement_minor_no_update BEFORE UPDATE ON core_agreement_minor
321
+ BEGIN
322
+ SELECT RAISE(ABORT, 'core_agreement_minor_fixed');
323
+ END;
324
+ CREATE TRIGGER core_person_no_replace BEFORE INSERT ON core_person
325
+ WHEN EXISTS (SELECT 1 FROM core_person p WHERE p.id = NEW.id)
326
+ BEGIN
327
+ SELECT RAISE(ABORT, 'core_person_exists');
328
+ END;
329
+
330
+ -- The write guard's `agreements` kind (stage 44, ADR 0641 as amended): a batch planned from a read
331
+ -- of what is due carries the counter it read (core_agreement_rev); a publish, a version taking
332
+ -- effect, an attach, a detach or a retire committed in between bumps it, and the batch fails
333
+ -- (`core_agreements_changed`). core_write_guard stores nothing, so it is made again with the new
334
+ -- kind, as migrations 0560, 0610, 0670 and 0690 did; every earlier kind is carried forward as
335
+ -- migration 0690 left it.
336
+ DROP TRIGGER core_write_guard_check;
337
+ DROP TABLE core_write_guard;
338
+
339
+ CREATE TABLE core_write_guard (
340
+ kind TEXT NOT NULL
341
+ CHECK (kind IN ('claimed', 'claimant', 'series', 'full', 'unmoved', 'held_cap', 'agreements')),
342
+ object_type TEXT NOT NULL,
343
+ record_id TEXT NOT NULL,
344
+ expected INTEGER NOT NULL
345
+ );
346
+
347
+ CREATE TRIGGER core_write_guard_check
348
+ BEFORE INSERT ON core_write_guard
349
+ BEGIN
350
+ SELECT CASE
351
+ WHEN NEW.kind = 'claimed' AND coalesce((SELECT held + taken FROM core_capacity
352
+ WHERE object_type = NEW.object_type AND record_id = NEW.record_id), 0) != NEW.expected
353
+ THEN RAISE(ABORT, 'core_recurrence_stale')
354
+ WHEN NEW.kind = 'series' AND coalesce((SELECT version FROM core_recurrence_series
355
+ WHERE object_type = NEW.object_type AND record_id = NEW.record_id), 0) != NEW.expected
356
+ THEN RAISE(ABORT, 'core_recurrence_stale')
357
+ WHEN NEW.kind = 'claimant' AND (SELECT count(*) FROM core_capacity_claim
358
+ WHERE for_type = NEW.object_type AND for_id = NEW.record_id
359
+ AND state IN ('held', 'confirmed')) != NEW.expected
360
+ THEN RAISE(ABORT, 'core_claim_live')
361
+ WHEN NEW.kind = 'full' AND NOT EXISTS (SELECT 1 FROM core_capacity
362
+ WHERE object_type = NEW.object_type AND record_id = NEW.record_id
363
+ AND places IS NOT NULL AND held + taken >= places)
364
+ THEN RAISE(ABORT, 'core_waitlist_not_full')
365
+ WHEN NEW.kind = 'unmoved' AND (SELECT count(*) FROM core_transition_history
366
+ WHERE object_type = NEW.object_type AND record_id = NEW.record_id) != NEW.expected
367
+ THEN RAISE(ABORT, 'core_moved_meanwhile')
368
+ WHEN NEW.kind = 'held_cap' AND (SELECT count(*) FROM core_capacity_claim
369
+ WHERE object_type = NEW.object_type AND record_id = NEW.record_id
370
+ AND state = 'held' AND for_type != 'waitlist') > NEW.expected
371
+ THEN RAISE(ABORT, 'core_holds_capped')
372
+ WHEN NEW.kind = 'agreements' AND coalesce((SELECT rev FROM core_agreement_rev
373
+ WHERE id = 1), 0) != NEW.expected
374
+ THEN RAISE(ABORT, 'core_agreements_changed')
375
+ ELSE RAISE(IGNORE)
376
+ END;
377
+ END;
378
+
379
+ -- A notice for the person only (stage 44, ADR 0643): no AI actor acting for its recipient lists it,
380
+ -- counts it or marks it read. A health "yes" notice is written in the acceptance's batch, so its
381
+ -- time alone would say who answered yes.
382
+ ALTER TABLE core_notifications ADD COLUMN human_only INTEGER NOT NULL DEFAULT 0
383
+ CHECK (human_only IN (0, 1));
384
+
385
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
386
+ UPDATE core_meta SET value = '851'
387
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 851;
@@ -0,0 +1,58 @@
1
+ -- Migration 0853: a guest's acceptances settle with what they paid for, and go with what they
2
+ -- did not (stage 44 step 3, ADR 0644). (0852 is reserved for another build's core_write_guard
3
+ -- remake; nothing here touches that table.)
4
+ --
5
+ -- A visitor buying without an account carries their acceptances in the buying call: the host
6
+ -- records them with the order (or the subscription checkout) and no Person (migration 0851's
7
+ -- insert trigger requires it). They become a Person's only when the purchase is real, and never
8
+ -- by the address alone, which anyone may type:
9
+ -- - an order: once something has been paid on it and it has a Person. That is the verified
10
+ -- payment's batch (the payment sets `paid`; a guest's drop-in gets its Person there too), or
11
+ -- `order.setPerson`'s when the address matched several People;
12
+ -- - a subscription checkout: once the provider's verified event made its subscription (the
13
+ -- host sets `subscription_ref` in that batch). Participant documents go to the holder, payer
14
+ -- documents to the payer (for a guest the same Person).
15
+ -- An order that closes with nothing paid (expired, or cancelled) deletes its guest acceptances,
16
+ -- their signatures and answers in the same statement: no contract was made, and they are nobody's
17
+ -- personal data to keep. Triggers, so every writer and every path does it in the batch that makes
18
+ -- it true, whatever order the host queues its statements in. Migration 0851's acceptance
19
+ -- triggers still judge each update and delete: a Person only from the order or the checkout, a
20
+ -- delete only of a guest's with no Person.
21
+
22
+ CREATE TRIGGER core_agreement_guest_settle_order
23
+ AFTER UPDATE OF paid, person_id ON core_order
24
+ WHEN NEW.person_id IS NOT NULL AND NEW.paid > 0
25
+ BEGIN
26
+ UPDATE core_agreement_acceptance SET person_id = NEW.person_id
27
+ WHERE order_id = NEW.id AND by = 'guest' AND person_id IS NULL AND erased_at IS NULL;
28
+ END;
29
+
30
+ CREATE TRIGGER core_agreement_guest_unpaid
31
+ AFTER UPDATE OF state ON core_order
32
+ WHEN NEW.state IN ('expired', 'cancelled') AND NEW.paid = 0
33
+ BEGIN
34
+ DELETE FROM core_agreement_answer WHERE acceptance_id IN (
35
+ SELECT id FROM core_agreement_acceptance
36
+ WHERE order_id = NEW.id AND by = 'guest' AND person_id IS NULL AND erased_at IS NULL);
37
+ DELETE FROM core_agreement_signature WHERE acceptance_id IN (
38
+ SELECT id FROM core_agreement_acceptance
39
+ WHERE order_id = NEW.id AND by = 'guest' AND person_id IS NULL AND erased_at IS NULL);
40
+ DELETE FROM core_agreement_acceptance
41
+ WHERE order_id = NEW.id AND by = 'guest' AND person_id IS NULL AND erased_at IS NULL;
42
+ END;
43
+
44
+ CREATE TRIGGER core_agreement_guest_settle_subscription
45
+ AFTER UPDATE OF subscription_ref ON core_subscription_checkout
46
+ WHEN NEW.subscription_ref IS NOT NULL
47
+ BEGIN
48
+ UPDATE core_agreement_acceptance SET person_id = CASE (
49
+ SELECT v.who FROM core_agreement_version v
50
+ WHERE v.id = core_agreement_acceptance.version_id)
51
+ WHEN 'payer' THEN NEW.payer_person_id ELSE NEW.holder_person_id END
52
+ WHERE subscription_checkout = NEW.id AND by = 'guest' AND person_id IS NULL
53
+ AND erased_at IS NULL;
54
+ END;
55
+
56
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
57
+ UPDATE core_meta SET value = '853'
58
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 853;
@@ -0,0 +1,149 @@
1
+ -- Migration 0854: the copy of a signed agreement (stage 44 step 4, ADR 0645). Nothing here touches
2
+ -- core_write_guard.
3
+ --
4
+ -- Each acceptance by a person is emailed to them once, as the host's own mail, with the signed
5
+ -- PDF the email gatekeeper makes. Whether it went is kept here, beside the acceptance, so the
6
+ -- acceptance itself never changes (migration 0851's triggers are untouched). One row per
7
+ -- acceptance, made:
8
+ -- - by trigger, when a person agrees for themselves or as a guardian (`self`, `guardian`), to
9
+ -- the address the acceptance holds (their account's sign-in address);
10
+ -- - by trigger, when a guest's acceptance settles to its purchase's Person (migration 0853), to
11
+ -- the address the purchase was made with (ADR 0644: it is evidence of that purchase only);
12
+ -- - by `agreement.copy.send`, when staff press "Email a copy" of a paper form, to an address the
13
+ -- host holds for the Person it is for.
14
+ -- Triggers, so every writer and every path makes it in the batch that records the acceptance.
15
+ --
16
+ -- Where a copy goes is the host's records', never a caller's: the insert trigger checks the
17
+ -- address against the acceptance (or, for paper, against the Person's own addresses), and no
18
+ -- update can change it, except erase clearing it. What may change is only how sending went.
19
+ --
20
+ -- (Edited in place after the review of PR #283, before it merged: `next_at` is set on every row
21
+ -- the sender still has work for, so each state's due rows are read in index order and a read
22
+ -- stops at its limit; a paper copy's account address must be a linked account's, as the code
23
+ -- reads it; erase clears the address.)
24
+
25
+ CREATE TABLE core_agreement_copy (
26
+ acceptance_id TEXT PRIMARY KEY,
27
+ -- Cleared (null) when the acceptance is erased.
28
+ to_address TEXT CHECK (to_address IS NULL OR length(to_address) BETWEEN 3 AND 320),
29
+ -- queued: to send; sending: one sender has claimed it; sent: the gatekeeper applied it;
30
+ -- waiting: held for a person's approval (asked again until it is sent or turned down);
31
+ -- failed: refused, or the outcome unknown (never sent twice); cancelled: the acceptance was
32
+ -- erased before it went.
33
+ state TEXT NOT NULL CHECK (state IN ('queued', 'sending', 'sent', 'waiting', 'failed', 'cancelled')),
34
+ -- The idempotency key of the current try: `agreement-copy:<acceptance id>`, and a new one only
35
+ -- after a try the gatekeeper proved was not sent (ADR 0574).
36
+ send_key TEXT NOT NULL CHECK (send_key LIKE 'agreement-copy:%'),
37
+ attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0),
38
+ queued_at TEXT NOT NULL,
39
+ -- When the sender next looks at it: queued and waiting rows when due, a sending row when its
40
+ -- claim goes stale. Every row the sender still has work for has one.
41
+ next_at TEXT,
42
+ claimed_at TEXT,
43
+ sent_at TEXT,
44
+ pending_action_id TEXT,
45
+ -- What went wrong, as a category (`queue_full`, `refused`, …): never the gatekeeper's or the
46
+ -- provider's words, which may name the recipient. Cleared on erase.
47
+ error TEXT CHECK (error IS NULL OR length(error) <= 500),
48
+ -- Paper: the staff user who pressed "Email a copy".
49
+ requested_by TEXT,
50
+ CHECK (state NOT IN ('queued', 'sending', 'waiting') OR next_at IS NOT NULL),
51
+ CHECK (to_address IS NOT NULL OR state IN ('cancelled', 'sent', 'failed'))
52
+ );
53
+ CREATE INDEX core_agreement_copy_due_idx ON core_agreement_copy (state, next_at);
54
+
55
+ -- What may be inserted: never over an existing row (no REPLACE, no upsert); only for an
56
+ -- acceptance that exists and is not erased; queued and due, with the first key; to the
57
+ -- acceptance's own address, or for paper to an address the host holds for its Person (an active,
58
+ -- linked customer account's sign-in address, or one of the Person's addresses), compared without
59
+ -- case.
60
+ CREATE TRIGGER core_agreement_copy_new
61
+ BEFORE INSERT ON core_agreement_copy
62
+ BEGIN
63
+ SELECT CASE
64
+ WHEN EXISTS (SELECT 1 FROM core_agreement_copy c WHERE c.acceptance_id = NEW.acceptance_id)
65
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
66
+ WHEN NEW.state != 'queued' OR NEW.attempts != 0 OR NEW.sent_at IS NOT NULL
67
+ OR NEW.next_at IS NULL OR NEW.to_address IS NULL
68
+ OR NEW.send_key != 'agreement-copy:' || NEW.acceptance_id
69
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
70
+ WHEN NOT EXISTS (
71
+ SELECT 1 FROM core_agreement_acceptance a
72
+ WHERE a.id = NEW.acceptance_id AND a.erased_at IS NULL AND a.person_id IS NOT NULL
73
+ AND (
74
+ (a.by != 'paper' AND a.email IS NOT NULL AND lower(a.email) = lower(NEW.to_address))
75
+ OR (a.by = 'paper' AND (
76
+ EXISTS (SELECT 1 FROM core_customer c WHERE c.person_id = a.person_id
77
+ AND c.status = 'active' AND c.link_state = 'linked'
78
+ AND lower(c.email) = lower(NEW.to_address))
79
+ OR EXISTS (SELECT 1 FROM core_person_emails e WHERE e.person_id = a.person_id
80
+ AND lower(e.email) = lower(NEW.to_address))))
81
+ )
82
+ )
83
+ THEN RAISE(ABORT, 'core_agreement_copy_address')
84
+ END;
85
+ END;
86
+
87
+ -- Who it is for, when it was asked for and by whom never change; where it goes changes only to
88
+ -- nothing, once its acceptance is erased; a copy sent stays sent.
89
+ CREATE TRIGGER core_agreement_copy_no_update
90
+ BEFORE UPDATE ON core_agreement_copy
91
+ BEGIN
92
+ SELECT CASE
93
+ WHEN NEW.acceptance_id IS NOT OLD.acceptance_id
94
+ OR NEW.queued_at IS NOT OLD.queued_at OR NEW.requested_by IS NOT OLD.requested_by
95
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
96
+ WHEN NEW.to_address IS NOT OLD.to_address AND NOT (NEW.to_address IS NULL
97
+ AND EXISTS (SELECT 1 FROM core_agreement_acceptance a
98
+ WHERE a.id = OLD.acceptance_id AND a.erased_at IS NOT NULL))
99
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
100
+ WHEN OLD.state = 'sent' AND NEW.state != 'sent'
101
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
102
+ END;
103
+ END;
104
+
105
+ -- A person agreeing (for themselves, or as a guardian for a child), signed in: the copy goes to
106
+ -- the address the acceptance holds. Paper sends none unless staff ask; a guest's waits for its
107
+ -- purchase.
108
+ CREATE TRIGGER core_agreement_copy_on_accept
109
+ AFTER INSERT ON core_agreement_acceptance
110
+ WHEN NEW.by IN ('self', 'guardian') AND NEW.email IS NOT NULL AND NEW.person_id IS NOT NULL
111
+ BEGIN
112
+ INSERT INTO core_agreement_copy (acceptance_id, to_address, state, send_key, attempts, queued_at,
113
+ next_at)
114
+ VALUES (NEW.id, NEW.email, 'queued', 'agreement-copy:' || NEW.id, 0, NEW.accepted_at,
115
+ NEW.accepted_at);
116
+ END;
117
+
118
+ -- A guest's acceptance once its purchase settles it to a Person (migration 0853's triggers, the
119
+ -- verified payment's batch or order.setPerson's): the copy goes to the address the purchase was
120
+ -- made with, which the acceptance holds. An order that closes unpaid deletes its guest rows
121
+ -- before any copy exists.
122
+ CREATE TRIGGER core_agreement_copy_on_settle
123
+ AFTER UPDATE OF person_id ON core_agreement_acceptance
124
+ WHEN OLD.person_id IS NULL AND NEW.person_id IS NOT NULL AND NEW.by = 'guest'
125
+ AND NEW.email IS NOT NULL AND NEW.erased_at IS NULL
126
+ BEGIN
127
+ INSERT INTO core_agreement_copy (acceptance_id, to_address, state, send_key, attempts, queued_at,
128
+ next_at)
129
+ SELECT NEW.id, NEW.email, 'queued', 'agreement-copy:' || NEW.id, 0,
130
+ strftime('%Y-%m-%dT%H:%M:%fZ', 'now'), strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
131
+ WHERE NOT EXISTS (SELECT 1 FROM core_agreement_copy c WHERE c.acceptance_id = NEW.id);
132
+ END;
133
+
134
+ -- Erase (and the Trash purge): a copy not yet gone is never sent, a claimed one included, and no
135
+ -- copy keeps the address. (A copy already waiting for a person's approval is cancelled here; the
136
+ -- approval itself and the gatekeeper's journal entry are step 8's, docs/stages/44-agreements.md.)
137
+ CREATE TRIGGER core_agreement_copy_on_erase
138
+ AFTER UPDATE OF erased_at ON core_agreement_acceptance
139
+ WHEN OLD.erased_at IS NULL AND NEW.erased_at IS NOT NULL
140
+ BEGIN
141
+ UPDATE core_agreement_copy
142
+ SET to_address = NULL, next_at = NULL, error = NULL, pending_action_id = NULL,
143
+ state = CASE WHEN state IN ('sent', 'failed') THEN state ELSE 'cancelled' END
144
+ WHERE acceptance_id = NEW.id;
145
+ END;
146
+
147
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
148
+ UPDATE core_meta SET value = '854'
149
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 854;