@composable-os/cli 0.1.0-beta.10 → 0.1.0-beta.11

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 (64) hide show
  1. package/dist/{accounts-X4CKM3HL.js → accounts-UHYLCLEY.js} +10 -11
  2. package/dist/{blocks-YWDHEBOK.js → blocks-5IFU25J6.js} +2 -2
  3. package/dist/chunk-2IVEVPMY.js +669 -0
  4. package/dist/{chunk-QGGJ2G6Z.js → chunk-2TWV3SVB.js} +3 -3
  5. package/dist/chunk-2XC5JLBO.js +1931 -0
  6. package/dist/chunk-324WJXNP.js +320 -0
  7. package/dist/chunk-6ELZRNUU.js +116 -0
  8. package/dist/{chunk-YJJHF65I.js → chunk-6WGFA2IV.js} +58 -174
  9. package/dist/{chunk-A4NTSYWV.js → chunk-6XTU2MTP.js} +196 -10
  10. package/dist/{chunk-Q4N55M2Z.js → chunk-7D4GKZOX.js} +66 -3
  11. package/dist/{chunk-GYSGVT6R.js → chunk-7OGCMQXG.js} +9 -9
  12. package/dist/chunk-DHS2CV2M.js +139 -0
  13. package/dist/{chunk-LJFLENFC.js → chunk-E64SG24J.js} +1 -1
  14. package/dist/{chunk-Z3RMMB6S.js → chunk-FJIZGS5Q.js} +45 -15
  15. package/dist/chunk-K34UKWEI.js +298 -0
  16. package/dist/{chunk-LLULPZAJ.js → chunk-LAAL4YGR.js} +4 -4
  17. package/dist/{chunk-CP6BXI6F.js → chunk-LO6WGU5Y.js} +1338 -974
  18. package/dist/{chunk-Y7JZFSTQ.js → chunk-MBIKVRAC.js} +6 -6
  19. package/dist/{chunk-WC5LHBBA.js → chunk-P6UWZ6QQ.js} +1 -1
  20. package/dist/{chunk-6VXHPKGG.js → chunk-PDD2ETVF.js} +8 -8
  21. package/dist/{chunk-6UPUF4ML.js → chunk-UB4I2QKR.js} +1957 -380
  22. package/dist/{chunk-M7A6FR2Z.js → chunk-WHKJGF36.js} +154 -322
  23. package/dist/{chunk-VY64DMQO.js → chunk-XIDLW3EP.js} +190 -21
  24. package/dist/{chunk-IFG4FRW5.js → chunk-YFTLIEWU.js} +1 -1
  25. package/dist/{chunk-JJHHNV46.js → chunk-YNUZRTPH.js} +5 -50
  26. package/dist/{chunk-WO6D3XP3.js → chunk-ZAWES2UD.js} +31 -18
  27. package/dist/{chunk-K6T42YCH.js → chunk-ZZFWMVJO.js} +5 -5
  28. package/dist/{cli-2DFENR62.js → cli-5UNY5G5L.js} +22 -20
  29. package/dist/{cli-YIOVSKOD.js → cli-UYZMVQMR.js} +16 -13
  30. package/dist/cli.js +1 -1
  31. package/dist/{client-repo-PRU3VCQQ.js → client-repo-SE4EKG6W.js} +2 -2
  32. package/dist/create.js +1 -1
  33. package/dist/{fields-2NC7LSYE.js → fields-FTWWMD7N.js} +3 -3
  34. package/dist/grants-LOW6TSWD.js +32 -0
  35. package/dist/invoke-6KVT6WJI.js +41 -0
  36. package/dist/moves-E2HKYCTK.js +41 -0
  37. package/dist/{order-PUWD65BC.js → order-U4KZLLSJ.js} +2 -2
  38. package/dist/{person-WIQQ7ITU.js → person-UHLVBVLR.js} +9 -10
  39. package/dist/plan-on-sale-TJPFCNL7.js +26 -0
  40. package/dist/platform/migrations/0610_waitlists.sql +54 -0
  41. package/dist/platform/migrations/0630_subscription_changes.sql +122 -0
  42. package/dist/platform/migrations/0650_person_links.sql +90 -0
  43. package/dist/platform/migrations/0670_unmoved_guard.sql +52 -0
  44. package/dist/platform/migrations/0690_sellable_refund_due.sql +98 -0
  45. package/dist/platform/sdk/capacity.ts +102 -0
  46. package/dist/platform/sdk/contract.ts +25 -0
  47. package/dist/platform/sdk/manifest.ts +77 -1
  48. package/dist/platform/sdk/sellable.ts +251 -1
  49. package/dist/platform/sdk/states.ts +163 -49
  50. package/dist/{run-RT2HVWMK.js → run-7P4RQALH.js} +2 -2
  51. package/dist/{run-PZUCSDDL.js → run-PO3W5BFA.js} +1 -1
  52. package/dist/{run-Y7T4TVOL.js → run-ZCGFMEZF.js} +2 -2
  53. package/dist/src-CRO35DYY.js +46 -0
  54. package/dist/{src-XTOD7EXW.js → src-I3IJ4NKC.js} +8 -2
  55. package/dist/{visibility-DLGHU4YN.js → visibility-TVXZ67FD.js} +12 -4
  56. package/package.json +2 -2
  57. package/dist/chunk-6GDEDOV7.js +0 -157
  58. package/dist/chunk-O2UIZHND.js +0 -249
  59. package/dist/chunk-R4W5XAUD.js +0 -837
  60. package/dist/chunk-TQFGGSA4.js +0 -748
  61. package/dist/grants-QXMKYMGB.js +0 -32
  62. package/dist/invoke-I3NNNXVL.js +0 -39
  63. package/dist/plan-on-sale-NB774CQL.js +0 -26
  64. package/dist/src-GYS6NBQB.js +0 -44
@@ -4,7 +4,7 @@ import {
4
4
  MEDIA_KINDS,
5
5
  MEDIA_TYPES,
6
6
  MEDIA_VIDEO_TYPE
7
- } from "./chunk-CP6BXI6F.js";
7
+ } from "./chunk-LO6WGU5Y.js";
8
8
 
9
9
  // ../core/src/fields/types.ts
10
10
  function defineObject(object) {
@@ -201,7 +201,21 @@ var subscriptionObject = defineObject({
201
201
  "canceledAt",
202
202
  "endedAt",
203
203
  "actionUrl",
204
- "createdAt"
204
+ "createdAt",
205
+ // The change it shows (stage 32b, ADRs 0530, 0531): the payer's own plan change or pause.
206
+ "changeId",
207
+ "changeKind",
208
+ "changeState",
209
+ "changeToPlanId",
210
+ "changeToPlanTitle",
211
+ "changeToAmount",
212
+ "pauseFrom",
213
+ "pauseUntil",
214
+ "changeEffectiveAt",
215
+ "changeRequestedAt",
216
+ "changeCallOutcome",
217
+ "changeActionUrl",
218
+ "changeEndOutcome"
205
219
  ],
206
220
  write: []
207
221
  },
@@ -288,6 +302,53 @@ var subscriptionObject = defineObject({
288
302
  text("actionUrl", "Confirm payment at", "action_url"),
289
303
  { ...when, name: "failingSince", label: "Failing since", column: "failing_since" },
290
304
  { ...when, name: "createdAt", label: "Started", column: "created_at" },
305
+ // Stage 32b (migration 0630): the plan change or pause it shows, asked for and not yet over,
306
+ // or the pause it is in. Written by the host only (core_subscription_change).
307
+ text("changeId", "Change", "change_id"),
308
+ {
309
+ name: "changeKind",
310
+ label: "Change",
311
+ type: "enum",
312
+ enumValues: ["upgrade", "downgrade", "pause"],
313
+ column: "change_kind",
314
+ filterable: true,
315
+ sortable: false
316
+ },
317
+ {
318
+ name: "changeState",
319
+ label: "Change state",
320
+ type: "enum",
321
+ enumValues: ["requested", "waiting", "applied", "lapsed", "withdrawn"],
322
+ column: "change_state",
323
+ filterable: true,
324
+ sortable: false
325
+ },
326
+ { ...id, name: "changeToPlanId", label: "Changing to", column: "change_to_plan_id" },
327
+ text("changeToPlanTitle", "Changing to", "change_to_plan_title"),
328
+ { ...amount, name: "changeToAmount", label: "New price", column: "change_to_amount" },
329
+ { ...when, name: "pauseFrom", label: "Paused from", column: "pause_from" },
330
+ { ...when, name: "pauseUntil", label: "Paused until", column: "pause_until" },
331
+ { ...when, name: "changeEffectiveAt", label: "Change from", column: "change_effective_at" },
332
+ { ...when, name: "changeRequestedAt", label: "Change asked", column: "change_requested_at" },
333
+ {
334
+ name: "changeCallOutcome",
335
+ label: "Provider's answer",
336
+ type: "enum",
337
+ enumValues: ["sent", "pending", "unknown", "refused"],
338
+ column: "change_call_outcome",
339
+ filterable: true,
340
+ sortable: false
341
+ },
342
+ text("changeActionUrl", "Confirm the change's payment at", "change_action_url"),
343
+ {
344
+ name: "changeEndOutcome",
345
+ label: "Ending it: provider's answer",
346
+ type: "enum",
347
+ enumValues: ["sent", "pending", "unknown", "refused"],
348
+ column: "change_end_outcome",
349
+ filterable: true,
350
+ sortable: false
351
+ },
291
352
  { ...when, name: "updatedAt", label: "Updated", column: "updated_at" }
292
353
  ]
293
354
  });
@@ -318,7 +379,9 @@ var ORDER_MACHINE = {
318
379
  // A late deposit staff honour (ADR 0429) makes an expired order part paid.
319
380
  { from: ["pending", "expired"], to: "part_paid", by: ["host"] },
320
381
  // A buyer may give up an order they have paid nothing on. Once money is on it, staff cancel.
321
- { from: "pending", to: "cancelled", by: ["staff", "customer"], final: true },
382
+ // The host too (ADR 0560): a drop-in booking that leaves its held place another way (its
383
+ // class cancelled) cancels its unpaid order in the same batch.
384
+ { from: "pending", to: "cancelled", by: ["staff", "customer", "host"], final: true },
322
385
  { from: "part_paid", to: "cancelled", by: ["staff"], final: true },
323
386
  // Not final: a late payment staff choose to honour makes it paid.
324
387
  { from: "pending", to: "expired", by: ["host"] },
@@ -2,22 +2,22 @@
2
2
  import {
3
3
  checkPerson,
4
4
  erasedAddresses
5
- } from "./chunk-VY64DMQO.js";
5
+ } from "./chunk-XIDLW3EP.js";
6
6
  import {
7
7
  notifyStatements
8
- } from "./chunk-K6T42YCH.js";
8
+ } from "./chunk-ZZFWMVJO.js";
9
9
  import {
10
10
  newId
11
- } from "./chunk-TQFGGSA4.js";
11
+ } from "./chunk-2XC5JLBO.js";
12
12
  import {
13
13
  ConfigError,
14
14
  ConflictError,
15
15
  NotFoundError,
16
16
  ValidationError
17
- } from "./chunk-A4NTSYWV.js";
17
+ } from "./chunk-6XTU2MTP.js";
18
18
  import {
19
19
  external_exports
20
- } from "./chunk-CP6BXI6F.js";
20
+ } from "./chunk-LO6WGU5Y.js";
21
21
 
22
22
  // ../core/src/customers/settings.ts
23
23
  var CUSTOMER_SIGNUP_SETTING = "customer_signup";
@@ -115,7 +115,7 @@ async function customerCodeReason(db, email, now = (/* @__PURE__ */ new Date()).
115
115
  if (invite) return "invite";
116
116
  if (signup === "buyers" && await hasBought(db, address)) return "buyer";
117
117
  if (await loadCustomerSignupSubscribers(db)) {
118
- const { findPlanOnSale } = await import("./plan-on-sale-NB774CQL.js");
118
+ const { findPlanOnSale } = await import("./plan-on-sale-TJPFCNL7.js");
119
119
  const found = await findPlanOnSale(db, now, options);
120
120
  if (found.state === "on_sale") return "subscription";
121
121
  }
@@ -125,8 +125,8 @@ async function customerMayGetCode(db, email) {
125
125
  return await customerCodeReason(db, email) !== null;
126
126
  }
127
127
  async function hasBought(db, address) {
128
- const { orgReader, visibleSource } = await import("./visibility-DLGHU4YN.js");
129
- const { orderObject } = await import("./order-PUWD65BC.js");
128
+ const { orgReader, visibleSource } = await import("./visibility-TVXZ67FD.js");
129
+ const { orderObject } = await import("./order-U4KZLLSJ.js");
130
130
  const owners = db.selectFrom("core_person_emails as e").innerJoin("core_person as p", "p.id", "e.person_id").select("e.person_id").where("e.email", "=", address).where("e.live", "=", 1).where("p.deleted_at", "is", null);
131
131
  const row = await db.selectFrom(visibleSource(orgReader(db), orderObject)).select("id").where((eb) => eb.or([eb("email", "=", address), eb("person_id", "in", owners)])).where("paid", ">", 0).executeTakeFirst();
132
132
  return Boolean(row);
@@ -166,7 +166,7 @@ async function creatorFor(db, address) {
166
166
  async function createPersonFor(db, address, customerId, now) {
167
167
  const user = await creatorFor(db.query, address);
168
168
  if (!user) throw new ConfigError("There is no admin to create the customer's Person for");
169
- const { invoke } = await import("./invoke-I3NNNXVL.js");
169
+ const { invoke } = await import("./invoke-6KVT6WJI.js");
170
170
  const person = await invoke(
171
171
  "person.create",
172
172
  { emails: [{ email: address, primary: true }] },
@@ -0,0 +1,139 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ priceLines,
4
+ sellableOf,
5
+ sellableProblems
6
+ } from "./chunk-6WGFA2IV.js";
7
+ import {
8
+ paymentsTarget
9
+ } from "./chunk-E64SG24J.js";
10
+ import {
11
+ loadOrderCurrency,
12
+ loadReturnOrigins
13
+ } from "./chunk-6ELZRNUU.js";
14
+ import {
15
+ ConfigError,
16
+ ValidationError,
17
+ buyableSource,
18
+ refreshSchemaRegistry,
19
+ scopeOf,
20
+ sql,
21
+ viewColumnOf
22
+ } from "./chunk-6XTU2MTP.js";
23
+ import {
24
+ listObjects
25
+ } from "./chunk-YFTLIEWU.js";
26
+
27
+ // ../core/src/orders/subscription-setup.ts
28
+ async function subscriptionSetup(db) {
29
+ let target = null;
30
+ try {
31
+ target = await paymentsTarget(db);
32
+ } catch (error) {
33
+ if (!(error instanceof ConfigError)) throw error;
34
+ }
35
+ return {
36
+ returnOrigins: [...await loadReturnOrigins(db)],
37
+ currency: await loadOrderCurrency(db),
38
+ target
39
+ };
40
+ }
41
+ function setupComplete(setup) {
42
+ return setup.returnOrigins.length > 0 && setup.currency !== null && setup.target !== null;
43
+ }
44
+
45
+ // ../core/src/customers/plan-on-sale.ts
46
+ var PUBLIC = Object.freeze({ kind: "public" });
47
+ var PLANS_LOOKED_AT = 50;
48
+ var PLAN_ON_SALE_MEMO_MS = 6e4;
49
+ var memo = null;
50
+ function forgetPlanOnSale() {
51
+ memo = null;
52
+ }
53
+ async function findPlanOnSale(db, now = (/* @__PURE__ */ new Date()).toISOString(), options = {}) {
54
+ const at = Date.now();
55
+ if (options.memo && memo && at - memo.at < PLAN_ON_SALE_MEMO_MS) return memo.value;
56
+ let value;
57
+ try {
58
+ value = await compute(db, now);
59
+ } catch (error) {
60
+ value = { state: "unknown", reason: error instanceof Error ? error.message : String(error) };
61
+ }
62
+ memo = { at, value };
63
+ return value;
64
+ }
65
+ async function hasPlanOnSale(db, now = (/* @__PURE__ */ new Date()).toISOString()) {
66
+ return (await findPlanOnSale(db, now)).state === "on_sale";
67
+ }
68
+ async function compute(db, now) {
69
+ const setup = await subscriptionSetup(db);
70
+ if (!setupComplete(setup)) return { state: "none" };
71
+ await refreshSchemaRegistry(db);
72
+ let unanswered = false;
73
+ for (const object of listObjects()) {
74
+ const sellable = sellableOf(object);
75
+ if (!sellable?.recurring || sellableProblems(sellable, object).length) continue;
76
+ const scope = scopeOf(PUBLIC, object, false);
77
+ if (scope.kind !== "public") continue;
78
+ let source;
79
+ try {
80
+ source = buyableSource({ principal: PUBLIC }, object);
81
+ } catch {
82
+ continue;
83
+ }
84
+ const readable = new Set(scope.columns);
85
+ const columnOf = (name) => {
86
+ const field = name ? object.fields.find((f) => f.name === name) : void 0;
87
+ const column = field ? viewColumnOf(field) : null;
88
+ return column && readable.has(column) ? column : null;
89
+ };
90
+ let query = db.selectFrom(source).select("id");
91
+ const price = columnOf(sellable.price);
92
+ if (price) query = query.where(sql.ref(price), ">", 0);
93
+ const day = now.slice(0, 10);
94
+ const from = columnOf(sellable.saleWindow?.from);
95
+ if (from) {
96
+ query = query.where(
97
+ (eb) => eb.or([eb(sql.ref(from), "is", null), eb(sql.ref(from), "<=", now)])
98
+ );
99
+ }
100
+ const until = columnOf(sellable.saleWindow?.until);
101
+ if (until) {
102
+ query = query.where(
103
+ (eb) => eb.or([eb(sql.ref(until), "is", null), eb(sql.ref(until), ">=", day)])
104
+ );
105
+ }
106
+ const rows = await query.orderBy("id").limit(PLANS_LOOKED_AT + 1).execute();
107
+ for (const { id } of rows.slice(0, PLANS_LOOKED_AT)) {
108
+ const found = await priced(db, object, id, setup.currency, now);
109
+ if (found) return { state: "on_sale", plan: found };
110
+ }
111
+ if (rows.length > PLANS_LOOKED_AT) unanswered = true;
112
+ }
113
+ return unanswered ? { state: "unknown", reason: `more than ${PLANS_LOOKED_AT} plans to look at` } : { state: "none" };
114
+ }
115
+ async function priced(db, object, id, currency, now) {
116
+ try {
117
+ const [line] = await priceLines(
118
+ db,
119
+ [{ object: object.name, id, quantity: 1 }],
120
+ currency,
121
+ now,
122
+ PUBLIC,
123
+ { plans: true }
124
+ );
125
+ return line?.recurring && line.unitPrice > 0 ? { object: object.name, id, title: line.title } : null;
126
+ } catch (error) {
127
+ if (error instanceof ValidationError) return null;
128
+ throw error;
129
+ }
130
+ }
131
+
132
+ export {
133
+ subscriptionSetup,
134
+ PLANS_LOOKED_AT,
135
+ PLAN_ON_SALE_MEMO_MS,
136
+ forgetPlanOnSale,
137
+ findPlanOnSale,
138
+ hasPlanOnSale
139
+ };
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  ConfigError
4
- } from "./chunk-A4NTSYWV.js";
4
+ } from "./chunk-6XTU2MTP.js";
5
5
 
6
6
  // ../core/src/orders/gateway.ts
7
7
  var PAYMENTS_VENDOR = "payments";
@@ -4,29 +4,29 @@ import {
4
4
  compileFilter,
5
5
  holdsAccess,
6
6
  jsonList
7
- } from "./chunk-K6T42YCH.js";
8
- import {
9
- CompiledQuery
10
- } from "./chunk-TQFGGSA4.js";
7
+ } from "./chunk-ZZFWMVJO.js";
11
8
  import {
9
+ CompiledQuery,
12
10
  ForbiddenError,
13
11
  NotFoundError,
14
12
  OperationError,
15
13
  ValidationError,
14
+ isLiveDependant,
15
+ linkGuardStatement,
16
16
  ownedSource,
17
17
  scopeOf,
18
18
  sql,
19
19
  unknownObject,
20
20
  viewColumnOf,
21
21
  visibleSource
22
- } from "./chunk-A4NTSYWV.js";
22
+ } from "./chunk-6XTU2MTP.js";
23
23
  import {
24
24
  getObject,
25
25
  isCoreObject
26
- } from "./chunk-IFG4FRW5.js";
26
+ } from "./chunk-YFTLIEWU.js";
27
27
  import {
28
28
  external_exports
29
- } from "./chunk-CP6BXI6F.js";
29
+ } from "./chunk-LO6WGU5Y.js";
30
30
 
31
31
  // ../core/src/assignment/index.ts
32
32
  var ASSIGNMENT_OPS = ["eq", "in", "between", "is_null", "is_not_null"];
@@ -309,7 +309,7 @@ function diffRecords(before, after, ignore = ["updatedAt"]) {
309
309
 
310
310
  // ../core/src/visibility/writes.ts
311
311
  var SET_BY_HOST = /* @__PURE__ */ new Set(["id", "createdAt", "updatedAt", "createdBy"]);
312
- async function assertVisibleWrites(ctx, all, checked = /* @__PURE__ */ new Set(), byHost = /* @__PURE__ */ new Set()) {
312
+ async function assertVisibleWrites(ctx, all, checked = /* @__PURE__ */ new Set(), byHost = /* @__PURE__ */ new Set(), guard) {
313
313
  if (ctx.principal.kind === "staff" || !all.length) return;
314
314
  const changes = byHost.size ? all.filter((change) => !byHost.has(change)) : all;
315
315
  if (!changes.length) return;
@@ -318,6 +318,16 @@ async function assertVisibleWrites(ctx, all, checked = /* @__PURE__ */ new Set()
318
318
  }
319
319
  const personId = ctx.principal.personId;
320
320
  const existing = /* @__PURE__ */ new Map();
321
+ const guarded = /* @__PURE__ */ new Set();
322
+ const holdToLink = (dependantId) => {
323
+ if (!guard) return false;
324
+ if (!guarded.has(dependantId)) {
325
+ guarded.add(dependantId);
326
+ guard(linkGuardStatement(personId, dependantId));
327
+ }
328
+ return true;
329
+ };
330
+ const mayOwn = async (scope, owner) => owner === personId || scope.dependantsWrite && typeof owner === "string" && await isLiveDependant(ctx.db, personId, owner) && holdToLink(owner);
321
331
  for (const change of changes) {
322
332
  const object = getObject(change.object);
323
333
  if (!object) throw unknownObject(change.object);
@@ -332,7 +342,7 @@ async function assertVisibleWrites(ctx, all, checked = /* @__PURE__ */ new Set()
332
342
  if (name !== owner && checked.has(change)) continue;
333
343
  if (name === owner) {
334
344
  if (change.action !== "create") throw new NotFoundError(object.name, change.id);
335
- if (values.after !== personId) {
345
+ if (!await mayOwn(scope, values.after)) {
336
346
  throw new ValidationError(`A new ${object.name} must belong to its caller`);
337
347
  }
338
348
  continue;
@@ -350,7 +360,7 @@ async function assertVisibleWrites(ctx, all, checked = /* @__PURE__ */ new Set()
350
360
  throw new ValidationError(`Unknown field "${name}" on ${object.name}`, { field: name });
351
361
  }
352
362
  if (change.action === "create") {
353
- if (owner !== "id" && diff[owner]?.after !== personId) {
363
+ if (owner !== "id" && !await mayOwn(scope, diff[owner]?.after)) {
354
364
  throw new ValidationError(`A new ${object.name} must belong to its caller`);
355
365
  }
356
366
  if (owner === "id") throw new ForbiddenError(`A customer cannot create a ${object.name}`);
@@ -361,10 +371,16 @@ async function assertVisibleWrites(ctx, all, checked = /* @__PURE__ */ new Set()
361
371
  for (const [name, ids] of existing) {
362
372
  const object = getObject(name);
363
373
  if (!object) throw unknownObject(name);
364
- const rows = await ctx.db.selectFrom(ownedSource(ctx, object)).select("id").where("id", "in", jsonList([...new Set(ids)])).execute();
374
+ const rows = await ctx.db.selectFrom(ownedSource(ctx, object)).select(["id", "owner"]).where("id", "in", jsonList([...new Set(ids)])).execute();
365
375
  const mine = new Set(rows.map((row) => String(row.id)));
366
376
  const missing = ids.find((id) => !mine.has(id));
367
377
  if (missing) throw new NotFoundError(name, missing);
378
+ for (const row of rows) {
379
+ const owner = row.owner === null || row.owner === void 0 ? null : String(row.owner);
380
+ if (owner !== null && owner !== personId && !holdToLink(owner)) {
381
+ throw new NotFoundError(name, String(row.id));
382
+ }
383
+ }
368
384
  }
369
385
  }
370
386
  var recordWriter = null;
@@ -385,7 +401,7 @@ function customerScopeFor(writer, name) {
385
401
  function assertSettable(writer, object, scope, values) {
386
402
  const widened = writer.widened?.get(object.name);
387
403
  for (const name of Object.keys(values)) {
388
- const reserved = name === scope.declaration.field || name === "id";
404
+ const reserved = name === scope.declaration.field || name === "id" || name === scope.declaration.guardians?.madeBy;
389
405
  if (!reserved && (scope.writable.has(name) || widened?.has(name))) continue;
390
406
  if (!reserved && scope.fields.some((field) => field.name === name)) {
391
407
  throw new ValidationError(`${object.name}.${name} cannot be changed`, { field: name });
@@ -434,17 +450,31 @@ async function recording(writer, write) {
434
450
  });
435
451
  return write(host);
436
452
  }
437
- async function createVisible(writer, name, values) {
453
+ async function ownerFor(writer, scope, forPersonId) {
454
+ if (forPersonId === void 0 || forPersonId === scope.personId) return scope.personId;
455
+ if (!scope.dependantsWrite || !await isLiveDependant(writer.reader.db, scope.personId, forPersonId)) {
456
+ throw new NotFoundError("person", forPersonId);
457
+ }
458
+ return forPersonId;
459
+ }
460
+ async function createVisible(writer, name, values, options = {}) {
438
461
  const { object, scope } = customerScopeFor(writer, name);
439
462
  assertSettable(writer, object, scope, values);
440
463
  if (isCoreObject(object.name) || !recordWriter) {
441
464
  throw new ForbiddenError(`A customer cannot create a ${object.name}`);
442
465
  }
466
+ const owner = await ownerFor(writer, scope, options.forPersonId);
443
467
  await assertRelationsVisible(writer.reader, object, values);
444
468
  await assertAccessRequired(writer.reader, object, values, null);
445
- const owned = { ...values, [scope.declaration.field]: scope.personId };
469
+ const madeBy = scope.declaration.guardians?.madeBy;
470
+ const owned = {
471
+ ...values,
472
+ [scope.declaration.field]: owner,
473
+ ...madeBy ? { [madeBy]: scope.personId } : {}
474
+ };
446
475
  const create = recordWriter.create;
447
- return { id: await recording(writer, (host) => create(host, object, owned)) };
476
+ const seamSet = new Set(madeBy ? [madeBy] : []);
477
+ return { id: await recording(writer, (host) => create(host, object, owned, { seamSet })) };
448
478
  }
449
479
  async function updateVisible(writer, name, id, values, options = {}) {
450
480
  const { object, scope } = customerScopeFor(writer, name);