@composable-os/cli 0.1.0-beta.11 → 0.1.0-beta.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{accounts-UHYLCLEY.js → accounts-GOCNY6WI.js} +17 -13
- package/dist/app-OSPZHKOI.js +43 -0
- package/dist/app-upload-LH4PXKI4.js +198 -0
- package/dist/{blocks-5IFU25J6.js → blocks-6DZIP47W.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-MBIKVRAC.js → chunk-5YDYQM6V.js} +830 -47
- 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-PDD2ETVF.js → chunk-O37YUZ52.js} +172 -16
- package/dist/{chunk-GYTEW66E.js → chunk-O3R4FS2J.js} +1 -1
- package/dist/{chunk-E64SG24J.js → chunk-Q6Y45DY6.js} +25 -2
- 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-MPCPTXLF.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,327 @@
|
|
|
1
|
+
-- Migration 1031: an invoice is an order; what a credit note takes off; the refunds it owes
|
|
2
|
+
-- (stage 35 steps 3 to 5, ADRs 0731, 0733 and 0735).
|
|
3
|
+
--
|
|
4
|
+
-- Issuing an invoice starts one order in the same batch (invoice.issue): its lines as the host
|
|
5
|
+
-- priced them, its staff discounts as stage 34 adjustments, its tax from the document. The order
|
|
6
|
+
-- names its invoice (`invoice_id`) and is due, not expiring: it has no `expires_at`, and its due
|
|
7
|
+
-- date is the invoice's (`balance_due_at`). Its money is moved only through the invoice's own
|
|
8
|
+
-- operations (the operations refuse the rest; the triggers below hold its price for any writer).
|
|
9
|
+
--
|
|
10
|
+
-- A credit note reduces what is owed first (`credited` on the order: balance = total - paid -
|
|
11
|
+
-- credited), then gives back what was paid: one row here per payment it gives money back on, a
|
|
12
|
+
-- provider's through the existing refund approval, a recorded one owed until a person records it
|
|
13
|
+
-- paid back.
|
|
14
|
+
|
|
15
|
+
-- The invoice an order was started by (null for every other order), fixed once written.
|
|
16
|
+
ALTER TABLE core_order ADD COLUMN invoice_id TEXT REFERENCES core_invoice (id);
|
|
17
|
+
-- What credit notes took off what was still owed (never what was paid: that is refunded).
|
|
18
|
+
ALTER TABLE core_order ADD COLUMN credited INTEGER NOT NULL DEFAULT 0 CHECK (credited >= 0);
|
|
19
|
+
|
|
20
|
+
-- One order per invoice.
|
|
21
|
+
CREATE UNIQUE INDEX core_order_invoice_id_uq ON core_order (invoice_id) WHERE invoice_id IS NOT NULL;
|
|
22
|
+
-- An unpaid invoice's order is overdue once its due date has passed (order.overdue).
|
|
23
|
+
CREATE INDEX core_order_invoice_due_idx ON core_order (balance_due_at)
|
|
24
|
+
WHERE state = 'pending' AND invoice_id IS NOT NULL;
|
|
25
|
+
|
|
26
|
+
-- An invoice's order is made only in the batch that issues it, after the invoice took its number
|
|
27
|
+
-- and named the order: a draft deleted or changed meanwhile, or issued twice, makes no order.
|
|
28
|
+
CREATE TRIGGER core_order_invoice_new
|
|
29
|
+
BEFORE INSERT ON core_order
|
|
30
|
+
WHEN NEW.invoice_id IS NOT NULL AND (NEW.expires_at IS NOT NULL OR NOT EXISTS (
|
|
31
|
+
SELECT 1 FROM core_invoice i
|
|
32
|
+
WHERE i.id = NEW.invoice_id AND i.order_id = NEW.id AND i.state = 'issued'
|
|
33
|
+
AND i.total = NEW.total AND i.currency = NEW.currency))
|
|
34
|
+
BEGIN
|
|
35
|
+
SELECT RAISE(ABORT, 'core_order_invoice');
|
|
36
|
+
END;
|
|
37
|
+
|
|
38
|
+
-- The invoice is the order's for good; its price is the invoice's; a credit is never taken back,
|
|
39
|
+
-- is only an invoice's, and never takes more than is owed.
|
|
40
|
+
CREATE TRIGGER core_order_invoice_fixed
|
|
41
|
+
BEFORE UPDATE OF invoice_id, total, discount, credited, paid, expires_at, currency ON core_order
|
|
42
|
+
WHEN NEW.invoice_id IS NOT OLD.invoice_id
|
|
43
|
+
OR (OLD.invoice_id IS NOT NULL AND (NEW.total IS NOT OLD.total
|
|
44
|
+
OR NEW.discount IS NOT OLD.discount OR NEW.expires_at IS NOT NULL
|
|
45
|
+
OR NEW.currency IS NOT OLD.currency))
|
|
46
|
+
OR NEW.credited < OLD.credited
|
|
47
|
+
OR (NEW.credited > 0 AND NEW.invoice_id IS NULL)
|
|
48
|
+
OR NEW.paid + NEW.credited > NEW.total
|
|
49
|
+
BEGIN
|
|
50
|
+
SELECT RAISE(ABORT, 'core_order_invoice');
|
|
51
|
+
END;
|
|
52
|
+
|
|
53
|
+
-- An invoice's order's tax is what its payments carried and its refunds gave back (each its share
|
|
54
|
+
-- of the document's tax, ADR 0731): never written on its own. The payment or refund row is written
|
|
55
|
+
-- before the order in the same batch (orders/payments.ts, refunds.ts, events.ts).
|
|
56
|
+
CREATE TRIGGER core_order_invoice_tax
|
|
57
|
+
BEFORE UPDATE OF tax_amount, tax_refunded ON core_order
|
|
58
|
+
WHEN OLD.invoice_id IS NOT NULL AND (
|
|
59
|
+
-- What the applied payments carried, less what those a reversal undid carried.
|
|
60
|
+
NEW.tax_amount IS NOT (SELECT coalesce(sum(p.tax_amount), 0) FROM core_order_payment p
|
|
61
|
+
WHERE p.order_id = NEW.id AND p.outcome = 'applied'
|
|
62
|
+
AND NOT EXISTS (SELECT 1 FROM core_order_payment q
|
|
63
|
+
WHERE q.order_id = p.order_id AND q.reverses = p.event_id))
|
|
64
|
+
OR NEW.tax_refunded IS NOT (SELECT coalesce(sum(r.tax_amount), 0) FROM core_order_refund r
|
|
65
|
+
WHERE r.order_id = NEW.id AND r.status = 'confirmed'))
|
|
66
|
+
BEGIN
|
|
67
|
+
SELECT RAISE(ABORT, 'core_order_invoice');
|
|
68
|
+
END;
|
|
69
|
+
|
|
70
|
+
-- An invoice's order is never deleted (retention, stage 35 step 11, is specified later), and never
|
|
71
|
+
-- replaced: an INSERT OR REPLACE deletes the row it replaces without firing a delete trigger (D1
|
|
72
|
+
-- runs with recursive triggers off), so an insert over an invoice's order is refused.
|
|
73
|
+
CREATE TRIGGER core_order_invoice_kept
|
|
74
|
+
BEFORE DELETE ON core_order
|
|
75
|
+
WHEN OLD.invoice_id IS NOT NULL
|
|
76
|
+
BEGIN
|
|
77
|
+
SELECT RAISE(ABORT, 'core_order_invoice');
|
|
78
|
+
END;
|
|
79
|
+
|
|
80
|
+
CREATE TRIGGER core_order_invoice_replaced
|
|
81
|
+
BEFORE INSERT ON core_order
|
|
82
|
+
WHEN EXISTS (SELECT 1 FROM core_order o WHERE o.id = NEW.id AND o.invoice_id IS NOT NULL)
|
|
83
|
+
BEGIN
|
|
84
|
+
SELECT RAISE(ABORT, 'core_order_invoice');
|
|
85
|
+
END;
|
|
86
|
+
|
|
87
|
+
-- Only an invoice's order is unpaid again by its reversals (ADR 0733): no other order moves from
|
|
88
|
+
-- paid or part paid back to pending, whoever writes it.
|
|
89
|
+
CREATE TRIGGER core_order_unpaid_again
|
|
90
|
+
BEFORE UPDATE OF state ON core_order
|
|
91
|
+
WHEN NEW.state = 'pending' AND OLD.state IN ('paid', 'part_paid') AND OLD.invoice_id IS NULL
|
|
92
|
+
BEGIN
|
|
93
|
+
SELECT RAISE(ABORT, 'core_order_invoice');
|
|
94
|
+
END;
|
|
95
|
+
|
|
96
|
+
-- An invoice's order's lines say what the invoice says: no price, quantity, discount or tax
|
|
97
|
+
-- changes, none deleted, and none added once the order exists (its lines are written in the
|
|
98
|
+
-- batch that starts it, before its first history row).
|
|
99
|
+
CREATE TRIGGER core_order_line_invoice_fixed
|
|
100
|
+
BEFORE UPDATE OF unit_price, quantity, line_total, line_discount, object_type, record_id,
|
|
101
|
+
tax_amount, order_id
|
|
102
|
+
ON core_order_line
|
|
103
|
+
WHEN (SELECT invoice_id FROM core_order WHERE id = OLD.order_id) IS NOT NULL
|
|
104
|
+
OR (SELECT invoice_id FROM core_order WHERE id = NEW.order_id) IS NOT NULL
|
|
105
|
+
BEGIN
|
|
106
|
+
SELECT RAISE(ABORT, 'core_order_invoice');
|
|
107
|
+
END;
|
|
108
|
+
|
|
109
|
+
CREATE TRIGGER core_order_line_invoice_kept
|
|
110
|
+
BEFORE DELETE ON core_order_line
|
|
111
|
+
WHEN (SELECT invoice_id FROM core_order WHERE id = OLD.order_id) IS NOT NULL
|
|
112
|
+
BEGIN
|
|
113
|
+
SELECT RAISE(ABORT, 'core_order_invoice');
|
|
114
|
+
END;
|
|
115
|
+
|
|
116
|
+
CREATE TRIGGER core_order_line_invoice_new
|
|
117
|
+
BEFORE INSERT ON core_order_line
|
|
118
|
+
WHEN ((SELECT invoice_id FROM core_order WHERE id = NEW.order_id) IS NOT NULL
|
|
119
|
+
AND EXISTS (SELECT 1 FROM core_transition_history h
|
|
120
|
+
WHERE h.object_type = 'order' AND h.record_id = NEW.order_id))
|
|
121
|
+
-- Nor over a line of one (a REPLACE, whatever order it names).
|
|
122
|
+
OR EXISTS (SELECT 1 FROM core_order_line l JOIN core_order o ON o.id = l.order_id
|
|
123
|
+
WHERE l.id = NEW.id AND o.invoice_id IS NOT NULL)
|
|
124
|
+
BEGIN
|
|
125
|
+
SELECT RAISE(ABORT, 'core_order_invoice');
|
|
126
|
+
END;
|
|
127
|
+
|
|
128
|
+
-- The version check covers what is credited too.
|
|
129
|
+
DROP TRIGGER core_order_stale;
|
|
130
|
+
CREATE TRIGGER core_order_stale
|
|
131
|
+
BEFORE UPDATE OF state, paid, refunded, total, discount, deposit, balance_due_at, tax_amount,
|
|
132
|
+
tax_refunded, credited
|
|
133
|
+
ON core_order
|
|
134
|
+
WHEN NEW.version <> OLD.version + 1
|
|
135
|
+
BEGIN
|
|
136
|
+
SELECT RAISE(ABORT, 'core_order_stale');
|
|
137
|
+
END;
|
|
138
|
+
|
|
139
|
+
-- The balance is what is still owed: what credit notes took off is not owed. An invoice's order's
|
|
140
|
+
-- tax, as staff and its customer read it, is the document's (what the invoice shows, beside its
|
|
141
|
+
-- lines' tax), never the figure its payments have carried so far: that one is the book's
|
|
142
|
+
-- (core_order.tax_amount, which `net` is still worked from).
|
|
143
|
+
DROP VIEW rec_order;
|
|
144
|
+
CREATE VIEW rec_order AS
|
|
145
|
+
SELECT
|
|
146
|
+
o.id, o.person_id, o.email, o.currency, o.total, o.paid,
|
|
147
|
+
o.total - o.paid - o.credited AS balance, o.refunded, o.state, o.pay, o.deposit,
|
|
148
|
+
o.balance_due_at, o.expires_at, o.placed_as, o.paid_at, o.cancelled_at, o.refunded_at,
|
|
149
|
+
o.created_at, o.updated_at,
|
|
150
|
+
CASE WHEN o.invoice_id IS NULL THEN o.tax_amount
|
|
151
|
+
ELSE coalesce((SELECT i.tax_total FROM core_invoice i WHERE i.id = o.invoice_id), 0)
|
|
152
|
+
END AS tax_amount,
|
|
153
|
+
o.tax_refunded, o.tax_country,
|
|
154
|
+
(o.paid - o.refunded) - (o.tax_amount - o.tax_refunded) AS net,
|
|
155
|
+
o.subscription_id, o.period_start, o.period_end, o.period_flag,
|
|
156
|
+
o.discount, o.invoice_id, o.credited
|
|
157
|
+
FROM core_order o;
|
|
158
|
+
|
|
159
|
+
-- Adjustments gain a kind only the host writes on an invoice's order: `prepaid` (a final
|
|
160
|
+
-- invoice's "less deposit paid", stage 35 step 6 writes its lines), naming the invoice. SQLite cannot change a CHECK, so the table is remade with its rows; nothing
|
|
161
|
+
-- references it, its triggers and its view go with it and are made again as migration 0950 made
|
|
162
|
+
-- them. A later remake carries these kinds forward and sorts after 1031.
|
|
163
|
+
DROP VIEW rec_order_adjustment;
|
|
164
|
+
DROP TRIGGER core_order_adjustment_fixed;
|
|
165
|
+
DROP TRIGGER core_order_adjustment_kept;
|
|
166
|
+
DROP TRIGGER core_order_adjustment_pending;
|
|
167
|
+
|
|
168
|
+
CREATE TABLE core_order_adjustment_new (
|
|
169
|
+
id TEXT PRIMARY KEY,
|
|
170
|
+
order_id TEXT NOT NULL REFERENCES core_order(id),
|
|
171
|
+
line_id TEXT REFERENCES core_order_line(id),
|
|
172
|
+
kind TEXT NOT NULL CHECK (kind IN ('code', 'automatic', 'staff', 'referral', 'prepaid')),
|
|
173
|
+
source_object TEXT,
|
|
174
|
+
source_id TEXT,
|
|
175
|
+
given_by TEXT,
|
|
176
|
+
reason TEXT,
|
|
177
|
+
percent REAL CHECK (percent IS NULL OR (percent > 0 AND percent <= 100)),
|
|
178
|
+
amount INTEGER NOT NULL CHECK (amount > 0),
|
|
179
|
+
approval TEXT,
|
|
180
|
+
created_at TEXT NOT NULL,
|
|
181
|
+
removed_at TEXT,
|
|
182
|
+
removed_by TEXT,
|
|
183
|
+
CHECK (kind <> 'staff' OR given_by IS NOT NULL),
|
|
184
|
+
CHECK ((removed_at IS NULL) = (removed_by IS NULL)),
|
|
185
|
+
CHECK (kind NOT IN ('prepaid') OR (source_object = 'invoice' AND source_id IS NOT NULL))
|
|
186
|
+
);
|
|
187
|
+
INSERT INTO core_order_adjustment_new (
|
|
188
|
+
id, order_id, line_id, kind, source_object, source_id, given_by, reason, percent, amount,
|
|
189
|
+
approval, created_at, removed_at, removed_by
|
|
190
|
+
)
|
|
191
|
+
SELECT
|
|
192
|
+
id, order_id, line_id, kind, source_object, source_id, given_by, reason, percent, amount,
|
|
193
|
+
approval, created_at, removed_at, removed_by
|
|
194
|
+
FROM core_order_adjustment;
|
|
195
|
+
DROP TABLE core_order_adjustment;
|
|
196
|
+
ALTER TABLE core_order_adjustment_new RENAME TO core_order_adjustment;
|
|
197
|
+
CREATE INDEX core_order_adjustment_order_idx ON core_order_adjustment (order_id, created_at);
|
|
198
|
+
|
|
199
|
+
CREATE TRIGGER core_order_adjustment_fixed
|
|
200
|
+
BEFORE UPDATE ON core_order_adjustment
|
|
201
|
+
WHEN NEW.id IS NOT OLD.id OR NEW.order_id IS NOT OLD.order_id OR NEW.line_id IS NOT OLD.line_id
|
|
202
|
+
OR NEW.kind IS NOT OLD.kind OR NEW.source_object IS NOT OLD.source_object
|
|
203
|
+
OR NEW.source_id IS NOT OLD.source_id OR NEW.given_by IS NOT OLD.given_by
|
|
204
|
+
OR NEW.percent IS NOT OLD.percent OR NEW.amount IS NOT OLD.amount
|
|
205
|
+
OR NEW.approval IS NOT OLD.approval OR NEW.created_at IS NOT OLD.created_at
|
|
206
|
+
OR (OLD.removed_at IS NOT NULL AND NEW.removed_at IS NOT OLD.removed_at)
|
|
207
|
+
OR (NEW.reason IS NOT OLD.reason AND NEW.reason IS NOT NULL)
|
|
208
|
+
-- An invoice's adjustments are what the invoice says: never taken off.
|
|
209
|
+
OR (NEW.removed_at IS NOT NULL
|
|
210
|
+
AND (SELECT invoice_id FROM core_order WHERE id = OLD.order_id) IS NOT NULL)
|
|
211
|
+
BEGIN
|
|
212
|
+
SELECT RAISE(ABORT, 'core_order_adjustment_fixed');
|
|
213
|
+
END;
|
|
214
|
+
|
|
215
|
+
CREATE TRIGGER core_order_adjustment_kept
|
|
216
|
+
BEFORE DELETE ON core_order_adjustment
|
|
217
|
+
BEGIN
|
|
218
|
+
SELECT RAISE(ABORT, 'core_order_adjustment_kept');
|
|
219
|
+
END;
|
|
220
|
+
|
|
221
|
+
-- An adjustment is added to or taken off a pending order only; an invoice's only in the batch
|
|
222
|
+
-- that starts its order (before anything is paid on it), and `prepaid` only there.
|
|
223
|
+
CREATE TRIGGER core_order_adjustment_pending
|
|
224
|
+
BEFORE INSERT ON core_order_adjustment
|
|
225
|
+
WHEN (SELECT state FROM core_order WHERE id = NEW.order_id) IS NOT 'pending'
|
|
226
|
+
OR (NEW.kind IN ('prepaid') AND NOT EXISTS (
|
|
227
|
+
SELECT 1 FROM core_order o WHERE o.id = NEW.order_id AND o.invoice_id = NEW.source_id))
|
|
228
|
+
BEGIN
|
|
229
|
+
SELECT RAISE(ABORT, 'core_order_adjustment_pending');
|
|
230
|
+
END;
|
|
231
|
+
|
|
232
|
+
-- Never over an existing adjustment (a REPLACE would delete it without the kept trigger firing).
|
|
233
|
+
CREATE TRIGGER core_order_adjustment_replaced
|
|
234
|
+
BEFORE INSERT ON core_order_adjustment
|
|
235
|
+
WHEN EXISTS (SELECT 1 FROM core_order_adjustment a WHERE a.id = NEW.id)
|
|
236
|
+
BEGIN
|
|
237
|
+
SELECT RAISE(ABORT, 'core_order_adjustment_kept');
|
|
238
|
+
END;
|
|
239
|
+
|
|
240
|
+
CREATE VIEW rec_order_adjustment AS
|
|
241
|
+
SELECT
|
|
242
|
+
a.id, a.order_id, o.person_id, a.line_id, a.kind, a.source_object, a.source_id, a.given_by,
|
|
243
|
+
a.reason, a.percent, a.amount, a.approval, a.created_at, a.removed_at, a.removed_by,
|
|
244
|
+
o.state AS order_state, o.currency
|
|
245
|
+
FROM core_order_adjustment a
|
|
246
|
+
JOIN core_order o ON o.id = a.order_id;
|
|
247
|
+
|
|
248
|
+
-- What a credit note gives back, per payment of the invoice's order, newest first (ADR 0735):
|
|
249
|
+
-- a provider's payment through the existing refund approval (`refund_id` names the refund asked
|
|
250
|
+
-- for, in the credit note's batch); a recorded payment owed until a person records it paid back
|
|
251
|
+
-- (order.recordRefund fills in `refund_id` and `paid_back_at`, once). A provider's refund a person
|
|
252
|
+
-- rejected, or that expired or failed, is asked for again for the same amount
|
|
253
|
+
-- (creditNote.askRefundAgain): `refund_id` names the new one and `attempt` moves on by one, only
|
|
254
|
+
-- while the old one was never confirmed. Never changed otherwise, never deleted.
|
|
255
|
+
CREATE TABLE core_credit_note_refund (
|
|
256
|
+
-- cnr_… (ids.ts).
|
|
257
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
258
|
+
credit_note_id TEXT NOT NULL REFERENCES core_credit_note (id),
|
|
259
|
+
order_id TEXT NOT NULL REFERENCES core_order (id),
|
|
260
|
+
provider TEXT NOT NULL,
|
|
261
|
+
payment_ref TEXT NOT NULL,
|
|
262
|
+
method TEXT NOT NULL CHECK (method IN ('provider', 'recorded')),
|
|
263
|
+
amount INTEGER NOT NULL CHECK (amount > 0),
|
|
264
|
+
refund_id TEXT,
|
|
265
|
+
paid_back_at TEXT,
|
|
266
|
+
attempt INTEGER NOT NULL DEFAULT 1 CHECK (attempt >= 1),
|
|
267
|
+
-- A provider's: when the provider confirmed its refund (the host's own write, with the
|
|
268
|
+
-- confirmation). Open rows (null) are what the minute cron watches for one never confirmed.
|
|
269
|
+
confirmed_at TEXT,
|
|
270
|
+
created_at TEXT NOT NULL,
|
|
271
|
+
CHECK (method = 'recorded' OR refund_id IS NOT NULL),
|
|
272
|
+
CHECK (confirmed_at IS NULL OR method = 'provider'),
|
|
273
|
+
CHECK ((paid_back_at IS NULL) OR (method = 'recorded' AND refund_id IS NOT NULL))
|
|
274
|
+
);
|
|
275
|
+
CREATE INDEX core_credit_note_refund_order_idx ON core_credit_note_refund (order_id, payment_ref);
|
|
276
|
+
CREATE INDEX core_credit_note_refund_note_idx ON core_credit_note_refund (credit_note_id);
|
|
277
|
+
-- A provider's refunds not yet confirmed: the minute cron reads only these (none when idle).
|
|
278
|
+
CREATE INDEX core_credit_note_refund_open_idx ON core_credit_note_refund (created_at)
|
|
279
|
+
WHERE method = 'provider' AND confirmed_at IS NULL;
|
|
280
|
+
|
|
281
|
+
-- Written in the batch that issues its credit note, never over another row.
|
|
282
|
+
CREATE TRIGGER core_credit_note_refund_new
|
|
283
|
+
BEFORE INSERT ON core_credit_note_refund
|
|
284
|
+
WHEN EXISTS (SELECT 1 FROM core_credit_note_refund r WHERE r.id = NEW.id)
|
|
285
|
+
OR NOT EXISTS (
|
|
286
|
+
SELECT 1 FROM core_credit_note n
|
|
287
|
+
JOIN core_invoice i ON i.id = n.invoice_id
|
|
288
|
+
WHERE n.id = NEW.credit_note_id AND n.state = 'issued' AND i.order_id = NEW.order_id)
|
|
289
|
+
BEGIN
|
|
290
|
+
SELECT RAISE(ABORT, 'core_credit_note_refund');
|
|
291
|
+
END;
|
|
292
|
+
|
|
293
|
+
CREATE TRIGGER core_credit_note_refund_fixed
|
|
294
|
+
BEFORE UPDATE ON core_credit_note_refund
|
|
295
|
+
WHEN NEW.id IS NOT OLD.id OR NEW.credit_note_id IS NOT OLD.credit_note_id
|
|
296
|
+
OR NEW.order_id IS NOT OLD.order_id OR NEW.provider IS NOT OLD.provider
|
|
297
|
+
OR NEW.payment_ref IS NOT OLD.payment_ref OR NEW.method IS NOT OLD.method
|
|
298
|
+
OR NEW.amount IS NOT OLD.amount OR NEW.created_at IS NOT OLD.created_at
|
|
299
|
+
OR OLD.paid_back_at IS NOT NULL OR NEW.refund_id IS NULL
|
|
300
|
+
-- A recorded one: paid back, once.
|
|
301
|
+
OR (OLD.method = 'recorded' AND (NEW.paid_back_at IS NULL OR NEW.attempt IS NOT OLD.attempt))
|
|
302
|
+
-- A provider's: marked confirmed once, when its refund is; or asked again, for the same
|
|
303
|
+
-- amount, when the one before was never confirmed.
|
|
304
|
+
OR (OLD.method = 'provider' AND OLD.confirmed_at IS NOT NULL)
|
|
305
|
+
OR (OLD.method = 'provider' AND NEW.confirmed_at IS NOT NULL AND (NEW.attempt IS NOT OLD.attempt
|
|
306
|
+
OR NEW.refund_id IS NOT OLD.refund_id OR NOT EXISTS (SELECT 1 FROM core_order_refund r
|
|
307
|
+
WHERE r.id = NEW.refund_id AND r.status = 'confirmed')))
|
|
308
|
+
OR (OLD.method = 'provider' AND NEW.confirmed_at IS NULL AND (NEW.paid_back_at IS NOT NULL
|
|
309
|
+
OR NEW.attempt IS NOT OLD.attempt + 1
|
|
310
|
+
OR EXISTS (SELECT 1 FROM core_order_refund r WHERE r.id = OLD.refund_id
|
|
311
|
+
AND r.status = 'confirmed')
|
|
312
|
+
OR NOT EXISTS (SELECT 1 FROM core_order_refund r WHERE r.id = NEW.refund_id
|
|
313
|
+
AND r.order_id = NEW.order_id AND r.payment_ref = NEW.payment_ref
|
|
314
|
+
AND r.amount = NEW.amount)))
|
|
315
|
+
BEGIN
|
|
316
|
+
SELECT RAISE(ABORT, 'core_credit_note_refund');
|
|
317
|
+
END;
|
|
318
|
+
|
|
319
|
+
CREATE TRIGGER core_credit_note_refund_kept
|
|
320
|
+
BEFORE DELETE ON core_credit_note_refund
|
|
321
|
+
BEGIN
|
|
322
|
+
SELECT RAISE(ABORT, 'core_credit_note_refund');
|
|
323
|
+
END;
|
|
324
|
+
|
|
325
|
+
-- Guarded: migrations land out of order (docs/parallel-work.md).
|
|
326
|
+
UPDATE core_meta SET value = '1031'
|
|
327
|
+
WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1031;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
-- Migration 1050: conversations (stage 36 step 1, ADR 0740). Their activities: migration 1430.
|
|
2
|
+
--
|
|
3
|
+
-- A conversation is a core record (core_conversation): one exchange with one person on one
|
|
4
|
+
-- channel, with its status and who has it. Its messages and its staff notes are activities that
|
|
5
|
+
-- point at it (core_activity.conversation_id): a message is an `email` or a `chat` activity on the
|
|
6
|
+
-- person's timeline, a note a `note` activity on the conversation only. Search, embeddings,
|
|
7
|
+
-- summaries, the Trash and erase already work on activities.
|
|
8
|
+
--
|
|
9
|
+
-- A private conversation (private_at set) is read only by admins and whoever has it: its
|
|
10
|
+
-- messages and notes leave the SQL views and search for everyone, as a private activity does
|
|
11
|
+
-- (migration 0111), and the operations show them to admins and the assignee only.
|
|
12
|
+
|
|
13
|
+
CREATE TABLE core_conversation (
|
|
14
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
15
|
+
-- Null while the sender matches no Person ("unknown sender"). Erase and the Trash purge delete
|
|
16
|
+
-- a Person's conversations with their messages first; the cascade is the backstop.
|
|
17
|
+
person_id TEXT REFERENCES core_person (id) ON DELETE CASCADE,
|
|
18
|
+
-- An unknown sender's address as it arrived, and its SHA-256 (lower-cased) to find their
|
|
19
|
+
-- conversation again. Both are cleared when staff link the conversation to a Person.
|
|
20
|
+
sender_address TEXT,
|
|
21
|
+
sender_hash TEXT,
|
|
22
|
+
channel TEXT NOT NULL CHECK (channel IN ('email', 'chat')),
|
|
23
|
+
-- The inbox address an email came to (null for chat).
|
|
24
|
+
inbox_address TEXT,
|
|
25
|
+
subject TEXT,
|
|
26
|
+
status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'waiting', 'closed')),
|
|
27
|
+
-- The staff account that has it (a core_user id), or null: unassigned.
|
|
28
|
+
assignee_id TEXT,
|
|
29
|
+
-- One of the admin's categories (Settings → Inbox, a later step).
|
|
30
|
+
category TEXT,
|
|
31
|
+
-- What it is about: any record (object name and id), or null.
|
|
32
|
+
about_object TEXT,
|
|
33
|
+
about_id TEXT,
|
|
34
|
+
-- Private: who marked it and when. Null: every staff account that reads the inbox sees it.
|
|
35
|
+
private_at TEXT,
|
|
36
|
+
private_by TEXT,
|
|
37
|
+
-- The sender check failed: matched to the Person all the same, and marked.
|
|
38
|
+
sender_unverified INTEGER NOT NULL DEFAULT 0 CHECK (sender_unverified IN (0, 1)),
|
|
39
|
+
opened_at TEXT NOT NULL,
|
|
40
|
+
last_inbound_at TEXT,
|
|
41
|
+
last_reply_at TEXT,
|
|
42
|
+
first_reply_at TEXT,
|
|
43
|
+
reply_due_at TEXT,
|
|
44
|
+
snoozed_until TEXT,
|
|
45
|
+
closed_at TEXT,
|
|
46
|
+
closed_by TEXT,
|
|
47
|
+
created_at TEXT NOT NULL,
|
|
48
|
+
updated_at TEXT NOT NULL,
|
|
49
|
+
-- Every update sets version to the one it read plus one: a write from an older read fails.
|
|
50
|
+
version INTEGER NOT NULL DEFAULT 0,
|
|
51
|
+
CHECK ((about_object IS NULL) = (about_id IS NULL)),
|
|
52
|
+
-- Only an email can come from an unknown sender: chat is from signed-in customers.
|
|
53
|
+
CHECK (person_id IS NOT NULL OR channel = 'email')
|
|
54
|
+
);
|
|
55
|
+
-- The inbox's hot reads: by status and due time, by assignee, and a person's conversations.
|
|
56
|
+
CREATE INDEX core_conversation_status_due_idx ON core_conversation (status, reply_due_at);
|
|
57
|
+
CREATE INDEX core_conversation_assignee_idx ON core_conversation (assignee_id, status);
|
|
58
|
+
CREATE INDEX core_conversation_person_idx ON core_conversation (person_id, last_inbound_at)
|
|
59
|
+
WHERE person_id IS NOT NULL;
|
|
60
|
+
-- The inbox's order (most recent first): a page walks it and stops at its limit.
|
|
61
|
+
CREATE INDEX core_conversation_activity_idx
|
|
62
|
+
ON core_conversation (coalesce(last_inbound_at, opened_at), id);
|
|
63
|
+
CREATE INDEX core_conversation_sender_idx ON core_conversation (sender_hash)
|
|
64
|
+
WHERE sender_hash IS NOT NULL;
|
|
65
|
+
CREATE INDEX core_conversation_about_idx ON core_conversation (about_object, about_id)
|
|
66
|
+
WHERE about_id IS NOT NULL;
|
|
67
|
+
|
|
68
|
+
-- A take or an assignment made from an older read (someone took it a moment before) fails, and
|
|
69
|
+
-- its whole batch.
|
|
70
|
+
CREATE TRIGGER core_conversation_stale
|
|
71
|
+
BEFORE UPDATE ON core_conversation
|
|
72
|
+
WHEN NEW.version <> OLD.version + 1
|
|
73
|
+
BEGIN
|
|
74
|
+
SELECT RAISE(ABORT, 'core_conversation_stale');
|
|
75
|
+
END;
|
|
76
|
+
|
|
77
|
+
-- Its messages and notes, the type `chat` and the private rule on core_activity come in migration
|
|
78
|
+
-- 1430: they remake core_activity, which must come after the note levels and kinds (migrations
|
|
79
|
+
-- 1230 and 1232) that it carries forward.
|
|
80
|
+
|
|
81
|
+
-- Guarded: migrations land out of order (docs/parallel-work.md).
|
|
82
|
+
UPDATE core_meta SET value = '1050'
|
|
83
|
+
WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1050;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
-- Migration 1090: open hours, when someone takes appointments (stage 49 step 3, ADR 0762).
|
|
2
|
+
--
|
|
3
|
+
-- Beside core_staff_away and read the same way: a weekly rule (weekdays, a local start and end
|
|
4
|
+
-- time in a zone, from a date and until one, empty: ongoing), checked by wall clock on the fly, so
|
|
5
|
+
-- 09:00 is 09:00 on both sides of a clock change; an end at or before the start is the next day's
|
|
6
|
+
-- (overnight hours). Or extra hours: a one-off range of instants, with the zone it was given in.
|
|
7
|
+
-- Optionally for some kinds of work only (`only_for`, a JSON list of {object, id}, the shift's kind:
|
|
8
|
+
-- a coach who does assessments only on Saturdays); empty: every kind. A one-off "not open" is time
|
|
9
|
+
-- off (core_staff_away), never a row here: one place for "not working".
|
|
10
|
+
--
|
|
11
|
+
-- Keyed by an account id (a staff account today; a practitioner account from stage 49 step 7),
|
|
12
|
+
-- text with no foreign key, as core_staff_time is. Set by the person or a rosterer
|
|
13
|
+
-- (`staffing.hours.set`); removed when they leave. About someone's working week: in no rec_* view,
|
|
14
|
+
-- so no member's SQL, export or the assistant's record reads see it.
|
|
15
|
+
CREATE TABLE core_staff_hours (
|
|
16
|
+
id TEXT PRIMARY KEY,
|
|
17
|
+
account_id TEXT NOT NULL,
|
|
18
|
+
kind TEXT NOT NULL CHECK (kind IN ('weekly', 'extra')),
|
|
19
|
+
-- Weekdays as a comma list of mon … sun.
|
|
20
|
+
weekdays TEXT,
|
|
21
|
+
start_time TEXT CHECK (start_time IS NULL OR start_time GLOB '[0-2][0-9]:[0-5][0-9]'),
|
|
22
|
+
end_time TEXT CHECK (end_time IS NULL OR end_time GLOB '[0-2][0-9]:[0-5][0-9]'),
|
|
23
|
+
from_date TEXT,
|
|
24
|
+
until_date TEXT,
|
|
25
|
+
starts_at TEXT,
|
|
26
|
+
ends_at TEXT,
|
|
27
|
+
zone TEXT NOT NULL,
|
|
28
|
+
only_for TEXT CHECK (only_for IS NULL OR (json_valid(only_for) AND json_type(only_for) = 'array')),
|
|
29
|
+
set_by TEXT NOT NULL,
|
|
30
|
+
set_at TEXT NOT NULL,
|
|
31
|
+
CHECK (kind != 'weekly' OR (weekdays IS NOT NULL AND start_time IS NOT NULL
|
|
32
|
+
AND end_time IS NOT NULL AND from_date IS NOT NULL AND starts_at IS NULL AND ends_at IS NULL)),
|
|
33
|
+
CHECK (kind != 'extra' OR (starts_at IS NOT NULL AND ends_at IS NOT NULL AND ends_at > starts_at
|
|
34
|
+
AND weekdays IS NULL AND start_time IS NULL AND end_time IS NULL AND from_date IS NULL
|
|
35
|
+
AND until_date IS NULL)),
|
|
36
|
+
CHECK (until_date IS NULL OR from_date IS NULL OR until_date >= from_date)
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
CREATE INDEX core_staff_hours_account ON core_staff_hours (account_id);
|
|
40
|
+
|
|
41
|
+
-- Guarded: migrations land out of order (docs/parallel-work.md).
|
|
42
|
+
UPDATE core_meta SET value = '1090'
|
|
43
|
+
WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1090;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
-- Migration 1091: claim margins on the time claim (stage 49 step 1, ADR 0760).
|
|
2
|
+
--
|
|
3
|
+
-- A staffed object may declare minutes kept free before and after a shift (`staffed.claimBefore`,
|
|
4
|
+
-- `claimAfter`, read from its kind: a service's buffer). They enter the claim: `starts` and `ends`
|
|
5
|
+
-- here are the shift's own time widened by them, so the overlap checks of migration 0811 refuse a
|
|
6
|
+
-- shift inside another's buffer, unchanged. The two columns keep how much each claim was widened
|
|
7
|
+
-- by, in seconds, so a reader that wants the shift's own time (the feed, a leaver's future shifts,
|
|
8
|
+
-- whether someone is away over it) has it without reading the kind again. Zero for every claim
|
|
9
|
+
-- without margins (every claim made before this migration). Written only by the triggers the
|
|
10
|
+
-- schema reconciler builds; no x_* table is touched.
|
|
11
|
+
ALTER TABLE core_staff_time ADD COLUMN margin_before INTEGER NOT NULL DEFAULT 0;
|
|
12
|
+
ALTER TABLE core_staff_time ADD COLUMN margin_after INTEGER NOT NULL DEFAULT 0;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
-- Migration 1092: one client's booked shifts never overlap, refused at commit (stage 49 step 4,
|
|
2
|
+
-- ADR 0763, the review of PR #430).
|
|
3
|
+
--
|
|
4
|
+
-- A shift booked at free times (`staffed.open`, an appointment) names its client, and a
|
|
5
|
+
-- customer's or visitor's booking must not overlap another live one of theirs. A uniqueness
|
|
6
|
+
-- cannot express a range, and the host's read before the write cannot see a booking another
|
|
7
|
+
-- batch commits meanwhile, so the write's batch carries one guard statement: an insert here
|
|
8
|
+
-- whose `clashes` is the count, read inside the batch, of the client's other live shifts of the
|
|
9
|
+
-- object overlapping the new time. D1 runs one batch at a time, so of two bookings racing for the
|
|
10
|
+
-- same client the second batch sees the first and fails whole. The trigger refuses a count above
|
|
11
|
+
-- zero and otherwise drops the row: the table never holds one. No migration touches an x_* table:
|
|
12
|
+
-- the count's SQL names the object's table and is written by the host (core/src/staffing/free.ts).
|
|
13
|
+
CREATE TABLE core_client_guard (
|
|
14
|
+
object_type TEXT NOT NULL,
|
|
15
|
+
record_id TEXT NOT NULL,
|
|
16
|
+
clashes INTEGER NOT NULL
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
CREATE TRIGGER core_client_guard_check
|
|
20
|
+
BEFORE INSERT ON core_client_guard
|
|
21
|
+
BEGIN
|
|
22
|
+
SELECT CASE
|
|
23
|
+
WHEN NEW.clashes > 0 THEN RAISE(ABORT, 'core_client_busy')
|
|
24
|
+
ELSE RAISE(IGNORE)
|
|
25
|
+
END;
|
|
26
|
+
END;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
-- Migration 1095: the `occupies` behaviour's claim on a thing (stage 49 step 2, ADR 0765).
|
|
2
|
+
--
|
|
3
|
+
-- A record of an object that declares `occupies` takes a thing (a room, a chair, a hall) for its
|
|
4
|
+
-- time. Every live record holds one claim here per thing it names that is claimed (only things
|
|
5
|
+
-- marked as taking one booking at a time, when the declaration says so): the record, the thing,
|
|
6
|
+
-- and the record's time with its margins (minutes before and after) as whole unix seconds,
|
|
7
|
+
-- half-open (09:00 to 10:00 and 10:00 to 11:00 do not clash). The database refuses a claim that
|
|
8
|
+
-- overlaps another on the same thing, whatever object holds it (an appointment and a class in one
|
|
9
|
+
-- private room), whoever writes and however many write at once: D1 runs one batch at a time, so
|
|
10
|
+
-- of two batches that would put two records in one room at once, the second fails whole.
|
|
11
|
+
--
|
|
12
|
+
-- The claims are kept by triggers the schema reconciler builds on each declaring object's table,
|
|
13
|
+
-- on the thing's table (its flag changing) and on the table its margins are read from
|
|
14
|
+
-- (core/src/occupies/sql.ts), as core_staff_time's are (migration 0811). No migration touches an
|
|
15
|
+
-- x_* table. D1 runs with recursive triggers off, so the rows an INSERT OR REPLACE or an UPDATE OR
|
|
16
|
+
-- REPLACE deletes fire no delete trigger: the checks below are BEFORE INSERT and BEFORE UPDATE on
|
|
17
|
+
-- every column (a REPLACE and an upsert are inserts first, and an id update is an update), and
|
|
18
|
+
-- each leaves out only the row's own key, which such a statement replaces. A claim left behind by
|
|
19
|
+
-- a row a REPLACE deleted can only refuse more, never let two occupants through.
|
|
20
|
+
CREATE TABLE core_thing_time (
|
|
21
|
+
-- The occupant: its object's registry name and its record.
|
|
22
|
+
object_type TEXT NOT NULL,
|
|
23
|
+
record_id TEXT NOT NULL,
|
|
24
|
+
-- The thing: its object's registry name and its record (a room).
|
|
25
|
+
thing_object TEXT NOT NULL,
|
|
26
|
+
thing_record TEXT NOT NULL,
|
|
27
|
+
-- The occupant's time with its margins, unix seconds.
|
|
28
|
+
starts INTEGER NOT NULL,
|
|
29
|
+
ends INTEGER NOT NULL,
|
|
30
|
+
PRIMARY KEY (object_type, record_id, thing_object, thing_record),
|
|
31
|
+
CHECK (ends > starts)
|
|
32
|
+
) WITHOUT ROWID;
|
|
33
|
+
|
|
34
|
+
-- By thing and start. Claims on one thing never overlap (these triggers keep it so), so in order of
|
|
35
|
+
-- start they also end in order: the one claim that starts last before a new one ends is the only
|
|
36
|
+
-- one that can overlap it. Each check reads that one row, however many bookings the thing has
|
|
37
|
+
-- before or after it.
|
|
38
|
+
CREATE INDEX core_thing_time_thing ON core_thing_time (thing_object, thing_record, starts, ends);
|
|
39
|
+
|
|
40
|
+
CREATE TRIGGER core_thing_time_insert
|
|
41
|
+
BEFORE INSERT ON core_thing_time
|
|
42
|
+
BEGIN
|
|
43
|
+
SELECT RAISE(ABORT, 'core_thing_double_booked')
|
|
44
|
+
WHERE (
|
|
45
|
+
SELECT c.ends FROM core_thing_time c
|
|
46
|
+
WHERE c.thing_object = NEW.thing_object AND c.thing_record = NEW.thing_record
|
|
47
|
+
AND NOT (c.object_type = NEW.object_type AND c.record_id = NEW.record_id)
|
|
48
|
+
AND c.starts < NEW.ends
|
|
49
|
+
ORDER BY c.starts DESC LIMIT 1
|
|
50
|
+
) > NEW.starts;
|
|
51
|
+
END;
|
|
52
|
+
|
|
53
|
+
CREATE TRIGGER core_thing_time_update
|
|
54
|
+
BEFORE UPDATE ON core_thing_time
|
|
55
|
+
BEGIN
|
|
56
|
+
SELECT RAISE(ABORT, 'core_thing_double_booked')
|
|
57
|
+
WHERE (
|
|
58
|
+
SELECT c.ends FROM core_thing_time c
|
|
59
|
+
WHERE c.thing_object = NEW.thing_object AND c.thing_record = NEW.thing_record
|
|
60
|
+
AND NOT (c.object_type = OLD.object_type AND c.record_id = OLD.record_id
|
|
61
|
+
AND c.thing_object = OLD.thing_object AND c.thing_record = OLD.thing_record)
|
|
62
|
+
AND NOT (c.object_type = NEW.object_type AND c.record_id = NEW.record_id)
|
|
63
|
+
AND c.starts < NEW.ends
|
|
64
|
+
ORDER BY c.starts DESC LIMIT 1
|
|
65
|
+
) > NEW.starts;
|
|
66
|
+
END;
|
|
67
|
+
|
|
68
|
+
-- Guarded: migrations land out of order (docs/parallel-work.md).
|
|
69
|
+
UPDATE core_meta SET value = '1095'
|
|
70
|
+
WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1095;
|