@composable-os/cli 0.1.0-beta.8 → 0.1.0-beta.9

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 (63) hide show
  1. package/dist/accounts-D6N77JGP.js +44 -0
  2. package/dist/{blocks-DEELKGDC.js → blocks-4CA4PP3P.js} +4 -4
  3. package/dist/{chunk-X6I2LWB3.js → chunk-2MLAPQU7.js} +1471 -359
  4. package/dist/{chunk-2S7375SC.js → chunk-3MIEYXRY.js} +3 -301
  5. package/dist/chunk-56A6RJIU.js +1040 -0
  6. package/dist/chunk-7CQGMVDO.js +63 -0
  7. package/dist/{chunk-STJZJIRA.js → chunk-7O4F3NU4.js} +1039 -1380
  8. package/dist/chunk-7U3OZLTK.js +1370 -0
  9. package/dist/{chunk-ZRWF6QZS.js → chunk-DU6OIFOU.js} +1 -0
  10. package/dist/chunk-E4WCYTCT.js +347 -0
  11. package/dist/{chunk-E5JLTLRM.js → chunk-GYTEW66E.js} +1 -1
  12. package/dist/chunk-LPD2VR5M.js +284 -0
  13. package/dist/chunk-NDKVIG23.js +2641 -0
  14. package/dist/chunk-NSSUOI3Z.js +333 -0
  15. package/dist/chunk-OV3OFYPI.js +746 -0
  16. package/dist/{chunk-KTXGEXTY.js → chunk-P6YZMS3F.js} +11 -5
  17. package/dist/{chunk-G3ORKXVP.js → chunk-RFJOPI42.js} +138 -14
  18. package/dist/{chunk-KOKYDJ7Z.js → chunk-RXRD34QW.js} +10 -8
  19. package/dist/{chunk-XDDBLYKW.js → chunk-STBMGUTM.js} +4613 -2307
  20. package/dist/chunk-VTQNOQGA.js +150 -0
  21. package/dist/chunk-XSE37HK5.js +426 -0
  22. package/dist/{chunk-LTQDLCP7.js → chunk-Z52IX5WL.js} +257 -845
  23. package/dist/chunk-ZSR5H4O7.js +552 -0
  24. package/dist/{cli-DTMGNDXN.js → cli-7A5XHYML.js} +16 -9
  25. package/dist/{cli-BRSACHSG.js → cli-T6MJM7TU.js} +20 -8
  26. package/dist/cli.js +4 -4
  27. package/dist/{client-repo-ZVZ6KW5S.js → client-repo-PC4ISVEZ.js} +4 -4
  28. package/dist/create.js +4 -4
  29. package/dist/{fields-64BQJA62.js → fields-L24XAIOA.js} +13 -11
  30. package/dist/invoke-TLYT6CFI.js +33 -0
  31. package/dist/order-IKQBUFCV.js +19 -0
  32. package/dist/{person-WMWRF2HV.js → person-DXEOOH2A.js} +9 -5
  33. package/dist/plan-on-sale-LRCK5MJG.js +24 -0
  34. package/dist/platform/migrations/0510_video_and_file_media.sql +109 -0
  35. package/dist/platform/migrations/0516_order_tax.sql +63 -0
  36. package/dist/platform/migrations/0530_subscriptions.sql +173 -0
  37. package/dist/platform/migrations/0531_subscription_trials.sql +56 -0
  38. package/dist/platform/migrations/0532_subscription_offers.sql +80 -0
  39. package/dist/platform/migrations/0533_allowances.sql +262 -0
  40. package/dist/platform/migrations/0560_recurring.sql +75 -0
  41. package/dist/platform/sdk/allowance.ts +508 -0
  42. package/dist/platform/sdk/capacity.ts +89 -0
  43. package/dist/platform/sdk/contract.ts +97 -1
  44. package/dist/platform/sdk/document.ts +161 -14
  45. package/dist/platform/sdk/field-types.ts +5 -0
  46. package/dist/platform/sdk/index.ts +3 -0
  47. package/dist/platform/sdk/manifest.ts +206 -5
  48. package/dist/platform/sdk/media.ts +62 -0
  49. package/dist/platform/sdk/recurring.ts +314 -0
  50. package/dist/platform/sdk/rich-text.ts +52 -4
  51. package/dist/platform/sdk/sellable.ts +120 -0
  52. package/dist/platform/sdk/states.ts +162 -1
  53. package/dist/platform/sdk/unique.ts +81 -0
  54. package/dist/run-BMSZKTN3.js +648 -0
  55. package/dist/run-GMQBAV6H.js +309 -0
  56. package/dist/{run-4JNFLYXV.js → run-TNHMG5RI.js} +51 -390
  57. package/dist/src-7DXXGZG2.js +38 -0
  58. package/dist/{src-J27WXNLY.js → src-BDG7PERB.js} +73 -1
  59. package/dist/visibility-XSBZTO66.js +33 -0
  60. package/package.json +2 -2
  61. package/dist/chunk-MAXLUQKB.js +0 -1287
  62. package/dist/invoke-NHHOLLF6.js +0 -26
  63. package/dist/src-G6OF5SKW.js +0 -31
package/dist/create.js CHANGED
@@ -2,16 +2,16 @@
2
2
  import {
3
3
  ghAvailable,
4
4
  ghVerifier
5
- } from "./chunk-E5JLTLRM.js";
5
+ } from "./chunk-GYTEW66E.js";
6
6
  import {
7
7
  Redactor,
8
8
  runCli,
9
9
  terminalIo
10
- } from "./chunk-G3ORKXVP.js";
11
- import "./chunk-ZRWF6QZS.js";
10
+ } from "./chunk-RFJOPI42.js";
11
+ import "./chunk-DU6OIFOU.js";
12
+ import "./chunk-KGHA67HG.js";
12
13
  import "./chunk-2ABSVMGB.js";
13
14
  import "./chunk-6UWRT4VM.js";
14
- import "./chunk-KGHA67HG.js";
15
15
  import "./chunk-5KSXSK7Y.js";
16
16
 
17
17
  // src/create.ts
@@ -1,28 +1,30 @@
1
1
  #!/usr/bin/env node
2
+ import {
3
+ CORE_OBJECTS,
4
+ activityObject,
5
+ getObject,
6
+ isCoreObject,
7
+ listObjects,
8
+ organisationObject,
9
+ personObject,
10
+ setExtensionObjects,
11
+ taskObject
12
+ } from "./chunk-3MIEYXRY.js";
2
13
  import {
3
14
  BASE_OBJECTS,
4
15
  BASE_TABLES,
5
- CORE_OBJECTS,
6
16
  ORDER_MACHINE,
7
17
  ORDER_STATES,
8
- activityObject,
9
18
  defineObject,
10
19
  getField,
11
- getObject,
12
- isCoreObject,
13
- listObjects,
14
20
  mediaObject,
15
21
  orderLineObject,
16
22
  orderObject,
17
- organisationObject,
18
- personObject,
19
- setExtensionObjects,
20
- taskObject,
21
23
  timelineFields
22
- } from "./chunk-2S7375SC.js";
24
+ } from "./chunk-ZSR5H4O7.js";
23
25
  import {
24
26
  MEDIA_TYPES
25
- } from "./chunk-X6I2LWB3.js";
27
+ } from "./chunk-2MLAPQU7.js";
26
28
  import "./chunk-5KSXSK7Y.js";
27
29
  export {
28
30
  BASE_OBJECTS,
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ coreInvoker,
4
+ coreRegistry,
5
+ createInvoker,
6
+ invoke,
7
+ invokeByName,
8
+ listOperations
9
+ } from "./chunk-STBMGUTM.js";
10
+ import "./chunk-7U3OZLTK.js";
11
+ import "./chunk-LPD2VR5M.js";
12
+ import "./chunk-Z52IX5WL.js";
13
+ import "./chunk-VTQNOQGA.js";
14
+ import "./chunk-NDKVIG23.js";
15
+ import "./chunk-NSSUOI3Z.js";
16
+ import "./chunk-56A6RJIU.js";
17
+ import "./chunk-OV3OFYPI.js";
18
+ import "./chunk-DU6OIFOU.js";
19
+ import "./chunk-KGHA67HG.js";
20
+ import "./chunk-6UWRT4VM.js";
21
+ import "./chunk-7O4F3NU4.js";
22
+ import "./chunk-3MIEYXRY.js";
23
+ import "./chunk-ZSR5H4O7.js";
24
+ import "./chunk-2MLAPQU7.js";
25
+ import "./chunk-5KSXSK7Y.js";
26
+ export {
27
+ coreInvoker,
28
+ coreRegistry,
29
+ createInvoker,
30
+ invoke,
31
+ invokeByName,
32
+ listOperations
33
+ };
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ BASE_OBJECTS,
4
+ BASE_TABLES,
5
+ ORDER_MACHINE,
6
+ ORDER_STATES,
7
+ orderLineObject,
8
+ orderObject
9
+ } from "./chunk-ZSR5H4O7.js";
10
+ import "./chunk-2MLAPQU7.js";
11
+ import "./chunk-5KSXSK7Y.js";
12
+ export {
13
+ BASE_OBJECTS,
14
+ BASE_TABLES,
15
+ ORDER_MACHINE,
16
+ ORDER_STATES,
17
+ orderLineObject,
18
+ orderObject
19
+ };
@@ -22,14 +22,18 @@ import {
22
22
  phoneInputSchema,
23
23
  socialProfileSchema,
24
24
  updatePerson
25
- } from "./chunk-LTQDLCP7.js";
26
- import "./chunk-6UWRT4VM.js";
25
+ } from "./chunk-Z52IX5WL.js";
26
+ import "./chunk-VTQNOQGA.js";
27
27
  import {
28
28
  TRASH_RETENTION_DAYS
29
- } from "./chunk-STJZJIRA.js";
29
+ } from "./chunk-56A6RJIU.js";
30
+ import "./chunk-OV3OFYPI.js";
30
31
  import "./chunk-KGHA67HG.js";
31
- import "./chunk-2S7375SC.js";
32
- import "./chunk-X6I2LWB3.js";
32
+ import "./chunk-6UWRT4VM.js";
33
+ import "./chunk-7O4F3NU4.js";
34
+ import "./chunk-3MIEYXRY.js";
35
+ import "./chunk-ZSR5H4O7.js";
36
+ import "./chunk-2MLAPQU7.js";
33
37
  import "./chunk-5KSXSK7Y.js";
34
38
  export {
35
39
  TRASH_RETENTION_DAYS,
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ PLANS_LOOKED_AT,
4
+ PLAN_ON_SALE_MEMO_MS,
5
+ findPlanOnSale,
6
+ forgetPlanOnSale,
7
+ hasPlanOnSale
8
+ } from "./chunk-7U3OZLTK.js";
9
+ import "./chunk-VTQNOQGA.js";
10
+ import "./chunk-NSSUOI3Z.js";
11
+ import "./chunk-OV3OFYPI.js";
12
+ import "./chunk-KGHA67HG.js";
13
+ import "./chunk-7O4F3NU4.js";
14
+ import "./chunk-3MIEYXRY.js";
15
+ import "./chunk-ZSR5H4O7.js";
16
+ import "./chunk-2MLAPQU7.js";
17
+ import "./chunk-5KSXSK7Y.js";
18
+ export {
19
+ PLANS_LOOKED_AT,
20
+ PLAN_ON_SALE_MEMO_MS,
21
+ findPlanOnSale,
22
+ forgetPlanOnSale,
23
+ hasPlanOnSale
24
+ };
@@ -0,0 +1,109 @@
1
+ -- Stage 42, Courses (ADRs 0475 and 0477): videos and downloadable files in the media library, and
2
+ -- which part of a published document each media item is in.
3
+ --
4
+ -- 1. core_media gains a kind. A `video` is held in Cloudflare Stream on the deployment's own
5
+ -- account: the row keeps Stream's id for it (`stream_uid`), its state (uploading, processing,
6
+ -- ready, failed, and Stream's reason when it failed), its duration and dimensions; nothing of
7
+ -- it is in R2, so it has no `key`. A `file` (a PDF, for downloads) is in R2 like a picture.
8
+ -- SQLite cannot change a CHECK, so the table is rebuilt with its rows, indexes and trigger.
9
+ -- 2. Deleting a row (the Trash purge) queues what it holds on core_blob_deletion in the same
10
+ -- batch: its R2 key, or `stream/<uid>` for a video, which the drain deletes from Stream.
11
+ -- 3. core_media_caption: a video's caption tracks, one per language (WebVTT, uploaded by staff or
12
+ -- generated by Stream).
13
+ -- 4. core_published_media gains `reach`: who outside the org reads the part of the field the item
14
+ -- is in (all who read the field, only holders of access to the record, or no one: a private
15
+ -- part). Existing rows are `all`, which is what media.serve has read them as until now; the
16
+ -- next publish of a record writes its exact reach.
17
+ CREATE TABLE core_media_next (
18
+ id TEXT PRIMARY KEY NOT NULL,
19
+ kind TEXT NOT NULL DEFAULT 'image' CHECK (kind IN ('image', 'video', 'file')),
20
+ key TEXT UNIQUE,
21
+ name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 200),
22
+ type TEXT NOT NULL,
23
+ bytes INTEGER NOT NULL CHECK (bytes > 0),
24
+ width INTEGER,
25
+ height INTEGER,
26
+ alt TEXT CHECK (alt IS NULL OR length(alt) <= 500),
27
+ stream_uid TEXT UNIQUE,
28
+ state TEXT,
29
+ state_reason TEXT CHECK (state_reason IS NULL OR length(state_reason) <= 200),
30
+ duration REAL,
31
+ created_at TEXT NOT NULL,
32
+ created_by TEXT,
33
+ updated_at TEXT NOT NULL,
34
+ updated_by TEXT,
35
+ deleted_at TEXT,
36
+ deleted_by TEXT,
37
+ sha256 TEXT,
38
+ CHECK (
39
+ (kind = 'image' AND type IN ('image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif')
40
+ AND key IS NOT NULL AND stream_uid IS NULL AND state IS NULL)
41
+ OR (kind = 'file' AND type IN ('application/pdf')
42
+ AND key IS NOT NULL AND stream_uid IS NULL AND state IS NULL)
43
+ OR (kind = 'video' AND type = 'video/stream' AND key IS NULL AND stream_uid IS NOT NULL
44
+ AND state IN ('uploading', 'processing', 'ready', 'failed'))
45
+ )
46
+ );
47
+
48
+ INSERT INTO core_media_next (id, kind, key, name, type, bytes, width, height, alt, created_at,
49
+ created_by, updated_at, updated_by, deleted_at, deleted_by, sha256)
50
+ SELECT id, 'image', key, name, type, bytes, width, height, alt, created_at, created_by,
51
+ updated_at, updated_by, deleted_at, deleted_by, sha256
52
+ FROM core_media;
53
+
54
+ DROP VIEW rec_media;
55
+ DROP TABLE core_media;
56
+ ALTER TABLE core_media_next RENAME TO core_media;
57
+
58
+ CREATE INDEX core_media_live_idx ON core_media (created_at) WHERE deleted_at IS NULL;
59
+ CREATE INDEX core_media_deleted_idx ON core_media (deleted_at) WHERE deleted_at IS NOT NULL;
60
+ CREATE INDEX core_media_sha256_idx ON core_media (sha256) WHERE deleted_at IS NULL AND sha256 IS NOT NULL;
61
+
62
+ -- The purge's delete queues what the row held, for the drain to delete after the commit.
63
+ CREATE TRIGGER core_media_object_ad AFTER DELETE ON core_media
64
+ BEGIN
65
+ INSERT OR IGNORE INTO core_blob_deletion (key, queued_at)
66
+ SELECT COALESCE(OLD.key, 'stream/' || OLD.stream_uid), strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
67
+ WHERE OLD.key IS NOT NULL OR OLD.stream_uid IS NOT NULL;
68
+ END;
69
+
70
+ -- The live media items, as staff list them. Outsiders read none of it: an item reaches them only
71
+ -- through media.serve (pictures and files) or media.play (videos).
72
+ CREATE VIEW rec_media AS
73
+ SELECT id, kind, key, name, type, bytes, width, height, alt, stream_uid, state, state_reason,
74
+ duration, created_at, created_by, updated_at, updated_by
75
+ FROM core_media WHERE deleted_at IS NULL;
76
+
77
+ -- A video's caption tracks: one per language. `state` is Stream's for a generated one.
78
+ CREATE TABLE core_media_caption (
79
+ media_id TEXT NOT NULL,
80
+ language TEXT NOT NULL CHECK (length(language) BETWEEN 2 AND 35),
81
+ label TEXT NOT NULL CHECK (length(label) BETWEEN 1 AND 100),
82
+ generated INTEGER NOT NULL DEFAULT 0 CHECK (generated IN (0, 1)),
83
+ state TEXT NOT NULL CHECK (state IN ('processing', 'ready', 'failed')),
84
+ created_at TEXT NOT NULL,
85
+ created_by TEXT,
86
+ PRIMARY KEY (media_id, language)
87
+ ) WITHOUT ROWID;
88
+
89
+ -- A purged video takes its captions with it (Stream deletes them with the video).
90
+ CREATE TRIGGER core_media_caption_ad AFTER DELETE ON core_media
91
+ BEGIN
92
+ DELETE FROM core_media_caption WHERE media_id = OLD.id;
93
+ END;
94
+
95
+ CREATE TABLE core_published_media_next (
96
+ media_id TEXT NOT NULL,
97
+ object_type TEXT NOT NULL,
98
+ record_id TEXT NOT NULL,
99
+ field TEXT NOT NULL,
100
+ reach TEXT NOT NULL DEFAULT 'all' CHECK (reach IN ('all', 'holders', 'private')),
101
+ PRIMARY KEY (media_id, object_type, record_id, field, reach)
102
+ ) WITHOUT ROWID;
103
+
104
+ INSERT INTO core_published_media_next (media_id, object_type, record_id, field, reach)
105
+ SELECT media_id, object_type, record_id, field, 'all' FROM core_published_media;
106
+
107
+ DROP TABLE core_published_media;
108
+ ALTER TABLE core_published_media_next RENAME TO core_published_media;
109
+ CREATE INDEX core_published_media_record_idx ON core_published_media (object_type, record_id);
@@ -0,0 +1,63 @@
1
+ -- Migration 0516: tax at checkout (stage 42 section 4, ADR 0479).
2
+ --
3
+ -- Prices are tax-inclusive. When a human admin turns tax on (core_settings `order_tax`) and the
4
+ -- object bought declares a product tax code (`sellable.tax`), the payments gatekeeper asks the
5
+ -- provider (Stripe Tax) to calculate tax inside each payment. What the provider says is stored
6
+ -- here, payment by payment, and never recalculated by the host. Totals do not change: tax is
7
+ -- inside them.
8
+
9
+ -- A checkout opened with tax: the lines its amount was made of, each with its code and place,
10
+ -- as sent to the provider (JSON). Null: opened without tax, exactly as before.
11
+ ALTER TABLE core_order_checkout ADD COLUMN tax_lines TEXT;
12
+
13
+ -- The provider's tax on one verified payment and the country it was charged for. Null: the
14
+ -- checkout was not taxed.
15
+ ALTER TABLE core_order_payment ADD COLUMN tax_amount INTEGER CHECK (tax_amount IS NULL OR tax_amount >= 0);
16
+ ALTER TABLE core_order_payment ADD COLUMN tax_country TEXT;
17
+
18
+ -- The order's running tax: the sum of its applied payments' tax, what of it the provider said
19
+ -- went back with refunds, and the country of the latest taxed payment.
20
+ ALTER TABLE core_order ADD COLUMN tax_amount INTEGER NOT NULL DEFAULT 0 CHECK (tax_amount >= 0);
21
+ ALTER TABLE core_order ADD COLUMN tax_refunded INTEGER NOT NULL DEFAULT 0 CHECK (tax_refunded >= 0);
22
+ ALTER TABLE core_order ADD COLUMN tax_country TEXT;
23
+
24
+ -- A line's tax: the sum of the provider's tax on its share of each payment, and the code it was
25
+ -- last charged at.
26
+ ALTER TABLE core_order_line ADD COLUMN tax_amount INTEGER NOT NULL DEFAULT 0 CHECK (tax_amount >= 0);
27
+ ALTER TABLE core_order_line ADD COLUMN tax_code TEXT;
28
+
29
+ -- The tax a refund gave back, when the provider's refund event says. Null: it did not say
30
+ -- (Stripe's Refund carries no tax figure; ADR 0479).
31
+ ALTER TABLE core_order_refund ADD COLUMN tax_amount INTEGER CHECK (tax_amount IS NULL OR tax_amount >= 0);
32
+
33
+ -- The version check covers the order's tax too.
34
+ DROP TRIGGER core_order_stale;
35
+ CREATE TRIGGER core_order_stale
36
+ BEFORE UPDATE OF state, paid, refunded, total, deposit, balance_due_at, tax_amount, tax_refunded
37
+ ON core_order
38
+ WHEN NEW.version <> OLD.version + 1
39
+ BEGIN
40
+ SELECT RAISE(ABORT, 'core_order_stale');
41
+ END;
42
+
43
+ -- `net`: what the company keeps, net of tax: paid less refunded, less the tax still held.
44
+ DROP VIEW rec_order;
45
+ CREATE VIEW rec_order AS
46
+ SELECT
47
+ id, person_id, email, currency, total, paid, total - paid AS balance, refunded, state, pay,
48
+ deposit, balance_due_at, expires_at, placed_as, paid_at, cancelled_at, refunded_at, created_at,
49
+ updated_at, tax_amount, tax_refunded, tax_country,
50
+ (paid - refunded) - (tax_amount - tax_refunded) AS net
51
+ FROM core_order;
52
+
53
+ DROP VIEW rec_order_line;
54
+ CREATE VIEW rec_order_line AS
55
+ SELECT
56
+ l.id, l.order_id, o.person_id, l.position, l.object_type, l.record_id, l.title, l.quantity,
57
+ l.unit_price, l.line_total, l.created_at, l.tax_amount, l.tax_code
58
+ FROM core_order_line l
59
+ JOIN core_order o ON o.id = l.order_id;
60
+
61
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
62
+ UPDATE core_meta SET value = '516'
63
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 516;
@@ -0,0 +1,173 @@
1
+ -- Migration 0530: subscriptions (stage 32a, ADRs 0485 to 0487).
2
+ --
3
+ -- The payment provider runs the schedule: it keeps the card, charges each period, retries and ends
4
+ -- the subscription. The host mirrors what the provider's verified events say (core_subscription),
5
+ -- and writes a paid order for every paid period (core_order's period columns). Nothing here
6
+ -- schedules or charges anything.
7
+
8
+ -- A subscription checkout the host opened: who it is for, the plan as priced then, and the trial.
9
+ -- The subscription itself exists only once the provider's verified event says it does; then
10
+ -- core_subscription is made with this row's id (the id the provider carries as metadata).
11
+ CREATE TABLE core_subscription_checkout (
12
+ -- sbs_… (ids.ts): the subscription's id to be.
13
+ id TEXT PRIMARY KEY NOT NULL,
14
+ provider TEXT NOT NULL,
15
+ -- The provider's checkout reference (a Stripe Checkout Session id).
16
+ ref TEXT NOT NULL,
17
+ -- Who pays (the provider's customer is theirs) and who holds what it grants. The same Person
18
+ -- in 32a; 32b lets them differ (a parent pays for a child, ADR 0487).
19
+ payer_person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
20
+ holder_person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
21
+ email TEXT CHECK (email IS NULL OR (email = lower(trim(email)) AND email <> '')),
22
+ placed_as TEXT NOT NULL CHECK (placed_as IN ('customer', 'public', 'staff')),
23
+ -- The plan: any record of an object that declares `sellable.recurring`.
24
+ plan_object TEXT NOT NULL,
25
+ plan_id TEXT NOT NULL,
26
+ plan_title TEXT NOT NULL,
27
+ -- One period's price as the host read it from the plan, minor units, tax included.
28
+ amount INTEGER NOT NULL CHECK (amount > 0),
29
+ currency TEXT NOT NULL CHECK (length(currency) = 3 AND currency = upper(currency)),
30
+ interval TEXT NOT NULL CHECK (interval IN ('week', 'month', 'year')),
31
+ -- The free first period asked for: days, or until a moment (a continuation). At most one.
32
+ trial_days INTEGER CHECK (trial_days IS NULL OR trial_days > 0),
33
+ trial_end TEXT,
34
+ -- The product tax code sent, when tax was asked for (ADR 0479). Null: nothing about tax sent.
35
+ tax_code TEXT,
36
+ -- The provider's subscription it started, once its first verified event arrived.
37
+ subscription_ref TEXT,
38
+ created_at TEXT NOT NULL,
39
+ CHECK (trial_days IS NULL OR trial_end IS NULL)
40
+ );
41
+ CREATE UNIQUE INDEX core_subscription_checkout_ref_uq ON core_subscription_checkout (provider, ref);
42
+ CREATE INDEX core_subscription_checkout_holder_idx
43
+ ON core_subscription_checkout (holder_person_id, plan_object);
44
+
45
+ CREATE TABLE core_subscription (
46
+ -- sbs_…: the checkout's id.
47
+ id TEXT PRIMARY KEY NOT NULL,
48
+ -- The Person who pays: the provider's customer is theirs; the portal, cancel and resume are
49
+ -- theirs. Null after the person is erased.
50
+ payer_person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
51
+ -- The Person who holds what it grants (access, 32b's allowances, the one-trial rule). The payer
52
+ -- in 32a (ADR 0487).
53
+ holder_person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
54
+ email TEXT CHECK (email IS NULL OR (email = lower(trim(email)) AND email <> '')),
55
+ placed_as TEXT NOT NULL CHECK (placed_as IN ('customer', 'public', 'staff')),
56
+ plan_object TEXT NOT NULL,
57
+ plan_id TEXT NOT NULL,
58
+ -- The plan's title and price as they were at the start: a period is charged what was agreed.
59
+ plan_title TEXT NOT NULL,
60
+ amount INTEGER NOT NULL CHECK (amount > 0),
61
+ currency TEXT NOT NULL CHECK (length(currency) = 3 AND currency = upper(currency)),
62
+ interval TEXT NOT NULL CHECK (interval IN ('week', 'month', 'year')),
63
+ tax_code TEXT,
64
+ -- The provider's state, mirrored: every move is the host's, from a verified event.
65
+ state TEXT NOT NULL DEFAULT 'incomplete'
66
+ CHECK (state IN ('incomplete', 'expired', 'trialing', 'active', 'past_due', 'unpaid', 'paused', 'ended')),
67
+ provider TEXT NOT NULL,
68
+ provider_ref TEXT NOT NULL,
69
+ customer_ref TEXT,
70
+ trial_end TEXT,
71
+ current_period_start TEXT,
72
+ current_period_end TEXT,
73
+ -- Set to end when the current period does (a flag on trialing or active, not a state).
74
+ cancel_at_period_end INTEGER NOT NULL DEFAULT 0 CHECK (cancel_at_period_end IN (0, 1)),
75
+ -- A date it is set to end on that was set another way (the provider's dashboard or a schedule):
76
+ -- the host's resume does not undo it (ADR 0485).
77
+ cancel_at TEXT,
78
+ canceled_at TEXT,
79
+ ended_at TEXT,
80
+ -- Why it ended or is ending, in the provider's words (cancellation_requested, payment_failed…).
81
+ end_reason TEXT,
82
+ -- The provider's page where the payer confirms a renewal (3D Secure), while one waits.
83
+ action_url TEXT,
84
+ -- When its renewal first failed, while it is failing.
85
+ failing_since TEXT,
86
+ -- The time of the provider read the row was last moved from: an older read changes nothing.
87
+ observed_at TEXT NOT NULL,
88
+ created_at TEXT NOT NULL,
89
+ updated_at TEXT NOT NULL,
90
+ version INTEGER NOT NULL DEFAULT 0
91
+ );
92
+ CREATE UNIQUE INDEX core_subscription_provider_ref_uq ON core_subscription (provider, provider_ref);
93
+ CREATE INDEX core_subscription_payer_idx ON core_subscription (payer_person_id, created_at);
94
+ CREATE INDEX core_subscription_holder_idx ON core_subscription (holder_person_id, plan_object, state);
95
+ CREATE INDEX core_subscription_failing_idx ON core_subscription (failing_since)
96
+ WHERE state IN ('past_due', 'unpaid');
97
+
98
+ -- A write made from an older read of the subscription fails, and its whole batch (as orders).
99
+ CREATE TRIGGER core_subscription_stale
100
+ BEFORE UPDATE OF state, cancel_at_period_end, current_period_end, observed_at ON core_subscription
101
+ WHEN NEW.version <> OLD.version + 1
102
+ BEGIN
103
+ SELECT RAISE(ABORT, 'core_subscription_stale');
104
+ END;
105
+
106
+ CREATE VIEW rec_subscription AS
107
+ SELECT
108
+ id, payer_person_id, holder_person_id, email, placed_as, plan_object, plan_id, plan_title,
109
+ amount, currency, interval, tax_code IS NOT NULL AS taxed, state, provider, provider_ref,
110
+ trial_end, current_period_start, current_period_end, cancel_at_period_end, cancel_at, canceled_at,
111
+ ended_at, end_reason, action_url, failing_since, created_at, updated_at
112
+ FROM core_subscription;
113
+
114
+ -- An order per paid period (ADR 0486): the subscription it is for, the period it paid, and the
115
+ -- provider's invoice. Null on every other order.
116
+ ALTER TABLE core_order ADD COLUMN subscription_id TEXT REFERENCES core_subscription (id) ON DELETE SET NULL;
117
+ ALTER TABLE core_order ADD COLUMN period_start TEXT;
118
+ ALTER TABLE core_order ADD COLUMN period_end TEXT;
119
+ ALTER TABLE core_order ADD COLUMN invoice_ref TEXT;
120
+ -- A period paid other than as agreed, recorded for what was actually paid and flagged for staff:
121
+ -- less (a coupon, the customer's balance), more, or on terms (currency, interval, price) that are
122
+ -- not the subscription's. Null: paid as agreed.
123
+ ALTER TABLE core_order ADD COLUMN period_flag TEXT
124
+ CHECK (period_flag IS NULL OR period_flag IN ('paid_less', 'paid_more', 'not_as_agreed'));
125
+ -- One order per invoice, whichever event or delivery reports it paid.
126
+ CREATE UNIQUE INDEX core_order_invoice_uq ON core_order (provider, invoice_ref)
127
+ WHERE invoice_ref IS NOT NULL;
128
+ CREATE INDEX core_order_subscription_idx ON core_order (subscription_id, period_start)
129
+ WHERE subscription_id IS NOT NULL;
130
+
131
+ -- The invoice a payment paid, so its refund is credited on that invoice.
132
+ ALTER TABLE core_order_payment ADD COLUMN invoice_ref TEXT;
133
+
134
+ -- A credit note's tax that arrived before its refund's confirmation waits on the refund asked
135
+ -- for (ADR 0486), and is applied when the confirmation comes.
136
+ ALTER TABLE core_order_refund ADD COLUMN reported_tax INTEGER CHECK (reported_tax IS NULL OR reported_tax >= 0);
137
+
138
+ -- core_payment_event gains the kind `subscription`: SQLite cannot change a CHECK, so the table is
139
+ -- rebuilt, its rows kept.
140
+ CREATE TABLE core_payment_event_new (
141
+ provider TEXT NOT NULL,
142
+ event_id TEXT NOT NULL,
143
+ kind TEXT NOT NULL CHECK (kind IN ('payment', 'refund', 'subscription')),
144
+ connection_id TEXT,
145
+ payload TEXT NOT NULL,
146
+ received_at TEXT NOT NULL,
147
+ applied_at TEXT,
148
+ outcome TEXT,
149
+ attempts INTEGER NOT NULL DEFAULT 0,
150
+ PRIMARY KEY (provider, event_id)
151
+ ) WITHOUT ROWID;
152
+ INSERT INTO core_payment_event_new
153
+ (provider, event_id, kind, connection_id, payload, received_at, applied_at, outcome, attempts)
154
+ SELECT provider, event_id, kind, connection_id, payload, received_at, applied_at, outcome, attempts
155
+ FROM core_payment_event;
156
+ DROP TABLE core_payment_event;
157
+ ALTER TABLE core_payment_event_new RENAME TO core_payment_event;
158
+ CREATE INDEX core_payment_event_unapplied_idx ON core_payment_event (received_at)
159
+ WHERE applied_at IS NULL;
160
+
161
+ DROP VIEW rec_order;
162
+ CREATE VIEW rec_order AS
163
+ SELECT
164
+ id, person_id, email, currency, total, paid, total - paid AS balance, refunded, state, pay,
165
+ deposit, balance_due_at, expires_at, placed_as, paid_at, cancelled_at, refunded_at, created_at,
166
+ updated_at, tax_amount, tax_refunded, tax_country,
167
+ (paid - refunded) - (tax_amount - tax_refunded) AS net,
168
+ subscription_id, period_start, period_end, period_flag
169
+ FROM core_order;
170
+
171
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
172
+ UPDATE core_meta SET value = '530'
173
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 530;
@@ -0,0 +1,56 @@
1
+ -- Migration 0531: one free trial per Person per plan object (stage 32a, build step 3, ADR 0489).
2
+ --
3
+ -- A trial is given only to a holder whose identity is proved (a signed-in customer starting their
4
+ -- own subscription, or staff starting one for a Person): a guest who only typed an address never
5
+ -- gets one, so nobody can spend someone else's trial or learn whether they had one.
6
+ --
7
+ -- Each checkout the host opens with a trial holds the holder's trial for that plan object while
8
+ -- the provider's page can still be completed (`held_until`). The trial is used once the provider
9
+ -- confirms a subscription with the plan's trial on it (`used_at`, in the verified event's batch).
10
+ -- A hold that lapses gives the trial back only once the provider says its page expired
11
+ -- uncompleted (`released_at`; the host asks before giving the trial again). The trigger refuses a
12
+ -- second hold while another is unreleased or the trial is used, so two starts at once give one
13
+ -- trial: the second start's whole batch fails.
14
+ CREATE TABLE core_subscription_trial (
15
+ -- The checkout that holds it (core_subscription_checkout.id, the subscription's id to be).
16
+ checkout_id TEXT PRIMARY KEY NOT NULL
17
+ REFERENCES core_subscription_checkout (id) ON DELETE CASCADE,
18
+ holder_person_id TEXT NOT NULL REFERENCES core_person (id) ON DELETE CASCADE,
19
+ -- The plan object the trial is for: one trial per Person across every record of it.
20
+ plan_object TEXT NOT NULL,
21
+ plan_id TEXT NOT NULL,
22
+ -- The provider's page and the provider's reference to it, so the same caller starting again
23
+ -- while it is open gets the same page (and no one else ever does), and its status can be asked.
24
+ url TEXT NOT NULL,
25
+ checkout_ref TEXT NOT NULL,
26
+ -- Who opened it: `customer:<customer id>` or `staff:<user id>`. The page goes back only to them.
27
+ opened_by TEXT NOT NULL,
28
+ -- The terms the page was opened with, returned with it: one period's price and the trial's days.
29
+ amount INTEGER NOT NULL,
30
+ trial_days INTEGER NOT NULL CHECK (trial_days > 0),
31
+ -- Until when the page can still be completed and its confirmation arrive.
32
+ held_until TEXT NOT NULL,
33
+ -- When the provider confirmed a subscription with this trial: from then on it is used for good.
34
+ used_at TEXT,
35
+ -- When a lapsed hold was released, once the provider said its page expired uncompleted.
36
+ released_at TEXT,
37
+ created_at TEXT NOT NULL
38
+ );
39
+ CREATE INDEX core_subscription_trial_holder_idx
40
+ ON core_subscription_trial (holder_person_id, plan_object);
41
+
42
+ CREATE TRIGGER core_subscription_trial_one
43
+ BEFORE INSERT ON core_subscription_trial
44
+ WHEN EXISTS (
45
+ SELECT 1 FROM core_subscription_trial t
46
+ WHERE t.holder_person_id = NEW.holder_person_id
47
+ AND t.plan_object = NEW.plan_object
48
+ AND (t.used_at IS NOT NULL OR t.released_at IS NULL)
49
+ )
50
+ BEGIN
51
+ SELECT RAISE(ABORT, 'core_subscription_trial_taken');
52
+ END;
53
+
54
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
55
+ UPDATE core_meta SET value = '531'
56
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 531;
@@ -0,0 +1,80 @@
1
+ -- Migration 0532: continuation offers (stage 32a, build step 4, ADRs 0491 and 0492).
2
+ --
3
+ -- A company moving its subscribers from another platform imports, for each one, an offer: an
4
+ -- address (and the one live Person who has it), a plan object and record, and the date the person
5
+ -- has already paid up to. The person signs in as that address and subscribes again, free until
6
+ -- the date, then charged by the provider. An offer is taken once (the provider's verified event
7
+ -- that makes the subscription marks it), expires on its date, and can be withdrawn by staff.
8
+ --
9
+ -- While a provider page opened from an offer can still be completed, the offer holds it
10
+ -- (`checkout_id`). The trigger refuses holding a page for an offer that already holds one, has
11
+ -- been taken or was withdrawn: of two takes at once, the second one's whole batch fails, so its
12
+ -- page is never handed out. A hold is released (`checkout_id` back to null) only once the
13
+ -- provider says the page expired uncompleted, as a trial's is (ADR 0489).
14
+ CREATE TABLE core_subscription_offer (
15
+ -- sof_… (ids.ts).
16
+ id TEXT PRIMARY KEY NOT NULL,
17
+ -- The plan: any record of an object that declares `sellable.recurring`.
18
+ plan_object TEXT NOT NULL,
19
+ plan_id TEXT NOT NULL,
20
+ -- The address the offer is for. Taking it needs a customer signed in as this address.
21
+ email TEXT NOT NULL CHECK (email = lower(trim(email)) AND email <> ''),
22
+ -- The one live Person who had the address when it was imported (people come in first; an
23
+ -- import never creates one). A merge repoints it. Null after the person is erased.
24
+ person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
25
+ -- Free until this moment (the date the person has paid up to elsewhere); charged from then.
26
+ free_until TEXT NOT NULL,
27
+ -- The other platform's reference for the old subscription, as the source gave it.
28
+ reference TEXT,
29
+ imported_by TEXT NOT NULL,
30
+ created_at TEXT NOT NULL,
31
+ updated_at TEXT NOT NULL,
32
+ -- The provider page that holds it while it can be completed (core_subscription_checkout.id).
33
+ checkout_id TEXT,
34
+ checkout_ref TEXT,
35
+ checkout_url TEXT,
36
+ held_until TEXT,
37
+ -- The subscription that took it, once the provider confirmed it. Final.
38
+ subscription_id TEXT,
39
+ taken_at TEXT,
40
+ -- Taken with too little time left for free time at the provider (under 48 hours): charged at
41
+ -- once, never given the plan's trial instead.
42
+ charged_from_start INTEGER NOT NULL DEFAULT 0 CHECK (charged_from_start IN (0, 1)),
43
+ withdrawn_at TEXT,
44
+ withdrawn_by TEXT,
45
+ withdraw_reason TEXT,
46
+ -- A withdrawn offer staff replaced with a fresh one (`reissue`): the new offer's id.
47
+ reissued_as TEXT,
48
+ -- Bumped by exactly one on every change (below): a write made from an older read fails.
49
+ version INTEGER NOT NULL DEFAULT 0
50
+ );
51
+ -- One offer per address per plan object, the import's key; a withdrawn one stays as it was, and
52
+ -- staff may reissue the address a fresh offer beside it.
53
+ CREATE UNIQUE INDEX core_subscription_offer_key_uq ON core_subscription_offer (plan_object, email)
54
+ WHERE withdrawn_at IS NULL;
55
+ CREATE INDEX core_subscription_offer_person_idx ON core_subscription_offer (person_id, plan_object);
56
+
57
+ CREATE TRIGGER core_subscription_offer_one_page
58
+ BEFORE UPDATE OF checkout_id ON core_subscription_offer
59
+ WHEN NEW.checkout_id IS NOT NULL
60
+ AND (OLD.checkout_id IS NOT NULL OR OLD.subscription_id IS NOT NULL OR OLD.withdrawn_at IS NOT NULL)
61
+ BEGIN
62
+ SELECT RAISE(ABORT, 'core_subscription_offer_held');
63
+ END;
64
+
65
+ -- A write made from an older read of the offer fails, and its whole batch (as a subscription's,
66
+ -- migration 0530): an import, a withdrawal or a take that read the offer before another change
67
+ -- to it commits nothing, and is told to try again.
68
+ CREATE TRIGGER core_subscription_offer_stale
69
+ BEFORE UPDATE ON core_subscription_offer
70
+ WHEN NEW.version <> OLD.version + 1
71
+ BEGIN
72
+ SELECT RAISE(ABORT, 'core_subscription_offer_stale');
73
+ END;
74
+
75
+ -- A checkout opened from an offer names it, so the provider's confirmation marks the offer taken.
76
+ ALTER TABLE core_subscription_checkout ADD COLUMN offer_id TEXT;
77
+
78
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
79
+ UPDATE core_meta SET value = '532'
80
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 532;