@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,194 @@
1
+ -- Migration 0952: an order paid another way (stage 34 step 3, ADR 0694).
2
+ --
3
+ -- Until now every row of core_order_payment was the provider's verified payment, and that was the
4
+ -- one door into `paid` (ADR 0427). This adds the second: a payment the host applies itself
5
+ -- (`applyOtherPayment`, orders/payments.ts), for money a person says they received (`recorded`:
6
+ -- a bank transfer, cash, a cheque, a card machine) and, in later steps, value the host holds (a
7
+ -- gift voucher, a gift card or account credit). Each is a payment row with its `method`.
8
+ --
9
+ -- A row the host makes is named `host` with an event id the host makes, so the primary key still
10
+ -- means "applied once". A recorded payment entered in error is undone by a second row (its amount
11
+ -- the first's, negated, and `reverses` naming it), never by an edit or a delete; a recorded
12
+ -- payment's refund is a refund row of method `recorded`, paid back by a person.
13
+
14
+ ALTER TABLE core_order_payment ADD COLUMN method TEXT NOT NULL DEFAULT 'provider'
15
+ CHECK (method IN ('provider', 'recorded', 'voucher', 'stored_value'));
16
+ -- A recorded payment: how it was paid, the person who says they received it, the day they did,
17
+ -- and the payer's reference (a bank reference, a cheque number). Blanked by an erase.
18
+ ALTER TABLE core_order_payment ADD COLUMN recorded_kind TEXT
19
+ CHECK (recorded_kind IS NULL OR recorded_kind IN ('transfer', 'cash', 'cheque', 'card_machine', 'other'));
20
+ ALTER TABLE core_order_payment ADD COLUMN recorded_by TEXT;
21
+ ALTER TABLE core_order_payment ADD COLUMN received_on TEXT;
22
+ ALTER TABLE core_order_payment ADD COLUMN reference TEXT;
23
+ -- The value a `voucher` or `stored_value` row spent (stages 34 steps 7 and 8 write them).
24
+ ALTER TABLE core_order_payment ADD COLUMN voucher_id TEXT;
25
+ ALTER TABLE core_order_payment ADD COLUMN stored_value_id TEXT;
26
+ -- A reversal: the event id of the host's row it undoes, and why (staff's words, blanked by an
27
+ -- erase). Each row is reversed at most once.
28
+ ALTER TABLE core_order_payment ADD COLUMN reverses TEXT;
29
+ ALTER TABLE core_order_payment ADD COLUMN reason TEXT;
30
+ CREATE UNIQUE INDEX core_order_payment_reverses_uq ON core_order_payment (reverses)
31
+ WHERE reverses IS NOT NULL;
32
+
33
+ -- What a payment row may be. Nothing a caller sends reaches these columns except through the
34
+ -- operations that call applyOtherPayment; this is the database's own floor under them.
35
+
36
+ -- The provider's rows are the provider's; the host's are never provider rows, and are applied.
37
+ CREATE TRIGGER core_order_payment_method
38
+ BEFORE INSERT ON core_order_payment
39
+ WHEN (NEW.method = 'provider') = (NEW.provider = 'host')
40
+ OR (NEW.method <> 'provider' AND NEW.outcome <> 'applied')
41
+ OR (NEW.method <> 'recorded'
42
+ AND (NEW.recorded_kind IS NOT NULL OR NEW.recorded_by IS NOT NULL OR NEW.reverses IS NOT NULL))
43
+ BEGIN
44
+ SELECT RAISE(ABORT, 'core_order_payment_method');
45
+ END;
46
+
47
+ -- A recorded payment names the person who received it, how and when.
48
+ CREATE TRIGGER core_order_payment_recorded
49
+ BEFORE INSERT ON core_order_payment
50
+ WHEN NEW.method = 'recorded'
51
+ AND (NEW.recorded_by IS NULL OR NEW.recorded_kind IS NULL OR NEW.received_on IS NULL)
52
+ BEGIN
53
+ SELECT RAISE(ABORT, 'core_order_payment_recorded');
54
+ END;
55
+
56
+ -- A voucher or stored value row is made only beside its ledger entry in the same batch (the
57
+ -- voucher redeemed, the value spent). Neither ledger exists yet (stage 34 steps 7 and 8), so none
58
+ -- can be made: those steps replace this trigger with one that looks for the entry.
59
+ CREATE TRIGGER core_order_payment_ledger
60
+ BEFORE INSERT ON core_order_payment
61
+ WHEN NEW.method IN ('voucher', 'stored_value')
62
+ BEGIN
63
+ SELECT RAISE(ABORT, 'core_order_payment_ledger');
64
+ END;
65
+
66
+ -- The host's own money in is positive; only a reversal is negative, and it undoes exactly one
67
+ -- applied recorded payment of the same order, with a reason.
68
+ CREATE TRIGGER core_order_payment_amount
69
+ BEFORE INSERT ON core_order_payment
70
+ WHEN NEW.provider = 'host' AND NEW.reverses IS NULL AND NEW.amount <= 0
71
+ BEGIN
72
+ SELECT RAISE(ABORT, 'core_order_payment_amount');
73
+ END;
74
+
75
+ CREATE TRIGGER core_order_payment_reversal
76
+ BEFORE INSERT ON core_order_payment
77
+ WHEN NEW.reverses IS NOT NULL
78
+ AND (NEW.reason IS NULL OR NOT EXISTS (
79
+ SELECT 1 FROM core_order_payment p
80
+ WHERE p.provider = 'host' AND p.event_id = NEW.reverses AND p.order_id = NEW.order_id
81
+ AND p.method = 'recorded' AND p.outcome = 'applied' AND p.reverses IS NULL
82
+ AND p.amount = -NEW.amount AND p.currency = NEW.currency))
83
+ BEGIN
84
+ SELECT RAISE(ABORT, 'core_order_payment_reversal');
85
+ END;
86
+
87
+ -- The host's rows are kept as written: an erase may only blank the reference and the reason.
88
+ CREATE TRIGGER core_order_payment_host_fixed
89
+ BEFORE UPDATE ON core_order_payment
90
+ WHEN OLD.provider = 'host'
91
+ AND (NEW.provider IS NOT OLD.provider OR NEW.event_id IS NOT OLD.event_id
92
+ OR NEW.order_id IS NOT OLD.order_id OR NEW.amount IS NOT OLD.amount
93
+ OR NEW.currency IS NOT OLD.currency OR NEW.outcome IS NOT OLD.outcome
94
+ OR NEW.payment_ref IS NOT OLD.payment_ref OR NEW.method IS NOT OLD.method
95
+ OR NEW.recorded_kind IS NOT OLD.recorded_kind OR NEW.recorded_by IS NOT OLD.recorded_by
96
+ OR NEW.received_on IS NOT OLD.received_on OR NEW.voucher_id IS NOT OLD.voucher_id
97
+ OR NEW.stored_value_id IS NOT OLD.stored_value_id OR NEW.reverses IS NOT OLD.reverses
98
+ OR (NEW.reference IS NOT OLD.reference AND NEW.reference IS NOT NULL)
99
+ OR (NEW.reason IS NOT OLD.reason AND NEW.reason IS NOT NULL))
100
+ BEGIN
101
+ SELECT RAISE(ABORT, 'core_order_payment_fixed');
102
+ END;
103
+
104
+ -- Nor deleted: a reversal deleted would make the payment it undid refundable again while the
105
+ -- order's paid stays as it is (review of PR #426).
106
+ CREATE TRIGGER core_order_payment_host_kept
107
+ BEFORE DELETE ON core_order_payment
108
+ WHEN OLD.provider = 'host'
109
+ BEGIN
110
+ SELECT RAISE(ABORT, 'core_order_payment_kept');
111
+ END;
112
+
113
+ -- Refunds by method (amends ADR 0429): what went back the way it came. A recorded payment's money
114
+ -- is paid back by a person, who records how; nothing is sent by the host.
115
+ ALTER TABLE core_order_refund ADD COLUMN method TEXT NOT NULL DEFAULT 'provider'
116
+ CHECK (method IN ('provider', 'recorded', 'voucher', 'stored_value'));
117
+ ALTER TABLE core_order_refund ADD COLUMN recorded_kind TEXT
118
+ CHECK (recorded_kind IS NULL OR recorded_kind IN ('transfer', 'cash', 'cheque', 'card_machine', 'other'));
119
+ ALTER TABLE core_order_refund ADD COLUMN reference TEXT;
120
+
121
+ CREATE TRIGGER core_order_refund_method
122
+ BEFORE INSERT ON core_order_refund
123
+ WHEN (NEW.method = 'provider') = (NEW.provider = 'host')
124
+ OR (NEW.method <> 'recorded' AND NEW.recorded_kind IS NOT NULL)
125
+ BEGIN
126
+ SELECT RAISE(ABORT, 'core_order_refund_method');
127
+ END;
128
+
129
+ -- Paid back by a person: confirmed as it is written, naming who, how, and the recorded payment.
130
+ CREATE TRIGGER core_order_refund_recorded
131
+ BEFORE INSERT ON core_order_refund
132
+ WHEN NEW.method = 'recorded'
133
+ AND (NEW.status <> 'confirmed' OR NEW.requested_by IS NULL OR NEW.recorded_kind IS NULL
134
+ OR NOT EXISTS (
135
+ SELECT 1 FROM core_order_payment p
136
+ WHERE p.provider = 'host' AND p.event_id = NEW.payment_ref AND p.order_id = NEW.order_id
137
+ AND p.method = 'recorded' AND p.reverses IS NULL))
138
+ BEGIN
139
+ SELECT RAISE(ABORT, 'core_order_refund_recorded');
140
+ END;
141
+
142
+ -- Paid back by a person is a fact recorded once: kept as written, never deleted.
143
+ CREATE TRIGGER core_order_refund_recorded_fixed
144
+ BEFORE UPDATE ON core_order_refund
145
+ WHEN OLD.method <> 'provider'
146
+ AND (NEW.id IS NOT OLD.id OR NEW.order_id IS NOT OLD.order_id OR NEW.provider IS NOT OLD.provider
147
+ OR NEW.payment_ref IS NOT OLD.payment_ref OR NEW.amount IS NOT OLD.amount
148
+ OR NEW.currency IS NOT OLD.currency OR NEW.status IS NOT OLD.status
149
+ OR NEW.method IS NOT OLD.method OR NEW.recorded_kind IS NOT OLD.recorded_kind
150
+ OR NEW.requested_by IS NOT OLD.requested_by OR NEW.confirmed_at IS NOT OLD.confirmed_at
151
+ OR (NEW.reason IS NOT OLD.reason AND NEW.reason IS NOT NULL)
152
+ OR (NEW.reference IS NOT OLD.reference AND NEW.reference IS NOT NULL))
153
+ BEGIN
154
+ SELECT RAISE(ABORT, 'core_order_refund_fixed');
155
+ END;
156
+
157
+ CREATE TRIGGER core_order_refund_recorded_kept
158
+ BEFORE DELETE ON core_order_refund
159
+ WHEN OLD.method <> 'provider'
160
+ BEGIN
161
+ SELECT RAISE(ABORT, 'core_order_refund_kept');
162
+ END;
163
+
164
+ -- Value given back onto a voucher or stored value comes with those ledgers (steps 7 and 8).
165
+ CREATE TRIGGER core_order_refund_ledger
166
+ BEFORE INSERT ON core_order_refund
167
+ WHEN NEW.method IN ('voucher', 'stored_value')
168
+ BEGIN
169
+ SELECT RAISE(ABORT, 'core_order_payment_ledger');
170
+ END;
171
+
172
+ -- Payments by method, for reports: money taken (the provider's and recorded), value redeemed
173
+ -- (vouchers and stored value, which is not new money), and reversals (negative, naming the row).
174
+ CREATE VIEW rec_order_payment AS
175
+ SELECT
176
+ p.provider || ':' || p.event_id AS id, p.provider, p.event_id, p.order_id, o.person_id,
177
+ p.method, p.recorded_kind, p.recorded_by, p.received_on, p.reference, p.voucher_id,
178
+ p.stored_value_id, p.reverses, p.reason, p.amount, p.currency, p.outcome, p.resolved,
179
+ p.tax_amount, p.received_at, o.state AS order_state
180
+ FROM core_order_payment p
181
+ JOIN core_order o ON o.id = p.order_id;
182
+
183
+ -- Refunds by method, for reports (review of PR #426): what went back, on which payment, and how.
184
+ CREATE VIEW rec_order_refund AS
185
+ SELECT
186
+ r.id, r.order_id, o.person_id, r.provider, r.payment_ref, r.method, r.recorded_kind,
187
+ r.reference, r.reason, r.amount, r.currency, r.status, r.requested_by, r.requested_at,
188
+ r.confirmed_at, r.tax_amount, r.created_at, o.state AS order_state
189
+ FROM core_order_refund r
190
+ JOIN core_order o ON o.id = r.order_id;
191
+
192
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
193
+ UPDATE core_meta SET value = '952'
194
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 952;
@@ -0,0 +1,183 @@
1
+ -- Migration 0953: the discount behaviour's uses (stage 34 step 4, ADR 0693).
2
+ --
3
+ -- A discount is a record of an object declaring `discount` (a pack's promo code or member's
4
+ -- price). The host works out an order's one deal when it prices the order (orders/orders.ts,
5
+ -- `priceOrder`) and writes it as `code` or `automatic` adjustments (migration 0950). Each order a
6
+ -- deal is on has one use here, on core_sellable_once's shape (ADR 0512):
7
+ --
8
+ -- - held from the order's start, in the batch that writes the order;
9
+ -- - used in the batch that first marks the order paid or part paid;
10
+ -- - released when the unpaid order expires or is cancelled, or when its code is changed or
11
+ -- taken off before it is paid. A refund keeps it used.
12
+ --
13
+ -- The limits are the discount record's (and its code record's), copied onto the use as the host
14
+ -- read them, so this trigger can refuse the use past them in the same batch: of two orders
15
+ -- started at once at the last use, one exists. `max_per_person` counts every Person sharing a
16
+ -- folded address with the use's (ADR 0692): `a+1@x` and `a+2@x` are one person. The expression is
17
+ -- migration 0955's, so its index serves it.
18
+ CREATE TABLE core_discount_use (
19
+ id TEXT PRIMARY KEY,
20
+ -- The discount record (its object's registry name and id), and the code record typed, if any.
21
+ discount_object TEXT NOT NULL,
22
+ discount_id TEXT NOT NULL,
23
+ code_object TEXT,
24
+ code_id TEXT,
25
+ kind TEXT NOT NULL CHECK (kind IN ('code', 'automatic')),
26
+ -- The order's Person; null for a guest (who never gets a per-person discount) and once the
27
+ -- Person is erased (their order loses its Person too).
28
+ person_id TEXT,
29
+ order_id TEXT NOT NULL REFERENCES core_order(id),
30
+ state TEXT NOT NULL CHECK (state IN ('held', 'used', 'released')),
31
+ -- What the deal took off the order, in minor units.
32
+ amount INTEGER NOT NULL CHECK (amount > 0),
33
+ -- The limits as the host read them at the hold; null: none.
34
+ max_uses INTEGER CHECK (max_uses IS NULL OR max_uses >= 0),
35
+ max_per_person INTEGER CHECK (max_per_person IS NULL OR max_per_person >= 0),
36
+ code_max_uses INTEGER CHECK (code_max_uses IS NULL OR code_max_uses >= 0),
37
+ created_at TEXT NOT NULL,
38
+ updated_at TEXT NOT NULL,
39
+ CHECK ((code_object IS NULL) = (code_id IS NULL)),
40
+ CHECK (code_max_uses IS NULL OR code_id IS NOT NULL)
41
+ );
42
+ CREATE INDEX core_discount_use_discount_idx
43
+ ON core_discount_use (discount_object, discount_id, state);
44
+ CREATE INDEX core_discount_use_code_idx
45
+ ON core_discount_use (code_object, code_id, state) WHERE code_id IS NOT NULL;
46
+ CREATE INDEX core_discount_use_order_idx ON core_discount_use (order_id);
47
+ CREATE INDEX core_discount_use_person_idx
48
+ ON core_discount_use (person_id, discount_object, discount_id) WHERE person_id IS NOT NULL;
49
+
50
+ -- The limits, in the batch that holds the use: a code's (its discount's, its own record's, a
51
+ -- person's).
52
+ CREATE TRIGGER core_discount_use_limit
53
+ BEFORE INSERT ON core_discount_use
54
+ WHEN NEW.state IN ('held', 'used') AND NEW.kind = 'code' AND (
55
+ (NEW.max_uses IS NOT NULL AND (
56
+ SELECT count(*) FROM core_discount_use u
57
+ WHERE u.discount_object = NEW.discount_object AND u.discount_id = NEW.discount_id
58
+ AND u.state IN ('held', 'used')
59
+ ) >= NEW.max_uses)
60
+ OR (NEW.code_max_uses IS NOT NULL AND (
61
+ SELECT count(*) FROM core_discount_use u
62
+ WHERE u.code_object = NEW.code_object AND u.code_id = NEW.code_id
63
+ AND u.state IN ('held', 'used')
64
+ ) >= NEW.code_max_uses)
65
+ OR (NEW.max_per_person IS NOT NULL AND (
66
+ SELECT count(*) FROM core_discount_use u
67
+ WHERE u.discount_object = NEW.discount_object AND u.discount_id = NEW.discount_id
68
+ AND u.state IN ('held', 'used')
69
+ AND u.person_id IN (
70
+ SELECT NEW.person_id
71
+ UNION
72
+ SELECT theirs.person_id FROM core_person_emails mine
73
+ JOIN core_person_emails theirs ON (
74
+ CASE
75
+ WHEN instr(theirs.email, '+') > 1 AND instr(theirs.email, '+') < instr(theirs.email, '@')
76
+ THEN substr(theirs.email, 1, instr(theirs.email, '+') - 1)
77
+ || substr(theirs.email, instr(theirs.email, '@'))
78
+ ELSE theirs.email
79
+ END
80
+ ) = (
81
+ CASE
82
+ WHEN instr(mine.email, '+') > 1 AND instr(mine.email, '+') < instr(mine.email, '@')
83
+ THEN substr(mine.email, 1, instr(mine.email, '+') - 1)
84
+ || substr(mine.email, instr(mine.email, '@'))
85
+ ELSE mine.email
86
+ END
87
+ )
88
+ WHERE mine.person_id = NEW.person_id
89
+ )
90
+ ) >= NEW.max_per_person)
91
+ )
92
+ BEGIN
93
+ SELECT RAISE(ABORT, 'core_discount_use_limit');
94
+ END;
95
+
96
+ -- The same for an automatic discount (a member's price), with its own message: the buyer typed
97
+ -- no code, so the host prices the order again without it (pipeline.ts) rather than tell them a
98
+ -- code is not valid (the review of PR #454).
99
+ CREATE TRIGGER core_discount_use_auto_limit
100
+ BEFORE INSERT ON core_discount_use
101
+ WHEN NEW.state IN ('held', 'used') AND NEW.kind = 'automatic' AND (
102
+ (NEW.max_uses IS NOT NULL AND (
103
+ SELECT count(*) FROM core_discount_use u
104
+ WHERE u.discount_object = NEW.discount_object AND u.discount_id = NEW.discount_id
105
+ AND u.state IN ('held', 'used')
106
+ ) >= NEW.max_uses)
107
+ OR (NEW.code_max_uses IS NOT NULL AND (
108
+ SELECT count(*) FROM core_discount_use u
109
+ WHERE u.code_object = NEW.code_object AND u.code_id = NEW.code_id
110
+ AND u.state IN ('held', 'used')
111
+ ) >= NEW.code_max_uses)
112
+ OR (NEW.max_per_person IS NOT NULL AND (
113
+ SELECT count(*) FROM core_discount_use u
114
+ WHERE u.discount_object = NEW.discount_object AND u.discount_id = NEW.discount_id
115
+ AND u.state IN ('held', 'used')
116
+ AND u.person_id IN (
117
+ SELECT NEW.person_id
118
+ UNION
119
+ SELECT theirs.person_id FROM core_person_emails mine
120
+ JOIN core_person_emails theirs ON (
121
+ CASE
122
+ WHEN instr(theirs.email, '+') > 1 AND instr(theirs.email, '+') < instr(theirs.email, '@')
123
+ THEN substr(theirs.email, 1, instr(theirs.email, '+') - 1)
124
+ || substr(theirs.email, instr(theirs.email, '@'))
125
+ ELSE theirs.email
126
+ END
127
+ ) = (
128
+ CASE
129
+ WHEN instr(mine.email, '+') > 1 AND instr(mine.email, '+') < instr(mine.email, '@')
130
+ THEN substr(mine.email, 1, instr(mine.email, '+') - 1)
131
+ || substr(mine.email, instr(mine.email, '@'))
132
+ ELSE mine.email
133
+ END
134
+ )
135
+ WHERE mine.person_id = NEW.person_id
136
+ )
137
+ ) >= NEW.max_per_person)
138
+ )
139
+ BEGIN
140
+ SELECT RAISE(ABORT, 'core_discount_use_auto_limit');
141
+ END;
142
+
143
+ -- A use moves once: held to used, or held to released. Nothing else about it changes, but its
144
+ -- Person (a merge moves it to the survivor; an erase blanks it). A use given back is a new row if it is taken again
145
+ -- (a late payment honoured), judged by the limits again.
146
+ CREATE TRIGGER core_discount_use_fixed
147
+ BEFORE UPDATE ON core_discount_use
148
+ WHEN NEW.id IS NOT OLD.id OR NEW.discount_object IS NOT OLD.discount_object
149
+ OR NEW.discount_id IS NOT OLD.discount_id OR NEW.code_object IS NOT OLD.code_object
150
+ OR NEW.code_id IS NOT OLD.code_id OR NEW.kind IS NOT OLD.kind
151
+ OR NEW.order_id IS NOT OLD.order_id OR NEW.amount IS NOT OLD.amount
152
+ OR NEW.max_uses IS NOT OLD.max_uses OR NEW.max_per_person IS NOT OLD.max_per_person
153
+ OR NEW.code_max_uses IS NOT OLD.code_max_uses OR NEW.created_at IS NOT OLD.created_at
154
+ OR (NEW.state IS NOT OLD.state AND NOT (OLD.state = 'held' AND NEW.state IN ('used', 'released')))
155
+ BEGIN
156
+ SELECT RAISE(ABORT, 'core_discount_use_fixed');
157
+ END;
158
+
159
+ CREATE TRIGGER core_discount_use_kept
160
+ BEFORE DELETE ON core_discount_use
161
+ BEGIN
162
+ SELECT RAISE(ABORT, 'core_discount_use_kept');
163
+ END;
164
+
165
+ -- A delivery's own price and its free-over amount as the host read them at the order's start
166
+ -- (ADR 0685), so a code put on the pending order later (order.applyCode) decides free postage
167
+ -- again on the goods' net after the deal (the spec's rule 8) from what the order was priced at.
168
+ ALTER TABLE core_order_delivery ADD COLUMN price INTEGER CHECK (price IS NULL OR price >= 0);
169
+ ALTER TABLE core_order_delivery ADD COLUMN free_over INTEGER
170
+ CHECK (free_over IS NULL OR free_over >= 0);
171
+
172
+ -- Uses for reports: what each discount was used on, its order's state and currency.
173
+ CREATE VIEW rec_discount_use AS
174
+ SELECT
175
+ u.id, u.discount_object, u.discount_id, u.code_object, u.code_id, u.kind, u.person_id,
176
+ u.order_id, u.state, u.amount, u.created_at, u.updated_at,
177
+ o.state AS order_state, o.currency, o.total AS order_total
178
+ FROM core_discount_use u
179
+ JOIN core_order o ON o.id = u.order_id;
180
+
181
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
182
+ UPDATE core_meta SET value = '953'
183
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 953;
@@ -0,0 +1,92 @@
1
+ -- Migration 0955: one person per folded address (stage 34, build step 2, ADR 0692, issue #225).
2
+ --
3
+ -- An address's folded form is the address with its `+tag` dropped (`a+1@x` and `a+2@x` are both
4
+ -- `a@x`), on every domain; dots are not folded (`a.b@x` and `ab@x` stay two). It is used only to
5
+ -- decide "the same person" for the one-per-Person rules: a free trial (ADR 0489), something sold
6
+ -- once per Person (an intro offer, ADR 0512), and later a per-person code, a new-customer check
7
+ -- and a referral. Never an identity: the Person, the account and the address mailed stay as typed.
8
+ --
9
+ -- The folded form is an expression over `core_person_emails.email` (stored lower-cased and
10
+ -- trimmed), indexed, and never stored: nothing is written when an address is, so adding one costs
11
+ -- what it did. `foldedSql` in packages/core/src/customers/accounts.ts writes the same expression
12
+ -- (it must match this one for the index to serve it) and `foldAddress` the same rule in code; a
13
+ -- test holds the three together.
14
+ --
15
+ -- Every address row counts, a Person in the Trash's too (their trial and intro offer stay theirs
16
+ -- and still count while they could be restored). A merged Person's addresses are deleted, and
17
+ -- their rows moved to the survivor; an erased Person's go with them.
18
+ CREATE INDEX core_person_emails_folded_idx ON core_person_emails (
19
+ CASE
20
+ WHEN instr(email, '+') > 1 AND instr(email, '+') < instr(email, '@')
21
+ THEN substr(email, 1, instr(email, '+') - 1) || substr(email, instr(email, '@'))
22
+ ELSE email
23
+ END
24
+ );
25
+
26
+ -- The database's backstop for two starts at once by two People who are one person: as migration
27
+ -- 0581's unique index (one Person) and 0531's trigger (one Person) refuse the second hold, these
28
+ -- refuse a hold while another Person sharing a folded address has one held or used on the same
29
+ -- object. A pass given by hand by a person on the staff (`source = 'by_hand'`, ADR 0513) is not
30
+ -- refused here: it is the staff's deliberate override (a family sharing one mailbox), and it
31
+ -- still counts against the others.
32
+ CREATE TRIGGER core_sellable_once_folded
33
+ BEFORE INSERT ON core_sellable_once
34
+ WHEN NEW.state IN ('held', 'used') AND NEW.source = 'order' AND EXISTS (
35
+ SELECT 1 FROM core_person_emails mine
36
+ JOIN core_person_emails theirs ON (
37
+ CASE
38
+ WHEN instr(theirs.email, '+') > 1 AND instr(theirs.email, '+') < instr(theirs.email, '@')
39
+ THEN substr(theirs.email, 1, instr(theirs.email, '+') - 1)
40
+ || substr(theirs.email, instr(theirs.email, '@'))
41
+ ELSE theirs.email
42
+ END
43
+ ) = (
44
+ CASE
45
+ WHEN instr(mine.email, '+') > 1 AND instr(mine.email, '+') < instr(mine.email, '@')
46
+ THEN substr(mine.email, 1, instr(mine.email, '+') - 1)
47
+ || substr(mine.email, instr(mine.email, '@'))
48
+ ELSE mine.email
49
+ END
50
+ )
51
+ JOIN core_sellable_once o ON o.person_id = theirs.person_id
52
+ WHERE mine.person_id = NEW.person_id
53
+ AND theirs.person_id <> NEW.person_id
54
+ AND o.object_type = NEW.object_type
55
+ AND o.state IN ('held', 'used')
56
+ )
57
+ BEGIN
58
+ SELECT RAISE(ABORT, 'core_sellable_once_folded');
59
+ END;
60
+
61
+ CREATE TRIGGER core_subscription_trial_folded
62
+ BEFORE INSERT ON core_subscription_trial
63
+ WHEN EXISTS (
64
+ SELECT 1 FROM core_person_emails mine
65
+ JOIN core_person_emails theirs ON (
66
+ CASE
67
+ WHEN instr(theirs.email, '+') > 1 AND instr(theirs.email, '+') < instr(theirs.email, '@')
68
+ THEN substr(theirs.email, 1, instr(theirs.email, '+') - 1)
69
+ || substr(theirs.email, instr(theirs.email, '@'))
70
+ ELSE theirs.email
71
+ END
72
+ ) = (
73
+ CASE
74
+ WHEN instr(mine.email, '+') > 1 AND instr(mine.email, '+') < instr(mine.email, '@')
75
+ THEN substr(mine.email, 1, instr(mine.email, '+') - 1)
76
+ || substr(mine.email, instr(mine.email, '@'))
77
+ ELSE mine.email
78
+ END
79
+ )
80
+ JOIN core_subscription_trial t ON t.holder_person_id = theirs.person_id
81
+ WHERE mine.person_id = NEW.holder_person_id
82
+ AND theirs.person_id <> NEW.holder_person_id
83
+ AND t.plan_object = NEW.plan_object
84
+ AND (t.used_at IS NOT NULL OR t.released_at IS NULL)
85
+ )
86
+ BEGIN
87
+ SELECT RAISE(ABORT, 'core_subscription_trial_taken');
88
+ END;
89
+
90
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
91
+ UPDATE core_meta SET value = '955'
92
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 955;
@@ -0,0 +1,59 @@
1
+ -- Stage 38 step 1 (ADR 0700): the published-text index. What `content.search` finds is the words
2
+ -- of each record's current published version, never its working copy.
3
+ --
4
+ -- One row per section of a published record: the words before the first heading of a text
5
+ -- field (section 0, where the record's title is indexed too), then one row per heading, with the
6
+ -- heading's anchor (made by the SDK's `headingAnchors` from its text and place, never stored
7
+ -- elsewhere). Written only by packages/core/src/content/published-text.ts, in the batch of every
8
+ -- publish (a person's, an approved request's, the minute cron's) and unpublish, so the index is
9
+ -- the current version or nothing. An object's declaration changing (a field the public reads,
10
+ -- the object becoming public) is caught up by the minute cron (`content.reindex`), which also
11
+ -- fills the index for what was published before this migration.
12
+ --
13
+ -- Not a `rec_*` view: dashboards and query.sql never read it. Every read goes through the
14
+ -- visibility seam (the search joins the reader's source), so a deleted record, a record the
15
+ -- public may not read and a record whose `public.when` fails are not found, whatever is here.
16
+ CREATE TABLE core_published_text (
17
+ id INTEGER PRIMARY KEY,
18
+ object_type TEXT NOT NULL,
19
+ record_id TEXT NOT NULL,
20
+ -- The version the words are from.
21
+ version INTEGER NOT NULL,
22
+ -- The content field the section is in (the record's title: its title field).
23
+ field TEXT NOT NULL,
24
+ -- The section's place in the field: 0 before the first heading, then 1, 2, ... per heading.
25
+ section INTEGER NOT NULL CHECK (section >= 0),
26
+ -- The heading's anchor; NULL for section 0.
27
+ anchor TEXT,
28
+ -- The heading's words (section 0: the record's title).
29
+ heading TEXT NOT NULL DEFAULT '',
30
+ body TEXT NOT NULL DEFAULT '',
31
+ UNIQUE (object_type, record_id, field, section)
32
+ );
33
+
34
+ CREATE VIRTUAL TABLE core_published_text_fts USING fts5(
35
+ heading,
36
+ body,
37
+ content = 'core_published_text',
38
+ content_rowid = 'id',
39
+ tokenize = 'unicode61 remove_diacritics 2',
40
+ prefix = '2 3'
41
+ );
42
+
43
+ CREATE TRIGGER core_published_text_ai AFTER INSERT ON core_published_text
44
+ BEGIN
45
+ INSERT INTO core_published_text_fts (rowid, heading, body) VALUES (NEW.id, NEW.heading, NEW.body);
46
+ END;
47
+
48
+ CREATE TRIGGER core_published_text_ad AFTER DELETE ON core_published_text
49
+ BEGIN
50
+ INSERT INTO core_published_text_fts (core_published_text_fts, rowid, heading, body)
51
+ VALUES ('delete', OLD.id, OLD.heading, OLD.body);
52
+ END;
53
+
54
+ CREATE TRIGGER core_published_text_au AFTER UPDATE ON core_published_text
55
+ BEGIN
56
+ INSERT INTO core_published_text_fts (core_published_text_fts, rowid, heading, body)
57
+ VALUES ('delete', OLD.id, OLD.heading, OLD.body);
58
+ INSERT INTO core_published_text_fts (rowid, heading, body) VALUES (NEW.id, NEW.heading, NEW.body);
59
+ END;
@@ -0,0 +1,68 @@
1
+ -- Stage 38 step 3 (ADR 0702): read and understood. A publishable object that declares
2
+ -- `acknowledge` asks the staff who must read a record to confirm its current version.
3
+ --
4
+ -- Each version of such a record says whether it asks everyone again (`read_again` 1) or is a
5
+ -- small fix (0); the first version always asks. A scheduled publish carries the choice to the
6
+ -- version the minute cron makes. Other objects' versions leave it NULL. Only
7
+ -- packages/core/src/records/publish.ts writes either table.
8
+ ALTER TABLE core_publish_version ADD COLUMN read_again INTEGER
9
+ CHECK (read_again IS NULL OR read_again IN (0, 1));
10
+ ALTER TABLE core_publish_schedule ADD COLUMN read_again INTEGER
11
+ CHECK (read_again IS NULL OR read_again IN (0, 1));
12
+
13
+ -- A confirmation: one staff account saying it has read and understood one published version.
14
+ -- Only the account itself makes one, by a person's tap (`ack.confirm`, `actors: ["human"]`);
15
+ -- `how` has no other value. Not in any `rec_*` view: dashboards, query.sql, exports and the
16
+ -- assistant's record reads never reach it.
17
+ CREATE TABLE core_publish_ack (
18
+ object_type TEXT NOT NULL,
19
+ record_id TEXT NOT NULL,
20
+ version INTEGER NOT NULL,
21
+ -- The staff account (core_user) that confirmed.
22
+ account_id TEXT NOT NULL,
23
+ confirmed_at TEXT NOT NULL,
24
+ how TEXT NOT NULL DEFAULT 'self' CHECK (how = 'self'),
25
+ PRIMARY KEY (object_type, record_id, version, account_id)
26
+ ) WITHOUT ROWID;
27
+ CREATE INDEX core_publish_ack_account_idx ON core_publish_ack (account_id);
28
+
29
+ -- Only the record's current version is confirmed, and only by a live person's account, once. A
30
+ -- second confirmation of one version is refused here, before any conflict clause is applied, so
31
+ -- `INSERT OR REPLACE` (which would delete the old row without its delete trigger) cannot rewrite
32
+ -- one either.
33
+ CREATE TRIGGER core_publish_ack_new
34
+ BEFORE INSERT ON core_publish_ack
35
+ BEGIN
36
+ SELECT CASE
37
+ WHEN EXISTS (SELECT 1 FROM core_publish_ack a
38
+ WHERE a.object_type = NEW.object_type AND a.record_id = NEW.record_id
39
+ AND a.version = NEW.version AND a.account_id = NEW.account_id)
40
+ THEN RAISE(ABORT, 'core_publish_ack_immutable')
41
+ WHEN NOT EXISTS (SELECT 1 FROM core_publish_version v
42
+ WHERE v.object_type = NEW.object_type AND v.record_id = NEW.record_id
43
+ AND v.version = NEW.version AND v.current = 1)
44
+ THEN RAISE(ABORT, 'core_publish_ack_not_current')
45
+ WHEN NOT EXISTS (SELECT 1 FROM core_user u
46
+ WHERE u.id = NEW.account_id AND u.kind = 'human' AND u.deactivated_at IS NULL)
47
+ THEN RAISE(ABORT, 'core_publish_ack_not_a_person')
48
+ END;
49
+ END;
50
+
51
+ -- Nothing a confirmation says ever changes, by anyone, host code included.
52
+ CREATE TRIGGER core_publish_ack_no_update
53
+ BEFORE UPDATE ON core_publish_ack
54
+ BEGIN
55
+ SELECT RAISE(ABORT, 'core_publish_ack_immutable');
56
+ END;
57
+
58
+ -- Kept for good, through deactivation and unpublishing. It goes only with what it is about: once
59
+ -- the account is gone (erased) or the version it confirms is gone (the record purged).
60
+ CREATE TRIGGER core_publish_ack_no_delete
61
+ BEFORE DELETE ON core_publish_ack
62
+ WHEN EXISTS (SELECT 1 FROM core_user u WHERE u.id = OLD.account_id)
63
+ AND EXISTS (SELECT 1 FROM core_publish_version v
64
+ WHERE v.object_type = OLD.object_type AND v.record_id = OLD.record_id
65
+ AND v.version = OLD.version)
66
+ BEGIN
67
+ SELECT RAISE(ABORT, 'core_publish_ack_immutable');
68
+ END;
@@ -0,0 +1,65 @@
1
+ -- Migration 0990: changing a customer account's sign-in address (stage 48, ADR 0712; block BF in
2
+ -- docs/parallel-work.md).
3
+ --
4
+ -- The member (or a staff admin for them) asks with the new address; the host emails a code to it;
5
+ -- the member enters the code, signed in to the same account, and only then does the account's
6
+ -- sign-in address change (with the Person's primary email, in one batch). One row per request.
7
+ --
8
+ -- - One open change per account: a newer request ends the open one as `replaced`, in the same
9
+ -- batch (the partial unique index refuses a second open row).
10
+ -- - The code is stored only as a hash. It lasts 15 minutes and allows 5 tries; the fifth wrong
11
+ -- one ends the change as `tries`.
12
+ -- - `old_email` is the address the change replaces. Erase reads a confirmed row's old address as
13
+ -- one of the Person's former sign-in addresses (issue #302) before the account and its rows go.
14
+ -- - `new_mailbox` is the new address with a `+tag` (and Gmail's dots) folded away (`mailboxKey`):
15
+ -- requests are counted per mailbox, as sign-in codes are (ADR 0459). Never an identity.
16
+ -- - A request is reserved before its code is sent, as a row ended `unsent`, by one conditional
17
+ -- insert that counts the account's and the mailbox's earlier rows: parallel requests cannot pass
18
+ -- the limits. Once the code went out, the request's batch reopens it; a code that did not go
19
+ -- out leaves it `unsent`, still counted.
20
+ -- - A change that is confirmed or ended never changes again (the trigger below): a batch planned
21
+ -- from an open change (a confirm, a cancel, a newer request ending it) fails whole if another
22
+ -- one closed it first, so a cancel never reports success for a change that went through, and
23
+ -- two confirms at once never both apply.
24
+ CREATE TABLE core_customer_email_change (
25
+ -- ech_… (ids.ts).
26
+ id TEXT PRIMARY KEY NOT NULL,
27
+ -- Erasing the account deletes its changes (erase reads them first).
28
+ customer_id TEXT NOT NULL REFERENCES core_customer (id) ON DELETE CASCADE,
29
+ new_email TEXT NOT NULL CHECK (new_email = lower(trim(new_email)) AND new_email <> ''),
30
+ new_mailbox TEXT NOT NULL,
31
+ old_email TEXT NOT NULL CHECK (old_email = lower(trim(old_email)) AND old_email <> ''),
32
+ -- 'customer' (the member asked) or the staff user id (usr_…) of the admin who started it.
33
+ started_by TEXT NOT NULL,
34
+ code_hash TEXT NOT NULL,
35
+ expires_at TEXT NOT NULL,
36
+ tries_left INTEGER NOT NULL DEFAULT 5 CHECK (tries_left BETWEEN 0 AND 5),
37
+ created_at TEXT NOT NULL,
38
+ confirmed_at TEXT,
39
+ -- When it ended without taking effect, and why: a newer request, staff cancelled it, five wrong
40
+ -- codes, the address proved to be in use at the business (or the account changed) when the code
41
+ -- was entered, or (`unsent`) its code has not gone out, or never did.
42
+ ended_at TEXT,
43
+ end_reason TEXT
44
+ CHECK (end_reason IN ('replaced', 'cancelled', 'tries', 'in_use', 'unsent')),
45
+ CHECK ((ended_at IS NULL) = (end_reason IS NULL)),
46
+ CHECK (confirmed_at IS NULL OR ended_at IS NULL),
47
+ CHECK (new_email <> old_email)
48
+ );
49
+ CREATE UNIQUE INDEX core_customer_email_change_open_uq ON core_customer_email_change (customer_id)
50
+ WHERE confirmed_at IS NULL AND ended_at IS NULL;
51
+ CREATE INDEX core_customer_email_change_customer_idx
52
+ ON core_customer_email_change (customer_id, created_at);
53
+ CREATE INDEX core_customer_email_change_mailbox_idx
54
+ ON core_customer_email_change (new_mailbox, created_at);
55
+
56
+ -- Closed is final: only an `unsent` row may be reopened (its code went out), and nothing else
57
+ -- about a confirmed or ended row may change.
58
+ CREATE TRIGGER core_customer_email_change_closed
59
+ BEFORE UPDATE ON core_customer_email_change
60
+ WHEN OLD.confirmed_at IS NOT NULL
61
+ OR (OLD.ended_at IS NOT NULL AND NOT (OLD.end_reason = 'unsent' AND NEW.ended_at IS NULL
62
+ AND NEW.end_reason IS NULL AND NEW.confirmed_at IS NULL))
63
+ BEGIN
64
+ SELECT RAISE(ABORT, 'core_email_change_closed');
65
+ END;