@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,60 @@
1
+ -- Migration 0911: texts (stage 30 step 3, ADRs 0672 and 0673). What the host keeps to send a
2
+ -- routine message by text: the month's spend against the text limit, the numbers checked by a
3
+ -- code, and the numbers that replied STOP. Never the words, and never a number in the log (a
4
+ -- salted hash for the caps and the stops).
5
+
6
+ -- The salt the host hashes numbers with (a text row's `recipient_key`, a stop): made once, here,
7
+ -- for this deployment. Not a secret against someone who reads this database (numbers are few
8
+ -- enough to try them all); it keeps one deployment's hashes from matching another's.
9
+ INSERT INTO core_settings (key, value, updated_at)
10
+ VALUES ('text_number_salt', json_quote(lower(hex(randomblob(16)))), strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
11
+ ON CONFLICT (key) DO NOTHING;
12
+
13
+ -- The month's spend on texts (ADR 0672): one row per month (`YYYY-MM` in the business's zone),
14
+ -- counted in the statement that claims a text (`estimated_minor`, segments times the price the
15
+ -- admin entered) and corrected by what the provider reports (`reported_minor`). A text claimed
16
+ -- that would take `estimated_minor` past the limit is `over_limit` instead. A checking code and
17
+ -- HELP's answer count too.
18
+ CREATE TABLE core_text_spend (
19
+ month TEXT PRIMARY KEY NOT NULL,
20
+ segments INTEGER NOT NULL DEFAULT 0,
21
+ estimated_minor INTEGER NOT NULL DEFAULT 0,
22
+ reported_minor INTEGER NOT NULL DEFAULT 0,
23
+ updated_at TEXT NOT NULL
24
+ );
25
+
26
+ -- A code sent to a Person's number, and whether it was typed back (ADR 0673). `number` is the
27
+ -- international form; the code itself is the provider's and never reaches the host. A number
28
+ -- checked by one Person is never texted for another (`number_hash`, issue #302's rule for
29
+ -- numbers). The member asks for at most 3 codes a day, and a number gets at most 3 a day
30
+ -- whoever asks (counted from `started_at`). Deleted with the Person.
31
+ CREATE TABLE core_phone_check (
32
+ id TEXT PRIMARY KEY NOT NULL,
33
+ person_id TEXT NOT NULL REFERENCES core_person (id) ON DELETE CASCADE,
34
+ number TEXT NOT NULL,
35
+ number_hash TEXT NOT NULL,
36
+ how TEXT NOT NULL DEFAULT 'code' CHECK (how IN ('code')),
37
+ started_at TEXT NOT NULL,
38
+ checked_at TEXT,
39
+ -- Tries at typing this code back: three, whatever the provider allows.
40
+ tries INTEGER NOT NULL DEFAULT 0
41
+ );
42
+ CREATE INDEX core_phone_check_person_idx ON core_phone_check (person_id, started_at);
43
+ CREATE INDEX core_phone_check_number_idx ON core_phone_check (number_hash, started_at);
44
+
45
+ -- Numbers that replied STOP (ADR 0672): a salted hash of each, never the number, kept after the
46
+ -- Person is erased so a number entered again (on anyone) is not texted. START from the number
47
+ -- deletes it. Stage 30's consent register (step 5) reads it as its suppression list for texts.
48
+ CREATE TABLE core_text_stop (
49
+ number_hash TEXT PRIMARY KEY NOT NULL,
50
+ how TEXT NOT NULL CHECK (how IN ('stop_reply')),
51
+ at TEXT NOT NULL
52
+ );
53
+
54
+ -- The texts ready to go (just prepared, released at the end of quiet hours, or back after a full
55
+ -- pending pool), and the held ones not yet put to a person: the minute cron's send step.
56
+ CREATE INDEX core_routine_mail_texts_idx ON core_routine_mail (status, updated_at)
57
+ WHERE channel = 'text' AND status IN ('ready', 'held');
58
+ -- Texts left `sending` by a host that stopped before it settled them: tried again after a while.
59
+ CREATE INDEX core_routine_mail_texts_sending_idx ON core_routine_mail (updated_at)
60
+ WHERE channel = 'text' AND status = 'sending';
@@ -0,0 +1,93 @@
1
+ -- Migration 0913: consent to be contacted (stage 30 step 5, ADR 0675). The consent register, the
2
+ -- suppression list, and the stop links' tokens (only their hashes). Journeys, the shared inbox
3
+ -- and Reviews read and write these only through packages/core/src/contact/ (`contactDecision`,
4
+ -- `stopLink`), never on their own.
5
+
6
+ -- The register: a Person's contact choices, one row per choice made, never changed afterwards.
7
+ -- The latest row (by `at`, then `seq`) for (Person, channel, scope) is the one in force; the
8
+ -- earlier ones are its history. `scope` is a purpose (`follow_up`, `news_and_offers`) or a
9
+ -- routine message kind (`bookings.reminder`); `value` is `on` or `off`, and for `news_and_offers`
10
+ -- `yes` or `no`. `how` says where the choice came from, `by_id` who made it (a staff user or a
11
+ -- customer account; null for a stop link or a text reply), `reason` why staff recorded it, and
12
+ -- `wording` the words the person was shown when they said yes. Deleted with the Person (erase,
13
+ -- by the cascade); a merge moves the rows to the survivor (person_id is the only column that may
14
+ -- change, and only to the Person it was merged into).
15
+ CREATE TABLE core_contact_choice (
16
+ seq INTEGER PRIMARY KEY AUTOINCREMENT,
17
+ id TEXT NOT NULL UNIQUE,
18
+ person_id TEXT NOT NULL REFERENCES core_person (id) ON DELETE CASCADE,
19
+ channel TEXT NOT NULL CHECK (channel IN ('email', 'text', 'push')),
20
+ scope TEXT NOT NULL CHECK (length(scope) BETWEEN 1 AND 80),
21
+ value TEXT NOT NULL CHECK (value IN ('on', 'off', 'yes', 'no')),
22
+ at TEXT NOT NULL,
23
+ how TEXT NOT NULL CHECK (how IN ('member', 'staff_on_request', 'staff_from_paper', 'form',
24
+ 'checkout', 'stop_link', 'stop_reply', 'start_reply', 'import', 'merge')),
25
+ by_id TEXT,
26
+ reason TEXT CHECK (reason IS NULL OR length(reason) <= 500),
27
+ wording TEXT CHECK (wording IS NULL OR length(wording) <= 1000),
28
+ -- A yes to news and offers carries the words the person was shown; nothing else says yes.
29
+ CHECK ((scope = 'news_and_offers') = (value IN ('yes', 'no'))),
30
+ CHECK (value != 'yes' OR wording IS NOT NULL)
31
+ );
32
+ CREATE INDEX core_contact_choice_person_idx ON core_contact_choice (person_id, channel, scope, at);
33
+
34
+ -- Append-only: a choice is never rewritten, only followed by a later one. A merge moves rows to
35
+ -- the survivor: person_id may change only to the Person the old one was merged into (as ADR
36
+ -- 0642's acceptances). A row goes only with its Person (the erase's cascade, which deletes the
37
+ -- Person first), never on its own.
38
+ CREATE TRIGGER core_contact_choice_no_update
39
+ BEFORE UPDATE ON core_contact_choice
40
+ BEGIN
41
+ SELECT CASE
42
+ WHEN NEW.seq IS NOT OLD.seq OR NEW.id IS NOT OLD.id OR NEW.channel IS NOT OLD.channel
43
+ OR NEW.scope IS NOT OLD.scope OR NEW.value IS NOT OLD.value OR NEW.at IS NOT OLD.at
44
+ OR NEW.how IS NOT OLD.how OR NEW.by_id IS NOT OLD.by_id OR NEW.reason IS NOT OLD.reason
45
+ OR NEW.wording IS NOT OLD.wording
46
+ THEN RAISE(ABORT, 'core_contact_choice_append_only')
47
+ WHEN NEW.person_id IS NOT OLD.person_id AND NEW.person_id IS NOT (
48
+ SELECT p.merged_into_id FROM core_person p WHERE p.id = OLD.person_id)
49
+ THEN RAISE(ABORT, 'core_contact_choice_append_only')
50
+ END;
51
+ END;
52
+
53
+ CREATE TRIGGER core_contact_choice_no_delete
54
+ BEFORE DELETE ON core_contact_choice
55
+ WHEN EXISTS (SELECT 1 FROM core_person p WHERE p.id = OLD.person_id)
56
+ BEGIN
57
+ SELECT RAISE(ABORT, 'core_contact_choice_append_only');
58
+ END;
59
+
60
+ -- The one-tap stop's tokens: 32 random bytes each, of which only the SHA-256 is kept, naming one
61
+ -- Person, one channel and one scope. Nothing here is secret: a token is the link in one email,
62
+ -- and knowing a hash makes no link. A stop link minted for a stoppable message stays good (no
63
+ -- expiry) until its Person is erased; a merge moves its rows to the survivor.
64
+ CREATE TABLE core_contact_stop_token (
65
+ token_hash TEXT PRIMARY KEY NOT NULL CHECK (length(token_hash) = 64),
66
+ person_id TEXT NOT NULL REFERENCES core_person (id) ON DELETE CASCADE,
67
+ channel TEXT NOT NULL CHECK (channel IN ('email', 'text', 'push')),
68
+ scope TEXT NOT NULL CHECK (length(scope) BETWEEN 1 AND 80),
69
+ created_at TEXT NOT NULL
70
+ );
71
+ CREATE INDEX core_contact_stop_token_person_idx ON core_contact_stop_token (person_id);
72
+
73
+ -- The suppression list: a salted hash of each address and number that asked to stop (a STOP
74
+ -- reply; a provider's "unsubscribed"), tied to no Person and kept after erase, so an address or
75
+ -- a number entered again, on anyone, is not sent to. START from the number deletes its row. The
76
+ -- salt is the host's (`text_number_salt`, migration 0911).
77
+ CREATE TABLE core_contact_suppression (
78
+ channel TEXT NOT NULL CHECK (channel IN ('email', 'text')),
79
+ value_hash TEXT NOT NULL,
80
+ how TEXT NOT NULL CHECK (how IN ('stop_reply', 'provider_unsubscribed')),
81
+ at TEXT NOT NULL,
82
+ PRIMARY KEY (channel, value_hash)
83
+ );
84
+
85
+ -- The numbers that replied STOP so far (migration 0911's core_text_stop) are the list's texts.
86
+ INSERT INTO core_contact_suppression (channel, value_hash, how, at)
87
+ SELECT 'text', number_hash, how, at FROM core_text_stop;
88
+ DROP TABLE core_text_stop;
89
+
90
+ -- A held routine message is asked again when a person approves it (a stop taken while it waited
91
+ -- skips it): found by its pending action, without reading the log.
92
+ CREATE INDEX core_routine_mail_pending_idx ON core_routine_mail (pending_action_id)
93
+ WHERE pending_action_id IS NOT NULL;
@@ -0,0 +1,29 @@
1
+ -- Migration 0916: who an address belongs to, for consent to be contacted (stage 30, ADR 0678).
2
+ --
3
+ -- The host matches the address a message is sent to with the People who hold it by the mailbox it
4
+ -- reaches, the same rule the suppression list hashes addresses by (`mailboxKey` in
5
+ -- packages/core/src/customers/limits.ts, ADR 0459): the `+tag` dropped on every domain, and on
6
+ -- gmail.com and googlemail.com the dots dropped and the domain gmail.com. So a member who stopped
7
+ -- at `john@gmail.com` is not mailed at `j.ohn+gym@googlemail.com`. Migration 0955's folded form
8
+ -- keeps dots, so this is an index of its own over an expression, nothing stored.
9
+ -- `peopleAtAddresses` in packages/core/src/visibility/guardians.ts writes the same expression (it
10
+ -- must match this one for the index to serve it); a test holds it and `mailboxKey` together.
11
+ CREATE INDEX core_person_emails_mailbox_idx ON core_person_emails (
12
+ CASE
13
+ WHEN substr(email, instr(email, '@') + 1) IN ('gmail.com', 'googlemail.com')
14
+ THEN replace(
15
+ CASE
16
+ WHEN instr(email, '+') > 1 AND instr(email, '+') < instr(email, '@')
17
+ THEN substr(email, 1, instr(email, '+') - 1)
18
+ ELSE substr(email, 1, instr(email, '@') - 1)
19
+ END,
20
+ '.',
21
+ ''
22
+ ) || '@gmail.com'
23
+ ELSE CASE
24
+ WHEN instr(email, '+') > 1 AND instr(email, '+') < instr(email, '@')
25
+ THEN substr(email, 1, instr(email, '+') - 1) || substr(email, instr(email, '@'))
26
+ ELSE email
27
+ END
28
+ END
29
+ );
@@ -0,0 +1,95 @@
1
+ -- Migration 0930: the stock ledger (stage 37 step 1, the `stock` behaviour, ADR 0680).
2
+ --
3
+ -- A record of an object that declares `stock` keeps its count on capacity's counter
4
+ -- (core_capacity, migration 0440): `places` is every unit that came in less every unit that went
5
+ -- out by a movement other than a sale, `taken` every unit sold, `held` what unpaid orders hold.
6
+ -- On hand is places - taken; available is on hand - held. Nobody writes the counter's places for
7
+ -- a stock record: the host changes it only with a movement here, in the same batch, so the
8
+ -- ledger's sum for a record is always its on hand. A sale is capacity's claim (held while the
9
+ -- order is unpaid, refused by the database past what is available, taken when paid) and writes
10
+ -- its `sold` row here when it is taken.
11
+ --
12
+ -- Every row carries a place: always 'default' until stock is kept in several places (stage 37's
13
+ -- designed later step), so places can be added without rewriting history.
14
+ --
15
+ -- Nobody changes or deletes a movement, by any statement shape. D1 runs with recursive triggers
16
+ -- off, so the row an INSERT OR REPLACE deletes fires no delete trigger (issue #269): the table
17
+ -- refuses an insert over an existing id, an update and a delete, and has no unique index but its
18
+ -- key. A dropped object's movements stay: they name records and staff, never customers.
19
+ CREATE TABLE core_stock_movement (
20
+ -- stm_… (ids.ts). A sale's row is named after its claim (stm_ + the claim's ULID).
21
+ id TEXT PRIMARY KEY NOT NULL,
22
+ -- The record whose stock moved: its object (as in core_capacity.object_type) and id.
23
+ object_type TEXT NOT NULL,
24
+ record_id TEXT NOT NULL,
25
+ place TEXT NOT NULL DEFAULT 'default',
26
+ -- Signed: what came in is positive, what went out negative.
27
+ quantity INTEGER NOT NULL CHECK (quantity <> 0),
28
+ kind TEXT NOT NULL CHECK (kind IN ('opening', 'received', 'sold', 'returned', 'written_off', 'counted', 'adjusted', 'used')),
29
+ -- What moved it: an order line ('order_line', its id), a record (its object, its id), or a
30
+ -- person's own correction ('user', their id).
31
+ source_type TEXT NOT NULL,
32
+ source_id TEXT NOT NULL,
33
+ -- The staff account that made it; NULL when the host did (a paid order's sale).
34
+ by_user TEXT,
35
+ at TEXT NOT NULL,
36
+ note TEXT CHECK (note IS NULL OR length(note) <= 500),
37
+ -- The cost of one, in minor units, for what was received.
38
+ unit_cost INTEGER CHECK (unit_cost IS NULL OR unit_cost >= 0),
39
+ CHECK (kind <> 'sold' OR quantity < 0),
40
+ CHECK (kind <> 'opening' OR quantity > 0)
41
+ );
42
+ CREATE INDEX core_stock_movement_record_idx ON core_stock_movement (object_type, record_id, at);
43
+ CREATE INDEX core_stock_movement_source_idx ON core_stock_movement (source_type, source_id);
44
+
45
+ CREATE TRIGGER core_stock_movement_new
46
+ BEFORE INSERT ON core_stock_movement
47
+ WHEN EXISTS (SELECT 1 FROM core_stock_movement m WHERE m.id = NEW.id)
48
+ BEGIN
49
+ SELECT RAISE(ABORT, 'core_stock_movement_immutable');
50
+ END;
51
+
52
+ -- Opening stock is a record's first stock, once: a second is refused (a correction is `adjusted`).
53
+ CREATE TRIGGER core_stock_opening_once
54
+ BEFORE INSERT ON core_stock_movement
55
+ WHEN NEW.kind = 'opening' AND EXISTS (
56
+ SELECT 1 FROM core_stock_movement m
57
+ WHERE m.object_type = NEW.object_type AND m.record_id = NEW.record_id AND m.kind = 'opening'
58
+ )
59
+ BEGIN
60
+ SELECT RAISE(ABORT, 'core_stock_opening_exists');
61
+ END;
62
+
63
+ CREATE TRIGGER core_stock_movement_no_update
64
+ BEFORE UPDATE ON core_stock_movement
65
+ BEGIN
66
+ SELECT RAISE(ABORT, 'core_stock_movement_immutable');
67
+ END;
68
+
69
+ CREATE TRIGGER core_stock_movement_no_delete
70
+ BEFORE DELETE ON core_stock_movement
71
+ BEGIN
72
+ SELECT RAISE(ABORT, 'core_stock_movement_immutable');
73
+ END;
74
+
75
+ -- rec_stock_movement: the ledger, for query.sql and dashboards. It names order lines and staff,
76
+ -- never a customer.
77
+ CREATE VIEW rec_stock_movement AS
78
+ SELECT
79
+ id,
80
+ object_type,
81
+ record_id,
82
+ place,
83
+ quantity,
84
+ kind,
85
+ source_type,
86
+ source_id,
87
+ by_user,
88
+ at,
89
+ note,
90
+ unit_cost
91
+ FROM core_stock_movement;
92
+
93
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
94
+ UPDATE core_meta SET value = '930'
95
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 930;
@@ -0,0 +1,25 @@
1
+ -- Migration 0931: low stock, once per crossing (stage 37 step 2, ADR 0681).
2
+ --
3
+ -- When a write takes a stock record's on hand to or below its reorder level, the staff who keep
4
+ -- stock are told once, until it rises above the level again. A row here says the record is at or
5
+ -- below its level now: the host deletes it in the batch of a write that leaves the record above
6
+ -- (or with no level), and inserts it, keeping the one already there, in the batch of a write that
7
+ -- leaves it at or below. `crossing` names this time below the level (each notice's dedupe key is
8
+ -- built from it) and `told_at` says its notices were written, so every later write while the
9
+ -- record stays low tells nobody again, however long it stays (notices are purged after 90 days),
10
+ -- and the next fall after a rise makes a new crossing and new notices. Nothing reads it but the host's own
11
+ -- statements (stock/low.ts); a dropped object's rows go with its counters.
12
+ CREATE TABLE core_stock_low (
13
+ object_type TEXT NOT NULL,
14
+ record_id TEXT NOT NULL,
15
+ -- stl_…: this crossing. A notice's dedupe key is 'stock_low:' || crossing.
16
+ crossing TEXT NOT NULL,
17
+ since TEXT NOT NULL,
18
+ -- When its notices were written; NULL until the end of the batch that began it.
19
+ told_at TEXT,
20
+ PRIMARY KEY (object_type, record_id)
21
+ );
22
+
23
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
24
+ UPDATE core_meta SET value = '931'
25
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 931;
@@ -0,0 +1,78 @@
1
+ -- Migration 0932: goods and delivery on orders (stage 37 step 3, ADR 0682).
2
+ --
3
+ -- An order with goods (a line of an object declaring `sellable.goods`) has a channel the host
4
+ -- works out from its delivery and never takes from a caller: `online` for a post or collect
5
+ -- delivery, `in_person` for "take it now". It is written when the order starts and never changes.
6
+ -- An order with no goods has none.
7
+ --
8
+ -- The order's delivery is one row here: the delivery line (a record of an object declaring
9
+ -- `sellable.delivery`), its kind and, for a post delivery, the address the goods go to. The
10
+ -- address is personal data kept by the host: it is in no rec_* view, export, dashboard or search,
11
+ -- and is read one order at a time (`order.delivery.get`). Erasing the buyer blanks it and marks
12
+ -- the row erased; nothing else changes a row.
13
+
14
+ ALTER TABLE core_order ADD COLUMN channel TEXT CHECK (channel IS NULL OR channel IN ('online', 'in_person'));
15
+
16
+ CREATE TRIGGER core_order_channel_fixed
17
+ BEFORE UPDATE OF channel ON core_order
18
+ WHEN NEW.channel IS NOT OLD.channel
19
+ BEGIN
20
+ SELECT RAISE(ABORT, 'core_order_channel_fixed');
21
+ END;
22
+
23
+ CREATE TABLE core_order_delivery (
24
+ order_id TEXT PRIMARY KEY REFERENCES core_order(id),
25
+ -- The order's delivery line: the option chosen, priced by the host (ADR 0685).
26
+ line_id TEXT NOT NULL UNIQUE REFERENCES core_order_line(id),
27
+ kind TEXT NOT NULL CHECK (kind IN ('post', 'collect', 'here')),
28
+ -- A post delivery's address; empty for collect and here, and once erased.
29
+ name TEXT,
30
+ line1 TEXT,
31
+ line2 TEXT,
32
+ city TEXT,
33
+ region TEXT,
34
+ postcode TEXT,
35
+ -- ISO 3166-1 alpha-2, upper case.
36
+ country TEXT CHECK (country IS NULL OR (length(country) = 2 AND country = upper(country))),
37
+ created_at TEXT NOT NULL,
38
+ -- When the buyer's erase blanked the address.
39
+ erased_at TEXT,
40
+ CHECK (kind = 'post' OR (name IS NULL AND line1 IS NULL AND line2 IS NULL AND city IS NULL
41
+ AND region IS NULL AND postcode IS NULL AND country IS NULL)),
42
+ CHECK (kind <> 'post' OR erased_at IS NOT NULL OR (name IS NOT NULL AND line1 IS NOT NULL
43
+ AND country IS NOT NULL))
44
+ );
45
+
46
+ -- A delivery is written with its order and never changed, except by the buyer's erase, which
47
+ -- blanks every part of the address at once and says when.
48
+ CREATE TRIGGER core_order_delivery_fixed
49
+ BEFORE UPDATE ON core_order_delivery
50
+ WHEN NEW.order_id IS NOT OLD.order_id OR NEW.line_id IS NOT OLD.line_id
51
+ OR NEW.kind IS NOT OLD.kind OR NEW.created_at IS NOT OLD.created_at
52
+ OR OLD.erased_at IS NOT NULL OR NEW.erased_at IS NULL
53
+ OR NEW.name IS NOT NULL OR NEW.line1 IS NOT NULL OR NEW.line2 IS NOT NULL
54
+ OR NEW.city IS NOT NULL OR NEW.region IS NOT NULL OR NEW.postcode IS NOT NULL
55
+ OR NEW.country IS NOT NULL
56
+ BEGIN
57
+ SELECT RAISE(ABORT, 'core_order_delivery_fixed');
58
+ END;
59
+
60
+ -- One delivery per order, written once: `INSERT OR REPLACE` would delete the row first without
61
+ -- firing a delete trigger (issue #269), so a second insert for the order or its line is refused.
62
+ CREATE TRIGGER core_order_delivery_once
63
+ BEFORE INSERT ON core_order_delivery
64
+ WHEN EXISTS (SELECT 1 FROM core_order_delivery
65
+ WHERE order_id = NEW.order_id OR line_id = NEW.line_id)
66
+ BEGIN
67
+ SELECT RAISE(ABORT, 'core_order_delivery_fixed');
68
+ END;
69
+
70
+ CREATE TRIGGER core_order_delivery_kept
71
+ BEFORE DELETE ON core_order_delivery
72
+ BEGIN
73
+ SELECT RAISE(ABORT, 'core_order_delivery_kept');
74
+ END;
75
+
76
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
77
+ UPDATE core_meta SET value = '932'
78
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 932;
@@ -0,0 +1,111 @@
1
+ -- Migration 0950: adjustments on orders (stage 34 step 1, ADR 0690).
2
+ --
3
+ -- An adjustment is money off an order, kept by the host: on one line, or on the whole order and
4
+ -- spread over its lines in proportion. Each line keeps its price (`line_total`) and its share of
5
+ -- the adjustments (`line_discount`); its net is the difference. The order's `total` is what is
6
+ -- owed after adjustments and `discount` is their sum, so everything downstream that reads the
7
+ -- total (the checkout, the amount-match rule, deposits, refunds) reads the net already.
8
+ --
9
+ -- Step 1 writes only `staff` adjustments (order.adjust, ADR 0691); `code`, `automatic` and
10
+ -- `referral` are the discount behaviour's and the referrals' (later steps).
11
+
12
+ ALTER TABLE core_order ADD COLUMN discount INTEGER NOT NULL DEFAULT 0 CHECK (discount >= 0);
13
+ ALTER TABLE core_order_line ADD COLUMN line_discount INTEGER NOT NULL DEFAULT 0
14
+ CHECK (line_discount >= 0 AND line_discount <= line_total);
15
+
16
+ -- The version check covers the discount too.
17
+ DROP TRIGGER core_order_stale;
18
+ CREATE TRIGGER core_order_stale
19
+ BEFORE UPDATE OF state, paid, refunded, total, discount, deposit, balance_due_at, tax_amount, tax_refunded
20
+ ON core_order
21
+ WHEN NEW.version <> OLD.version + 1
22
+ BEGIN
23
+ SELECT RAISE(ABORT, 'core_order_stale');
24
+ END;
25
+
26
+ -- One adjustment. Never changed once written, except: a pending order's adjustment taken off is
27
+ -- marked removed (once), and an erase blanks a staff reason. Amounts are positive minor units.
28
+ CREATE TABLE core_order_adjustment (
29
+ id TEXT PRIMARY KEY,
30
+ order_id TEXT NOT NULL REFERENCES core_order(id),
31
+ -- The line it is on; null: the whole order, spread over its lines in proportion.
32
+ line_id TEXT REFERENCES core_order_line(id),
33
+ kind TEXT NOT NULL CHECK (kind IN ('code', 'automatic', 'staff', 'referral')),
34
+ -- The discount record it came from (the discount behaviour, a later step).
35
+ source_object TEXT,
36
+ source_id TEXT,
37
+ -- A staff adjustment: the person who gave it and why.
38
+ given_by TEXT,
39
+ reason TEXT,
40
+ -- Asked as a percentage (of the line, or of each line of the order): kept for what was asked.
41
+ percent REAL CHECK (percent IS NULL OR (percent > 0 AND percent <= 100)),
42
+ amount INTEGER NOT NULL CHECK (amount > 0),
43
+ -- The pending action it waited in, when it was over the limit or asked by an AI.
44
+ approval TEXT,
45
+ created_at TEXT NOT NULL,
46
+ removed_at TEXT,
47
+ removed_by TEXT,
48
+ CHECK (kind <> 'staff' OR given_by IS NOT NULL),
49
+ CHECK ((removed_at IS NULL) = (removed_by IS NULL))
50
+ );
51
+ CREATE INDEX core_order_adjustment_order_idx ON core_order_adjustment (order_id, created_at);
52
+
53
+ CREATE TRIGGER core_order_adjustment_fixed
54
+ BEFORE UPDATE ON core_order_adjustment
55
+ WHEN NEW.id IS NOT OLD.id OR NEW.order_id IS NOT OLD.order_id OR NEW.line_id IS NOT OLD.line_id
56
+ OR NEW.kind IS NOT OLD.kind OR NEW.source_object IS NOT OLD.source_object
57
+ OR NEW.source_id IS NOT OLD.source_id OR NEW.given_by IS NOT OLD.given_by
58
+ OR NEW.percent IS NOT OLD.percent OR NEW.amount IS NOT OLD.amount
59
+ OR NEW.approval IS NOT OLD.approval OR NEW.created_at IS NOT OLD.created_at
60
+ OR (OLD.removed_at IS NOT NULL AND NEW.removed_at IS NOT OLD.removed_at)
61
+ OR (NEW.reason IS NOT OLD.reason AND NEW.reason IS NOT NULL)
62
+ BEGIN
63
+ SELECT RAISE(ABORT, 'core_order_adjustment_fixed');
64
+ END;
65
+
66
+ CREATE TRIGGER core_order_adjustment_kept
67
+ BEFORE DELETE ON core_order_adjustment
68
+ BEGIN
69
+ SELECT RAISE(ABORT, 'core_order_adjustment_kept');
70
+ END;
71
+
72
+ -- An adjustment is added to or taken off a pending order only.
73
+ CREATE TRIGGER core_order_adjustment_pending
74
+ BEFORE INSERT ON core_order_adjustment
75
+ WHEN (SELECT state FROM core_order WHERE id = NEW.order_id) IS NOT 'pending'
76
+ BEGIN
77
+ SELECT RAISE(ABORT, 'core_order_adjustment_pending');
78
+ END;
79
+
80
+ DROP VIEW rec_order;
81
+ CREATE VIEW rec_order AS
82
+ SELECT
83
+ id, person_id, email, currency, total, paid, total - paid AS balance, refunded, state, pay,
84
+ deposit, balance_due_at, expires_at, placed_as, paid_at, cancelled_at, refunded_at, created_at,
85
+ updated_at, tax_amount, tax_refunded, tax_country,
86
+ (paid - refunded) - (tax_amount - tax_refunded) AS net,
87
+ subscription_id, period_start, period_end, period_flag,
88
+ discount
89
+ FROM core_order;
90
+
91
+ DROP VIEW rec_order_line;
92
+ CREATE VIEW rec_order_line AS
93
+ SELECT
94
+ l.id, l.order_id, o.person_id, l.position, l.object_type, l.record_id, l.title, l.quantity,
95
+ l.unit_price, l.line_total, l.created_at, l.tax_amount, l.tax_code,
96
+ l.line_discount, l.line_total - l.line_discount AS line_net
97
+ FROM core_order_line l
98
+ JOIN core_order o ON o.id = l.order_id;
99
+
100
+ -- Adjustments for reports: what each discount or person took off, and on which order.
101
+ CREATE VIEW rec_order_adjustment AS
102
+ SELECT
103
+ a.id, a.order_id, o.person_id, a.line_id, a.kind, a.source_object, a.source_id, a.given_by,
104
+ a.reason, a.percent, a.amount, a.approval, a.created_at, a.removed_at, a.removed_by,
105
+ o.state AS order_state, o.currency
106
+ FROM core_order_adjustment a
107
+ JOIN core_order o ON o.id = a.order_id;
108
+
109
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
110
+ UPDATE core_meta SET value = '950'
111
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 950;
@@ -0,0 +1,33 @@
1
+ -- Migration 0951: an order's price and its payment page never cross (stage 34 step 1, review of
2
+ -- PR #380; ADR 0690).
3
+ --
4
+ -- A checkout is opened for the amount the host read, then the provider is asked for a page, then
5
+ -- the checkout is recorded. A discount given in between would leave a page for the old price,
6
+ -- which the verified payment would not match. The database refuses both orders of that race:
7
+
8
+ -- The total of an order whose payment page is open does not change (a discount given after the
9
+ -- page was recorded).
10
+ CREATE TRIGGER core_order_total_checkout_open
11
+ BEFORE UPDATE OF total ON core_order
12
+ WHEN NEW.total IS NOT OLD.total
13
+ AND EXISTS (SELECT 1 FROM core_order_checkout WHERE order_id = NEW.id)
14
+ BEGIN
15
+ SELECT RAISE(ABORT, 'core_order_checkout_open');
16
+ END;
17
+
18
+ -- A page for the whole price or the deposit is recorded only for what the order costs now (a
19
+ -- discount given while the page was being opened).
20
+ CREATE TRIGGER core_order_checkout_amount
21
+ BEFORE INSERT ON core_order_checkout
22
+ WHEN (NEW.kind = 'full'
23
+ AND NEW.amount IS NOT (SELECT total FROM core_order WHERE id = NEW.order_id))
24
+ OR (NEW.kind = 'deposit'
25
+ AND NEW.amount IS NOT (SELECT CASE WHEN deposit < total THEN deposit END
26
+ FROM core_order WHERE id = NEW.order_id))
27
+ BEGIN
28
+ SELECT RAISE(ABORT, 'core_order_checkout_stale');
29
+ END;
30
+
31
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
32
+ UPDATE core_meta SET value = '951'
33
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 951;