@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,21 @@
1
+ -- Migration 1273: what keeps a time entry for a person, and why it was reopened (stage 58 step 2,
2
+ -- ADR 0851).
3
+ --
4
+ -- `held`: the host never approves the entry on a match with the roster. 'person' when no one but
5
+ -- an approver should take its times on trust (an approver entered it for someone; its person
6
+ -- entered a finish the host had marked missing); 'approver' once an approver has sent it back or
7
+ -- reopened it, which also stops its person withdrawing it (and entering the same times again for
8
+ -- the host to approve). Never cleared: an approver decides the entry from then on.
9
+ --
10
+ -- `reopened_why`: why an approver reopened an approved entry, kept on it for the person and the
11
+ -- approvers to read (never in the audit log, a notice or an error) until it is approved or sent
12
+ -- back again.
13
+ --
14
+ -- Migration 1272 is stage 58 step 3's (pay), numbered by the spec before this step.
15
+ ALTER TABLE core_time_entry ADD COLUMN held TEXT CHECK (held IS NULL OR held IN ('person', 'approver'));
16
+ ALTER TABLE core_time_entry ADD COLUMN reopened_why TEXT
17
+ CHECK (reopened_why IS NULL OR length(reopened_why) <= 500);
18
+
19
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
20
+ UPDATE core_meta SET value = '1273'
21
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1273;
@@ -0,0 +1,260 @@
1
+ -- Migration 1330: accounting sync's records (stage 61 step 2; the contract is ADR 0880). The host
2
+ -- sends its own money to the books a human admin connected: its settings, the mapping a person
3
+ -- chose from the books' own lists, the links between host objects and the books' records, the
4
+ -- outbox of entries to send, each money event to the entry it went in, and the bill-tos waiting
5
+ -- for a person to choose their contact.
6
+ --
7
+ -- New core tables only: no x_* table, no core_write_guard kind, in no rec_* view, so no member's
8
+ -- SQL, export, search or the assistant's record reads reach them. Read and written only through
9
+ -- the accounting.* operations (`accounting:manage`, a person) and, from stage 61 step 4, the host's
10
+ -- runner. No token or secret is held here: those stay in the gatekeeper (ADR 0151).
11
+
12
+ -- The one settings row (id 1). The connection and the books resource on it; the start date
13
+ -- (decision 6: nothing dated before it is ever sent; required before turning on); approval
14
+ -- (decision 4: `approved_days` days approved so far, sending is automatic once it reaches
15
+ -- `approve_until`); the settle delay in days; on or off. Set by a human admin
16
+ -- (accounting.settings.update); the runner counts approved days.
17
+ CREATE TABLE core_accounting_settings (
18
+ id INTEGER PRIMARY KEY CHECK (id = 1),
19
+ connection_id TEXT,
20
+ resource TEXT,
21
+ start_date TEXT CHECK (start_date IS NULL
22
+ OR start_date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
23
+ approved_days INTEGER NOT NULL DEFAULT 0 CHECK (approved_days >= 0),
24
+ approve_until INTEGER NOT NULL DEFAULT 14 CHECK (approve_until BETWEEN 14 AND 365),
25
+ settle_days INTEGER NOT NULL DEFAULT 2 CHECK (settle_days BETWEEN 1 AND 14),
26
+ enabled INTEGER NOT NULL DEFAULT 0 CHECK (enabled IN (0, 1)),
27
+ -- When the admins were told sending is automatic (once; cleared when approval is put back on).
28
+ automatic_told_at TEXT,
29
+ updated_by TEXT NOT NULL,
30
+ updated_at TEXT NOT NULL,
31
+ CHECK ((connection_id IS NULL) = (resource IS NULL)),
32
+ -- On needs the books and a start date.
33
+ CHECK (enabled = 0 OR (connection_id IS NOT NULL AND start_date IS NOT NULL))
34
+ );
35
+
36
+ -- The mapping (`accounting:manage`, a person): which of the books' accounts or tax rates each
37
+ -- kind of host money goes to, per connection (refs are the books' own). `host_key` is a category
38
+ -- key, a tax code, or a recorded-payment kind for `category`, `tax` and `method`; empty for the
39
+ -- single ones. `books_name` is the name as read from the books when chosen, for the log. A change
40
+ -- affects only entries not yet sent: a sent entry keeps its lines as sent.
41
+ CREATE TABLE core_accounting_map (
42
+ connection_id TEXT NOT NULL,
43
+ kind TEXT NOT NULL CHECK (kind IN ('category', 'tax', 'fees', 'clearing', 'bank', 'method',
44
+ 'rounding', 'refunds', 'invoice_default')),
45
+ host_key TEXT NOT NULL DEFAULT '' CHECK (length(host_key) <= 100),
46
+ books_ref TEXT NOT NULL CHECK (length(books_ref) BETWEEN 1 AND 200),
47
+ books_name TEXT NOT NULL CHECK (length(books_name) BETWEEN 1 AND 200),
48
+ set_by TEXT NOT NULL,
49
+ set_at TEXT NOT NULL,
50
+ CHECK ((kind IN ('category', 'tax', 'method')) = (host_key != '')),
51
+ PRIMARY KEY (connection_id, kind, host_key)
52
+ ) WITHOUT ROWID;
53
+
54
+ -- A host object to the books' record it is: a Person or Organisation to a contact, an invoice or
55
+ -- credit note to a document, a payment to a payment, a payout or a day's summary to an entry.
56
+ -- Unique both ways per connection: one host object has one books record of its kind, and one
57
+ -- books record is one host object's.
58
+ CREATE TABLE core_accounting_link (
59
+ connection_id TEXT NOT NULL,
60
+ object TEXT NOT NULL CHECK (object IN ('person', 'organisation', 'invoice', 'credit_note',
61
+ 'payment', 'payout', 'summary')),
62
+ object_id TEXT NOT NULL,
63
+ books_kind TEXT NOT NULL CHECK (books_kind IN ('contact', 'document', 'payment', 'entry')),
64
+ books_ref TEXT NOT NULL CHECK (length(books_ref) BETWEEN 1 AND 200),
65
+ created_at TEXT NOT NULL,
66
+ CHECK (books_kind = CASE
67
+ WHEN object IN ('person', 'organisation') THEN 'contact'
68
+ WHEN object IN ('invoice', 'credit_note') THEN 'document'
69
+ WHEN object = 'payment' THEN 'payment'
70
+ ELSE 'entry' END),
71
+ PRIMARY KEY (connection_id, object, object_id),
72
+ UNIQUE (connection_id, books_kind, books_ref)
73
+ ) WITHOUT ROWID;
74
+
75
+ -- The outbox: one entry to send, built by host code from host records. `reference` is fixed
76
+ -- (`cos:<kind>:<source id>:<n>`), stored on the books' record so an uncertain outcome is resolved
77
+ -- by looking it up before anything is sent again; unique, so nothing is queued twice.
78
+ -- `carries_date` is the date it carries in the books (moved past a lock date, decision 7);
79
+ -- `belongs_date` the day its money belongs to. `lines` are the lines as sent (amounts in minor
80
+ -- units, mapping refs), kept so the log shows exactly what went; `detail` is what a contact or
81
+ -- document carried about the person it is for, which an erase may clear.
82
+ CREATE TABLE core_accounting_post (
83
+ id TEXT PRIMARY KEY,
84
+ connection_id TEXT NOT NULL,
85
+ kind TEXT NOT NULL CHECK (kind IN ('summary', 'adjustment', 'transfer', 'contact', 'invoice',
86
+ 'credit_note', 'payment', 'refund', 'void')),
87
+ source_kind TEXT NOT NULL,
88
+ source_id TEXT NOT NULL,
89
+ n INTEGER NOT NULL DEFAULT 1 CHECK (n >= 1),
90
+ reference TEXT NOT NULL UNIQUE CHECK (substr(reference, 1, 4) = 'cos:'
91
+ AND length(reference) <= 100),
92
+ carries_date TEXT NOT NULL CHECK (carries_date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
93
+ belongs_date TEXT NOT NULL CHECK (belongs_date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
94
+ -- The entry's total in minor units (the sum of its money in), for the log.
95
+ amount INTEGER NOT NULL DEFAULT 0,
96
+ lines TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(lines) AND json_type(lines) = 'array'),
97
+ detail TEXT CHECK (detail IS NULL OR json_valid(detail)),
98
+ depends_on TEXT,
99
+ state TEXT NOT NULL CHECK (state IN ('awaiting_approval', 'queued', 'sending', 'sent',
100
+ 'retrying', 'held', 'unknown', 'skipped')),
101
+ attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0),
102
+ next_attempt_at TEXT,
103
+ error_code TEXT,
104
+ error_text TEXT CHECK (error_text IS NULL OR length(error_text) <= 1000),
105
+ books_ref TEXT,
106
+ sent_at TEXT,
107
+ approved_by TEXT,
108
+ approved_at TEXT,
109
+ skipped_by TEXT,
110
+ skipped_at TEXT,
111
+ skip_reason TEXT CHECK (skip_reason IS NULL OR length(skip_reason) BETWEEN 1 AND 500),
112
+ created_at TEXT NOT NULL,
113
+ updated_at TEXT NOT NULL,
114
+ CHECK (depends_on IS NULL OR depends_on != id),
115
+ CHECK ((state = 'sent') = (books_ref IS NOT NULL AND sent_at IS NOT NULL)),
116
+ CHECK ((state = 'skipped') = (skipped_by IS NOT NULL AND skipped_at IS NOT NULL
117
+ AND skip_reason IS NOT NULL)),
118
+ CHECK ((approved_by IS NULL) = (approved_at IS NULL))
119
+ );
120
+
121
+ -- What the runner picks up (by state and when), a day's batch and log, and what waits for a post.
122
+ CREATE INDEX core_accounting_post_state ON core_accounting_post (state, next_attempt_at);
123
+ CREATE INDEX core_accounting_post_day ON core_accounting_post (carries_date, kind);
124
+ CREATE INDEX core_accounting_post_depends ON core_accounting_post (depends_on)
125
+ WHERE depends_on IS NOT NULL;
126
+
127
+ -- An entry is its reference: its id and reference never change, whatever its state. This also
128
+ -- refuses an UPDATE OR REPLACE that would take another entry's id or reference (D1 runs with
129
+ -- recursive triggers off, so the row such a REPLACE deletes fires no delete trigger).
130
+ CREATE TRIGGER core_accounting_post_identity
131
+ BEFORE UPDATE OF id, reference ON core_accounting_post
132
+ BEGIN
133
+ SELECT RAISE(ABORT, 'core_accounting_post_fixed')
134
+ WHERE NEW.id IS NOT OLD.id OR NEW.reference IS NOT OLD.reference;
135
+ END;
136
+
137
+ -- A sent entry is in the books, and a skipped one is never sent: nothing about either changes,
138
+ -- who approved it included, and it keeps its state. (A sent entry is put right by a new entry,
139
+ -- never edited.) Only `detail` may be cleared, by an erase, and `updated_at` moves with it.
140
+ CREATE TRIGGER core_accounting_post_sent
141
+ BEFORE UPDATE ON core_accounting_post
142
+ WHEN OLD.state IN ('sent', 'skipped')
143
+ BEGIN
144
+ SELECT RAISE(ABORT, 'core_accounting_post_fixed')
145
+ WHERE NEW.state IS NOT OLD.state OR NEW.connection_id IS NOT OLD.connection_id
146
+ OR NEW.kind IS NOT OLD.kind OR NEW.source_kind IS NOT OLD.source_kind
147
+ OR NEW.source_id IS NOT OLD.source_id OR NEW.n IS NOT OLD.n
148
+ OR NEW.carries_date IS NOT OLD.carries_date OR NEW.belongs_date IS NOT OLD.belongs_date
149
+ OR NEW.amount IS NOT OLD.amount OR NEW.lines IS NOT OLD.lines
150
+ OR (NEW.detail IS NOT OLD.detail AND NEW.detail IS NOT NULL)
151
+ OR NEW.depends_on IS NOT OLD.depends_on OR NEW.attempts IS NOT OLD.attempts
152
+ OR NEW.next_attempt_at IS NOT OLD.next_attempt_at OR NEW.error_code IS NOT OLD.error_code
153
+ OR NEW.error_text IS NOT OLD.error_text OR NEW.books_ref IS NOT OLD.books_ref
154
+ OR NEW.sent_at IS NOT OLD.sent_at OR NEW.approved_by IS NOT OLD.approved_by
155
+ OR NEW.approved_at IS NOT OLD.approved_at OR NEW.skipped_by IS NOT OLD.skipped_by
156
+ OR NEW.skipped_at IS NOT OLD.skipped_at OR NEW.skip_reason IS NOT OLD.skip_reason
157
+ OR NEW.created_at IS NOT OLD.created_at;
158
+ END;
159
+
160
+ -- An entry that may be in the books leaves its state only as its outcome allows: one being sent
161
+ -- becomes sent, retrying or held (refused: nothing written), or unknown; one whose outcome is not
162
+ -- known becomes sent (found by its reference) or queued (not found). The database cannot tell
163
+ -- whether the look-up ran before `unknown` became `queued`: the runner never moves it otherwise.
164
+ CREATE TRIGGER core_accounting_post_outcome
165
+ BEFORE UPDATE OF state ON core_accounting_post
166
+ WHEN OLD.state IN ('sending', 'unknown') AND NEW.state IS NOT OLD.state
167
+ BEGIN
168
+ SELECT RAISE(ABORT, 'core_accounting_post_fixed')
169
+ WHERE NOT ((OLD.state = 'sending' AND NEW.state IN ('sent', 'retrying', 'held', 'unknown'))
170
+ OR (OLD.state = 'unknown' AND NEW.state IN ('sent', 'queued')));
171
+ END;
172
+
173
+ -- An entry is never deleted once it might be in the books (sent, being sent, or its outcome not
174
+ -- known) or was skipped (the log lists its money as not in the books).
175
+ CREATE TRIGGER core_accounting_post_delete
176
+ BEFORE DELETE ON core_accounting_post
177
+ WHEN OLD.state IN ('sending', 'sent', 'unknown', 'skipped')
178
+ BEGIN
179
+ SELECT RAISE(ABORT, 'core_accounting_post_fixed');
180
+ END;
181
+
182
+ -- A REPLACE of an entry (an insert) is refused: it would delete the row without the delete
183
+ -- trigger. This refuses `INSERT … ON CONFLICT DO NOTHING` of a queued reference too, so the runner
184
+ -- reads what is queued before it queues.
185
+ CREATE TRIGGER core_accounting_post_insert
186
+ BEFORE INSERT ON core_accounting_post
187
+ BEGIN
188
+ SELECT RAISE(ABORT, 'core_accounting_post_fixed')
189
+ WHERE EXISTS (SELECT 1 FROM core_accounting_post p WHERE p.id = NEW.id
190
+ OR p.reference = NEW.reference);
191
+ END;
192
+
193
+ -- The start date is fixed once an entry to the books it applies to may be in them (sent, being
194
+ -- sent, or its outcome not known): moving it would send days twice or leave a gap. Here as well
195
+ -- as in accounting.settings.update, so a write planned before an entry was claimed fails whole.
196
+ -- Other books have their own start: nothing is in them yet.
197
+ CREATE TRIGGER core_accounting_settings_start
198
+ BEFORE UPDATE OF start_date ON core_accounting_settings
199
+ WHEN NEW.start_date IS NOT OLD.start_date
200
+ BEGIN
201
+ SELECT RAISE(ABORT, 'core_accounting_start_fixed')
202
+ WHERE EXISTS (SELECT 1 FROM core_accounting_post p WHERE p.connection_id = NEW.connection_id
203
+ AND p.state IN ('sending', 'sent', 'unknown'));
204
+ END;
205
+
206
+ CREATE TRIGGER core_accounting_settings_start_replace
207
+ BEFORE INSERT ON core_accounting_settings
208
+ BEGIN
209
+ SELECT RAISE(ABORT, 'core_accounting_start_fixed')
210
+ WHERE EXISTS (SELECT 1 FROM core_accounting_settings s WHERE s.id = NEW.id
211
+ AND s.start_date IS NOT NEW.start_date)
212
+ AND EXISTS (SELECT 1 FROM core_accounting_post p WHERE p.connection_id = NEW.connection_id
213
+ AND p.state IN ('sending', 'sent', 'unknown'));
214
+ END;
215
+
216
+ -- Each money event (a payment, a refund, a fee, a payout line, a document event) to the entry it
217
+ -- went in, unique by source: nothing is counted in two days, and order tables are not altered.
218
+ CREATE TABLE core_accounting_item (
219
+ source_kind TEXT NOT NULL,
220
+ source_id TEXT NOT NULL,
221
+ post_id TEXT NOT NULL,
222
+ -- The day the event belongs to (its moment's local day), when it went into a later one.
223
+ belongs_date TEXT NOT NULL CHECK (belongs_date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
224
+ amount INTEGER NOT NULL,
225
+ created_at TEXT NOT NULL,
226
+ PRIMARY KEY (source_kind, source_id)
227
+ ) WITHOUT ROWID;
228
+
229
+ CREATE INDEX core_accounting_item_post ON core_accounting_item (post_id);
230
+
231
+ -- A bill-to waiting for a person (decision 8): `unsure`, its candidates in the books (at most
232
+ -- 10: ref, name, email, tax number, archived), and the entry waiting on it; or `merged`, two
233
+ -- contacts in the books for one customer after a merge in the host, listed and never merged in
234
+ -- the books by the host. Decided once: a contact chosen, a new one, or dismissed.
235
+ CREATE TABLE core_accounting_match (
236
+ id TEXT PRIMARY KEY,
237
+ connection_id TEXT NOT NULL,
238
+ reason TEXT NOT NULL CHECK (reason IN ('unsure', 'merged')),
239
+ object TEXT NOT NULL CHECK (object IN ('person', 'organisation')),
240
+ object_id TEXT NOT NULL,
241
+ post_id TEXT,
242
+ candidates TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(candidates)
243
+ AND json_type(candidates) = 'array' AND json_array_length(candidates) <= 10),
244
+ state TEXT NOT NULL DEFAULT 'open' CHECK (state IN ('open', 'linked', 'created', 'dismissed')),
245
+ chosen_ref TEXT,
246
+ decided_by TEXT,
247
+ decided_at TEXT,
248
+ created_at TEXT NOT NULL,
249
+ CHECK ((state = 'open') = (decided_by IS NULL AND decided_at IS NULL)),
250
+ CHECK ((state = 'linked') = (chosen_ref IS NOT NULL))
251
+ );
252
+
253
+ -- One open question per bill-to and reason.
254
+ CREATE UNIQUE INDEX core_accounting_match_open ON core_accounting_match
255
+ (connection_id, reason, object, object_id) WHERE state = 'open';
256
+ CREATE INDEX core_accounting_match_object ON core_accounting_match (object, object_id);
257
+
258
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
259
+ UPDATE core_meta SET value = '1330'
260
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1330;
@@ -0,0 +1,24 @@
1
+ -- Migration 1350: a customer's private calendar feed (stage 62 step 1, ADR 0890).
2
+ --
3
+ -- A customer account's live bookings, and those of the children it is a live guardian of, as an
4
+ -- iCalendar feed a calendar app subscribes to with no sign-in: `GET /api/v1/me/feed/<token>.ics`.
5
+ -- The token is the only secret: 32 random bytes, shown to the customer once when they make it,
6
+ -- and stored here only as the SHA-256 of the token as written in the address (64 hex
7
+ -- characters), never the token. One feed per account: making a new one (rotating) replaces the
8
+ -- old, whose address then answers as any missing path does.
9
+ --
10
+ -- Made only by the customer themselves (`me.calendarFeed.rotate`); revoked by them, by staff
11
+ -- (`customer.revokeCalendarFeed`), and deleted when the account is disabled, deleted or erased (in
12
+ -- that change's batch, and by the cascade when the account row goes). The feed answers only while
13
+ -- the account is active and linked to its Person, whatever this table holds.
14
+ --
15
+ -- Its own table, apart from core_staff_feed: a staff token is never looked up on the customer's
16
+ -- route, nor the reverse. About an account, and a secret: in no rec_* view, export or search
17
+ -- index. No trigger.
18
+ CREATE TABLE core_customer_feed (
19
+ -- The customer account whose bookings it shows (core_customer.id).
20
+ customer_id TEXT PRIMARY KEY NOT NULL REFERENCES core_customer (id) ON DELETE CASCADE,
21
+ -- SHA-256 of the token, lowercase hex. A lookup by it reads one row.
22
+ token_hash TEXT NOT NULL UNIQUE CHECK (length(token_hash) = 64),
23
+ created_at TEXT NOT NULL
24
+ ) WITHOUT ROWID;
@@ -0,0 +1,77 @@
1
+ -- Migration 1390: locations, a base record (stage 64 step 1, ADR 0910).
2
+ --
3
+ -- One place the business works from, inside the one organisation (ADR 0001): never a tenant.
4
+ -- A gym's second studio, later a trades business's depot. Its name (unique, whatever the case),
5
+ -- a short name for a badge, its address (the business's, not personal data), its time zone, whether
6
+ -- the public is shown it, its own drop-in price in minor units (empty: not set), open or closed,
7
+ -- and staff notes.
8
+ --
9
+ -- Every host has one location from the start: the main location, made here from the
10
+ -- organisation's name and zone (`org_name`, `org_timezone`, JSON strings in core_settings), or
11
+ -- "Main location" in UTC on a host the setup wizard has not named yet. It is the row with
12
+ -- `main = 1`, with the first id there can be (a ULID of time zero, so it sorts first); its zone
13
+ -- follows the organisation's (settings.update). Every statement is written to
14
+ -- run again without change: the table, its indexes, triggers and view only if missing, and the
15
+ -- main location only while there is none.
16
+ --
17
+ -- Nothing deletes a location: it is closed. The last open location cannot be closed, refused by
18
+ -- the database for every writer (two closes at once of the last two open ones: one lands).
19
+ CREATE TABLE IF NOT EXISTS core_location (
20
+ -- NOT NULL: a rowid table's TEXT key would otherwise take a NULL, which no comparison matches.
21
+ id TEXT PRIMARY KEY NOT NULL,
22
+ name TEXT NOT NULL CHECK (length(trim(name)) BETWEEN 1 AND 120),
23
+ short_name TEXT CHECK (short_name IS NULL OR length(short_name) BETWEEN 1 AND 16),
24
+ address TEXT CHECK (address IS NULL OR length(address) <= 500),
25
+ timezone TEXT NOT NULL CHECK (length(timezone) BETWEEN 1 AND 64),
26
+ public INTEGER NOT NULL DEFAULT 1 CHECK (public IN (0, 1)),
27
+ drop_in_price INTEGER CHECK (drop_in_price IS NULL OR drop_in_price >= 0),
28
+ state TEXT NOT NULL DEFAULT 'open' CHECK (state IN ('open', 'closed')),
29
+ notes TEXT CHECK (notes IS NULL OR length(notes) <= 5000),
30
+ main INTEGER NOT NULL DEFAULT 0 CHECK (main IN (0, 1)),
31
+ created_at TEXT NOT NULL,
32
+ created_by TEXT,
33
+ updated_at TEXT NOT NULL,
34
+ updated_by TEXT
35
+ );
36
+ CREATE UNIQUE INDEX IF NOT EXISTS core_location_name_idx ON core_location (name COLLATE NOCASE);
37
+ CREATE UNIQUE INDEX IF NOT EXISTS core_location_main_idx ON core_location (main) WHERE main = 1;
38
+
39
+ CREATE TRIGGER IF NOT EXISTS core_location_last_open
40
+ BEFORE UPDATE OF state ON core_location
41
+ WHEN OLD.state = 'open' AND NEW.state <> 'open'
42
+ AND NOT EXISTS (SELECT 1 FROM core_location WHERE state = 'open' AND id <> OLD.id)
43
+ BEGIN
44
+ SELECT RAISE(ABORT, 'core_location_last_open');
45
+ END;
46
+
47
+ CREATE TRIGGER IF NOT EXISTS core_location_main_kept
48
+ BEFORE UPDATE OF main ON core_location
49
+ WHEN OLD.main <> NEW.main
50
+ BEGIN
51
+ SELECT RAISE(ABORT, 'core_location_main_kept');
52
+ END;
53
+
54
+ -- The locations, as staff list them. `listed` is what the public declaration tests (ADR 0916):
55
+ -- 'yes' only for a location shown to the public that is open.
56
+ CREATE VIEW IF NOT EXISTS rec_location AS
57
+ SELECT id, name, short_name, address, timezone, public, drop_in_price, state, notes, main,
58
+ CASE WHEN public = 1 AND state = 'open' THEN 'yes' ELSE 'no' END AS listed,
59
+ created_at, created_by, updated_at, updated_by
60
+ FROM core_location;
61
+
62
+ INSERT INTO core_location (id, name, timezone, main, created_at, updated_at)
63
+ SELECT 'loc_00000000000000000000000001',
64
+ coalesce(
65
+ nullif(trim((SELECT json_extract(value, '$') FROM core_settings
66
+ WHERE key = 'org_name' AND json_type(value) = 'text')), ''),
67
+ 'Main location'
68
+ ),
69
+ coalesce(
70
+ (SELECT json_extract(value, '$') FROM core_settings
71
+ WHERE key = 'org_timezone' AND json_type(value) = 'text'),
72
+ 'UTC'
73
+ ),
74
+ 1,
75
+ strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
76
+ strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
77
+ WHERE NOT EXISTS (SELECT 1 FROM core_location WHERE main = 1);
@@ -0,0 +1,213 @@
1
+ -- Migration 1410: the host's copy of a business's booking system (stage 65 step 3, ADR 0922).
2
+ --
3
+ -- A business that keeps its booking system loads lists exported from it (a `members` list: people
4
+ -- and their memberships and passes; a `timetable` list: classes, their bookings and their coaches),
5
+ -- and the host keeps the least of them the first wave uses. Every row here is a copy of what the
6
+ -- booking system said in the list it came from: nothing in it is the business's own record, each
7
+ -- load of a kind replaces the copy of that kind, and nothing is ever written back.
8
+ --
9
+ -- New tables only, all core: in no rec_* view, so no query.sql, export, search, dashboard or AI
10
+ -- record read reaches them; they are read only through the `outside.*` operations. No x_* table and
11
+ -- no core_write_guard kind. No foreign key to core_person or core_user: an erased or merged Person's
12
+ -- outside rows are removed by erase and merge themselves (stage 65 step 10), and a link to a Person
13
+ -- who is gone or in the Trash counts as no link (every count and load joins the live Persons).
14
+ --
15
+ -- An outside person is matched to a Person by address, never merged: a link by address changes
16
+ -- nothing on the Person. `link_how` says how: `address` (the host found exactly one Person with
17
+ -- the address) or `staff` (a person with bookingSystem:manage chose, with why). A staff link with
18
+ -- no Person is staff saying "nobody here"; that decision itself is kept by hash in
19
+ -- core_outside_unlinked, so it outlives the row and keeps no name or address.
20
+
21
+ -- The one row of settings (a human admin's, outside.settings.update): the source, on or off, the
22
+ -- outside types that make a Person a member (NULL: every type of kind `membership`, no pass), the
23
+ -- business's two links (each https, shown as is), the largest age a members list counts for, and
24
+ -- the saved column mappings and value maps per kind (stage 65 step 4).
25
+ CREATE TABLE core_outside_settings (
26
+ id INTEGER PRIMARY KEY CHECK (id = 1),
27
+ source TEXT NOT NULL DEFAULT 'file' CHECK (source IN ('file')),
28
+ enabled INTEGER NOT NULL DEFAULT 1 CHECK (enabled IN (0, 1)),
29
+ member_types TEXT CHECK (
30
+ member_types IS NULL OR (json_valid(member_types) AND json_type(member_types) = 'array')
31
+ ),
32
+ manage_url TEXT CHECK (
33
+ manage_url IS NULL OR (substr(manage_url, 1, 8) = 'https://' AND length(manage_url) <= 500)
34
+ ),
35
+ book_url TEXT CHECK (
36
+ book_url IS NULL OR (substr(book_url, 1, 8) = 'https://' AND length(book_url) <= 500)
37
+ ),
38
+ largest_age_days INTEGER NOT NULL DEFAULT 31 CHECK (largest_age_days BETWEEN 1 AND 90),
39
+ mappings TEXT NOT NULL DEFAULT '{}' CHECK (json_valid(mappings)),
40
+ updated_by TEXT,
41
+ updated_at TEXT
42
+ );
43
+ INSERT INTO core_outside_settings (id) VALUES (1);
44
+
45
+ -- Each loaded list: its kind, the date its file was exported (what its age is counted from), who
46
+ -- loaded it and when, and counts only (rows read, refused, kept): never a name or an address. One
47
+ -- `current` list per kind is the copy; a replaced or removed one stays 30 days as a log.
48
+ CREATE TABLE core_outside_list (
49
+ id TEXT PRIMARY KEY,
50
+ kind TEXT NOT NULL CHECK (kind IN ('members', 'timetable')),
51
+ source TEXT NOT NULL CHECK (source IN ('file')),
52
+ exported_on TEXT NOT NULL CHECK (exported_on GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
53
+ loaded_by TEXT NOT NULL,
54
+ loaded_at TEXT NOT NULL,
55
+ row_count INTEGER NOT NULL DEFAULT 0 CHECK (row_count >= 0),
56
+ refused_count INTEGER NOT NULL DEFAULT 0 CHECK (refused_count >= 0),
57
+ kept_count INTEGER NOT NULL DEFAULT 0 CHECK (kept_count >= 0),
58
+ -- The column mapping used (headers only), for the log.
59
+ mapping TEXT NOT NULL DEFAULT '{}' CHECK (json_valid(mapping)),
60
+ state TEXT NOT NULL DEFAULT 'current' CHECK (state IN ('current', 'replaced', 'removed')),
61
+ ended_at TEXT,
62
+ CHECK ((state = 'current') = (ended_at IS NULL))
63
+ );
64
+ CREATE UNIQUE INDEX core_outside_list_current ON core_outside_list (kind) WHERE state = 'current';
65
+ CREATE INDEX core_outside_list_ended ON core_outside_list (ended_at) WHERE ended_at IS NOT NULL;
66
+
67
+ -- An outside person kept from the current members list: only while they are a member, hold a live
68
+ -- pass, are booked in the current timetable, or are staff-linked (`why_kept`). The address as the
69
+ -- host normalises it, and its folded form (ADR 0692) the matching reads. Never a phone, a postal
70
+ -- address, a birth date, a card or a note.
71
+ CREATE TABLE core_outside_person (
72
+ ref TEXT PRIMARY KEY CHECK (length(ref) BETWEEN 1 AND 600),
73
+ list_id TEXT NOT NULL,
74
+ address TEXT CHECK (address IS NULL OR (address = lower(trim(address)) AND address <> '')),
75
+ address_folded TEXT CHECK ((address IS NULL) = (address_folded IS NULL)),
76
+ given_name TEXT CHECK (given_name IS NULL OR length(given_name) <= 500),
77
+ family_name TEXT CHECK (family_name IS NULL OR length(family_name) <= 500),
78
+ member_status TEXT NOT NULL CHECK (
79
+ member_status IN ('member', 'not_member', 'suspended', 'expired', 'ended', 'declined')
80
+ ),
81
+ active INTEGER NOT NULL CHECK (active IN (0, 1)),
82
+ why_kept TEXT NOT NULL CHECK (why_kept IN ('member', 'pass', 'booked', 'staff')),
83
+ person_id TEXT,
84
+ link_how TEXT CHECK (link_how IS NULL OR link_how IN ('address', 'staff')),
85
+ -- A staff link's who and why (a person with bookingSystem:manage).
86
+ link_by TEXT,
87
+ link_why TEXT CHECK (link_why IS NULL OR length(link_why) <= 500),
88
+ linked_at TEXT,
89
+ CHECK ((link_how IS NULL) = (linked_at IS NULL)),
90
+ CHECK (link_how IS NOT NULL OR person_id IS NULL),
91
+ CHECK (link_how IS NOT 'address' OR person_id IS NOT NULL),
92
+ CHECK ((link_how IS 'staff') = (link_by IS NOT NULL))
93
+ );
94
+ CREATE INDEX core_outside_person_address ON core_outside_person (address_folded)
95
+ WHERE address_folded IS NOT NULL;
96
+ CREATE INDEX core_outside_person_person ON core_outside_person (person_id)
97
+ WHERE person_id IS NOT NULL;
98
+
99
+ -- What the business sells that grants membership (`membership`) or uses (`pass`), as listed.
100
+ CREATE TABLE core_outside_membership_type (
101
+ ref TEXT PRIMARY KEY CHECK (length(ref) BETWEEN 1 AND 600),
102
+ list_id TEXT NOT NULL,
103
+ name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 500),
104
+ kind TEXT NOT NULL CHECK (kind IN ('membership', 'pass')),
105
+ active INTEGER NOT NULL CHECK (active IN (0, 1))
106
+ );
107
+
108
+ -- A membership or pass a kept outside person holds, as listed.
109
+ CREATE TABLE core_outside_membership (
110
+ ref TEXT PRIMARY KEY CHECK (length(ref) BETWEEN 1 AND 1200),
111
+ list_id TEXT NOT NULL,
112
+ person_ref TEXT NOT NULL,
113
+ type_ref TEXT NOT NULL,
114
+ name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 500),
115
+ kind TEXT NOT NULL CHECK (kind IN ('membership', 'pass')),
116
+ status TEXT NOT NULL CHECK (status IN ('live', 'paused', 'ended', 'cancelled')),
117
+ from_date TEXT CHECK (from_date IS NULL OR from_date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
118
+ until_date TEXT CHECK (until_date IS NULL OR until_date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
119
+ uses_total INTEGER CHECK (uses_total IS NULL OR uses_total >= 0),
120
+ uses_left INTEGER CHECK (uses_left IS NULL OR uses_left >= 0),
121
+ renews INTEGER NOT NULL CHECK (renews IN (0, 1))
122
+ );
123
+ CREATE INDEX core_outside_membership_person ON core_outside_membership (person_ref);
124
+
125
+ -- The timetable list's class types, classes, bookings and coaches.
126
+ CREATE TABLE core_outside_class_type (
127
+ ref TEXT PRIMARY KEY CHECK (length(ref) BETWEEN 1 AND 600),
128
+ list_id TEXT NOT NULL,
129
+ name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 500),
130
+ description TEXT CHECK (description IS NULL OR length(description) <= 500),
131
+ active INTEGER NOT NULL CHECK (active IN (0, 1))
132
+ );
133
+
134
+ CREATE TABLE core_outside_class (
135
+ ref TEXT PRIMARY KEY CHECK (length(ref) BETWEEN 1 AND 600),
136
+ list_id TEXT NOT NULL,
137
+ class_type_ref TEXT NOT NULL,
138
+ title TEXT NOT NULL CHECK (length(title) BETWEEN 1 AND 500),
139
+ starts_at TEXT NOT NULL,
140
+ ends_at TEXT NOT NULL,
141
+ location_name TEXT CHECK (location_name IS NULL OR length(location_name) <= 500),
142
+ staff_ref TEXT,
143
+ places INTEGER CHECK (places IS NULL OR places >= 0),
144
+ booked INTEGER NOT NULL CHECK (booked >= 0),
145
+ waiting INTEGER NOT NULL CHECK (waiting >= 0),
146
+ cancelled INTEGER NOT NULL CHECK (cancelled IN (0, 1)),
147
+ CHECK (ends_at > starts_at)
148
+ );
149
+ CREATE INDEX core_outside_class_starts ON core_outside_class (starts_at);
150
+ CREATE INDEX core_outside_class_staff ON core_outside_class (staff_ref) WHERE staff_ref IS NOT NULL;
151
+
152
+ CREATE TABLE core_outside_booking (
153
+ ref TEXT PRIMARY KEY CHECK (length(ref) BETWEEN 1 AND 600),
154
+ list_id TEXT NOT NULL,
155
+ class_ref TEXT NOT NULL,
156
+ person_ref TEXT NOT NULL,
157
+ state TEXT NOT NULL CHECK (
158
+ state IN ('booked', 'waiting', 'attended', 'late_cancelled', 'no_show', 'cancelled')
159
+ )
160
+ );
161
+ CREATE INDEX core_outside_booking_class ON core_outside_booking (class_ref);
162
+ CREATE INDEX core_outside_booking_person ON core_outside_booking (person_ref);
163
+
164
+ -- A coach as the booking system names them, matched to a staff account by address the same way.
165
+ CREATE TABLE core_outside_staff (
166
+ ref TEXT PRIMARY KEY CHECK (length(ref) BETWEEN 1 AND 600),
167
+ list_id TEXT NOT NULL,
168
+ display_name TEXT NOT NULL CHECK (length(display_name) BETWEEN 1 AND 500),
169
+ address TEXT CHECK (address IS NULL OR (address = lower(trim(address)) AND address <> '')),
170
+ active INTEGER NOT NULL CHECK (active IN (0, 1)),
171
+ user_id TEXT,
172
+ link_how TEXT CHECK (link_how IS NULL OR link_how IN ('address', 'staff')),
173
+ link_by TEXT,
174
+ link_why TEXT CHECK (link_why IS NULL OR length(link_why) <= 500),
175
+ linked_at TEXT,
176
+ CHECK ((link_how IS NULL) = (linked_at IS NULL)),
177
+ CHECK (link_how IS NOT NULL OR user_id IS NULL),
178
+ CHECK (link_how IS NOT 'address' OR user_id IS NOT NULL),
179
+ CHECK ((link_how IS 'staff') = (link_by IS NOT NULL))
180
+ );
181
+ CREATE INDEX core_outside_staff_user ON core_outside_staff (user_id) WHERE user_id IS NOT NULL;
182
+
183
+ -- An outside person or coach waiting for a person to link them: the candidates (Persons, or staff
184
+ -- accounts) and why nobody was linked. Kept while the outside row is; replaced at each load.
185
+ CREATE TABLE core_outside_match (
186
+ kind TEXT NOT NULL CHECK (kind IN ('person', 'staff')),
187
+ ref TEXT NOT NULL,
188
+ why TEXT NOT NULL CHECK (why IN ('several_people', 'shared_address', 'no_address')),
189
+ candidates TEXT NOT NULL DEFAULT '[]' CHECK (
190
+ json_valid(candidates) AND json_type(candidates) = 'array'
191
+ ),
192
+ created_at TEXT NOT NULL,
193
+ PRIMARY KEY (kind, ref)
194
+ );
195
+
196
+ -- An erased Person's outside ref and address, by hash only (as core_erased_address): the next
197
+ -- load keeps nothing of them. Written by erase (stage 65 step 10); read at every load.
198
+ CREATE TABLE core_outside_suppressed (
199
+ hash TEXT PRIMARY KEY,
200
+ kind TEXT NOT NULL CHECK (kind IN ('ref', 'address')),
201
+ created_at TEXT NOT NULL
202
+ );
203
+
204
+ -- Staff's "nobody here" for an outside person or coach (outside.unlink), by the hash of the ref
205
+ -- only: the next load links that ref by address never again, until a person links it by hand, and
206
+ -- keeps no row for it unless it is kept for another reason (a member, a live pass, booked).
207
+ CREATE TABLE core_outside_unlinked (
208
+ kind TEXT NOT NULL CHECK (kind IN ('person', 'staff')),
209
+ hash TEXT NOT NULL,
210
+ unlinked_by TEXT NOT NULL,
211
+ unlinked_at TEXT NOT NULL,
212
+ PRIMARY KEY (kind, hash)
213
+ );