@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.
- package/dist/{accounts-UHYLCLEY.js → accounts-GOCNY6WI.js} +17 -13
- package/dist/app-RPRZMLU5.js +43 -0
- package/dist/app-upload-YJVEKZC6.js +198 -0
- package/dist/{blocks-5IFU25J6.js → blocks-DE4RRWPJ.js} +207 -25
- package/dist/{chunk-XIDLW3EP.js → chunk-2B6YUYZV.js} +23499 -11766
- package/dist/{run-7P4RQALH.js → chunk-2CCHB37H.js} +9 -146
- package/dist/{chunk-DU6OIFOU.js → chunk-3OGDWKJ3.js} +530 -434
- package/dist/{chunk-DHS2CV2M.js → chunk-5D7BDF4Y.js} +15 -13
- package/dist/{chunk-ZAWES2UD.js → chunk-6CM3WZG5.js} +13 -2483
- package/dist/{chunk-7D4GKZOX.js → chunk-CIBJGB5H.js} +422 -75
- package/dist/chunk-EPDTPGZJ.js +330 -0
- package/dist/{chunk-LAAL4YGR.js → chunk-HAYF4G5V.js} +66 -23
- package/dist/chunk-HCG6T53D.js +61904 -0
- package/dist/{chunk-K34UKWEI.js → chunk-JX4IFWNT.js} +64 -32
- package/dist/{chunk-6XTU2MTP.js → chunk-KFHKNXGH.js} +632 -418
- package/dist/chunk-KMFMD52Z.js +13276 -0
- package/dist/{chunk-E4WCYTCT.js → chunk-LX2TTLVY.js} +174 -55
- package/dist/{chunk-GYTEW66E.js → chunk-O3R4FS2J.js} +1 -1
- package/dist/{chunk-E64SG24J.js → chunk-Q6Y45DY6.js} +25 -2
- package/dist/{chunk-MBIKVRAC.js → chunk-QJZGYJVV.js} +823 -44
- package/dist/{chunk-PDD2ETVF.js → chunk-QQ3YSL5F.js} +172 -16
- package/dist/chunk-RGKJKC5T.js +876 -0
- package/dist/{chunk-YFTLIEWU.js → chunk-RSEWEFAV.js} +12 -2
- package/dist/{chunk-2ABSVMGB.js → chunk-SMJZIK2O.js} +1 -1
- package/dist/{chunk-LO6WGU5Y.js → chunk-U72NEGCT.js} +8089 -3464
- package/dist/{chunk-P6UWZ6QQ.js → chunk-Z4IKF23A.js} +11 -7
- package/dist/{chunk-2IVEVPMY.js → chunk-Z7OVQ6BC.js} +105 -61
- package/dist/{chunk-6UWRT4VM.js → chunk-ZJSFX4RD.js} +175 -3
- package/dist/{cli-5UNY5G5L.js → cli-OKQHHSOE.js} +17 -25
- package/dist/{cli-UYZMVQMR.js → cli-VYSD2S4Z.js} +227 -81
- package/dist/cli.js +5 -5
- package/dist/{client-repo-SE4EKG6W.js → client-repo-NNUCHQ2Y.js} +7 -5
- package/dist/create.js +5 -5
- package/dist/{fields-FTWWMD7N.js → fields-GTHE6TTS.js} +27 -3
- package/dist/gateway-DPPEQZT4.js +25 -0
- package/dist/grants-6JY4XZO7.js +27 -0
- package/dist/invoke-MRONRZ5Q.js +33 -0
- package/dist/moves-UAUCMWL5.js +36 -0
- package/dist/{order-U4KZLLSJ.js → order-RKLGT74L.js} +2 -2
- package/dist/{person-UHLVBVLR.js → person-YOZTFV4Q.js} +11 -10
- package/dist/plan-on-sale-H4F26YIN.js +26 -0
- package/dist/platform/migrations/0730_practitioners.sql +79 -0
- package/dist/platform/migrations/0750_routine_mail.sql +98 -0
- package/dist/platform/migrations/0770_clear_fixed_role_grants.sql +64 -0
- package/dist/platform/migrations/0810_recurrence_exceptions.sql +66 -0
- package/dist/platform/migrations/0811_staff_time.sql +109 -0
- package/dist/platform/migrations/0812_staff_offers.sql +112 -0
- package/dist/platform/migrations/0813_pay_periods.sql +241 -0
- package/dist/platform/migrations/0814_staff_feed.sql +47 -0
- package/dist/platform/migrations/0815_checklists.sql +237 -0
- package/dist/platform/migrations/0816_checklist_schedules.sql +121 -0
- package/dist/platform/migrations/0817_checklist_day_window.sql +35 -0
- package/dist/platform/migrations/0850_agreements.sql +271 -0
- package/dist/platform/migrations/0851_agreement_acceptances.sql +387 -0
- package/dist/platform/migrations/0853_agreement_enforcement.sql +58 -0
- package/dist/platform/migrations/0854_agreement_copy.sql +149 -0
- package/dist/platform/migrations/0855_agreement_retention.sql +279 -0
- package/dist/platform/migrations/0856_subscription_cooling_off.sql +159 -0
- package/dist/platform/migrations/0857_agreement_copy_guardian.sql +337 -0
- package/dist/platform/migrations/0858_agreement_desk.sql +175 -0
- package/dist/platform/migrations/0910_routine_messages.sql +99 -0
- package/dist/platform/migrations/0911_texts.sql +60 -0
- package/dist/platform/migrations/0913_contact_consent.sql +93 -0
- package/dist/platform/migrations/0916_contact_mailbox_index.sql +29 -0
- package/dist/platform/migrations/0930_stock_movements.sql +95 -0
- package/dist/platform/migrations/0931_stock_low.sql +25 -0
- package/dist/platform/migrations/0932_order_delivery.sql +78 -0
- package/dist/platform/migrations/0950_order_adjustments.sql +111 -0
- package/dist/platform/migrations/0951_order_price_guards.sql +33 -0
- package/dist/platform/migrations/0952_order_paid_another_way.sql +194 -0
- package/dist/platform/migrations/0953_discount_uses.sql +183 -0
- package/dist/platform/migrations/0955_folded_addresses.sql +92 -0
- package/dist/platform/migrations/0970_published_text.sql +59 -0
- package/dist/platform/migrations/0971_publish_acks.sql +68 -0
- package/dist/platform/migrations/0990_customer_email_change.sql +65 -0
- package/dist/platform/migrations/1010_intake_links.sql +148 -0
- package/dist/platform/migrations/1030_invoices.sql +747 -0
- package/dist/platform/migrations/1031_invoice_orders.sql +327 -0
- package/dist/platform/migrations/1050_inbox_conversations.sql +83 -0
- package/dist/platform/migrations/1090_staff_hours.sql +43 -0
- package/dist/platform/migrations/1091_staff_time_margins.sql +12 -0
- package/dist/platform/migrations/1092_client_guard.sql +26 -0
- package/dist/platform/migrations/1095_thing_time.sql +70 -0
- package/dist/platform/migrations/1130_devices.sql +162 -0
- package/dist/platform/migrations/1135_person_codes.sql +88 -0
- package/dist/platform/migrations/1150_finance_periods.sql +316 -0
- package/dist/platform/migrations/1151_payouts.sql +202 -0
- package/dist/platform/migrations/1153_finance_money_dates.sql +18 -0
- package/dist/platform/migrations/1170_journeys.sql +318 -0
- package/dist/platform/migrations/1171_person_birthday.sql +52 -0
- package/dist/platform/migrations/1175_person_signals.sql +29 -0
- package/dist/platform/migrations/1190_restricted_values.sql +25 -0
- package/dist/platform/migrations/1230_note_levels.sql +113 -0
- package/dist/platform/migrations/1231_person_carers.sql +33 -0
- package/dist/platform/migrations/1232_note_kinds.sql +122 -0
- package/dist/platform/migrations/1270_time_entries.sql +145 -0
- package/dist/platform/migrations/1271_leave.sql +249 -0
- package/dist/platform/migrations/1273_time_entry_reopened.sql +21 -0
- package/dist/platform/migrations/1330_accounting.sql +260 -0
- package/dist/platform/migrations/1350_customer_feed.sql +24 -0
- package/dist/platform/migrations/1390_locations.sql +77 -0
- package/dist/platform/migrations/1410_outside_copy.sql +213 -0
- package/dist/platform/migrations/1430_inbox_activities.sql +231 -0
- package/dist/platform/migrations/1450_wrong_guesses.sql +23 -0
- package/dist/platform/sdk/acknowledge.ts +100 -0
- package/dist/platform/sdk/agreements.ts +185 -0
- package/dist/platform/sdk/allowance.ts +98 -8
- package/dist/platform/sdk/automation-check.ts +120 -9
- package/dist/platform/sdk/automation.ts +78 -10
- package/dist/platform/sdk/behaviour.ts +26 -6
- package/dist/platform/sdk/calendar-entry.ts +258 -0
- package/dist/platform/sdk/capacity.ts +1 -2
- package/dist/platform/sdk/checklists.ts +118 -0
- package/dist/platform/sdk/contract.ts +489 -27
- package/dist/platform/sdk/discount.ts +408 -0
- package/dist/platform/sdk/document.ts +4 -2
- package/dist/platform/sdk/field-types.ts +5 -0
- package/dist/platform/sdk/formula/compile.ts +10 -0
- package/dist/platform/sdk/index.ts +17 -0
- package/dist/platform/sdk/intake.ts +380 -0
- package/dist/platform/sdk/journey-signals.ts +215 -0
- package/dist/platform/sdk/manifest.ts +1236 -65
- package/dist/platform/sdk/mine.ts +161 -0
- package/dist/platform/sdk/occupies.ts +192 -0
- package/dist/platform/sdk/questions.ts +681 -0
- package/dist/platform/sdk/recurring.ts +90 -9
- package/dist/platform/sdk/restricted-fields.ts +129 -0
- package/dist/platform/sdk/rich-text.ts +112 -0
- package/dist/platform/sdk/routine-mail.ts +1272 -0
- package/dist/platform/sdk/runtime.ts +25 -12
- package/dist/platform/sdk/screens.ts +91 -0
- package/dist/platform/sdk/sellable.ts +344 -0
- package/dist/platform/sdk/slug.ts +7 -2
- package/dist/platform/sdk/staffed.ts +474 -0
- package/dist/platform/sdk/states.ts +68 -26
- package/dist/platform/sdk/stock.ts +332 -0
- package/dist/platform/sdk/team.ts +83 -0
- package/dist/platform/sdk/testing.ts +70 -6
- package/dist/platform/sdk/worked-on.ts +134 -0
- package/dist/{products-2H5DR232.js → products-GCQLSZ3U.js} +2 -2
- package/dist/run-7ID64RA6.js +65 -0
- package/dist/run-FH7OGGTV.js +131 -0
- package/dist/run-OYZIAP6M.js +3363 -0
- package/dist/{run-ZCGFMEZF.js → run-QOFRRSZV.js} +41 -60
- package/dist/{run-PO3W5BFA.js → run-TJAEWR5U.js} +21 -51
- package/dist/{src-I3IJ4NKC.js → src-52VERUPK.js} +306 -4
- package/dist/src-PHRELWN5.js +40 -0
- package/dist/{visibility-TVXZ67FD.js → visibility-SZCINPP4.js} +21 -4
- package/package.json +2 -2
- package/dist/chunk-2TWV3SVB.js +0 -349
- package/dist/chunk-2XC5JLBO.js +0 -1931
- package/dist/chunk-324WJXNP.js +0 -320
- package/dist/chunk-6ELZRNUU.js +0 -116
- package/dist/chunk-6WGFA2IV.js +0 -643
- package/dist/chunk-7CQGMVDO.js +0 -63
- package/dist/chunk-7OGCMQXG.js +0 -284
- package/dist/chunk-FJIZGS5Q.js +0 -698
- package/dist/chunk-UB4I2QKR.js +0 -31575
- package/dist/chunk-WHKJGF36.js +0 -2418
- package/dist/chunk-YNUZRTPH.js +0 -182
- package/dist/chunk-ZZFWMVJO.js +0 -704
- package/dist/grants-LOW6TSWD.js +0 -32
- package/dist/invoke-6KVT6WJI.js +0 -41
- package/dist/moves-E2HKYCTK.js +0 -41
- package/dist/plan-on-sale-TJPFCNL7.js +0 -26
- package/dist/src-CRO35DYY.js +0 -46
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
-- Migration 0812: offers and takes of shifts, and what the roster's notices and its uncovered
|
|
2
|
+
-- shifts read (stage 45 step 3, ADRs 0623 and 0624).
|
|
3
|
+
--
|
|
4
|
+
-- An offer is a shift (a record of an object that declares `staffed`) its person asks someone
|
|
5
|
+
-- else to take, or an uncovered shift a rosterer offers to everyone eligible. Exactly one take
|
|
6
|
+
-- wins: an offer moves out of `open` once, and the database refuses every later move, so of
|
|
7
|
+
-- twenty takes at once one commits and the rest fail whole (D1 runs one batch at a time). That
|
|
8
|
+
-- is the take's guard: the take's statement updates the offer by its id alone, and the trigger
|
|
9
|
+
-- below fails the batch when the offer is no longer open. No core_write_guard kind is needed.
|
|
10
|
+
--
|
|
11
|
+
-- D1 runs with recursive triggers off, so an INSERT OR REPLACE deletes the rows it replaces
|
|
12
|
+
-- without firing a delete or update trigger. An offer is therefore never replaced: a BEFORE
|
|
13
|
+
-- INSERT trigger refuses an id that exists, and a second open offer of one shift, before any
|
|
14
|
+
-- REPLACE could delete the first.
|
|
15
|
+
CREATE TABLE core_staff_offer (
|
|
16
|
+
id TEXT PRIMARY KEY,
|
|
17
|
+
-- The shift: its object's registry name and its record.
|
|
18
|
+
object_type TEXT NOT NULL,
|
|
19
|
+
record_id TEXT NOT NULL,
|
|
20
|
+
-- The shift's person when offered (core_user.id); null for an uncovered shift a rosterer offers.
|
|
21
|
+
from_account TEXT,
|
|
22
|
+
-- Who made it: the shift's person, or a rosterer.
|
|
23
|
+
made_by TEXT NOT NULL,
|
|
24
|
+
-- One named person it is offered to; null for everyone eligible.
|
|
25
|
+
to_account TEXT,
|
|
26
|
+
-- The shift's start (unix seconds), kept current while open: offers lapse in order of it.
|
|
27
|
+
starts INTEGER NOT NULL,
|
|
28
|
+
-- The shift's kind of work (its object and record), kept current while open: a taker's list
|
|
29
|
+
-- reads only offers of the kinds they are qualified for (the take checks again).
|
|
30
|
+
kind_object TEXT,
|
|
31
|
+
kind_record TEXT,
|
|
32
|
+
state TEXT NOT NULL CHECK (state IN ('open', 'taken', 'withdrawn', 'cancelled', 'lapsed')),
|
|
33
|
+
made_at TEXT NOT NULL,
|
|
34
|
+
taken_by TEXT,
|
|
35
|
+
taken_at TEXT,
|
|
36
|
+
-- When and by whom it left `open` (the host's user for a lapse or a cancel), and why.
|
|
37
|
+
ended_at TEXT,
|
|
38
|
+
ended_by TEXT,
|
|
39
|
+
why TEXT CHECK (why IS NULL OR length(why) <= 200),
|
|
40
|
+
CHECK (state != 'taken' OR (taken_by IS NOT NULL AND taken_at IS NOT NULL)),
|
|
41
|
+
CHECK (state = 'open' OR ended_at IS NOT NULL),
|
|
42
|
+
CHECK (from_account IS NULL OR to_account IS NULL OR to_account != from_account),
|
|
43
|
+
CHECK (from_account IS NULL OR taken_by IS NULL OR taken_by != from_account)
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
-- One open offer per shift; the open offers are read through it (the minute cron's lapse).
|
|
47
|
+
CREATE UNIQUE INDEX core_staff_offer_open ON core_staff_offer (object_type, record_id)
|
|
48
|
+
WHERE state = 'open';
|
|
49
|
+
-- The open offers in order of their shift's start: the minute cron lapses the nearest first, and
|
|
50
|
+
-- takers' lists read the soonest, however many far ones are open.
|
|
51
|
+
CREATE INDEX core_staff_offer_due ON core_staff_offer (starts) WHERE state = 'open';
|
|
52
|
+
-- A person's open offers, from them and to them (theirs to see; cancelled when they leave).
|
|
53
|
+
CREATE INDEX core_staff_offer_from ON core_staff_offer (from_account) WHERE state = 'open';
|
|
54
|
+
CREATE INDEX core_staff_offer_to ON core_staff_offer (to_account) WHERE state = 'open';
|
|
55
|
+
|
|
56
|
+
CREATE TRIGGER core_staff_offer_insert
|
|
57
|
+
BEFORE INSERT ON core_staff_offer
|
|
58
|
+
BEGIN
|
|
59
|
+
SELECT RAISE(ABORT, 'core_staff_offer_replaced')
|
|
60
|
+
WHERE EXISTS (SELECT 1 FROM core_staff_offer WHERE id = NEW.id);
|
|
61
|
+
SELECT RAISE(ABORT, 'core_staff_offer_closed') WHERE NEW.state != 'open';
|
|
62
|
+
SELECT RAISE(ABORT, 'core_staff_offer_open')
|
|
63
|
+
WHERE EXISTS (
|
|
64
|
+
SELECT 1 FROM core_staff_offer
|
|
65
|
+
WHERE object_type = NEW.object_type AND record_id = NEW.record_id AND state = 'open'
|
|
66
|
+
);
|
|
67
|
+
END;
|
|
68
|
+
|
|
69
|
+
-- An offer leaves `open` once (taken, withdrawn, cancelled or lapsed), and what it offers never
|
|
70
|
+
-- changes. The take's race is decided here.
|
|
71
|
+
CREATE TRIGGER core_staff_offer_update
|
|
72
|
+
BEFORE UPDATE ON core_staff_offer
|
|
73
|
+
BEGIN
|
|
74
|
+
SELECT RAISE(ABORT, 'core_staff_offer_closed') WHERE OLD.state != 'open';
|
|
75
|
+
SELECT RAISE(ABORT, 'core_staff_offer_fixed')
|
|
76
|
+
WHERE NEW.id != OLD.id OR NEW.object_type != OLD.object_type OR NEW.record_id != OLD.record_id
|
|
77
|
+
OR NEW.from_account IS NOT OLD.from_account OR NEW.to_account IS NOT OLD.to_account
|
|
78
|
+
OR NEW.made_by != OLD.made_by OR NEW.made_at != OLD.made_at;
|
|
79
|
+
END;
|
|
80
|
+
|
|
81
|
+
-- A batch planned from a read of what it changes (a leaver's future shifts and patterns, ADR 0624)
|
|
82
|
+
-- carries how many rows it expects a statement to change, or how many rows of a kind it read; the
|
|
83
|
+
-- batch fails whole when that is no longer so (`core_staff_changed`), so what it records and marks
|
|
84
|
+
-- is exactly what it changed. Nothing is stored.
|
|
85
|
+
CREATE TABLE core_staff_guard (
|
|
86
|
+
expected INTEGER NOT NULL,
|
|
87
|
+
actual INTEGER NOT NULL
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
CREATE TRIGGER core_staff_guard_check
|
|
91
|
+
BEFORE INSERT ON core_staff_guard
|
|
92
|
+
BEGIN
|
|
93
|
+
SELECT CASE
|
|
94
|
+
WHEN NEW.actual != NEW.expected THEN RAISE(ABORT, 'core_staff_changed')
|
|
95
|
+
ELSE RAISE(IGNORE)
|
|
96
|
+
END;
|
|
97
|
+
END;
|
|
98
|
+
|
|
99
|
+
-- Who is qualified for one kind of work (the offer's eligible people), read by the kind.
|
|
100
|
+
CREATE INDEX core_staff_qualification_kind
|
|
101
|
+
ON core_staff_qualification (kind_object, kind_record);
|
|
102
|
+
|
|
103
|
+
-- The roster's daily work (the uncovered notice): the local date each last ran, so it runs once a
|
|
104
|
+
-- day whatever the minute cron does.
|
|
105
|
+
CREATE TABLE core_staff_run (
|
|
106
|
+
name TEXT PRIMARY KEY,
|
|
107
|
+
last_date TEXT NOT NULL
|
|
108
|
+
) WITHOUT ROWID;
|
|
109
|
+
|
|
110
|
+
-- Guarded: migrations land out of order (docs/parallel-work.md).
|
|
111
|
+
UPDATE core_meta SET value = '812'
|
|
112
|
+
WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 812;
|
|
@@ -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;
|