@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
package/dist/chunk-7OGCMQXG.js
DELETED
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
checkPerson,
|
|
4
|
-
erasedAddresses
|
|
5
|
-
} from "./chunk-XIDLW3EP.js";
|
|
6
|
-
import {
|
|
7
|
-
notifyStatements
|
|
8
|
-
} from "./chunk-ZZFWMVJO.js";
|
|
9
|
-
import {
|
|
10
|
-
newId
|
|
11
|
-
} from "./chunk-2XC5JLBO.js";
|
|
12
|
-
import {
|
|
13
|
-
ConfigError,
|
|
14
|
-
ConflictError,
|
|
15
|
-
NotFoundError,
|
|
16
|
-
ValidationError
|
|
17
|
-
} from "./chunk-6XTU2MTP.js";
|
|
18
|
-
import {
|
|
19
|
-
external_exports
|
|
20
|
-
} from "./chunk-LO6WGU5Y.js";
|
|
21
|
-
|
|
22
|
-
// ../core/src/customers/settings.ts
|
|
23
|
-
var CUSTOMER_SIGNUP_SETTING = "customer_signup";
|
|
24
|
-
var CUSTOMER_SIGNUP_MODES = ["open", "buyers", "invite_only"];
|
|
25
|
-
var customerSignupModeSchema = external_exports.enum(CUSTOMER_SIGNUP_MODES);
|
|
26
|
-
var DEFAULT_CUSTOMER_SIGNUP = "invite_only";
|
|
27
|
-
async function loadCustomerSignup(db) {
|
|
28
|
-
const row = await db.selectFrom("core_settings").select("value").where("key", "=", CUSTOMER_SIGNUP_SETTING).executeTakeFirst();
|
|
29
|
-
if (!row) return DEFAULT_CUSTOMER_SIGNUP;
|
|
30
|
-
try {
|
|
31
|
-
const parsed = customerSignupModeSchema.safeParse(JSON.parse(row.value));
|
|
32
|
-
return parsed.success ? parsed.data : DEFAULT_CUSTOMER_SIGNUP;
|
|
33
|
-
} catch {
|
|
34
|
-
return DEFAULT_CUSTOMER_SIGNUP;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
var CUSTOMER_SIGNUP_SUBSCRIBERS_SETTING = "customer_signup_subscribers";
|
|
38
|
-
var DEFAULT_CUSTOMER_SIGNUP_SUBSCRIBERS = false;
|
|
39
|
-
async function loadCustomerSignupSubscribers(db) {
|
|
40
|
-
const row = await db.selectFrom("core_settings").select("value").where("key", "=", CUSTOMER_SIGNUP_SUBSCRIBERS_SETTING).executeTakeFirst();
|
|
41
|
-
if (!row) return DEFAULT_CUSTOMER_SIGNUP_SUBSCRIBERS;
|
|
42
|
-
try {
|
|
43
|
-
return JSON.parse(row.value) === true;
|
|
44
|
-
} catch {
|
|
45
|
-
return DEFAULT_CUSTOMER_SIGNUP_SUBSCRIBERS;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
var CUSTOMER_ERASE_DUE_SETTING = "customer_erase_due_days";
|
|
49
|
-
var DEFAULT_ERASE_DUE_DAYS = 30;
|
|
50
|
-
var eraseDueDaysSchema = external_exports.number().int().min(1).max(365);
|
|
51
|
-
async function loadEraseDueDays(db) {
|
|
52
|
-
const row = await db.selectFrom("core_settings").select("value").where("key", "=", CUSTOMER_ERASE_DUE_SETTING).executeTakeFirst();
|
|
53
|
-
if (!row) return DEFAULT_ERASE_DUE_DAYS;
|
|
54
|
-
try {
|
|
55
|
-
const parsed = eraseDueDaysSchema.safeParse(JSON.parse(row.value));
|
|
56
|
-
return parsed.success ? parsed.data : DEFAULT_ERASE_DUE_DAYS;
|
|
57
|
-
} catch {
|
|
58
|
-
return DEFAULT_ERASE_DUE_DAYS;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
var CUSTOMER_EMAIL_CONNECTION_SETTING = "customer_email_connection";
|
|
62
|
-
async function loadCustomerEmailConnection(db) {
|
|
63
|
-
const row = await db.selectFrom("core_settings").select("value").where("key", "=", CUSTOMER_EMAIL_CONNECTION_SETTING).executeTakeFirst();
|
|
64
|
-
if (!row) return null;
|
|
65
|
-
try {
|
|
66
|
-
const value = JSON.parse(row.value);
|
|
67
|
-
return typeof value === "string" && value ? value : null;
|
|
68
|
-
} catch {
|
|
69
|
-
return null;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// ../core/src/customers/accounts.ts
|
|
74
|
-
var emailSchema = external_exports.string().trim().toLowerCase().pipe(external_exports.email().max(320));
|
|
75
|
-
function normaliseCustomerEmail(email) {
|
|
76
|
-
const parsed = emailSchema.safeParse(email);
|
|
77
|
-
return parsed.success ? parsed.data : null;
|
|
78
|
-
}
|
|
79
|
-
function toAccount(row) {
|
|
80
|
-
return {
|
|
81
|
-
id: row.id,
|
|
82
|
-
personId: row.person_id,
|
|
83
|
-
email: row.email,
|
|
84
|
-
status: row.status,
|
|
85
|
-
linkState: row.link_state
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
var ACCOUNT_COLUMNS = ["id", "person_id", "email", "status", "link_state"];
|
|
89
|
-
async function loadCustomer(db, id) {
|
|
90
|
-
const row = await db.selectFrom("core_customer").select(ACCOUNT_COLUMNS).where("id", "=", id).executeTakeFirst();
|
|
91
|
-
return row ? toAccount(row) : null;
|
|
92
|
-
}
|
|
93
|
-
async function loadCustomerSignIn(db, customerId, personId) {
|
|
94
|
-
const row = await db.selectFrom("core_customer").select(["email", "email_verified"]).where("id", "=", customerId).where("person_id", "=", personId).where("status", "=", "active").executeTakeFirst();
|
|
95
|
-
return row ? { email: row.email, verified: row.email_verified === 1 } : null;
|
|
96
|
-
}
|
|
97
|
-
async function loadCustomerByEmail(db, email) {
|
|
98
|
-
const address = normaliseCustomerEmail(email);
|
|
99
|
-
if (!address) return null;
|
|
100
|
-
const row = await db.selectFrom("core_customer").select(ACCOUNT_COLUMNS).where("email", "=", address).executeTakeFirst();
|
|
101
|
-
return row ? toAccount(row) : null;
|
|
102
|
-
}
|
|
103
|
-
async function isErased(db, address) {
|
|
104
|
-
return (await erasedAddresses(db, [address])).size > 0;
|
|
105
|
-
}
|
|
106
|
-
async function customerCodeReason(db, email, now = (/* @__PURE__ */ new Date()).toISOString(), options = {}) {
|
|
107
|
-
const address = normaliseCustomerEmail(email);
|
|
108
|
-
if (!address) return null;
|
|
109
|
-
if (await isErased(db, address)) return null;
|
|
110
|
-
const account = await loadCustomerByEmail(db, address);
|
|
111
|
-
if (account) return account.status === "active" ? "account" : null;
|
|
112
|
-
const signup = await loadCustomerSignup(db);
|
|
113
|
-
if (signup === "open") return "open";
|
|
114
|
-
const invite = await db.selectFrom("core_customer_invite").select("email").where("email", "=", address).executeTakeFirst();
|
|
115
|
-
if (invite) return "invite";
|
|
116
|
-
if (signup === "buyers" && await hasBought(db, address)) return "buyer";
|
|
117
|
-
if (await loadCustomerSignupSubscribers(db)) {
|
|
118
|
-
const { findPlanOnSale } = await import("./plan-on-sale-TJPFCNL7.js");
|
|
119
|
-
const found = await findPlanOnSale(db, now, options);
|
|
120
|
-
if (found.state === "on_sale") return "subscription";
|
|
121
|
-
}
|
|
122
|
-
return null;
|
|
123
|
-
}
|
|
124
|
-
async function customerMayGetCode(db, email) {
|
|
125
|
-
return await customerCodeReason(db, email) !== null;
|
|
126
|
-
}
|
|
127
|
-
async function hasBought(db, address) {
|
|
128
|
-
const { orgReader, visibleSource } = await import("./visibility-TVXZ67FD.js");
|
|
129
|
-
const { orderObject } = await import("./order-U4KZLLSJ.js");
|
|
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
|
-
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
|
-
return Boolean(row);
|
|
133
|
-
}
|
|
134
|
-
async function inviteCustomerStatement(db, email, invitedByUserId, now) {
|
|
135
|
-
const address = normaliseCustomerEmail(email);
|
|
136
|
-
if (!address) throw new ValidationError("Not an email address", { field: "email" });
|
|
137
|
-
if (await isErased(db, address)) {
|
|
138
|
-
throw new ValidationError("This address belongs to an erased person", { field: "email" });
|
|
139
|
-
}
|
|
140
|
-
const statement = db.insertInto("core_customer_invite").values({ email: address, invited_by: invitedByUserId, invited_at: now }).onConflict(
|
|
141
|
-
(oc) => oc.column("email").doUpdateSet({ invited_by: invitedByUserId, invited_at: now })
|
|
142
|
-
);
|
|
143
|
-
return { email: address, statement };
|
|
144
|
-
}
|
|
145
|
-
async function inviteCustomer(db, email, invitedByUserId, now = (/* @__PURE__ */ new Date()).toISOString()) {
|
|
146
|
-
const { email: address, statement } = await inviteCustomerStatement(
|
|
147
|
-
db.query,
|
|
148
|
-
email,
|
|
149
|
-
invitedByUserId,
|
|
150
|
-
now
|
|
151
|
-
);
|
|
152
|
-
await db.batch([statement]);
|
|
153
|
-
return { email: address };
|
|
154
|
-
}
|
|
155
|
-
var CUSTOMER_LINK_AUDIT = "customer.link";
|
|
156
|
-
async function sameEmailPeople(db, address) {
|
|
157
|
-
const { conflicts } = await checkPerson(db, { emails: [address] });
|
|
158
|
-
return [...new Set(conflicts.map((c) => c.id))];
|
|
159
|
-
}
|
|
160
|
-
async function creatorFor(db, address) {
|
|
161
|
-
const active = db.selectFrom("core_user").select(["id", "email", "name", "role"]).where("kind", "=", "human").where("deactivated_at", "is", null);
|
|
162
|
-
const invite = await db.selectFrom("core_customer_invite").select("invited_by").where("email", "=", address).executeTakeFirst();
|
|
163
|
-
const row = (invite ? await active.where("id", "=", invite.invited_by).executeTakeFirst() : void 0) ?? await active.where("role", "=", "admin").orderBy("id").limit(1).executeTakeFirst();
|
|
164
|
-
return row ? { id: row.id, email: row.email, name: row.name, role: row.role } : null;
|
|
165
|
-
}
|
|
166
|
-
async function createPersonFor(db, address, customerId, now) {
|
|
167
|
-
const user = await creatorFor(db.query, address);
|
|
168
|
-
if (!user) throw new ConfigError("There is no admin to create the customer's Person for");
|
|
169
|
-
const { invoke } = await import("./invoke-6KVT6WJI.js");
|
|
170
|
-
const person = await invoke(
|
|
171
|
-
"person.create",
|
|
172
|
-
{ emails: [{ email: address, primary: true }] },
|
|
173
|
-
{
|
|
174
|
-
actor: "automation",
|
|
175
|
-
user,
|
|
176
|
-
origin: { surface: "system", automationId: CUSTOMER_LINK_AUDIT, sessionId: customerId },
|
|
177
|
-
db,
|
|
178
|
-
clock: () => now
|
|
179
|
-
}
|
|
180
|
-
);
|
|
181
|
-
return person.id;
|
|
182
|
-
}
|
|
183
|
-
async function linkCustomer(db, customerId, now = /* @__PURE__ */ new Date()) {
|
|
184
|
-
const account = await loadCustomer(db.query, customerId);
|
|
185
|
-
if (!account) throw new NotFoundError("customer", customerId);
|
|
186
|
-
if (account.personId && account.linkState === "linked") {
|
|
187
|
-
return { linkState: "linked", personId: account.personId, created: false };
|
|
188
|
-
}
|
|
189
|
-
if (await isErased(db.query, account.email)) {
|
|
190
|
-
throw new ValidationError("This address belongs to an erased person");
|
|
191
|
-
}
|
|
192
|
-
let matches = await sameEmailPeople(db.query, account.email);
|
|
193
|
-
let created = false;
|
|
194
|
-
if (matches.length === 0) {
|
|
195
|
-
try {
|
|
196
|
-
matches = [await createPersonFor(db, account.email, account.id, now)];
|
|
197
|
-
created = true;
|
|
198
|
-
} catch (error) {
|
|
199
|
-
if (!(error instanceof ConflictError)) throw error;
|
|
200
|
-
matches = await sameEmailPeople(db.query, account.email);
|
|
201
|
-
if (!matches.length) throw error;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
let result;
|
|
205
|
-
if (matches.length === 1) {
|
|
206
|
-
const personId = matches[0];
|
|
207
|
-
const holder = await db.query.selectFrom("core_customer").select("id").where("person_id", "=", personId).where("id", "!=", account.id).executeTakeFirst();
|
|
208
|
-
result = holder ? { linkState: "needs_merge", personId: null, created, reason: "person_has_account" } : { linkState: "linked", personId, created };
|
|
209
|
-
} else {
|
|
210
|
-
result = { linkState: "needs_merge", personId: null, created, reason: "several_people" };
|
|
211
|
-
}
|
|
212
|
-
const at = now.toISOString();
|
|
213
|
-
const statements = [
|
|
214
|
-
db.query.updateTable("core_customer").set({ person_id: result.personId, link_state: result.linkState, updated_at: at }).where("id", "=", account.id),
|
|
215
|
-
db.query.insertInto("core_audit").values({
|
|
216
|
-
id: newId("audit"),
|
|
217
|
-
at,
|
|
218
|
-
operation: CUSTOMER_LINK_AUDIT,
|
|
219
|
-
risk: "write",
|
|
220
|
-
actor: "customer",
|
|
221
|
-
user_id: account.id,
|
|
222
|
-
origin: JSON.stringify({ surface: "client" }),
|
|
223
|
-
input: JSON.stringify({ customerId: account.id }),
|
|
224
|
-
result: JSON.stringify({
|
|
225
|
-
linkState: result.linkState,
|
|
226
|
-
personId: result.personId,
|
|
227
|
-
created: result.created,
|
|
228
|
-
...result.reason ? { reason: result.reason, people: matches } : {}
|
|
229
|
-
}),
|
|
230
|
-
gate_decision: "auto",
|
|
231
|
-
outcome: "ok",
|
|
232
|
-
error: null
|
|
233
|
-
})
|
|
234
|
-
];
|
|
235
|
-
if (result.linkState === "needs_merge") {
|
|
236
|
-
const body = result.reason === "several_people" ? `${account.email} signed in to their customer account, and ${matches.length} people have this address. Merge them: the account links at the next sign-in.` : `${account.email} signed in to a new customer account, and the person with this address already has another one. Disable one account, then ask them to sign in again.`;
|
|
237
|
-
const plan = await notifyStatements(
|
|
238
|
-
db.query,
|
|
239
|
-
{
|
|
240
|
-
to: [{ role: "admin" }],
|
|
241
|
-
type: "system.message",
|
|
242
|
-
title: "A customer account needs a merge",
|
|
243
|
-
body,
|
|
244
|
-
link: matches[0] ? { kind: "record", object: "person", id: matches[0] } : null,
|
|
245
|
-
dedupeKey: `customer.needs_merge:${account.id}`,
|
|
246
|
-
source: { actor: "system" }
|
|
247
|
-
},
|
|
248
|
-
at
|
|
249
|
-
);
|
|
250
|
-
statements.push(...plan.statements);
|
|
251
|
-
}
|
|
252
|
-
await db.batch(statements);
|
|
253
|
-
return result;
|
|
254
|
-
}
|
|
255
|
-
function customerSignedInStatement(db, customerId, now) {
|
|
256
|
-
return db.updateTable("core_customer").set({ last_sign_in_at: now, updated_at: now }).where("id", "=", customerId);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
export {
|
|
260
|
-
CUSTOMER_SIGNUP_SETTING,
|
|
261
|
-
customerSignupModeSchema,
|
|
262
|
-
loadCustomerSignup,
|
|
263
|
-
CUSTOMER_SIGNUP_SUBSCRIBERS_SETTING,
|
|
264
|
-
loadCustomerSignupSubscribers,
|
|
265
|
-
CUSTOMER_ERASE_DUE_SETTING,
|
|
266
|
-
eraseDueDaysSchema,
|
|
267
|
-
loadEraseDueDays,
|
|
268
|
-
CUSTOMER_EMAIL_CONNECTION_SETTING,
|
|
269
|
-
loadCustomerEmailConnection,
|
|
270
|
-
normaliseCustomerEmail,
|
|
271
|
-
loadCustomer,
|
|
272
|
-
loadCustomerSignIn,
|
|
273
|
-
loadCustomerByEmail,
|
|
274
|
-
customerCodeReason,
|
|
275
|
-
customerMayGetCode,
|
|
276
|
-
hasBought,
|
|
277
|
-
inviteCustomerStatement,
|
|
278
|
-
inviteCustomer,
|
|
279
|
-
CUSTOMER_LINK_AUDIT,
|
|
280
|
-
sameEmailPeople,
|
|
281
|
-
creatorFor,
|
|
282
|
-
linkCustomer,
|
|
283
|
-
customerSignedInStatement
|
|
284
|
-
};
|