@composable-os/cli 0.1.0-beta.10 → 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 (173) hide show
  1. package/dist/{accounts-X4CKM3HL.js → accounts-GOCNY6WI.js} +17 -14
  2. package/dist/app-RPRZMLU5.js +43 -0
  3. package/dist/app-upload-YJVEKZC6.js +198 -0
  4. package/dist/{blocks-YWDHEBOK.js → blocks-DE4RRWPJ.js} +207 -25
  5. package/dist/{chunk-VY64DMQO.js → chunk-2B6YUYZV.js} +23576 -11674
  6. package/dist/{run-RT2HVWMK.js → chunk-2CCHB37H.js} +9 -146
  7. package/dist/{chunk-DU6OIFOU.js → chunk-3OGDWKJ3.js} +530 -434
  8. package/dist/chunk-5D7BDF4Y.js +141 -0
  9. package/dist/{chunk-WO6D3XP3.js → chunk-6CM3WZG5.js} +13 -2470
  10. package/dist/chunk-CIBJGB5H.js +962 -0
  11. package/dist/chunk-EPDTPGZJ.js +330 -0
  12. package/dist/{chunk-LLULPZAJ.js → chunk-HAYF4G5V.js} +66 -23
  13. package/dist/chunk-HCG6T53D.js +61904 -0
  14. package/dist/chunk-JX4IFWNT.js +330 -0
  15. package/dist/{chunk-A4NTSYWV.js → chunk-KFHKNXGH.js} +786 -386
  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-LJFLENFC.js → chunk-Q6Y45DY6.js} +25 -2
  20. package/dist/{chunk-Y7JZFSTQ.js → chunk-QJZGYJVV.js} +823 -44
  21. package/dist/{chunk-6VXHPKGG.js → chunk-QQ3YSL5F.js} +172 -16
  22. package/dist/chunk-RGKJKC5T.js +876 -0
  23. package/dist/{chunk-IFG4FRW5.js → chunk-RSEWEFAV.js} +12 -2
  24. package/dist/{chunk-2ABSVMGB.js → chunk-SMJZIK2O.js} +1 -1
  25. package/dist/{chunk-CP6BXI6F.js → chunk-U72NEGCT.js} +8818 -3829
  26. package/dist/{chunk-WC5LHBBA.js → chunk-Z4IKF23A.js} +11 -7
  27. package/dist/chunk-Z7OVQ6BC.js +713 -0
  28. package/dist/{chunk-6UWRT4VM.js → chunk-ZJSFX4RD.js} +175 -3
  29. package/dist/{cli-2DFENR62.js → cli-OKQHHSOE.js} +17 -23
  30. package/dist/{cli-YIOVSKOD.js → cli-VYSD2S4Z.js} +231 -82
  31. package/dist/cli.js +5 -5
  32. package/dist/{client-repo-PRU3VCQQ.js → client-repo-NNUCHQ2Y.js} +7 -5
  33. package/dist/create.js +5 -5
  34. package/dist/{fields-2NC7LSYE.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-PUWD65BC.js → order-RKLGT74L.js} +2 -2
  40. package/dist/{person-WIQQ7ITU.js → person-YOZTFV4Q.js} +11 -11
  41. package/dist/plan-on-sale-H4F26YIN.js +26 -0
  42. package/dist/platform/migrations/0610_waitlists.sql +54 -0
  43. package/dist/platform/migrations/0630_subscription_changes.sql +122 -0
  44. package/dist/platform/migrations/0650_person_links.sql +90 -0
  45. package/dist/platform/migrations/0670_unmoved_guard.sql +52 -0
  46. package/dist/platform/migrations/0690_sellable_refund_due.sql +98 -0
  47. package/dist/platform/migrations/0730_practitioners.sql +79 -0
  48. package/dist/platform/migrations/0750_routine_mail.sql +98 -0
  49. package/dist/platform/migrations/0770_clear_fixed_role_grants.sql +64 -0
  50. package/dist/platform/migrations/0810_recurrence_exceptions.sql +66 -0
  51. package/dist/platform/migrations/0811_staff_time.sql +109 -0
  52. package/dist/platform/migrations/0812_staff_offers.sql +112 -0
  53. package/dist/platform/migrations/0813_pay_periods.sql +241 -0
  54. package/dist/platform/migrations/0814_staff_feed.sql +47 -0
  55. package/dist/platform/migrations/0815_checklists.sql +237 -0
  56. package/dist/platform/migrations/0816_checklist_schedules.sql +121 -0
  57. package/dist/platform/migrations/0817_checklist_day_window.sql +35 -0
  58. package/dist/platform/migrations/0850_agreements.sql +271 -0
  59. package/dist/platform/migrations/0851_agreement_acceptances.sql +387 -0
  60. package/dist/platform/migrations/0853_agreement_enforcement.sql +58 -0
  61. package/dist/platform/migrations/0854_agreement_copy.sql +149 -0
  62. package/dist/platform/migrations/0855_agreement_retention.sql +279 -0
  63. package/dist/platform/migrations/0856_subscription_cooling_off.sql +159 -0
  64. package/dist/platform/migrations/0857_agreement_copy_guardian.sql +337 -0
  65. package/dist/platform/migrations/0858_agreement_desk.sql +175 -0
  66. package/dist/platform/migrations/0910_routine_messages.sql +99 -0
  67. package/dist/platform/migrations/0911_texts.sql +60 -0
  68. package/dist/platform/migrations/0913_contact_consent.sql +93 -0
  69. package/dist/platform/migrations/0916_contact_mailbox_index.sql +29 -0
  70. package/dist/platform/migrations/0930_stock_movements.sql +95 -0
  71. package/dist/platform/migrations/0931_stock_low.sql +25 -0
  72. package/dist/platform/migrations/0932_order_delivery.sql +78 -0
  73. package/dist/platform/migrations/0950_order_adjustments.sql +111 -0
  74. package/dist/platform/migrations/0951_order_price_guards.sql +33 -0
  75. package/dist/platform/migrations/0952_order_paid_another_way.sql +194 -0
  76. package/dist/platform/migrations/0953_discount_uses.sql +183 -0
  77. package/dist/platform/migrations/0955_folded_addresses.sql +92 -0
  78. package/dist/platform/migrations/0970_published_text.sql +59 -0
  79. package/dist/platform/migrations/0971_publish_acks.sql +68 -0
  80. package/dist/platform/migrations/0990_customer_email_change.sql +65 -0
  81. package/dist/platform/migrations/1010_intake_links.sql +148 -0
  82. package/dist/platform/migrations/1030_invoices.sql +747 -0
  83. package/dist/platform/migrations/1031_invoice_orders.sql +327 -0
  84. package/dist/platform/migrations/1050_inbox_conversations.sql +83 -0
  85. package/dist/platform/migrations/1090_staff_hours.sql +43 -0
  86. package/dist/platform/migrations/1091_staff_time_margins.sql +12 -0
  87. package/dist/platform/migrations/1092_client_guard.sql +26 -0
  88. package/dist/platform/migrations/1095_thing_time.sql +70 -0
  89. package/dist/platform/migrations/1130_devices.sql +162 -0
  90. package/dist/platform/migrations/1135_person_codes.sql +88 -0
  91. package/dist/platform/migrations/1150_finance_periods.sql +316 -0
  92. package/dist/platform/migrations/1151_payouts.sql +202 -0
  93. package/dist/platform/migrations/1153_finance_money_dates.sql +18 -0
  94. package/dist/platform/migrations/1170_journeys.sql +318 -0
  95. package/dist/platform/migrations/1171_person_birthday.sql +52 -0
  96. package/dist/platform/migrations/1175_person_signals.sql +29 -0
  97. package/dist/platform/migrations/1190_restricted_values.sql +25 -0
  98. package/dist/platform/migrations/1230_note_levels.sql +113 -0
  99. package/dist/platform/migrations/1231_person_carers.sql +33 -0
  100. package/dist/platform/migrations/1232_note_kinds.sql +122 -0
  101. package/dist/platform/migrations/1270_time_entries.sql +145 -0
  102. package/dist/platform/migrations/1271_leave.sql +249 -0
  103. package/dist/platform/migrations/1273_time_entry_reopened.sql +21 -0
  104. package/dist/platform/migrations/1330_accounting.sql +260 -0
  105. package/dist/platform/migrations/1350_customer_feed.sql +24 -0
  106. package/dist/platform/migrations/1390_locations.sql +77 -0
  107. package/dist/platform/migrations/1410_outside_copy.sql +213 -0
  108. package/dist/platform/migrations/1430_inbox_activities.sql +231 -0
  109. package/dist/platform/migrations/1450_wrong_guesses.sql +23 -0
  110. package/dist/platform/sdk/acknowledge.ts +100 -0
  111. package/dist/platform/sdk/agreements.ts +185 -0
  112. package/dist/platform/sdk/allowance.ts +98 -8
  113. package/dist/platform/sdk/automation-check.ts +120 -9
  114. package/dist/platform/sdk/automation.ts +78 -10
  115. package/dist/platform/sdk/behaviour.ts +26 -6
  116. package/dist/platform/sdk/calendar-entry.ts +258 -0
  117. package/dist/platform/sdk/capacity.ts +103 -2
  118. package/dist/platform/sdk/checklists.ts +118 -0
  119. package/dist/platform/sdk/contract.ts +514 -27
  120. package/dist/platform/sdk/discount.ts +408 -0
  121. package/dist/platform/sdk/document.ts +4 -2
  122. package/dist/platform/sdk/field-types.ts +5 -0
  123. package/dist/platform/sdk/formula/compile.ts +10 -0
  124. package/dist/platform/sdk/index.ts +17 -0
  125. package/dist/platform/sdk/intake.ts +380 -0
  126. package/dist/platform/sdk/journey-signals.ts +215 -0
  127. package/dist/platform/sdk/manifest.ts +1313 -66
  128. package/dist/platform/sdk/mine.ts +161 -0
  129. package/dist/platform/sdk/occupies.ts +192 -0
  130. package/dist/platform/sdk/questions.ts +681 -0
  131. package/dist/platform/sdk/recurring.ts +90 -9
  132. package/dist/platform/sdk/restricted-fields.ts +129 -0
  133. package/dist/platform/sdk/rich-text.ts +112 -0
  134. package/dist/platform/sdk/routine-mail.ts +1272 -0
  135. package/dist/platform/sdk/runtime.ts +25 -12
  136. package/dist/platform/sdk/screens.ts +91 -0
  137. package/dist/platform/sdk/sellable.ts +595 -1
  138. package/dist/platform/sdk/slug.ts +7 -2
  139. package/dist/platform/sdk/staffed.ts +474 -0
  140. package/dist/platform/sdk/states.ts +220 -64
  141. package/dist/platform/sdk/stock.ts +332 -0
  142. package/dist/platform/sdk/team.ts +83 -0
  143. package/dist/platform/sdk/testing.ts +70 -6
  144. package/dist/platform/sdk/worked-on.ts +134 -0
  145. package/dist/{products-2H5DR232.js → products-GCQLSZ3U.js} +2 -2
  146. package/dist/run-7ID64RA6.js +65 -0
  147. package/dist/run-FH7OGGTV.js +131 -0
  148. package/dist/run-OYZIAP6M.js +3363 -0
  149. package/dist/{run-Y7T4TVOL.js → run-QOFRRSZV.js} +41 -60
  150. package/dist/{run-PZUCSDDL.js → run-TJAEWR5U.js} +21 -51
  151. package/dist/{src-XTOD7EXW.js → src-52VERUPK.js} +312 -4
  152. package/dist/src-PHRELWN5.js +40 -0
  153. package/dist/visibility-SZCINPP4.js +58 -0
  154. package/package.json +2 -2
  155. package/dist/chunk-6GDEDOV7.js +0 -157
  156. package/dist/chunk-6UPUF4ML.js +0 -29998
  157. package/dist/chunk-7CQGMVDO.js +0 -63
  158. package/dist/chunk-GYSGVT6R.js +0 -284
  159. package/dist/chunk-JJHHNV46.js +0 -227
  160. package/dist/chunk-K6T42YCH.js +0 -704
  161. package/dist/chunk-M7A6FR2Z.js +0 -2586
  162. package/dist/chunk-O2UIZHND.js +0 -249
  163. package/dist/chunk-Q4N55M2Z.js +0 -552
  164. package/dist/chunk-QGGJ2G6Z.js +0 -349
  165. package/dist/chunk-R4W5XAUD.js +0 -837
  166. package/dist/chunk-TQFGGSA4.js +0 -748
  167. package/dist/chunk-YJJHF65I.js +0 -759
  168. package/dist/chunk-Z3RMMB6S.js +0 -668
  169. package/dist/grants-QXMKYMGB.js +0 -32
  170. package/dist/invoke-I3NNNXVL.js +0 -39
  171. package/dist/plan-on-sale-NB774CQL.js +0 -26
  172. package/dist/src-GYS6NBQB.js +0 -44
  173. package/dist/visibility-DLGHU4YN.js +0 -33
@@ -0,0 +1,241 @@
1
+ -- Migration 0813: pay periods (stage 45 step 6, ADR 0627). Who is counted for pay, the mark that
2
+ -- a shift was not worked, and each locked pay period with its lines. No rates and no money are
3
+ -- held anywhere (decision 14): hours (as whole minutes) and counts only; payroll applies the rates.
4
+ --
5
+ -- All of it is about staff accounts and their pay: core tables in no rec_* view, so no dashboard,
6
+ -- query.sql, export.csv or the assistant's record reads reach them. They are read only through
7
+ -- the host's tool-only operations, by a person holding the pay permission (`rostering:pay`).
8
+ --
9
+ -- A locked period never changes, for every writer. The triggers below refuse it here; the
10
+ -- triggers the schema reconciler builds on each staffed object's own table refuse a change of the
11
+ -- person, times, kind or liveness of a shift inside a locked period (staffing/sql.ts). D1 runs
12
+ -- with recursive triggers off, so the rows an INSERT OR REPLACE or an UPDATE OR REPLACE deletes
13
+ -- fire no delete trigger: each table's BEFORE INSERT also refuses replacing a row it protects,
14
+ -- and its BEFORE UPDATE refuses every change it does not allow (a replace is one of them).
15
+
16
+ -- Who is counted (ADR 0627): `by_time`, `by_class` or `not_counted` (no row at all is not
17
+ -- counted), from a local date on, so a period totalled again uses the basis in force on each
18
+ -- shift's own date. Set by a person holding the pay permission.
19
+ CREATE TABLE core_staff_pay_basis (
20
+ account_id TEXT NOT NULL,
21
+ -- In force from this local date (the organisation's zone) until the next row's.
22
+ from_date TEXT NOT NULL CHECK (from_date GLOB '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'),
23
+ basis TEXT NOT NULL CHECK (basis IN ('by_time', 'by_class', 'not_counted')),
24
+ set_by TEXT NOT NULL,
25
+ set_at TEXT NOT NULL,
26
+ PRIMARY KEY (account_id, from_date)
27
+ ) WITHOUT ROWID;
28
+
29
+ -- A shift marked not worked (ADR 0627): it counts for nothing, with why. Set by a rosterer after
30
+ -- the shift started. `starts` is the shift's start when marked (unix seconds), read by the lock.
31
+ CREATE TABLE core_staff_not_worked (
32
+ object_type TEXT NOT NULL,
33
+ record_id TEXT NOT NULL,
34
+ starts INTEGER NOT NULL,
35
+ reason TEXT NOT NULL CHECK (length(reason) BETWEEN 1 AND 500),
36
+ marked_by TEXT NOT NULL,
37
+ marked_at TEXT NOT NULL,
38
+ PRIMARY KEY (object_type, record_id)
39
+ ) WITHOUT ROWID;
40
+
41
+ -- One version of one pay period (ADR 0627). Locking writes it `locking`, its lines, then
42
+ -- `locked`, in one batch; a locked version is reopened (with why) and never changed again;
43
+ -- locking that period again writes the next version and keeps the old. The dates are local in
44
+ -- `zone`; `starts` and `ends` are their local midnights as unix seconds (half-open), which the
45
+ -- shifts' locks read.
46
+ CREATE TABLE core_pay_period (
47
+ id TEXT PRIMARY KEY,
48
+ start_date TEXT NOT NULL,
49
+ end_date TEXT NOT NULL,
50
+ zone TEXT NOT NULL,
51
+ starts INTEGER NOT NULL,
52
+ ends INTEGER NOT NULL,
53
+ version INTEGER NOT NULL CHECK (version >= 1),
54
+ state TEXT NOT NULL CHECK (state IN ('locking', 'locked', 'reopened')),
55
+ locked_by TEXT NOT NULL,
56
+ locked_at TEXT NOT NULL,
57
+ reopened_by TEXT,
58
+ reopened_at TEXT,
59
+ reopen_reason TEXT CHECK (reopen_reason IS NULL OR length(reopen_reason) BETWEEN 1 AND 500),
60
+ CHECK (end_date >= start_date),
61
+ CHECK (ends > starts),
62
+ CHECK ((state = 'reopened') = (reopened_by IS NOT NULL AND reopened_at IS NOT NULL
63
+ AND reopen_reason IS NOT NULL)),
64
+ UNIQUE (start_date, version)
65
+ );
66
+
67
+ -- At most one version of a period is locked (or being locked) at a time.
68
+ CREATE UNIQUE INDEX core_pay_period_held ON core_pay_period (start_date)
69
+ WHERE state IN ('locking', 'locked');
70
+ -- What a shift's lock reads: the locked periods by end, so a write reads only the periods that
71
+ -- end after its shift starts (never every past period: the review of PR #287).
72
+ CREATE INDEX core_pay_period_state ON core_pay_period (state, ends, starts);
73
+
74
+ -- One line per shift counted in a locked period (ADR 0627): who, on what basis, the shift as it
75
+ -- was, the minutes counted (its own minutes plus the kind's before and after minutes, or none
76
+ -- when marked not worked), and its evidence. The person's name and email are kept as they were:
77
+ -- the business's payroll records, which it may have to keep.
78
+ CREATE TABLE core_pay_line (
79
+ period_id TEXT NOT NULL,
80
+ n INTEGER NOT NULL,
81
+ account_id TEXT NOT NULL,
82
+ name TEXT NOT NULL,
83
+ email TEXT NOT NULL,
84
+ basis TEXT NOT NULL CHECK (basis IN ('by_time', 'by_class')),
85
+ object_type TEXT NOT NULL,
86
+ record_id TEXT NOT NULL,
87
+ kind_object TEXT,
88
+ kind_record TEXT,
89
+ kind_name TEXT NOT NULL,
90
+ title TEXT NOT NULL,
91
+ place TEXT,
92
+ -- The shift's own time, unix seconds, and the local date and times in the period's zone.
93
+ starts INTEGER NOT NULL,
94
+ ends INTEGER NOT NULL,
95
+ local_date TEXT NOT NULL,
96
+ local_start TEXT NOT NULL,
97
+ local_end TEXT NOT NULL,
98
+ shift_minutes INTEGER NOT NULL CHECK (shift_minutes >= 0),
99
+ before_minutes INTEGER NOT NULL CHECK (before_minutes >= 0),
100
+ after_minutes INTEGER NOT NULL CHECK (after_minutes >= 0),
101
+ -- 0 when marked not worked; else shift + before + after.
102
+ counted_minutes INTEGER NOT NULL CHECK (counted_minutes >= 0),
103
+ -- 1, or 0 when marked not worked.
104
+ counted INTEGER NOT NULL CHECK (counted IN (0, 1)),
105
+ not_worked_reason TEXT,
106
+ -- Evidence: whether its list was taken (null: the kind of shift keeps none), who took it (names,
107
+ -- comma separated), and whether someone other than its person did (for review).
108
+ list_taken INTEGER CHECK (list_taken IS NULL OR list_taken IN (0, 1)),
109
+ marked_by TEXT,
110
+ flagged INTEGER NOT NULL CHECK (flagged IN (0, 1)),
111
+ PRIMARY KEY (period_id, n)
112
+ ) WITHOUT ROWID;
113
+
114
+ CREATE INDEX core_pay_line_account ON core_pay_line (period_id, account_id);
115
+
116
+ -- A period's rows: made `locking` with a version no other holds and dates that overlap no
117
+ -- locked period; never replaced (a REPLACE would delete a locked one without a trigger).
118
+ CREATE TRIGGER core_pay_period_insert
119
+ BEFORE INSERT ON core_pay_period
120
+ BEGIN
121
+ SELECT RAISE(ABORT, 'core_pay_period_fixed')
122
+ WHERE NEW.state != 'locking'
123
+ OR EXISTS (SELECT 1 FROM core_pay_period p WHERE p.id = NEW.id);
124
+ -- Two locks of one period at once: the second finds the first's version (it is held).
125
+ SELECT RAISE(ABORT, 'core_pay_period_held')
126
+ WHERE EXISTS (SELECT 1 FROM core_pay_period p
127
+ WHERE p.start_date = NEW.start_date AND p.version = NEW.version);
128
+ SELECT RAISE(ABORT, 'core_pay_period_overlap')
129
+ WHERE EXISTS (SELECT 1 FROM core_pay_period p WHERE p.state IN ('locking', 'locked')
130
+ AND p.starts < NEW.ends AND p.ends > NEW.starts);
131
+ END;
132
+
133
+ -- Only `locking` to `locked`, and `locked` to `reopened` with who, when and why; nothing else
134
+ -- ever changes, and a reopened version never changes again.
135
+ CREATE TRIGGER core_pay_period_update
136
+ BEFORE UPDATE ON core_pay_period
137
+ BEGIN
138
+ SELECT RAISE(ABORT, 'core_pay_period_fixed')
139
+ WHERE NEW.id IS NOT OLD.id OR NEW.start_date IS NOT OLD.start_date
140
+ OR NEW.end_date IS NOT OLD.end_date OR NEW.zone IS NOT OLD.zone
141
+ OR NEW.starts IS NOT OLD.starts OR NEW.ends IS NOT OLD.ends
142
+ OR NEW.version IS NOT OLD.version OR NEW.locked_by IS NOT OLD.locked_by
143
+ OR NEW.locked_at IS NOT OLD.locked_at
144
+ OR NOT ((OLD.state = 'locking' AND NEW.state = 'locked' AND NEW.reopened_by IS NULL)
145
+ OR (OLD.state = 'locked' AND NEW.state = 'reopened'));
146
+ END;
147
+
148
+ CREATE TRIGGER core_pay_period_delete
149
+ BEFORE DELETE ON core_pay_period
150
+ BEGIN
151
+ SELECT RAISE(ABORT, 'core_pay_period_fixed');
152
+ END;
153
+
154
+ -- Lines are written only into a version being locked (in its own batch), and never changed or
155
+ -- removed: a locked period's lines are its totals for good.
156
+ CREATE TRIGGER core_pay_line_insert
157
+ BEFORE INSERT ON core_pay_line
158
+ BEGIN
159
+ SELECT RAISE(ABORT, 'core_pay_period_fixed')
160
+ WHERE NOT EXISTS (SELECT 1 FROM core_pay_period p WHERE p.id = NEW.period_id
161
+ AND p.state = 'locking')
162
+ OR EXISTS (SELECT 1 FROM core_pay_line l WHERE l.period_id = NEW.period_id AND l.n = NEW.n);
163
+ END;
164
+
165
+ CREATE TRIGGER core_pay_line_update
166
+ BEFORE UPDATE ON core_pay_line
167
+ BEGIN
168
+ SELECT RAISE(ABORT, 'core_pay_period_fixed');
169
+ END;
170
+
171
+ CREATE TRIGGER core_pay_line_delete
172
+ BEFORE DELETE ON core_pay_line
173
+ BEGIN
174
+ SELECT RAISE(ABORT, 'core_pay_period_fixed');
175
+ END;
176
+
177
+ -- Who is counted cannot change for a date inside a locked period: a basis from a date on or
178
+ -- before a locked period's last date would change what that period totals.
179
+ CREATE TRIGGER core_staff_pay_basis_insert
180
+ BEFORE INSERT ON core_staff_pay_basis
181
+ BEGIN
182
+ SELECT RAISE(ABORT, 'core_pay_period_locked')
183
+ WHERE EXISTS (SELECT 1 FROM core_pay_period p WHERE p.state IN ('locking', 'locked')
184
+ AND p.end_date >= NEW.from_date);
185
+ END;
186
+
187
+ CREATE TRIGGER core_staff_pay_basis_update
188
+ BEFORE UPDATE ON core_staff_pay_basis
189
+ BEGIN
190
+ SELECT RAISE(ABORT, 'core_pay_period_locked')
191
+ WHERE EXISTS (SELECT 1 FROM core_pay_period p WHERE p.state IN ('locking', 'locked')
192
+ AND (p.end_date >= NEW.from_date OR p.end_date >= OLD.from_date));
193
+ END;
194
+
195
+ CREATE TRIGGER core_staff_pay_basis_delete
196
+ BEFORE DELETE ON core_staff_pay_basis
197
+ BEGIN
198
+ SELECT RAISE(ABORT, 'core_pay_period_locked')
199
+ WHERE EXISTS (SELECT 1 FROM core_pay_period p WHERE p.state IN ('locking', 'locked')
200
+ AND p.end_date >= OLD.from_date);
201
+ END;
202
+
203
+ -- A shift inside a locked period is not marked or unmarked (a REPLACE of a mark is an insert,
204
+ -- and the row it would replace is checked too).
205
+ CREATE TRIGGER core_staff_not_worked_insert
206
+ BEFORE INSERT ON core_staff_not_worked
207
+ BEGIN
208
+ SELECT RAISE(ABORT, 'core_pay_period_locked')
209
+ WHERE EXISTS (SELECT 1 FROM core_pay_period p WHERE p.state IN ('locking', 'locked')
210
+ AND (
211
+ (p.ends > NEW.starts AND p.starts <= NEW.starts)
212
+ OR EXISTS (SELECT 1 FROM core_staff_not_worked m WHERE m.object_type = NEW.object_type
213
+ AND m.record_id = NEW.record_id AND p.ends > m.starts AND p.starts <= m.starts)
214
+ ));
215
+ END;
216
+
217
+ CREATE TRIGGER core_staff_not_worked_update
218
+ BEFORE UPDATE ON core_staff_not_worked
219
+ BEGIN
220
+ SELECT RAISE(ABORT, 'core_pay_period_locked')
221
+ WHERE EXISTS (SELECT 1 FROM core_pay_period p WHERE p.state IN ('locking', 'locked')
222
+ AND ((p.ends > NEW.starts AND p.starts <= NEW.starts)
223
+ OR (p.ends > OLD.starts AND p.starts <= OLD.starts)
224
+ OR EXISTS (SELECT 1 FROM core_staff_not_worked m WHERE m.object_type = NEW.object_type
225
+ AND m.record_id = NEW.record_id AND p.ends > m.starts AND p.starts <= m.starts)));
226
+ END;
227
+
228
+ CREATE TRIGGER core_staff_not_worked_delete
229
+ BEFORE DELETE ON core_staff_not_worked
230
+ BEGIN
231
+ SELECT RAISE(ABORT, 'core_pay_period_locked')
232
+ WHERE EXISTS (SELECT 1 FROM core_pay_period p WHERE p.state IN ('locking', 'locked')
233
+ AND p.ends > OLD.starts AND p.starts <= OLD.starts);
234
+ END;
235
+
236
+ -- A period's marks, read by their shifts' start (a period's totals and the lock's guard).
237
+ CREATE INDEX core_staff_not_worked_starts ON core_staff_not_worked (starts);
238
+
239
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
240
+ UPDATE core_meta SET value = '813'
241
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 813;
@@ -0,0 +1,47 @@
1
+ -- Migration 0814: a staff member's private calendar feed (stage 45 step 5, ADR 0628).
2
+ --
3
+ -- A person's own shifts, as an iCalendar feed a calendar app subscribes to with no sign-in:
4
+ -- `GET /api/v1/staffing/feed/<token>.ics`. The token is the only secret: 32 random bytes, shown
5
+ -- to the person once when it is made, and stored here only as the SHA-256 of the token as written
6
+ -- in the address (64 hex characters), never the token. One feed per account: making a new one
7
+ -- (rotating) replaces the old, whose address then answers as any missing path does.
8
+ --
9
+ -- The feed answers only while its account is active and may still call the tool that made it
10
+ -- (`tool`, the extension tool's operation name, e.g. x.rostering.feedRotate): a person deactivated
11
+ -- or no longer holding the pack's permission gets nothing, whatever this table holds. Deactivation
12
+ -- and a change of role, pack role or grants that takes the tool away delete the row in their own
13
+ -- batch, so the address stays dead if the access comes back.
14
+ --
15
+ -- About a staff account, and a secret: in no rec_* view, so no member's SQL, export, dashboard or
16
+ -- the assistant's record reads see it. No trigger: nothing else is written from it.
17
+ CREATE TABLE core_staff_feed (
18
+ -- The staff account whose shifts it shows (core_user.id).
19
+ account_id TEXT PRIMARY KEY NOT NULL,
20
+ -- SHA-256 of the token, lowercase hex. A lookup by it reads one row.
21
+ token_hash TEXT NOT NULL UNIQUE CHECK (length(token_hash) = 64),
22
+ -- The tool that made it (x.<pack>.<tool>): the account must still be able to call it.
23
+ tool TEXT NOT NULL CHECK (length(tool) BETWEEN 1 AND 120),
24
+ created_at TEXT NOT NULL
25
+ ) WITHOUT ROWID;
26
+
27
+ -- A pack that made feeds (the review of PR #295, ADR 0628). Disabled or quarantined, its feeds are
28
+ -- paused: the rows stay and nothing answers (the fetch needs the pack active), and re-enabling
29
+ -- resumes them. Upgraded to a manifest without the tool that made a feed, or removed, its feeds
30
+ -- are deleted, so bringing the tool back never brings an old address back. Extension ids are
31
+ -- lowercase letters and digits (EXTENSION_ID_RE), so LIKE's wildcards cannot appear in one.
32
+ CREATE TRIGGER core_staff_feed_tool_removed
33
+ AFTER UPDATE OF manifest ON core_extensions
34
+ BEGIN
35
+ DELETE FROM core_staff_feed
36
+ WHERE tool LIKE 'x.' || NEW.id || '.%'
37
+ AND NOT EXISTS (
38
+ SELECT 1 FROM json_each(NEW.manifest, '$.tools') t
39
+ WHERE core_staff_feed.tool = 'x.' || NEW.id || '.' || t.key
40
+ );
41
+ END;
42
+
43
+ CREATE TRIGGER core_staff_feed_extension_removed
44
+ AFTER DELETE ON core_extensions
45
+ BEGIN
46
+ DELETE FROM core_staff_feed WHERE tool LIKE 'x.' || OLD.id || '.%';
47
+ END;
@@ -0,0 +1,237 @@
1
+ -- Migration 0815: checklists (stage 45 step 4b, ADR 0650).
2
+ --
3
+ -- A checklist is a list of steps a business writes once (a close-down clean, opening up) and
4
+ -- picks on something that happens (a duty, through a `checklist` field). Each record that is live
5
+ -- with one gets a **run**: the steps copied as they were when the run was made, so a template
6
+ -- changed later changes no run already made. Its person ticks each step (done, or not done with
7
+ -- why, and a note where the step asks for one); someone who rosters may tick for them, with a
8
+ -- reason, and the tick keeps both accounts. A run marked done never changes again; one not done by
9
+ -- its close and grace is closed by the host as missed.
10
+ --
11
+ -- About staff, and what they did: in no rec_* view, so no SQL, export, dashboard or the
12
+ -- assistant's record reads see it. Read only through the host's operations, which give a run to
13
+ -- its person and to those who roster.
14
+ --
15
+ -- **A closed run never changes**, for every writer and every statement shape. D1 runs with
16
+ -- recursive_triggers off, so a REPLACE (INSERT OR REPLACE, UPSERT's conflict, UPDATE OR REPLACE)
17
+ -- deletes the row it replaces without firing a delete trigger: every insert and update trigger
18
+ -- below therefore also checks the row it would replace. The host never inserts a run that is there
19
+ -- already (it reads first, `NOT EXISTS`), so these triggers refuse only what nothing should do, and
20
+ -- no host step can fail on them for ever.
21
+
22
+ -- A template. Not personal: every staff account reads it. Written by people holding
23
+ -- checklists:write; an AI's write waits for a person (the operation's review), so a template
24
+ -- staff are asked to follow is always one a person approved.
25
+ CREATE TABLE core_checklist (
26
+ id TEXT PRIMARY KEY NOT NULL,
27
+ name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 120),
28
+ -- What it is for (a line for whoever picks it).
29
+ purpose TEXT CHECK (purpose IS NULL OR length(purpose) <= 500),
30
+ -- Only an active template may be picked; one made inactive keeps the runs it gave.
31
+ active INTEGER NOT NULL DEFAULT 1 CHECK (active IN (0, 1)),
32
+ -- Bumped by every change of its name or steps; a run keeps the version it copied.
33
+ version INTEGER NOT NULL DEFAULT 1 CHECK (version >= 1),
34
+ created_by TEXT NOT NULL,
35
+ created_at TEXT NOT NULL,
36
+ updated_by TEXT NOT NULL,
37
+ updated_at TEXT NOT NULL
38
+ );
39
+
40
+ CREATE INDEX core_checklist_name ON core_checklist (active, name);
41
+
42
+ -- A template's steps as they are now, in order (position from 0). Replaced whole by a change of
43
+ -- the template; runs already made keep their own copy.
44
+ CREATE TABLE core_checklist_step (
45
+ checklist_id TEXT NOT NULL,
46
+ position INTEGER NOT NULL CHECK (position BETWEEN 0 AND 49),
47
+ text TEXT NOT NULL CHECK (length(text) BETWEEN 1 AND 300),
48
+ required INTEGER NOT NULL DEFAULT 1 CHECK (required IN (0, 1)),
49
+ -- Asks for a note: a tick of it as done needs one.
50
+ asks_note INTEGER NOT NULL DEFAULT 0 CHECK (asks_note IN (0, 1)),
51
+ PRIMARY KEY (checklist_id, position)
52
+ ) WITHOUT ROWID;
53
+
54
+ -- A run: one template, as it was, attached to one record (a shift). One per record, ever.
55
+ CREATE TABLE core_checklist_run (
56
+ id TEXT PRIMARY KEY NOT NULL,
57
+ object_type TEXT NOT NULL,
58
+ record_id TEXT NOT NULL,
59
+ checklist_id TEXT NOT NULL,
60
+ version INTEGER NOT NULL,
61
+ -- The template's name and steps as they were when the run was made: a JSON array of
62
+ -- { "text", "required", "note" }. Never changed.
63
+ name TEXT NOT NULL,
64
+ steps TEXT NOT NULL CHECK (json_valid(steps) AND json_type(steps) = 'array'
65
+ AND json_array_length(steps) BETWEEN 1 AND 50),
66
+ -- Whose run it is: the record's person, kept current by the host while it is open (a shift
67
+ -- reassigned moves to its new person; the ticks keep who made them), fixed when it closes.
68
+ account_id TEXT,
69
+ -- When it may be ticked (unix seconds): the record's opening and closing times, kept current by
70
+ -- the host while it is open. Ticks are taken until `closes` and the grace after it.
71
+ opens INTEGER NOT NULL,
72
+ closes INTEGER NOT NULL,
73
+ state TEXT NOT NULL CHECK (state IN ('open', 'done', 'missed', 'cancelled')),
74
+ made_at TEXT NOT NULL,
75
+ -- The record left its live states (cancelled, the Trash, removed) after the run opened: when,
76
+ -- and by whom when that was someone who rosters (or the host). Set by the host's record writes;
77
+ -- cleared if it comes back. At the close, a record ended by someone who does not roster is
78
+ -- missed, not cancelled.
79
+ ended_at TEXT,
80
+ ended_by TEXT,
81
+ -- Closed: when; by whom (the person, someone who rosters for them, whoever cancelled it, or
82
+ -- null: the host); and, marked done for someone, why.
83
+ closed_at TEXT,
84
+ closed_by TEXT,
85
+ closed_why TEXT CHECK (closed_why IS NULL OR length(closed_why) <= 500),
86
+ -- Missed: the positions of the required steps nobody ticked, as a JSON array.
87
+ missed_steps TEXT,
88
+ UNIQUE (object_type, record_id)
89
+ );
90
+
91
+ -- The open runs by their close (the minute cron's read), a person's runs, a template's.
92
+ CREATE INDEX core_checklist_run_open ON core_checklist_run (closes) WHERE state = 'open';
93
+ CREATE INDEX core_checklist_run_opens ON core_checklist_run (opens);
94
+ CREATE INDEX core_checklist_run_account ON core_checklist_run (account_id, opens);
95
+ CREATE INDEX core_checklist_run_template ON core_checklist_run (checklist_id, opens);
96
+
97
+ -- Ticks of a run's steps (position from 0, as in the run's steps). **A tick is never changed or
98
+ -- removed**: a later tick of the same step (by the person, or by someone who rosters for them) is a
99
+ -- new row, and the latest counts; a clear is a `cleared` tick. So every tick anyone made stays,
100
+ -- the earlier ones readable as history by those who roster and by their authors.
101
+ CREATE TABLE core_checklist_tick (
102
+ -- Time-ordered (a ULID): with `ticked_at`, the latest of a step is the last.
103
+ id TEXT PRIMARY KEY NOT NULL,
104
+ run_id TEXT NOT NULL,
105
+ position INTEGER NOT NULL CHECK (position BETWEEN 0 AND 49),
106
+ -- done; not done, with why; or cleared (the step counts as not ticked again).
107
+ state TEXT NOT NULL CHECK (state IN ('done', 'not_done', 'cleared')),
108
+ why TEXT CHECK (why IS NULL OR length(why) BETWEEN 1 AND 500),
109
+ -- What the person wrote on the step. Private: its author and those who roster only.
110
+ note TEXT CHECK (note IS NULL OR length(note) BETWEEN 1 AND 1000),
111
+ -- Who ticked it, and whose run it was for. On someone's behalf: someone who rosters ticked it
112
+ -- for the run's person, with why; never shown or counted as the person's own tick.
113
+ ticked_by TEXT NOT NULL,
114
+ ticked_for TEXT NOT NULL,
115
+ on_behalf INTEGER NOT NULL CHECK (on_behalf IN (0, 1)),
116
+ on_behalf_why TEXT CHECK (on_behalf_why IS NULL OR length(on_behalf_why) BETWEEN 1 AND 500),
117
+ ticked_at TEXT NOT NULL,
118
+ CHECK (state != 'not_done' OR why IS NOT NULL),
119
+ CHECK ((on_behalf = 0 AND ticked_by = ticked_for AND on_behalf_why IS NULL)
120
+ OR (on_behalf = 1 AND ticked_by != ticked_for AND on_behalf_why IS NOT NULL))
121
+ );
122
+
123
+ -- A run's ticks, step by step, latest last.
124
+ CREATE INDEX core_checklist_tick_run ON core_checklist_tick (run_id, position, ticked_at, id);
125
+
126
+ -- A correction: a note someone who rosters adds beside a closed run. Never changed or removed.
127
+ CREATE TABLE core_checklist_note (
128
+ id TEXT PRIMARY KEY NOT NULL,
129
+ run_id TEXT NOT NULL,
130
+ text TEXT NOT NULL CHECK (length(text) BETWEEN 1 AND 1000),
131
+ written_by TEXT NOT NULL,
132
+ written_at TEXT NOT NULL
133
+ );
134
+
135
+ CREATE INDEX core_checklist_note_run ON core_checklist_note (run_id, written_at);
136
+
137
+ -- A run is inserted once, open, and never over another (a REPLACE would delete a closed run, or an
138
+ -- open one's ticks' run, without a delete trigger).
139
+ CREATE TRIGGER core_checklist_run_insert
140
+ BEFORE INSERT ON core_checklist_run
141
+ BEGIN
142
+ SELECT RAISE(ABORT, 'core_checklist_fixed')
143
+ WHERE NEW.state != 'open' OR NEW.closed_at IS NOT NULL OR NEW.closed_by IS NOT NULL
144
+ OR NEW.missed_steps IS NOT NULL
145
+ OR EXISTS (SELECT 1 FROM core_checklist_run r WHERE r.id = NEW.id
146
+ OR (r.object_type = NEW.object_type AND r.record_id = NEW.record_id));
147
+ END;
148
+
149
+ -- A closed run never changes. An open one keeps what it is (its record, template and steps); the
150
+ -- host moves its person and times, marks who ended its record, and closes it: done only with
151
+ -- every required step's latest tick done or not done.
152
+ CREATE TRIGGER core_checklist_run_update
153
+ BEFORE UPDATE ON core_checklist_run
154
+ BEGIN
155
+ SELECT RAISE(ABORT, 'core_checklist_closed') WHERE OLD.state != 'open';
156
+ SELECT RAISE(ABORT, 'core_checklist_fixed')
157
+ WHERE NEW.id IS NOT OLD.id OR NEW.object_type IS NOT OLD.object_type
158
+ OR NEW.record_id IS NOT OLD.record_id OR NEW.checklist_id IS NOT OLD.checklist_id
159
+ OR NEW.version IS NOT OLD.version OR NEW.name IS NOT OLD.name OR NEW.steps IS NOT OLD.steps
160
+ OR NEW.made_at IS NOT OLD.made_at
161
+ OR (NEW.state = 'open' AND (NEW.closed_at IS NOT NULL OR NEW.closed_by IS NOT NULL
162
+ OR NEW.missed_steps IS NOT NULL))
163
+ OR (NEW.state != 'open' AND NEW.closed_at IS NULL);
164
+ SELECT RAISE(ABORT, 'core_checklist_incomplete')
165
+ WHERE NEW.state = 'done' AND EXISTS (
166
+ SELECT 1 FROM json_each(NEW.steps) s
167
+ WHERE json_extract(s.value, '$.required') = 1
168
+ AND coalesce((SELECT t.state FROM core_checklist_tick t
169
+ WHERE t.run_id = NEW.id AND t.position = s.key
170
+ ORDER BY t.ticked_at DESC, t.id DESC LIMIT 1), 'cleared') = 'cleared');
171
+ END;
172
+
173
+ -- A run is never removed once closed or ticked.
174
+ CREATE TRIGGER core_checklist_run_delete
175
+ BEFORE DELETE ON core_checklist_run
176
+ BEGIN
177
+ SELECT RAISE(ABORT, 'core_checklist_closed')
178
+ WHERE OLD.state != 'open'
179
+ OR EXISTS (SELECT 1 FROM core_checklist_tick t WHERE t.run_id = OLD.id);
180
+ END;
181
+
182
+ -- A tick is added to an open run's step only, never over another tick (a REPLACE of its id); a
183
+ -- step that asks for a note is not ticked done without one.
184
+ CREATE TRIGGER core_checklist_tick_insert
185
+ BEFORE INSERT ON core_checklist_tick
186
+ BEGIN
187
+ SELECT RAISE(ABORT, 'core_checklist_closed')
188
+ WHERE NOT EXISTS (SELECT 1 FROM core_checklist_run r WHERE r.id = NEW.run_id
189
+ AND r.state = 'open');
190
+ SELECT RAISE(ABORT, 'core_checklist_fixed')
191
+ WHERE EXISTS (SELECT 1 FROM core_checklist_tick t WHERE t.id = NEW.id)
192
+ OR NEW.position >= (SELECT json_array_length(r.steps) FROM core_checklist_run r
193
+ WHERE r.id = NEW.run_id);
194
+ -- At most 50 ticks of one step of one run: the history every read carries stays bounded.
195
+ SELECT RAISE(ABORT, 'core_checklist_too_many_ticks')
196
+ WHERE (SELECT count(*) FROM core_checklist_tick t WHERE t.run_id = NEW.run_id
197
+ AND t.position = NEW.position) >= 50;
198
+ SELECT RAISE(ABORT, 'core_checklist_note_needed')
199
+ WHERE NEW.state = 'done' AND (NEW.note IS NULL OR trim(NEW.note) = '')
200
+ AND (SELECT json_extract(r.steps, '$[' || NEW.position || '].note') FROM core_checklist_run r
201
+ WHERE r.id = NEW.run_id) = 1;
202
+ END;
203
+
204
+ -- A tick is never changed or removed, on an open run or a closed one.
205
+ CREATE TRIGGER core_checklist_tick_update
206
+ BEFORE UPDATE ON core_checklist_tick
207
+ BEGIN
208
+ SELECT RAISE(ABORT, 'core_checklist_fixed');
209
+ END;
210
+
211
+ CREATE TRIGGER core_checklist_tick_delete
212
+ BEFORE DELETE ON core_checklist_tick
213
+ BEGIN
214
+ SELECT RAISE(ABORT, 'core_checklist_fixed');
215
+ END;
216
+
217
+ -- A correction is added beside a closed run, and never changed, replaced or removed.
218
+ CREATE TRIGGER core_checklist_note_insert
219
+ BEFORE INSERT ON core_checklist_note
220
+ BEGIN
221
+ SELECT RAISE(ABORT, 'core_checklist_fixed')
222
+ WHERE EXISTS (SELECT 1 FROM core_checklist_note n WHERE n.id = NEW.id)
223
+ OR NOT EXISTS (SELECT 1 FROM core_checklist_run r WHERE r.id = NEW.run_id
224
+ AND r.state != 'open');
225
+ END;
226
+
227
+ CREATE TRIGGER core_checklist_note_update
228
+ BEFORE UPDATE ON core_checklist_note
229
+ BEGIN
230
+ SELECT RAISE(ABORT, 'core_checklist_fixed');
231
+ END;
232
+
233
+ CREATE TRIGGER core_checklist_note_delete
234
+ BEFORE DELETE ON core_checklist_note
235
+ BEGIN
236
+ SELECT RAISE(ABORT, 'core_checklist_fixed');
237
+ END;
@@ -0,0 +1,121 @@
1
+ -- Migration 0816: checklists on their own, without a roster (ADR 0651; a follow-on to stage 45
2
+ -- step 4b, ADR 0650).
3
+ --
4
+ -- A checklist template may now have a standing schedule of its own: the days of the week it is
5
+ -- due (in the business's time zone), a time it is due by, and who may do it: everyone on staff,
6
+ -- the people holding a role (a pack role too: a cleaner, a coach), or the people granted a
7
+ -- permission. Each due day gets one run (`object_type` 'checklist.day', `record_id`
8
+ -- '<checklist id>:<YYYY-MM-DD>', so the run table's own one-per-record rule makes it one a day),
9
+ -- open from the start of that local day to the start of the next. Anyone who may do it ticks it;
10
+ -- whoever ticks the last step finishes it; one not finished by the end of its day is closed by the
11
+ -- host as missed. Everything migration 0815 holds for a run (a closed run never changes, ticks are
12
+ -- insert-only, done only once every required step is ticked) holds for these too.
13
+ --
14
+ -- **Only someone allowed ticks**, for every writer: a day run copies who may do it from its
15
+ -- template when it is made (`doers_kind`, `doers_value`, fixed from then on), and the triggers
16
+ -- below refuse a tick for anyone else, a tick on someone's behalf by anyone who does not manage
17
+ -- checklists (an admin, or an account granted checklists:write), and a finish by anyone else.
18
+
19
+ -- The template's schedule. `days` is a JSON array of ISO weekdays (1 Monday … 7 Sunday), or null:
20
+ -- no schedule (a duty's checklist only, as before). `due_by` is a local time (HH:MM), or null.
21
+ ALTER TABLE core_checklist ADD COLUMN days TEXT
22
+ CHECK (days IS NULL OR (json_valid(days) AND json_type(days) = 'array'
23
+ AND json_array_length(days) BETWEEN 1 AND 7));
24
+ ALTER TABLE core_checklist ADD COLUMN due_by TEXT
25
+ CHECK (due_by IS NULL OR (length(due_by) = 5 AND due_by GLOB '[0-2][0-9]:[0-5][0-9]'));
26
+ -- Who may do it: 'staff' (every staff account that may write), 'role' (an account with the role
27
+ -- or pack role `doers_value`), 'grant' (an account granted the permission `doers_value`).
28
+ ALTER TABLE core_checklist ADD COLUMN doers_kind TEXT
29
+ CHECK (doers_kind IS NULL OR doers_kind IN ('staff', 'role', 'grant'));
30
+ ALTER TABLE core_checklist ADD COLUMN doers_value TEXT
31
+ CHECK (doers_value IS NULL OR length(doers_value) BETWEEN 1 AND 81);
32
+
33
+ -- A day run's copy of who may do it and when it is due by (unix seconds). Null on a record's run.
34
+ ALTER TABLE core_checklist_run ADD COLUMN doers_kind TEXT
35
+ CHECK (doers_kind IS NULL OR doers_kind IN ('staff', 'role', 'grant'));
36
+ ALTER TABLE core_checklist_run ADD COLUMN doers_value TEXT;
37
+ ALTER TABLE core_checklist_run ADD COLUMN due_at INTEGER;
38
+
39
+ -- A day run is inserted with who may do it, and a record's run without.
40
+ CREATE TRIGGER core_checklist_day_run_insert
41
+ BEFORE INSERT ON core_checklist_run
42
+ BEGIN
43
+ SELECT RAISE(ABORT, 'core_checklist_fixed')
44
+ WHERE (NEW.object_type = 'checklist.day') != (NEW.doers_kind IS NOT NULL)
45
+ OR (NEW.doers_kind = 'staff' AND NEW.doers_value IS NOT NULL)
46
+ OR (NEW.doers_kind IN ('role', 'grant') AND NEW.doers_value IS NULL)
47
+ OR (NEW.object_type != 'checklist.day' AND NEW.due_at IS NOT NULL)
48
+ OR (NEW.object_type = 'checklist.day' AND NEW.account_id IS NOT NULL);
49
+ END;
50
+
51
+ -- Who may do a day run, who manages checklists, and what of a day run never changes. Its day,
52
+ -- who may do it and when it is due are fixed; while open it is nobody's own (no account); it is
53
+ -- finished by someone who may do it, for themselves, or by someone who manages checklists for
54
+ -- someone who may, with why.
55
+ CREATE TRIGGER core_checklist_day_run_update
56
+ BEFORE UPDATE ON core_checklist_run
57
+ WHEN OLD.object_type = 'checklist.day'
58
+ BEGIN
59
+ -- A closed run never changes (as migration 0815's own trigger says, whichever fires first).
60
+ SELECT RAISE(ABORT, 'core_checklist_closed') WHERE OLD.state != 'open';
61
+ SELECT RAISE(ABORT, 'core_checklist_fixed')
62
+ WHERE NEW.doers_kind IS NOT OLD.doers_kind OR NEW.doers_value IS NOT OLD.doers_value
63
+ OR NEW.due_at IS NOT OLD.due_at OR NEW.opens IS NOT OLD.opens OR NEW.closes IS NOT OLD.closes
64
+ OR NEW.ended_at IS NOT NULL OR NEW.ended_by IS NOT NULL
65
+ OR (NEW.state = 'open' AND NEW.account_id IS NOT NULL)
66
+ OR NEW.state = 'cancelled';
67
+ SELECT RAISE(ABORT, 'core_checklist_not_allowed')
68
+ WHERE NEW.state = 'done' AND NOT (
69
+ -- Someone who may do it, finishing it themselves.
70
+ (NEW.account_id = NEW.closed_by AND NEW.closed_why IS NULL AND EXISTS (
71
+ SELECT 1 FROM core_user u WHERE u.id = NEW.closed_by AND u.kind = 'human'
72
+ AND u.deactivated_at IS NULL AND u.support_grant_id IS NULL
73
+ AND (u.pack_role IS NOT NULL OR u.role != 'viewer')
74
+ AND (OLD.doers_kind = 'staff'
75
+ OR (OLD.doers_kind = 'role' AND (u.pack_role = OLD.doers_value
76
+ OR (u.pack_role IS NULL AND u.role = OLD.doers_value)))
77
+ OR (OLD.doers_kind = 'grant' AND json_valid(u.granted_permissions) AND EXISTS (
78
+ SELECT 1 FROM json_each(u.granted_permissions) g WHERE g.value = OLD.doers_value)))))
79
+ -- Someone who manages checklists, for someone who may do it, with why.
80
+ OR (NEW.account_id IS NOT NEW.closed_by AND NEW.closed_why IS NOT NULL
81
+ AND EXISTS (SELECT 1 FROM core_user m WHERE m.id = NEW.closed_by AND m.kind = 'human'
82
+ AND m.deactivated_at IS NULL AND m.pack_role IS NULL
83
+ AND (m.role = 'admin' OR (json_valid(m.granted_permissions) AND EXISTS (
84
+ SELECT 1 FROM json_each(m.granted_permissions) g WHERE g.value = 'checklists:write'))))
85
+ AND EXISTS (SELECT 1 FROM core_user u WHERE u.id = NEW.account_id AND u.kind = 'human'
86
+ AND u.deactivated_at IS NULL AND u.support_grant_id IS NULL
87
+ AND (u.pack_role IS NOT NULL OR u.role != 'viewer')
88
+ AND (OLD.doers_kind = 'staff'
89
+ OR (OLD.doers_kind = 'role' AND (u.pack_role = OLD.doers_value
90
+ OR (u.pack_role IS NULL AND u.role = OLD.doers_value)))
91
+ OR (OLD.doers_kind = 'grant' AND json_valid(u.granted_permissions) AND EXISTS (
92
+ SELECT 1 FROM json_each(u.granted_permissions) g WHERE g.value = OLD.doers_value)))))
93
+ );
94
+ END;
95
+
96
+ -- A day run's tick is for someone who may do it, made by them, or for them by someone who
97
+ -- manages checklists.
98
+ CREATE TRIGGER core_checklist_day_tick_insert
99
+ BEFORE INSERT ON core_checklist_tick
100
+ WHEN EXISTS (SELECT 1 FROM core_checklist_run r WHERE r.id = NEW.run_id
101
+ AND r.object_type = 'checklist.day')
102
+ BEGIN
103
+ SELECT RAISE(ABORT, 'core_checklist_not_allowed')
104
+ WHERE NOT EXISTS (
105
+ SELECT 1 FROM core_checklist_run r JOIN core_user u ON u.id = NEW.ticked_for
106
+ WHERE r.id = NEW.run_id AND u.kind = 'human' AND u.deactivated_at IS NULL
107
+ AND u.support_grant_id IS NULL AND (u.pack_role IS NOT NULL OR u.role != 'viewer')
108
+ AND (r.doers_kind = 'staff'
109
+ OR (r.doers_kind = 'role' AND (u.pack_role = r.doers_value
110
+ OR (u.pack_role IS NULL AND u.role = r.doers_value)))
111
+ OR (r.doers_kind = 'grant' AND json_valid(u.granted_permissions) AND EXISTS (
112
+ SELECT 1 FROM json_each(u.granted_permissions) g WHERE g.value = r.doers_value))))
113
+ OR (NEW.on_behalf = 1 AND NOT EXISTS (
114
+ SELECT 1 FROM core_user m WHERE m.id = NEW.ticked_by AND m.kind = 'human'
115
+ AND m.deactivated_at IS NULL AND m.pack_role IS NULL
116
+ AND (m.role = 'admin' OR (json_valid(m.granted_permissions) AND EXISTS (
117
+ SELECT 1 FROM json_each(m.granted_permissions) g WHERE g.value = 'checklists:write')))));
118
+ END;
119
+
120
+ -- The schedules the minute cron reads: the active templates that have one.
121
+ CREATE INDEX core_checklist_scheduled ON core_checklist (active) WHERE days IS NOT NULL;