@odla-ai/chapter 0.0.2 → 0.4.0
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/index.cjs +532 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +616 -5
- package/dist/index.d.ts +616 -5
- package/dist/index.js +532 -4
- package/dist/index.js.map +1 -1
- package/dist/ui/index.d.ts +121 -1
- package/dist/ui/index.js +503 -1
- package/dist/ui/index.js.map +1 -1
- package/dist/worker/index.cjs +817 -43
- package/dist/worker/index.cjs.map +1 -1
- package/dist/worker/index.d.cts +88 -4
- package/dist/worker/index.d.ts +88 -4
- package/dist/worker/index.js +816 -44
- package/dist/worker/index.js.map +1 -1
- package/package.json +6 -1
package/dist/index.js
CHANGED
|
@@ -19,6 +19,14 @@ var admins = {
|
|
|
19
19
|
note: attr("string", { optional: true })
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
|
+
var superAdmins = {
|
|
23
|
+
attrs: {
|
|
24
|
+
id: id(),
|
|
25
|
+
email: attr("string", { unique: true, indexed: true }),
|
|
26
|
+
note: attr("string", { optional: true }),
|
|
27
|
+
createdAt: attr("number", { indexed: true })
|
|
28
|
+
}
|
|
29
|
+
};
|
|
22
30
|
var applications = {
|
|
23
31
|
attrs: {
|
|
24
32
|
id: id(),
|
|
@@ -106,8 +114,16 @@ var emailLog = {
|
|
|
106
114
|
sentAt: attr("number", { indexed: true })
|
|
107
115
|
}
|
|
108
116
|
};
|
|
109
|
-
function chapterDb(mode) {
|
|
110
|
-
const entities =
|
|
117
|
+
function chapterDb(mode, auth) {
|
|
118
|
+
const entities = {};
|
|
119
|
+
if (mode === "chapter") {
|
|
120
|
+
entities.applications = applications;
|
|
121
|
+
entities.groups = groups;
|
|
122
|
+
entities.meetings = meetings;
|
|
123
|
+
entities.emailLog = emailLog;
|
|
124
|
+
}
|
|
125
|
+
if (auth.source === "table") entities.admins = admins;
|
|
126
|
+
if (auth.superAdmins) entities.superAdmins = superAdmins;
|
|
111
127
|
const schema = { entities, links: {} };
|
|
112
128
|
const rules = {};
|
|
113
129
|
for (const ns of Object.keys(entities)) {
|
|
@@ -264,6 +280,168 @@ function buildGroupSeed(config) {
|
|
|
264
280
|
return row;
|
|
265
281
|
}
|
|
266
282
|
|
|
283
|
+
// src/auth.ts
|
|
284
|
+
function resolveAuth(mode, auth) {
|
|
285
|
+
const a = auth ?? {};
|
|
286
|
+
const source = a.source ?? (mode === "hub" ? "table" : "claim");
|
|
287
|
+
if (source !== "claim" && source !== "table") {
|
|
288
|
+
throw new Error(`defineChapter.auth.source: must be "claim" or "table" \u2014 got ${JSON.stringify(a.source)}`);
|
|
289
|
+
}
|
|
290
|
+
const claim = a.claim ?? "role";
|
|
291
|
+
if (typeof claim !== "string" || claim === "") {
|
|
292
|
+
throw new Error("defineChapter.auth.claim: must be a non-empty string");
|
|
293
|
+
}
|
|
294
|
+
const ladder = a.ladder ?? ["provisional", "member", "admin"];
|
|
295
|
+
if (!Array.isArray(ladder) || ladder.length === 0 || !ladder.every((r) => typeof r === "string" && r !== "")) {
|
|
296
|
+
throw new Error("defineChapter.auth.ladder: must be a non-empty array of role strings");
|
|
297
|
+
}
|
|
298
|
+
const adminRole = ladder[ladder.length - 1];
|
|
299
|
+
const superAdmins2 = a.superAdmins ?? source === "claim";
|
|
300
|
+
return { source, claim, ladder, adminRole, superAdmins: superAdmins2 };
|
|
301
|
+
}
|
|
302
|
+
function roleFromClaim(payload, auth) {
|
|
303
|
+
const raw = payload[auth.claim];
|
|
304
|
+
return typeof raw === "string" && auth.ladder.includes(raw) ? raw : auth.ladder[0];
|
|
305
|
+
}
|
|
306
|
+
function isAdminRole(role, auth) {
|
|
307
|
+
return role === auth.adminRole;
|
|
308
|
+
}
|
|
309
|
+
function canChangeRole(ctx) {
|
|
310
|
+
const { auth } = ctx;
|
|
311
|
+
if (!auth.ladder.includes(ctx.newRole)) {
|
|
312
|
+
return { ok: false, status: 400, error: `role must be one of: ${auth.ladder.join(", ")}` };
|
|
313
|
+
}
|
|
314
|
+
if (ctx.actorId === ctx.targetId) {
|
|
315
|
+
return { ok: false, status: 400, error: "you cannot change your own role" };
|
|
316
|
+
}
|
|
317
|
+
if (ctx.targetIsSuper && !ctx.actorIsSuper) {
|
|
318
|
+
return { ok: false, status: 403, error: "this person is a super-admin; their access is managed in odla Studio" };
|
|
319
|
+
}
|
|
320
|
+
const touchesAdmin = ctx.newRole === auth.adminRole || ctx.targetCurrentRole === auth.adminRole;
|
|
321
|
+
if (auth.superAdmins && touchesAdmin && !ctx.actorIsSuper) {
|
|
322
|
+
return { ok: false, status: 403, error: `only super-admins can create or change an ${auth.adminRole}` };
|
|
323
|
+
}
|
|
324
|
+
return { ok: true };
|
|
325
|
+
}
|
|
326
|
+
async function getVaultSecret(db, name) {
|
|
327
|
+
try {
|
|
328
|
+
const value = await db.secrets.get(name);
|
|
329
|
+
return typeof value === "string" && value !== "" ? value : void 0;
|
|
330
|
+
} catch {
|
|
331
|
+
return void 0;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// src/pipeline.ts
|
|
336
|
+
var DEFAULT_STAGES = [
|
|
337
|
+
"submitted",
|
|
338
|
+
"paid_pending_vetting",
|
|
339
|
+
"call_scheduled",
|
|
340
|
+
"interviewed",
|
|
341
|
+
"approved",
|
|
342
|
+
"declined",
|
|
343
|
+
"refunded"
|
|
344
|
+
];
|
|
345
|
+
var DEFAULT_BOOKABLE = ["submitted", "paid_pending_vetting", "call_scheduled"];
|
|
346
|
+
var DEFAULT_APPROVABLE = ["paid_pending_vetting", "call_scheduled", "interviewed"];
|
|
347
|
+
function resolvePipeline(p) {
|
|
348
|
+
const usingDefaults = !p?.stages;
|
|
349
|
+
const stages = p?.stages ?? [...DEFAULT_STAGES];
|
|
350
|
+
if (!Array.isArray(stages) || stages.length === 0 || !stages.every((s) => typeof s === "string" && s !== "")) {
|
|
351
|
+
throw new Error("defineChapter.pipeline.stages: must be a non-empty array of status strings");
|
|
352
|
+
}
|
|
353
|
+
if (new Set(stages).size !== stages.length) {
|
|
354
|
+
throw new Error("defineChapter.pipeline.stages: statuses must be unique");
|
|
355
|
+
}
|
|
356
|
+
const initial = p?.initial ?? stages[0];
|
|
357
|
+
if (!stages.includes(initial)) {
|
|
358
|
+
throw new Error(`defineChapter.pipeline.initial: "${initial}" is not one of the stages`);
|
|
359
|
+
}
|
|
360
|
+
const bookableFrom = p?.bookableFrom ?? (usingDefaults ? [...DEFAULT_BOOKABLE] : []);
|
|
361
|
+
const approvableFrom = p?.approvableFrom ?? (usingDefaults ? [...DEFAULT_APPROVABLE] : []);
|
|
362
|
+
for (const [name, subset] of [
|
|
363
|
+
["bookableFrom", bookableFrom],
|
|
364
|
+
["approvableFrom", approvableFrom]
|
|
365
|
+
]) {
|
|
366
|
+
for (const s of subset) {
|
|
367
|
+
if (!stages.includes(s)) throw new Error(`defineChapter.pipeline.${name}: "${s}" is not one of the stages`);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return { stages, bookableFrom, approvableFrom, initial };
|
|
371
|
+
}
|
|
372
|
+
function stageIndex(status, p) {
|
|
373
|
+
return p.stages.indexOf(status);
|
|
374
|
+
}
|
|
375
|
+
function canTransition(from, to, p) {
|
|
376
|
+
const fi = p.stages.indexOf(from);
|
|
377
|
+
const ti = p.stages.indexOf(to);
|
|
378
|
+
return fi >= 0 && ti >= 0 && ti >= fi;
|
|
379
|
+
}
|
|
380
|
+
function canBook(status, p) {
|
|
381
|
+
return p.bookableFrom.includes(status);
|
|
382
|
+
}
|
|
383
|
+
function canApprove(status, p) {
|
|
384
|
+
return p.approvableFrom.includes(status);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// src/member.ts
|
|
388
|
+
var DEFAULT_REQUIRED = ["firstName", "lastName", "email", "referral", "whoYouAre", "message"];
|
|
389
|
+
var DEFAULT_OPTIONAL = ["referralName", "linkedin", "phone", "state"];
|
|
390
|
+
function resolveApplication(a) {
|
|
391
|
+
const required = a?.required ?? DEFAULT_REQUIRED;
|
|
392
|
+
const optional = a?.optional ?? DEFAULT_OPTIONAL;
|
|
393
|
+
for (const [name, arr] of [["required", required], ["optional", optional]]) {
|
|
394
|
+
if (!Array.isArray(arr) || !arr.every((f) => typeof f === "string" && f !== "")) {
|
|
395
|
+
throw new Error(`defineChapter.application.${name}: must be an array of field-name strings`);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
required,
|
|
400
|
+
optional,
|
|
401
|
+
maxLen: a?.maxLen ?? {},
|
|
402
|
+
defaultMaxLen: a?.defaultMaxLen ?? 2e3,
|
|
403
|
+
bodyCap: a?.bodyCap ?? 32768
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
async function submitApplication(db, chapter, fields, opts) {
|
|
407
|
+
const app = chapter.application;
|
|
408
|
+
for (const f of app.required) {
|
|
409
|
+
const v = fields[f];
|
|
410
|
+
if (typeof v !== "string" || v.trim() === "") return { ok: false, error: `${f} is required` };
|
|
411
|
+
}
|
|
412
|
+
for (const f of [...app.required, ...app.optional]) {
|
|
413
|
+
const v = fields[f];
|
|
414
|
+
const cap = app.maxLen[f] ?? app.defaultMaxLen;
|
|
415
|
+
if (typeof v === "string" && v.length > cap) return { ok: false, error: `${f} exceeds ${cap} characters` };
|
|
416
|
+
}
|
|
417
|
+
const id2 = opts.newId();
|
|
418
|
+
const row = { id: id2, status: chapter.pipeline.initial, createdAt: opts.now };
|
|
419
|
+
for (const f of [...app.required, ...app.optional]) {
|
|
420
|
+
if (typeof fields[f] === "string") row[f] = fields[f].trim();
|
|
421
|
+
}
|
|
422
|
+
if (fields.focus !== void 0) row.focus = fields.focus;
|
|
423
|
+
if (opts.groupId) row.groupId = opts.groupId;
|
|
424
|
+
const { duplicate } = await db.transact(
|
|
425
|
+
[{ t: "update", ns: "applications", id: id2, attrs: row }],
|
|
426
|
+
opts.submissionId ? { mutationId: `join:${opts.submissionId}` } : void 0
|
|
427
|
+
);
|
|
428
|
+
return { ok: true, id: id2, duplicate, status: chapter.pipeline.initial };
|
|
429
|
+
}
|
|
430
|
+
function joinConfig(group, paymentsReady2) {
|
|
431
|
+
return {
|
|
432
|
+
id: group.id,
|
|
433
|
+
name: group.name,
|
|
434
|
+
standardPriceCents: group.standardPriceCents ?? 0,
|
|
435
|
+
foundingDiscountCents: group.foundingDiscountCents ?? 0,
|
|
436
|
+
disclaimerText: group.disclaimerText ?? "",
|
|
437
|
+
refundPolicyText: group.refundPolicyText ?? "",
|
|
438
|
+
trustCopy: group.trustCopy ?? "",
|
|
439
|
+
commitmentText: group.commitmentText ?? "",
|
|
440
|
+
normsText: group.normsText ?? "",
|
|
441
|
+
paymentsReady: paymentsReady2
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
|
|
267
445
|
// src/config.ts
|
|
268
446
|
var SLUG = /^[a-z0-9][a-z0-9-]{1,62}$/;
|
|
269
447
|
function isResolvedCrm(x) {
|
|
@@ -287,7 +465,10 @@ function defineChapter(config) {
|
|
|
287
465
|
throw new Error("defineChapter.prices.standardCents: required in chapter mode");
|
|
288
466
|
}
|
|
289
467
|
}
|
|
290
|
-
const
|
|
468
|
+
const auth = resolveAuth(mode, config.auth);
|
|
469
|
+
const pipeline = resolvePipeline(config.pipeline);
|
|
470
|
+
const application = resolveApplication(config.application);
|
|
471
|
+
const { schema, rules } = chapterDb(mode, auth);
|
|
291
472
|
const services = config.services ?? ["db", "calendar", "o11y"];
|
|
292
473
|
const chapter = {
|
|
293
474
|
config,
|
|
@@ -295,6 +476,9 @@ function defineChapter(config) {
|
|
|
295
476
|
name,
|
|
296
477
|
mode,
|
|
297
478
|
crm,
|
|
479
|
+
auth,
|
|
480
|
+
pipeline,
|
|
481
|
+
application,
|
|
298
482
|
schema,
|
|
299
483
|
rules,
|
|
300
484
|
services,
|
|
@@ -335,11 +519,355 @@ function createChapterIntegration(chapter, options = {}) {
|
|
|
335
519
|
probes: [...crmDesc.probes ?? []]
|
|
336
520
|
};
|
|
337
521
|
}
|
|
522
|
+
|
|
523
|
+
// src/email.ts
|
|
524
|
+
function render(template, vars) {
|
|
525
|
+
return template.replace(/\{\{(\w+)\}\}/g, (_, key) => vars[key] ?? "");
|
|
526
|
+
}
|
|
527
|
+
function groupVars(group, vars) {
|
|
528
|
+
return {
|
|
529
|
+
...vars,
|
|
530
|
+
refundPolicyText: group.refundPolicyText ?? "",
|
|
531
|
+
commitmentText: group.commitmentText ?? "",
|
|
532
|
+
normsText: group.normsText ?? ""
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
function renderTemplateBody(group, template, vars) {
|
|
536
|
+
const tpl = group.emailTemplates?.[template];
|
|
537
|
+
if (!tpl) return null;
|
|
538
|
+
return render(tpl.text, groupVars(group, vars));
|
|
539
|
+
}
|
|
540
|
+
function isAlreadySent(priorRows) {
|
|
541
|
+
return priorRows.some((row) => !row.error);
|
|
542
|
+
}
|
|
543
|
+
function planDelivery(input) {
|
|
544
|
+
const tpl = input.group.emailTemplates?.[input.template];
|
|
545
|
+
if (!tpl) return { deliver: false, reason: "template-missing" };
|
|
546
|
+
if (tpl.enabled === false && !input.force) return { deliver: false, reason: "disabled" };
|
|
547
|
+
const vars = groupVars(input.group, input.vars);
|
|
548
|
+
const isProd = input.envName === "prod";
|
|
549
|
+
const redirect = !isProd && !!input.group.debugEmail;
|
|
550
|
+
const transport = !isProd && !redirect ? "log-only" : input.cloudflareReady ? "cloudflare" : "log-only";
|
|
551
|
+
const to = redirect ? input.group.debugEmail : input.to;
|
|
552
|
+
const subject = (redirect ? "[dev] " : "") + render(tpl.subject, vars);
|
|
553
|
+
const text = redirect ? `(dev redirect; original recipient: ${input.to})
|
|
554
|
+
|
|
555
|
+
` + render(tpl.text, vars) : render(tpl.text, vars);
|
|
556
|
+
return { deliver: true, transport, to, subject, text, redirected: redirect };
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// src/payments.ts
|
|
560
|
+
function parseSigHeader(header) {
|
|
561
|
+
const parts = {};
|
|
562
|
+
for (const p of header.split(",")) {
|
|
563
|
+
const [k, v] = p.split("=", 2);
|
|
564
|
+
if (k && v !== void 0) parts[k] = v;
|
|
565
|
+
}
|
|
566
|
+
return { t: parts.t, v1: parts.v1 };
|
|
567
|
+
}
|
|
568
|
+
function toHex(buf) {
|
|
569
|
+
return [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
570
|
+
}
|
|
571
|
+
function timingSafeEqual(a, b) {
|
|
572
|
+
if (a.length !== b.length) return false;
|
|
573
|
+
let diff = 0;
|
|
574
|
+
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
575
|
+
return diff === 0;
|
|
576
|
+
}
|
|
577
|
+
async function verifyStripeSignature(payload, header, secret, opts = {}) {
|
|
578
|
+
const { t, v1 } = parseSigHeader(header);
|
|
579
|
+
if (!t || !v1) return false;
|
|
580
|
+
const ts = Number(t);
|
|
581
|
+
if (!Number.isFinite(ts)) return false;
|
|
582
|
+
const nowSec = (opts.now ?? Date.now()) / 1e3;
|
|
583
|
+
const tolerance = opts.toleranceSec ?? 300;
|
|
584
|
+
if (Math.abs(nowSec - ts) > tolerance) return false;
|
|
585
|
+
const enc = new TextEncoder();
|
|
586
|
+
const key = await crypto.subtle.importKey("raw", enc.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
587
|
+
const mac = await crypto.subtle.sign("HMAC", key, enc.encode(`${t}.${payload}`));
|
|
588
|
+
return timingSafeEqual(toHex(mac), v1);
|
|
589
|
+
}
|
|
590
|
+
function paymentsReady(group, hasSecretKey) {
|
|
591
|
+
return Boolean(group.stripePublishableKey && group.stripePriceId && hasSecretKey);
|
|
592
|
+
}
|
|
593
|
+
function stripeForm(params) {
|
|
594
|
+
const out = new URLSearchParams();
|
|
595
|
+
for (const [k, v] of Object.entries(params)) {
|
|
596
|
+
if (v === void 0 || v === null) continue;
|
|
597
|
+
if (typeof v === "object") {
|
|
598
|
+
for (const [k2, v2] of Object.entries(v)) {
|
|
599
|
+
if (v2 !== void 0 && v2 !== null) out.append(`${k}[${k2}]`, String(v2));
|
|
600
|
+
}
|
|
601
|
+
} else {
|
|
602
|
+
out.append(k, String(v));
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
return out.toString();
|
|
606
|
+
}
|
|
607
|
+
function subscriptionIdempotencyKey(applicationId) {
|
|
608
|
+
return `sub:${applicationId}`;
|
|
609
|
+
}
|
|
610
|
+
function webhookMutationId(eventId) {
|
|
611
|
+
return `stripe:${eventId}`;
|
|
612
|
+
}
|
|
613
|
+
function findApplicationRef(obj) {
|
|
614
|
+
const metaOf = (v) => v && typeof v === "object" ? v.metadata ?? {} : {};
|
|
615
|
+
const pick = (m) => typeof m.applicationId === "string" ? m.applicationId : void 0;
|
|
616
|
+
const applicationId = pick(metaOf(obj)) ?? pick(metaOf(obj.subscription_details)) ?? pick(metaOf(obj.parent?.subscription_details));
|
|
617
|
+
const customerId = typeof obj.customer === "string" ? obj.customer : void 0;
|
|
618
|
+
return { ...applicationId ? { applicationId } : {}, ...customerId ? { customerId } : {} };
|
|
619
|
+
}
|
|
620
|
+
function normalizeWebhookEvent(event) {
|
|
621
|
+
const obj = event.data?.object ?? {};
|
|
622
|
+
const ref = findApplicationRef(obj);
|
|
623
|
+
switch (event.type) {
|
|
624
|
+
case "invoice.paid": {
|
|
625
|
+
const lines = obj.lines?.data ?? [];
|
|
626
|
+
const periodEnd = lines[0]?.period?.end;
|
|
627
|
+
const renewalAt = typeof periodEnd === "number" ? periodEnd * 1e3 : void 0;
|
|
628
|
+
const kind = obj.billing_reason === "subscription_create" ? "first_payment" : "renewal";
|
|
629
|
+
return { kind, ...ref, ...renewalAt !== void 0 ? { renewalAt } : {} };
|
|
630
|
+
}
|
|
631
|
+
case "charge.refunded":
|
|
632
|
+
return { kind: "refunded", ...ref };
|
|
633
|
+
case "customer.subscription.deleted":
|
|
634
|
+
return { kind: "canceled", ...ref };
|
|
635
|
+
default:
|
|
636
|
+
return { kind: "ignored", type: event.type };
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
function firstPaymentPatch(currentStatus, renewalAt) {
|
|
640
|
+
return {
|
|
641
|
+
...currentStatus === "submitted" ? { status: "paid_pending_vetting" } : {},
|
|
642
|
+
...renewalAt !== void 0 ? { renewalAt } : {}
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
function renewalPatch(renewalAt) {
|
|
646
|
+
return { renewalAt };
|
|
647
|
+
}
|
|
648
|
+
function refundedPatch() {
|
|
649
|
+
return { status: "refunded" };
|
|
650
|
+
}
|
|
651
|
+
function canceledPatch() {
|
|
652
|
+
return { canceled: true };
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// src/network.ts
|
|
656
|
+
import { createRecord, updateRecord } from "@odla-ai/crm";
|
|
657
|
+
function sharedPersonInput(person) {
|
|
658
|
+
const email = person.email.toLowerCase();
|
|
659
|
+
const fullName = [person.firstName, person.lastName].filter(Boolean).join(" ").trim();
|
|
660
|
+
const input = { name: person.name ?? fullName ?? email, email };
|
|
661
|
+
if (input.name === "") input.name = email;
|
|
662
|
+
if (person.firstName) input.firstName = person.firstName;
|
|
663
|
+
if (person.lastName) input.lastName = person.lastName;
|
|
664
|
+
if (person.phone) input.phone = person.phone;
|
|
665
|
+
if (person.linkedin) input.linkedin = person.linkedin;
|
|
666
|
+
return input;
|
|
667
|
+
}
|
|
668
|
+
async function projectSharedRecord(deps, person) {
|
|
669
|
+
const email = person.email.toLowerCase();
|
|
670
|
+
const input = sharedPersonInput(person);
|
|
671
|
+
const crmDeps = { crm: deps.crm, db: deps.db, now: deps.now, newId: deps.newId };
|
|
672
|
+
const { crm_record } = await deps.db.query({
|
|
673
|
+
crm_record: { $: { where: { type: "person", primaryEmail: email }, limit: 1 } }
|
|
674
|
+
});
|
|
675
|
+
const existing = crm_record?.[0];
|
|
676
|
+
if (existing && typeof existing.id === "string") {
|
|
677
|
+
await updateRecord(crmDeps, { id: existing.id, input });
|
|
678
|
+
return { recordId: existing.id };
|
|
679
|
+
}
|
|
680
|
+
const created = await createRecord(crmDeps, { type: "person", input, mutationId: `share:${person.hubRecordId}` });
|
|
681
|
+
return { recordId: created.id };
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// src/session.ts
|
|
685
|
+
function applicationSummary(app) {
|
|
686
|
+
return {
|
|
687
|
+
id: app.id,
|
|
688
|
+
firstName: app.firstName ?? null,
|
|
689
|
+
lastName: app.lastName ?? null,
|
|
690
|
+
email: app.email ?? null,
|
|
691
|
+
status: app.status,
|
|
692
|
+
createdAt: app.createdAt ?? null,
|
|
693
|
+
meetingLink: app.meetingLink ?? null,
|
|
694
|
+
paid: Boolean(app.stripeSubscriptionId) && app.status !== "refunded",
|
|
695
|
+
renewalAt: app.renewalAt ?? null,
|
|
696
|
+
canceled: app.canceled === true
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
function memberApplication(app, meeting, defaultTimezone) {
|
|
700
|
+
const summary = applicationSummary(app);
|
|
701
|
+
let meetingAt = app.meetingAt ?? null;
|
|
702
|
+
let meetUrl = null;
|
|
703
|
+
let timezone = defaultTimezone;
|
|
704
|
+
if (meeting) {
|
|
705
|
+
timezone = meeting.timezone ?? timezone;
|
|
706
|
+
if (meeting.status === "scheduled") {
|
|
707
|
+
meetingAt = meeting.startAt ?? null;
|
|
708
|
+
meetUrl = meeting.meetUrl ?? null;
|
|
709
|
+
} else {
|
|
710
|
+
meetingAt = null;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
return { ...summary, meetingAt, meetUrl, timezone };
|
|
714
|
+
}
|
|
715
|
+
function memberSession(user, opts) {
|
|
716
|
+
return {
|
|
717
|
+
userId: user.userId,
|
|
718
|
+
email: user.email ?? null,
|
|
719
|
+
role: user.role,
|
|
720
|
+
superAdmin: opts.superAdmin,
|
|
721
|
+
application: opts.application
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
// src/scheduling.ts
|
|
726
|
+
var SCHEDULING_DEFAULTS = {
|
|
727
|
+
slotMinutes: 45,
|
|
728
|
+
days: [1, 2, 3, 4, 5],
|
|
729
|
+
startHour: 9,
|
|
730
|
+
endHour: 17,
|
|
731
|
+
timezone: "America/Los_Angeles",
|
|
732
|
+
minNoticeHours: 24,
|
|
733
|
+
windowDays: 14,
|
|
734
|
+
summaryTemplate: "Introduction call with {{firstName}} {{lastName}}"
|
|
735
|
+
};
|
|
736
|
+
function isValidTimeZone(tz) {
|
|
737
|
+
try {
|
|
738
|
+
new Intl.DateTimeFormat(void 0, { timeZone: tz });
|
|
739
|
+
return true;
|
|
740
|
+
} catch {
|
|
741
|
+
return false;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
function resolveScheduling(config) {
|
|
745
|
+
const d = config ?? {};
|
|
746
|
+
const c = {
|
|
747
|
+
slotMinutes: d.slotMinutes ?? SCHEDULING_DEFAULTS.slotMinutes,
|
|
748
|
+
days: d.days ?? SCHEDULING_DEFAULTS.days,
|
|
749
|
+
startHour: d.startHour ?? SCHEDULING_DEFAULTS.startHour,
|
|
750
|
+
endHour: d.endHour ?? SCHEDULING_DEFAULTS.endHour,
|
|
751
|
+
timezone: d.timezone ?? SCHEDULING_DEFAULTS.timezone,
|
|
752
|
+
minNoticeHours: d.minNoticeHours ?? SCHEDULING_DEFAULTS.minNoticeHours,
|
|
753
|
+
windowDays: d.windowDays ?? SCHEDULING_DEFAULTS.windowDays,
|
|
754
|
+
summaryTemplate: d.summaryTemplate ?? SCHEDULING_DEFAULTS.summaryTemplate
|
|
755
|
+
};
|
|
756
|
+
const fail = (msg) => {
|
|
757
|
+
throw new Error(`scheduling: ${msg}`);
|
|
758
|
+
};
|
|
759
|
+
if (!(c.slotMinutes >= 15 && c.slotMinutes <= 240)) fail("slotMinutes must be 15\u2013240");
|
|
760
|
+
if (!(c.windowDays >= 1 && c.windowDays <= 62)) fail("windowDays must be 1\u201362 (FreeBusy caps at 62)");
|
|
761
|
+
if (!(c.minNoticeHours >= 0 && c.minNoticeHours <= 336)) fail("minNoticeHours must be 0\u2013336");
|
|
762
|
+
if (!(c.startHour >= 0 && c.startHour < c.endHour && c.endHour <= 24)) fail("require 0 \u2264 startHour < endHour \u2264 24");
|
|
763
|
+
const days = [...c.days];
|
|
764
|
+
if (!days.length || !days.every((n) => Number.isInteger(n) && n >= 0 && n <= 6)) {
|
|
765
|
+
fail("days must be a non-empty list of weekday integers 0\u20136");
|
|
766
|
+
}
|
|
767
|
+
if (typeof c.timezone !== "string" || !isValidTimeZone(c.timezone)) fail(`invalid IANA timezone "${c.timezone}"`);
|
|
768
|
+
if (typeof c.summaryTemplate !== "string") fail("summaryTemplate must be a string");
|
|
769
|
+
return { ...c, days };
|
|
770
|
+
}
|
|
771
|
+
var BOOKABLE_STATUSES = ["submitted", "paid_pending_vetting", "call_scheduled"];
|
|
772
|
+
function canBookFrom(status) {
|
|
773
|
+
return BOOKABLE_STATUSES.includes(status);
|
|
774
|
+
}
|
|
775
|
+
function slotWindow(now, windowDays) {
|
|
776
|
+
return { from: now, to: now + windowDays * 864e5 };
|
|
777
|
+
}
|
|
778
|
+
function endForSlot(startAt, slotMinutes) {
|
|
779
|
+
return startAt + slotMinutes * 6e4;
|
|
780
|
+
}
|
|
781
|
+
function isSlotAvailable(slots, startAt) {
|
|
782
|
+
return slots.some((s) => s.startAt === startAt);
|
|
783
|
+
}
|
|
784
|
+
function renderSummary(template, app) {
|
|
785
|
+
return template.replace("{{firstName}}", app.firstName ?? "").replace("{{lastName}}", app.lastName ?? "");
|
|
786
|
+
}
|
|
787
|
+
function bookingDecision(existing) {
|
|
788
|
+
const eventId = existing?.googleEventId ?? null;
|
|
789
|
+
return { reschedule: Boolean(eventId), eventId };
|
|
790
|
+
}
|
|
791
|
+
function introIdempotencyKey(applicationId) {
|
|
792
|
+
return `application:${applicationId}:intro`;
|
|
793
|
+
}
|
|
794
|
+
function meetingCreateRow(i) {
|
|
795
|
+
return {
|
|
796
|
+
id: i.meetingId,
|
|
797
|
+
applicationId: i.applicationId,
|
|
798
|
+
groupId: i.groupId,
|
|
799
|
+
startAt: i.startAt,
|
|
800
|
+
endAt: i.endAt,
|
|
801
|
+
timezone: i.timezone,
|
|
802
|
+
status: "scheduled",
|
|
803
|
+
googleEventId: i.googleEventId,
|
|
804
|
+
...i.meetUrl ? { meetUrl: i.meetUrl } : {},
|
|
805
|
+
...i.htmlLink ? { htmlLink: i.htmlLink } : {},
|
|
806
|
+
drift: "none",
|
|
807
|
+
createdAt: i.createdAt
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
function meetingRescheduleUpdate(startAt, endAt) {
|
|
811
|
+
return { startAt, endAt, drift: "none" };
|
|
812
|
+
}
|
|
813
|
+
function applicationBookingUpdate(currentStatus, startAt, htmlLink) {
|
|
814
|
+
return {
|
|
815
|
+
meetingAt: startAt,
|
|
816
|
+
...htmlLink ? { meetingLink: htmlLink } : {},
|
|
817
|
+
...currentStatus !== "call_scheduled" ? { status: "call_scheduled" } : {}
|
|
818
|
+
};
|
|
819
|
+
}
|
|
338
820
|
export {
|
|
821
|
+
BOOKABLE_STATUSES,
|
|
822
|
+
SCHEDULING_DEFAULTS,
|
|
823
|
+
applicationBookingUpdate,
|
|
824
|
+
applicationSummary,
|
|
825
|
+
bookingDecision,
|
|
339
826
|
buildGroupSeed,
|
|
827
|
+
canApprove,
|
|
828
|
+
canBook,
|
|
829
|
+
canBookFrom,
|
|
830
|
+
canChangeRole,
|
|
831
|
+
canTransition,
|
|
832
|
+
canceledPatch,
|
|
340
833
|
chapterDb,
|
|
341
834
|
createChapterIntegration,
|
|
342
835
|
defaultCrm,
|
|
343
|
-
defineChapter
|
|
836
|
+
defineChapter,
|
|
837
|
+
endForSlot,
|
|
838
|
+
findApplicationRef,
|
|
839
|
+
firstPaymentPatch,
|
|
840
|
+
getVaultSecret,
|
|
841
|
+
introIdempotencyKey,
|
|
842
|
+
isAdminRole,
|
|
843
|
+
isAlreadySent,
|
|
844
|
+
isSlotAvailable,
|
|
845
|
+
joinConfig,
|
|
846
|
+
meetingCreateRow,
|
|
847
|
+
meetingRescheduleUpdate,
|
|
848
|
+
memberApplication,
|
|
849
|
+
memberSession,
|
|
850
|
+
normalizeWebhookEvent,
|
|
851
|
+
paymentsReady,
|
|
852
|
+
planDelivery,
|
|
853
|
+
projectSharedRecord,
|
|
854
|
+
refundedPatch,
|
|
855
|
+
render,
|
|
856
|
+
renderSummary,
|
|
857
|
+
renderTemplateBody,
|
|
858
|
+
renewalPatch,
|
|
859
|
+
resolveApplication,
|
|
860
|
+
resolveAuth,
|
|
861
|
+
resolvePipeline,
|
|
862
|
+
resolveScheduling,
|
|
863
|
+
roleFromClaim,
|
|
864
|
+
sharedPersonInput,
|
|
865
|
+
slotWindow,
|
|
866
|
+
stageIndex,
|
|
867
|
+
stripeForm,
|
|
868
|
+
submitApplication,
|
|
869
|
+
subscriptionIdempotencyKey,
|
|
870
|
+
verifyStripeSignature,
|
|
871
|
+
webhookMutationId
|
|
344
872
|
};
|
|
345
873
|
//# sourceMappingURL=index.js.map
|