@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/worker/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/worker.ts"],"sourcesContent":["// chapterWorker — the Cloudflare Worker for a chapter/hub site. This entry\n// (@odla-ai/chapter/worker) is separate from the core so the CLI can load\n// odla.config.mjs without pulling in worker-runtime deps.\n//\n// The worker is the ONLY thing that talks to odla-db, using the app key\n// (ODLA_API_KEY), which bypasses the deny-all rules. Browsers never receive a\n// db credential. Access is admin-only: a request is authorized when its Clerk\n// session JWT verifies AND the user's lowercased email has a row in the\n// Studio-seeded `admins` allowlist.\n//\n// hub mode routes: GET /api/config, GET /api/me, /api/crm/*, else ASSETS.\n// chapter mode adds the public member/join/Stripe/booking surface (ported next).\nimport { initAdmin } from \"@odla-ai/db\";\nimport { createCrmRoutes } from \"@odla-ai/crm\";\nimport { createRemoteJWKSet, jwtVerify } from \"jose\";\nimport type { Chapter } from \"./types\";\n\ninterface EmailPayload {\n from: string;\n to: string[];\n subject: string;\n text?: string;\n html?: string;\n replyTo?: string;\n headers?: Record<string, string>;\n}\n\n/** The Worker env a chapter site provides (wrangler vars + the ODLA_API_KEY\n * secret pushed by provision). */\nexport interface ChapterEnv {\n ASSETS: { fetch(req: Request): Promise<Response> };\n ODLA_ENDPOINT: string;\n ODLA_TENANT: string;\n ODLA_PLATFORM: string;\n ODLA_APP_ID: string;\n ODLA_ENV: string;\n ODLA_API_KEY: string;\n SEND_EMAIL?: { send(payload: EmailPayload): Promise<{ messageId: string }> };\n EMAIL_FROM?: string;\n}\n\n/** Options for {@link chapterWorker}. */\nexport interface ChapterWorkerOptions {\n chapter: Chapter;\n /** CRM mount point. Default \"/api/crm\". */\n crmBasePath?: string;\n}\n\ntype PublicConfig = { env?: string; clerkPublishableKey?: string | null; issuer?: string | null };\ntype Db = ReturnType<typeof initAdmin>;\n\nconst json = (body: unknown, status = 200): Response =>\n new Response(JSON.stringify(body), { status, headers: { \"content-type\": \"application/json\" } });\n\n/**\n * Build the Cloudflare `ExportedHandler` for a chapter/hub site: Clerk-JWT\n * verification, the odla-db admins-allowlist gate, the mounted @odla-ai/crm\n * routes, and the static-asset fallback. In hub mode it serves /api/config,\n * /api/me, /api/crm/*; chapter mode adds the public member surface\n * (join/Stripe/booking — ported next).\n *\n * Observability is a host concern, not a chapter dependency. To trace, wrap the\n * result in your worker entry — `export default withObservability(chapterWorker(\n * { chapter }))` — with `withObservability` from `@odla-ai/o11y`. Sites that\n * don't run o11y bundle `@odla-ai/chapter/worker` without installing it.\n */\nexport function chapterWorker(options: ChapterWorkerOptions) {\n const { chapter } = options;\n const crmBase = options.crmBasePath ?? \"/api/crm\";\n\n let publicConfigCache: { value: PublicConfig; at: number } | null = null;\n const jwksByIssuer = new Map<string, ReturnType<typeof createRemoteJWKSet>>();\n\n async function getPublicConfig(env: ChapterEnv): Promise<PublicConfig> {\n if (publicConfigCache && Date.now() - publicConfigCache.at < 5 * 60_000) return publicConfigCache.value;\n const res = await fetch(\n `${env.ODLA_PLATFORM}/registry/apps/${env.ODLA_APP_ID}/public-config?env=${env.ODLA_ENV}`,\n );\n if (!res.ok) throw new Error(`public-config fetch failed: ${res.status}`);\n const value = (await res.json()) as PublicConfig;\n publicConfigCache = { value, at: Date.now() };\n return value;\n }\n\n async function verifyUser(req: Request, env: ChapterEnv): Promise<{ userId: string; email?: string } | null> {\n const header = req.headers.get(\"authorization\") ?? \"\";\n if (!header.startsWith(\"Bearer \")) return null;\n const token = header.slice(7);\n const { issuer } = await getPublicConfig(env);\n if (!issuer) return null;\n let jwks = jwksByIssuer.get(issuer);\n if (!jwks) {\n jwks = createRemoteJWKSet(new URL(`${issuer}/.well-known/jwks.json`));\n jwksByIssuer.set(issuer, jwks);\n }\n try {\n const { payload } = await jwtVerify(token, jwks, { issuer });\n if (!payload.sub) return null;\n return { userId: payload.sub, email: typeof payload.email === \"string\" ? payload.email : undefined };\n } catch {\n return null;\n }\n }\n\n function makeDb(env: ChapterEnv): Db {\n return initAdmin({ appId: env.ODLA_TENANT, adminToken: env.ODLA_API_KEY, endpoint: env.ODLA_ENDPOINT });\n }\n\n // The allowlist gate: no route ever writes `admins`, so membership can only be\n // granted by a human in odla Studio.\n async function isAdminEmail(db: Db, email: string | undefined): Promise<boolean> {\n if (!email) return false;\n const { admins } = await db.query({ admins: { $: { where: { email: email.toLowerCase() }, limit: 1 } } });\n return Array.isArray(admins) && admins.length > 0;\n }\n\n function crmSender(env: ChapterEnv) {\n if (!env.SEND_EMAIL || !env.EMAIL_FROM) return undefined;\n const binding = env.SEND_EMAIL;\n return { async send(payload: EmailPayload): Promise<{ messageId: string }> { return binding.send(payload); } };\n }\n\n const handler = {\n async fetch(req: Request, env: ChapterEnv): Promise<Response> {\n const url = new URL(req.url);\n\n // Public: the SPA reads the Clerk publishable key to boot sign-in.\n if (url.pathname === \"/api/config\") {\n try {\n const { clerkPublishableKey } = await getPublicConfig(env);\n return json({ clerkPublishableKey: clerkPublishableKey ?? null, env: env.ODLA_ENV });\n } catch {\n return json({ clerkPublishableKey: null, env: env.ODLA_ENV });\n }\n }\n\n // Auth: is the signed-in user an allowlisted admin?\n if (url.pathname === \"/api/me\") {\n const u = await verifyUser(req, env);\n if (!u) return json({ authorized: false }, 401);\n const authorized = await isAdminEmail(makeDb(env), u.email);\n return json({ authorized, email: u.email ?? null });\n }\n\n // CRM admin surface.\n if (url.pathname === crmBase || url.pathname.startsWith(crmBase + \"/\")) {\n const db = makeDb(env);\n const routes = createCrmRoutes({\n crm: chapter.crm,\n db: db as never,\n authorize: async (r: Request) => {\n const u = await verifyUser(r, env);\n if (!u || !(await isAdminEmail(db, u.email))) return null;\n return u.email ? { userId: u.userId, email: u.email } : { userId: u.userId };\n },\n sender: crmSender(env),\n from: env.EMAIL_FROM,\n envName: env.ODLA_ENV,\n baseUrl: url.origin,\n basePath: crmBase,\n });\n const res = await routes(req);\n if (res) return res;\n return json({ error: \"not found\" }, 404);\n }\n\n // chapter mode adds the public member/join/Stripe/booking routes here.\n\n // Everything else is the static site.\n return env.ASSETS.fetch(req);\n },\n };\n\n return handler;\n}\n"],"mappings":";AAYA,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;AAChC,SAAS,oBAAoB,iBAAiB;AAqC9C,IAAM,OAAO,CAAC,MAAe,SAAS,QACpC,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG,EAAE,QAAQ,SAAS,EAAE,gBAAgB,mBAAmB,EAAE,CAAC;AAczF,SAAS,cAAc,SAA+B;AAC3D,QAAM,EAAE,QAAQ,IAAI;AACpB,QAAM,UAAU,QAAQ,eAAe;AAEvC,MAAI,oBAAgE;AACpE,QAAM,eAAe,oBAAI,IAAmD;AAE5E,iBAAe,gBAAgB,KAAwC;AACrE,QAAI,qBAAqB,KAAK,IAAI,IAAI,kBAAkB,KAAK,IAAI,IAAQ,QAAO,kBAAkB;AAClG,UAAM,MAAM,MAAM;AAAA,MAChB,GAAG,IAAI,aAAa,kBAAkB,IAAI,WAAW,sBAAsB,IAAI,QAAQ;AAAA,IACzF;AACA,QAAI,CAAC,IAAI,GAAI,OAAM,IAAI,MAAM,+BAA+B,IAAI,MAAM,EAAE;AACxE,UAAM,QAAS,MAAM,IAAI,KAAK;AAC9B,wBAAoB,EAAE,OAAO,IAAI,KA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IAAI;AAAA,UACb,SAAS,IAAI;AAAA,UACb,UAAU;AAAA,QACZ,CAAC;AACD,cAAM,MAAM,MAAM,OAAO,GAAG;AAC5B,YAAI,IAAK,QAAO;AAChB,eAAO,KAAK,EAAE,OAAO,YAAY,GAAG,GAAG;AAAA,MACzC;AAKA,aAAO,IAAI,OAAO,MAAM,GAAG;AAAA,IAC7B;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/worker-context.ts","../../src/auth.ts","../../src/worker-routes.ts","../../src/member.ts","../../src/network.ts","../../src/scheduling.ts","../../src/session.ts","../../src/worker-routes-schedule.ts","../../src/payments.ts","../../src/payments-stripe.ts","../../src/worker-routes-payments.ts","../../src/worker.ts"],"sourcesContent":["// Shared context for the chapter Worker: env typing, the odla-db admin client,\n// Clerk-JWT verification, and the source-aware auth helpers. Split out of\n// worker.ts so the route modules share ONE construction of the caches (public\n// config + JWKS) per site, and the worker entry stays a thin composer under the\n// per-file LOC cap. Nothing here is re-exported from the worker entry, so the\n// public `@odla-ai/chapter/worker` surface is unchanged by the split.\nimport { initAdmin } from \"@odla-ai/db\";\nimport { createRemoteJWKSet, jwtVerify } from \"jose\";\nimport { isAdminRole, roleFromClaim } from \"./auth\";\nimport type { Chapter } from \"./types\";\n\n/** The Cloudflare SEND_EMAIL binding payload. */\nexport interface EmailPayload {\n from: string;\n to: string[];\n subject: string;\n text?: string;\n html?: string;\n replyTo?: string;\n headers?: Record<string, string>;\n}\n\n/** The Worker env a chapter site provides (wrangler vars + the ODLA_API_KEY\n * secret pushed by provision). */\nexport interface ChapterEnv {\n ASSETS: { fetch(req: Request): Promise<Response> };\n ODLA_ENDPOINT: string;\n ODLA_TENANT: string;\n ODLA_PLATFORM: string;\n ODLA_APP_ID: string;\n ODLA_ENV: string;\n ODLA_API_KEY: string;\n SEND_EMAIL?: { send(payload: EmailPayload): Promise<{ messageId: string }> };\n EMAIL_FROM?: string;\n}\n\n/** Options for `chapterWorker`. */\nexport interface ChapterWorkerOptions {\n chapter: Chapter;\n /** CRM mount point. Default \"/api/crm\". */\n crmBasePath?: string;\n}\n\n/** The registry public-config a site reads to boot Clerk sign-in. */\nexport type PublicConfig = { env?: string; clerkPublishableKey?: string | null; issuer?: string | null };\n/** The odla-db admin client type. */\nexport type Db = ReturnType<typeof initAdmin>;\n\n/** A verified session: the Clerk `sub`, optional email, and the raw JWT payload\n * (so the role claim can be read for auth source \"claim\"). */\nexport interface Verified {\n userId: string;\n email?: string;\n payload: Record<string, unknown>;\n}\n\n/** JSON response helper. */\nexport const json = (body: unknown, status = 200): Response =>\n new Response(JSON.stringify(body), { status, headers: { \"content-type\": \"application/json\" } });\n\n/**\n * Build the per-site Worker context: env-independent helpers closing over the\n * public-config and JWKS caches, plus the source-aware auth gate (JWT claim or\n * the odla-db `admins` allowlist). Constructed once per `chapterWorker` and\n * shared by every route module.\n */\nexport function createWorkerContext(options: ChapterWorkerOptions) {\n const { chapter } = options;\n const auth = chapter.auth;\n const crmBase = options.crmBasePath ?? \"/api/crm\";\n\n let publicConfigCache: { value: PublicConfig; at: number } | null = null;\n const jwksByIssuer = new Map<string, ReturnType<typeof createRemoteJWKSet>>();\n\n async function getPublicConfig(env: ChapterEnv): Promise<PublicConfig> {\n if (publicConfigCache && Date.now() - publicConfigCache.at < 5 * 60_000) return publicConfigCache.value;\n const res = await fetch(`${env.ODLA_PLATFORM}/registry/apps/${env.ODLA_APP_ID}/public-config?env=${env.ODLA_ENV}`);\n if (!res.ok) throw new Error(`public-config fetch failed: ${res.status}`);\n const value = (await res.json()) as PublicConfig;\n publicConfigCache = { value, at: Date.now() };\n return value;\n }\n\n async function verifyUser(req: Request, env: ChapterEnv): Promise<Verified | null> {\n const header = req.headers.get(\"authorization\") ?? \"\";\n if (!header.startsWith(\"Bearer \")) return null;\n const token = header.slice(7);\n const { issuer } = await getPublicConfig(env);\n if (!issuer) return null;\n let jwks = jwksByIssuer.get(issuer);\n if (!jwks) {\n jwks = createRemoteJWKSet(new URL(`${issuer}/.well-known/jwks.json`));\n jwksByIssuer.set(issuer, jwks);\n }\n try {\n const { payload } = await jwtVerify(token, jwks, { issuer });\n if (!payload.sub) return null;\n return {\n userId: payload.sub,\n email: typeof payload.email === \"string\" ? payload.email : undefined,\n payload: payload as Record<string, unknown>,\n };\n } catch {\n return null;\n }\n }\n\n function makeDb(env: ChapterEnv): Db {\n return initAdmin({ appId: env.ODLA_TENANT, adminToken: env.ODLA_API_KEY, endpoint: env.ODLA_ENDPOINT });\n }\n\n // The `admins` allowlist gate (auth source \"table\"): no route ever writes it, so\n // membership can only be granted by a human in odla Studio.\n async function isAdminEmail(db: Db, email: string | undefined): Promise<boolean> {\n if (!email) return false;\n const { admins } = await db.query({ admins: { $: { where: { email: email.toLowerCase() }, limit: 1 } } });\n return Array.isArray(admins) && admins.length > 0;\n }\n\n // The read-only `superAdmins` tier — queried, never written (Studio-only).\n async function isSuperAdminEmail(db: Db, email: string | undefined): Promise<boolean> {\n if (!auth.superAdmins || !email) return false;\n const { superAdmins } = await db.query({ superAdmins: { $: { where: { email: email.toLowerCase() }, limit: 1 } } });\n return Array.isArray(superAdmins) && superAdmins.length > 0;\n }\n\n // The user's role, resolved per the auth source: a JWT claim, or the `admins`\n // allowlist synthesized to the admin rung / the lowest one.\n async function roleFor(db: Db, u: Verified): Promise<string> {\n if (auth.source === \"claim\") return roleFromClaim(u.payload, auth);\n return (await isAdminEmail(db, u.email)) ? auth.adminRole : (auth.ladder[0] as string);\n }\n\n // Admin authorization — the boolean gate used by /api/me and the CRM surface.\n async function isAdmin(db: Db, u: Verified): Promise<boolean> {\n if (auth.source === \"claim\") return isAdminRole(roleFromClaim(u.payload, auth), auth);\n return isAdminEmail(db, u.email);\n }\n\n function crmSender(env: ChapterEnv) {\n if (!env.SEND_EMAIL || !env.EMAIL_FROM) return undefined;\n const binding = env.SEND_EMAIL;\n return {\n async send(payload: EmailPayload): Promise<{ messageId: string }> {\n return binding.send(payload);\n },\n };\n }\n\n return { chapter, auth, crmBase, getPublicConfig, verifyUser, makeDb, isAdminEmail, isSuperAdminEmail, roleFor, isAdmin, crmSender };\n}\n\n/** The value returned by {@link createWorkerContext}, threaded to every route. */\nexport type WorkerContext = ReturnType<typeof createWorkerContext>;\n","// Identity + authorization for a chapter/hub site — the pieces every membership\n// site needs and none should re-derive: a resolved role policy, role resolution\n// from a JWT claim, the privilege-escalation guard, and a tenant-vault read.\n// Everything here is pure or structural (no runtime @odla-ai/db import), so it is\n// trivially testable and the worker stays the only thing that talks to odla-db.\nimport type { ChapterAuth, ChapterMode, ResolvedAuth } from \"./types\";\n\n/**\n * Apply defaults + validate the auth config into a {@link ResolvedAuth}. Defaults\n * by mode: `chapter` → the `provisional/member/admin` claim ladder with the\n * `superAdmins` tier (Silver & Salt); `hub` → the `admins` allowlist table, no\n * super tier (Built Not Found). Throws at import on a bad policy.\n */\nexport function resolveAuth(mode: ChapterMode, auth: ChapterAuth | undefined): ResolvedAuth {\n const a = auth ?? {};\n const source = a.source ?? (mode === \"hub\" ? \"table\" : \"claim\");\n if (source !== \"claim\" && source !== \"table\") {\n throw new Error(`defineChapter.auth.source: must be \"claim\" or \"table\" — got ${JSON.stringify(a.source)}`);\n }\n const claim = a.claim ?? \"role\";\n if (typeof claim !== \"string\" || claim === \"\") {\n throw new Error(\"defineChapter.auth.claim: must be a non-empty string\");\n }\n const ladder = a.ladder ?? [\"provisional\", \"member\", \"admin\"];\n if (!Array.isArray(ladder) || ladder.length === 0 || !ladder.every((r) => typeof r === \"string\" && r !== \"\")) {\n throw new Error(\"defineChapter.auth.ladder: must be a non-empty array of role strings\");\n }\n const adminRole = ladder[ladder.length - 1] as string;\n const superAdmins = a.superAdmins ?? source === \"claim\";\n return { source, claim, ladder, adminRole, superAdmins };\n}\n\n/** The role from a verified JWT payload, per the resolved policy. An unknown or\n * missing claim falls back to the lowest ladder rung (fail safe, never admin). */\nexport function roleFromClaim(payload: Record<string, unknown>, auth: ResolvedAuth): string {\n const raw = payload[auth.claim];\n return typeof raw === \"string\" && auth.ladder.includes(raw) ? raw : (auth.ladder[0] as string);\n}\n\n/** Does a role meet the admin bar (the highest ladder rung)? */\nexport function isAdminRole(role: string, auth: ResolvedAuth): boolean {\n return role === auth.adminRole;\n}\n\n/** Inputs to the role-change guard — resolved by the caller (route) from the\n * identity provider + the read-only `superAdmins` table. */\nexport interface RoleChangeContext {\n actorId: string;\n actorIsSuper: boolean;\n targetId: string;\n targetCurrentRole: string;\n targetIsSuper: boolean;\n newRole: string;\n auth: ResolvedAuth;\n}\n\n/** The result of {@link canChangeRole}: allow, or deny with the HTTP status +\n * message the route should return. */\nexport type GuardResult = { ok: true } | { ok: false; status: number; error: string };\n\n/**\n * The privilege-escalation guard — package-enforced so every site gets it and\n * none re-derives it. Denies: an out-of-ladder role; changing your own role;\n * touching a super-admin unless you are one; and (when a `superAdmins` tier\n * exists) creating or altering an admin unless you are a super-admin. Note the\n * super-admin tier itself is never writable here — it lives in the read-only\n * `superAdmins` table, set only in odla Studio.\n */\nexport function canChangeRole(ctx: RoleChangeContext): GuardResult {\n const { auth } = ctx;\n if (!auth.ladder.includes(ctx.newRole)) {\n return { ok: false, status: 400, error: `role must be one of: ${auth.ladder.join(\", \")}` };\n }\n if (ctx.actorId === ctx.targetId) {\n return { ok: false, status: 400, error: \"you cannot change your own role\" };\n }\n if (ctx.targetIsSuper && !ctx.actorIsSuper) {\n return { ok: false, status: 403, error: \"this person is a super-admin; their access is managed in odla Studio\" };\n }\n const touchesAdmin = ctx.newRole === auth.adminRole || ctx.targetCurrentRole === auth.adminRole;\n if (auth.superAdmins && touchesAdmin && !ctx.actorIsSuper) {\n return { ok: false, status: 403, error: `only super-admins can create or change an ${auth.adminRole}` };\n }\n return { ok: true };\n}\n\n/** Structural view of odla-db's tenant-vault read, so chapter takes no runtime\n * dependency on @odla-ai/db. The worker's admin client satisfies this. */\nexport interface SecretStore {\n secrets: { get(name: string): Promise<string> };\n}\n\n/**\n * Read a tenant-vault secret by name; `undefined` when it is absent or the vault\n * errors, so callers degrade gracefully (e.g. `paymentsReady: false`) rather than\n * throwing. Never logs the value.\n */\nexport async function getVaultSecret(db: SecretStore, name: string): Promise<string | undefined> {\n try {\n const value = await db.secrets.get(name);\n return typeof value === \"string\" && value !== \"\" ? value : undefined;\n } catch {\n return undefined;\n }\n}\n","// Route handlers for the chapter Worker. Each returns a Response when it owns the\n// request, or null to fall through to the next handler (finally ASSETS). Split\n// out of worker.ts so the entry stays a thin composer under the per-file LOC cap;\n// behaviour and route order are unchanged from the original single-file handler.\nimport { createCrmRoutes } from \"@odla-ai/crm\";\nimport { getVaultSecret, isAdminRole } from \"./auth\";\nimport { joinConfig, submitApplication } from \"./member\";\nimport { projectSharedRecord } from \"./network\";\nimport { resolveScheduling } from \"./scheduling\";\nimport { memberApplication } from \"./session\";\nimport type { ApplicationRecord, MeetingRecord, MemberApplication } from \"./session\";\nimport { json } from \"./worker-context\";\nimport type { ChapterEnv, WorkerContext } from \"./worker-context\";\nimport type { ChapterDb, ChapterScheduling } from \"./types\";\n\n// The signed-in member's own application (chapter mode): the latest by email,\n// folded with its latest scheduled meeting and rendered in the group's timezone.\n// The meetings row is canonical, so no live-calendar reconcile is needed here.\nasync function memberSessionApplication(db: ChapterDb, chapterId: string, email: string): Promise<MemberApplication | null> {\n const apps = (await db.query({ applications: { $: { where: { email }, order: { createdAt: \"desc\" }, limit: 1 } } })).applications;\n const app = Array.isArray(apps) ? apps[0] : undefined;\n if (!app) return null;\n const meetings = (\n await db.query({ meetings: { $: { where: { applicationId: app.id, status: \"scheduled\" }, order: { createdAt: \"desc\" }, limit: 1 } } })\n ).meetings;\n const meeting = Array.isArray(meetings) ? meetings[0] : undefined;\n const groups = (await db.query({ groups: { $: { where: { id: chapterId }, limit: 1 } } })).groups;\n const group = Array.isArray(groups) ? groups[0] : undefined;\n const timezone = resolveScheduling(group?.schedulingJson as ChapterScheduling | undefined).timezone;\n return memberApplication(app as unknown as ApplicationRecord, meeting as unknown as MeetingRecord | undefined, timezone);\n}\n\n/** One route handler: owns the request (Response) or falls through (null). */\nexport type Route = (req: Request, url: URL, env: ChapterEnv, ctx: WorkerContext) => Promise<Response | null>;\n\n/** GET /api/config — the public Clerk publishable key, so the SPA can boot sign-in. */\nexport const handleConfig: Route = async (_req, url, env, ctx) => {\n if (url.pathname !== \"/api/config\") return null;\n try {\n const { clerkPublishableKey } = await ctx.getPublicConfig(env);\n return json({ clerkPublishableKey: clerkPublishableKey ?? null, env: env.ODLA_ENV });\n } catch {\n return json({ clerkPublishableKey: null, env: env.ODLA_ENV });\n }\n};\n\n/** GET /api/me — the signed-in user's role, admin authorization, and super-admin\n * tier. In chapter mode it also carries the member's own `application` (their\n * status + booked call), so the member area renders from one call. */\nexport const handleMe: Route = async (req, url, env, ctx) => {\n if (url.pathname !== \"/api/me\") return null;\n const u = await ctx.verifyUser(req, env);\n if (!u) return json({ authorized: false }, 401);\n const db = ctx.makeDb(env);\n const role = await ctx.roleFor(db, u);\n const superAdmin = await ctx.isSuperAdminEmail(db, u.email);\n const base = { authorized: isAdminRole(role, ctx.auth), role, superAdmin, email: u.email ?? null };\n if (ctx.chapter.mode !== \"chapter\" || !u.email) return json(base);\n const application = await memberSessionApplication(db as unknown as ChapterDb, ctx.chapter.id, u.email);\n return json({ ...base, application });\n};\n\n/** /api/crm/* — the CRM admin surface (mounted at ctx.crmBase). */\nexport const handleCrm: Route = async (req, url, env, ctx) => {\n const crmBase = ctx.crmBase;\n if (url.pathname !== crmBase && !url.pathname.startsWith(crmBase + \"/\")) return null;\n const db = ctx.makeDb(env);\n const routes = createCrmRoutes({\n crm: ctx.chapter.crm,\n db: db as never,\n authorize: async (r: Request) => {\n const u = await ctx.verifyUser(r, env);\n if (!u || !(await ctx.isAdmin(db, u))) return null;\n return u.email ? { userId: u.userId, email: u.email } : { userId: u.userId };\n },\n sender: ctx.crmSender(env),\n from: env.EMAIL_FROM,\n envName: env.ODLA_ENV,\n baseUrl: url.origin,\n basePath: crmBase,\n });\n const res = await routes(req);\n if (res) return res;\n return json({ error: \"not found\" }, 404);\n};\n\n/** POST /api/network/shared — the hub push projection into this site's crm_record\n * (vault-secret gated, works in both modes). */\nexport const handleNetworkShared: Route = async (req, url, env, ctx) => {\n if (req.method !== \"POST\" || url.pathname !== \"/api/network/shared\") return null;\n const db = ctx.makeDb(env);\n const secret = await getVaultSecret(db as unknown as ChapterDb, \"network_share_secret\");\n const provided = (req.headers.get(\"authorization\") ?? \"\").replace(/^Bearer /, \"\");\n if (!secret || provided.length !== secret.length || provided !== secret) {\n return json({ error: \"unauthorized\" }, 401);\n }\n let person: Record<string, unknown>;\n try {\n person = JSON.parse(await req.text()) as Record<string, unknown>;\n } catch {\n return json({ error: \"invalid JSON body\" }, 400);\n }\n if (typeof person.email !== \"string\" || typeof person.hubRecordId !== \"string\") {\n return json({ error: \"email and hubRecordId are required\" }, 400);\n }\n const { recordId } = await projectSharedRecord(\n { crm: ctx.chapter.crm, db: db as unknown as ChapterDb, now: () => Date.now(), newId: () => crypto.randomUUID() },\n person as never,\n );\n return json({ recordId });\n};\n\n/** Chapter-mode public member surface: GET /api/join-config, POST /api/applications.\n * Returns null in hub mode so those paths fall through to ASSETS. */\nexport const handleMember: Route = async (req, url, env, ctx) => {\n const chapter = ctx.chapter;\n if (chapter.mode !== \"chapter\") return null;\n\n // Public join config: prices + policy copy + payment readiness (B1/C2).\n if (req.method === \"GET\" && url.pathname === \"/api/join-config\") {\n const db = ctx.makeDb(env);\n const groupId = url.searchParams.get(\"group\") ?? chapter.id;\n const { groups } = await db.query({ groups: { $: { where: { id: groupId }, limit: 1 } } });\n const group = Array.isArray(groups) ? groups[0] : undefined;\n if (!group) return json({ error: \"not found\" }, 404);\n const stripeKey = await getVaultSecret(db as unknown as ChapterDb, \"stripe_secret_key\");\n const paymentsReady = Boolean(group.stripePublishableKey && group.stripePriceId && stripeKey);\n return json(joinConfig(group as never, paymentsReady));\n }\n\n // Public application submit — validated, body-capped, idempotent (B2/B3).\n if (req.method === \"POST\" && url.pathname === \"/api/applications\") {\n const raw = await req.text();\n if (raw.length > chapter.application.bodyCap) return json({ error: \"request body too large\" }, 413);\n let parsed: Record<string, unknown>;\n try {\n parsed = JSON.parse(raw) as Record<string, unknown>;\n } catch {\n return json({ error: \"invalid JSON body\" }, 400);\n }\n const submissionId = typeof parsed.submissionId === \"string\" ? parsed.submissionId : undefined;\n const result = await submitApplication(ctx.makeDb(env) as unknown as ChapterDb, chapter, parsed, {\n submissionId,\n groupId: chapter.id,\n now: Date.now(),\n newId: () => crypto.randomUUID(),\n });\n if (!result.ok) return json({ error: result.error }, 400);\n return json({ id: result.id, duplicate: result.duplicate, status: result.status });\n }\n\n return null;\n};\n","// The public member surface logic: the join config a site's join page reads (B1)\n// and the idempotent application submit (B2 validation + B3 exactly-once). Both\n// take the structural ChapterDb, so they're tested against an in-memory fake and\n// carry no runtime @odla-ai/db import. The worker builds the real db client, does\n// Clerk verification, enforces the body cap, and mounts these on chapter routes.\nimport type { Chapter, ChapterApplication, ChapterDb, ResolvedApplication } from \"./types\";\n\n// Silver & Salt's join form. `focus` (a json field) is always accepted.\nconst DEFAULT_REQUIRED = [\"firstName\", \"lastName\", \"email\", \"referral\", \"whoYouAre\", \"message\"];\nconst DEFAULT_OPTIONAL = [\"referralName\", \"linkedin\", \"phone\", \"state\"];\n\n/** Apply defaults + validate the application config. Throws at import on bad shape. */\nexport function resolveApplication(a: ChapterApplication | undefined): ResolvedApplication {\n const required = a?.required ?? DEFAULT_REQUIRED;\n const optional = a?.optional ?? DEFAULT_OPTIONAL;\n for (const [name, arr] of [[\"required\", required], [\"optional\", optional]] as const) {\n if (!Array.isArray(arr) || !arr.every((f) => typeof f === \"string\" && f !== \"\")) {\n throw new Error(`defineChapter.application.${name}: must be an array of field-name strings`);\n }\n }\n return {\n required,\n optional,\n maxLen: a?.maxLen ?? {},\n defaultMaxLen: a?.defaultMaxLen ?? 2000,\n bodyCap: a?.bodyCap ?? 32768,\n };\n}\n\n/** A validated submission, or a 400-worthy validation error the route returns. */\nexport type SubmitResult =\n | { ok: true; id: string; duplicate: boolean; status: string }\n | { ok: false; error: string };\n\n/**\n * Submit a membership application (B2 + B3). Validates the configured required\n * fields + max lengths, writes the `applications` row at the pipeline's initial\n * status, and — when the client supplies a `submissionId` — stamps it as the\n * transaction's mutationId (`join:${submissionId}`) so a double-tap can never\n * create two applications (the second returns `duplicate: true`). Idempotency is\n * package-enforced. `now`/`newId` are injected (deterministic in tests).\n */\nexport async function submitApplication(\n db: ChapterDb,\n chapter: Chapter,\n fields: Record<string, unknown>,\n opts: { submissionId?: string; groupId?: string; now: number; newId: () => string },\n): Promise<SubmitResult> {\n const app = chapter.application;\n for (const f of app.required) {\n const v = fields[f];\n if (typeof v !== \"string\" || v.trim() === \"\") return { ok: false, error: `${f} is required` };\n }\n for (const f of [...app.required, ...app.optional]) {\n const v = fields[f];\n const cap = app.maxLen[f] ?? app.defaultMaxLen;\n if (typeof v === \"string\" && v.length > cap) return { ok: false, error: `${f} exceeds ${cap} characters` };\n }\n\n const id = opts.newId();\n const row: Record<string, unknown> = { id, status: chapter.pipeline.initial, createdAt: opts.now };\n for (const f of [...app.required, ...app.optional]) {\n if (typeof fields[f] === \"string\") row[f] = (fields[f] as string).trim();\n }\n if (fields.focus !== undefined) row.focus = fields.focus;\n if (opts.groupId) row.groupId = opts.groupId;\n\n const { duplicate } = await db.transact(\n [{ t: \"update\", ns: \"applications\", id, attrs: row }],\n opts.submissionId ? { mutationId: `join:${opts.submissionId}` } : undefined,\n );\n return { ok: true, id, duplicate, status: chapter.pipeline.initial };\n}\n\n/** The `groups`-row fields the join config exposes. */\nexport interface JoinConfigGroup {\n id: string;\n name: string;\n standardPriceCents?: number;\n foundingDiscountCents?: number;\n disclaimerText?: string;\n refundPolicyText?: string;\n trustCopy?: string;\n commitmentText?: string;\n normsText?: string;\n}\n\n/**\n * The public join config (B1) a site's join page reads: copy + prices from the\n * group row plus `paymentsReady`. When payments aren't wired the join flow drops\n * the payment step (C2) — the worker computes `paymentsReady` from the group's\n * Stripe keys + vault secret. Pure.\n */\nexport function joinConfig(group: JoinConfigGroup, paymentsReady: boolean): Record<string, unknown> {\n return {\n id: group.id,\n name: group.name,\n standardPriceCents: group.standardPriceCents ?? 0,\n foundingDiscountCents: group.foundingDiscountCents ?? 0,\n disclaimerText: group.disclaimerText ?? \"\",\n refundPolicyText: group.refundPolicyText ?? \"\",\n trustCopy: group.trustCopy ?? \"\",\n commitmentText: group.commitmentText ?? \"\",\n normsText: group.normsText ?? \"\",\n paymentsReady,\n };\n}\n","// The hub → chapter people projection (push model). The network hub curates\n// prospects and pushes a person's contact data into THIS chapter's own\n// crm_record, so a chapter admin sees network prospects beside their applicants.\n//\n// Invariants (package-enforced so no site re-derives them):\n// - One-way: the chapter never writes back to the hub through this path.\n// - Idempotent: keyed by the hub's record id (a re-share updates, never\n// duplicates) AND unified by primaryEmail — a shared prospect who later\n// submits an application lands on the SAME crm_record, so the two projections\n// compose instead of forking the person.\n// - A person may be shared with many chapters; that fan-out is hub-side, so each\n// chapter's projection here is independent.\n//\n// Reuses @odla-ai/crm's record ops (full validation via crm.prepare), driven by\n// the resolved chapter CRM engine + the structural ChapterDb.\nimport { createRecord, updateRecord } from \"@odla-ai/crm\";\nimport type { Crm } from \"@odla-ai/crm\";\nimport type { ChapterDb } from \"./types\";\n\n/** The contact data the hub shares for a prospect. `hubRecordId` is the stable\n * idempotency key (the hub's crm_record id). */\nexport interface SharedPerson {\n email: string;\n name?: string;\n firstName?: string;\n lastName?: string;\n phone?: string;\n linkedin?: string;\n hubRecordId: string;\n}\n\n/** Map a shared prospect to a crm `person` input (only the fields the default\n * person type accepts). Name falls back to first+last, then the email. */\nexport function sharedPersonInput(person: SharedPerson): Record<string, unknown> {\n const email = person.email.toLowerCase();\n const fullName = [person.firstName, person.lastName].filter(Boolean).join(\" \").trim();\n const input: Record<string, unknown> = { name: person.name ?? fullName ?? email, email };\n if (input.name === \"\") input.name = email;\n if (person.firstName) input.firstName = person.firstName;\n if (person.lastName) input.lastName = person.lastName;\n if (person.phone) input.phone = person.phone;\n if (person.linkedin) input.linkedin = person.linkedin;\n return input;\n}\n\n/** Deps for the projection — the resolved CRM engine, the structural db, and\n * injected clock/id (deterministic in tests). */\nexport interface ProjectionDeps {\n crm: Crm;\n db: ChapterDb;\n now: () => number;\n newId: () => string;\n}\n\n/**\n * Upsert a hub-shared prospect into this chapter's `crm_record` (push\n * projection). Resolves an existing person by lowercased `primaryEmail` and\n * updates it, else creates one with a `share:${hubRecordId}` mutationId. Returns\n * the chapter-side record id. Callers wrap this in `.catch` so a projection\n * failure never fails the hub's share request.\n */\nexport async function projectSharedRecord(deps: ProjectionDeps, person: SharedPerson): Promise<{ recordId: string }> {\n const email = person.email.toLowerCase();\n const input = sharedPersonInput(person);\n const crmDeps = { crm: deps.crm, db: deps.db as never, now: deps.now, newId: deps.newId };\n const { crm_record } = await deps.db.query({\n crm_record: { $: { where: { type: \"person\", primaryEmail: email }, limit: 1 } },\n });\n const existing = crm_record?.[0];\n if (existing && typeof existing.id === \"string\") {\n await updateRecord(crmDeps, { id: existing.id, input });\n return { recordId: existing.id };\n }\n const created = await createRecord(crmDeps, { type: \"person\", input, mutationId: `share:${person.hubRecordId}` });\n return { recordId: created.id };\n}\n","// Scheduling core — config resolution + the booking invariants, ported from the\n// proven Silver & Salt worker. Everything here is PURE (no @odla-ai/calendar, no\n// db), so the correctness properties are unit-testable and package-enforced; the\n// worker route owns only the I/O (FreeBusy, computeBookableSlots, calendar\n// create/reschedule, the db writes) and calls these.\n//\n// Package-enforced invariants:\n// - the `meetings` row is canonical; applications.meetingAt and the calendar\n// event are projections written from it.\n// - one intro event per application, forever: a rebooking RESCHEDULES the\n// existing event (preserving its Meet link + invite thread), never creates a\n// second — see {@link bookingDecision} + {@link introIdempotencyKey}.\n// - status never moves backward on booking; you can only book from an early\n// stage — see {@link canBookFrom} + {@link applicationBookingUpdate}.\n// - endAt is always derived server-side, never client-supplied — see\n// {@link endForSlot}.\nimport type { ChapterScheduling } from \"./types\";\n\n/** A fully-resolved scheduling config (every field present). */\nexport interface ResolvedScheduling {\n slotMinutes: number;\n days: readonly number[];\n startHour: number;\n endHour: number;\n timezone: string;\n minNoticeHours: number;\n windowDays: number;\n summaryTemplate: string;\n}\n\n/** The S&S-proven defaults: 45-minute weekday slots, 9–5 Pacific, 24h notice,\n * a 14-day window. The summary is generic (a chapter's group seed supplies a\n * name-branded one). */\nexport const SCHEDULING_DEFAULTS: ResolvedScheduling = {\n slotMinutes: 45,\n days: [1, 2, 3, 4, 5],\n startHour: 9,\n endHour: 17,\n timezone: \"America/Los_Angeles\",\n minNoticeHours: 24,\n windowDays: 14,\n summaryTemplate: \"Introduction call with {{firstName}} {{lastName}}\",\n};\n\nfunction isValidTimeZone(tz: string): boolean {\n try {\n new Intl.DateTimeFormat(undefined, { timeZone: tz });\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Resolve a group's scheduling config against the defaults, validating every\n * bound the way S&S does at config-write time (throws on a bad config, so a\n * misconfiguration surfaces immediately rather than yielding empty slots).\n * `windowDays` is capped at 62 because Google FreeBusy is.\n */\nexport function resolveScheduling(config?: ChapterScheduling): ResolvedScheduling {\n const d = config ?? {};\n const c: ResolvedScheduling = {\n slotMinutes: d.slotMinutes ?? SCHEDULING_DEFAULTS.slotMinutes,\n days: d.days ?? SCHEDULING_DEFAULTS.days,\n startHour: d.startHour ?? SCHEDULING_DEFAULTS.startHour,\n endHour: d.endHour ?? SCHEDULING_DEFAULTS.endHour,\n timezone: d.timezone ?? SCHEDULING_DEFAULTS.timezone,\n minNoticeHours: d.minNoticeHours ?? SCHEDULING_DEFAULTS.minNoticeHours,\n windowDays: d.windowDays ?? SCHEDULING_DEFAULTS.windowDays,\n summaryTemplate: d.summaryTemplate ?? SCHEDULING_DEFAULTS.summaryTemplate,\n };\n const fail = (msg: string): never => {\n throw new Error(`scheduling: ${msg}`);\n };\n if (!(c.slotMinutes >= 15 && c.slotMinutes <= 240)) fail(\"slotMinutes must be 15–240\");\n if (!(c.windowDays >= 1 && c.windowDays <= 62)) fail(\"windowDays must be 1–62 (FreeBusy caps at 62)\");\n if (!(c.minNoticeHours >= 0 && c.minNoticeHours <= 336)) fail(\"minNoticeHours must be 0–336\");\n if (!(c.startHour >= 0 && c.startHour < c.endHour && c.endHour <= 24)) fail(\"require 0 ≤ startHour < endHour ≤ 24\");\n const days = [...c.days];\n if (!days.length || !days.every((n) => Number.isInteger(n) && n >= 0 && n <= 6)) {\n fail(\"days must be a non-empty list of weekday integers 0–6\");\n }\n if (typeof c.timezone !== \"string\" || !isValidTimeZone(c.timezone)) fail(`invalid IANA timezone \"${c.timezone}\"`);\n if (typeof c.summaryTemplate !== \"string\") fail(\"summaryTemplate must be a string\");\n return { ...c, days };\n}\n\n/** Statuses a member may book/reschedule from (early pipeline only). */\nexport const BOOKABLE_STATUSES: readonly string[] = [\"submitted\", \"paid_pending_vetting\", \"call_scheduled\"];\n\n/** Whether an application at `status` may book a call. */\nexport function canBookFrom(status: string): boolean {\n return BOOKABLE_STATUSES.includes(status);\n}\n\n/** The availability window: `[now, now + windowDays]` in epoch ms. */\nexport function slotWindow(now: number, windowDays: number): { from: number; to: number } {\n return { from: now, to: now + windowDays * 86_400_000 };\n}\n\n/** The slot's end instant, always derived from its start (never client-supplied). */\nexport function endForSlot(startAt: number, slotMinutes: number): number {\n return startAt + slotMinutes * 60_000;\n}\n\n/** Double-book pre-check: the requested start must land exactly on a currently\n * bookable slot boundary. */\nexport function isSlotAvailable(slots: readonly { startAt: number }[], startAt: number): boolean {\n return slots.some((s) => s.startAt === startAt);\n}\n\n/** Render a meeting summary from its template (`{{firstName}}`/`{{lastName}}`). */\nexport function renderSummary(template: string, app: { firstName?: string | null; lastName?: string | null }): string {\n return template.replace(\"{{firstName}}\", app.firstName ?? \"\").replace(\"{{lastName}}\", app.lastName ?? \"\");\n}\n\n/** The prior scheduled meeting for an application, as far as booking cares. */\nexport interface ExistingMeeting {\n id: string;\n googleEventId?: string | null;\n meetUrl?: string | null;\n htmlLink?: string | null;\n}\n\n/** Decide reschedule-vs-create: reschedule iff there's an existing event to move,\n * so the Meet link + invite thread survive and no second event is minted. */\nexport function bookingDecision(existing: ExistingMeeting | null | undefined): { reschedule: boolean; eventId: string | null } {\n const eventId = existing?.googleEventId ?? null;\n return { reschedule: Boolean(eventId), eventId };\n}\n\n/** The create idempotency key — one intro event per application, forever, so a\n * retried create returns the same booking rather than a duplicate. */\nexport function introIdempotencyKey(applicationId: string): string {\n return `application:${applicationId}:intro`;\n}\n\n/** The canonical `meetings` row for a first booking. A `type` (not `interface`)\n * so it stays assignable to a db op's `attrs` (Record<string, unknown>). */\nexport type NewMeetingRow = {\n id: string;\n applicationId: string;\n groupId: string;\n startAt: number;\n endAt: number;\n timezone: string;\n status: \"scheduled\";\n googleEventId: string;\n meetUrl?: string;\n htmlLink?: string;\n drift: \"none\";\n createdAt: number;\n};\n\n/** Build the new `meetings` row after the calendar created the event. Optional\n * scalars are omitted (never null), per the odla-db porting rule. */\nexport function meetingCreateRow(i: {\n meetingId: string;\n applicationId: string;\n groupId: string;\n startAt: number;\n endAt: number;\n timezone: string;\n googleEventId: string;\n meetUrl?: string | null;\n htmlLink?: string | null;\n createdAt: number;\n}): NewMeetingRow {\n return {\n id: i.meetingId,\n applicationId: i.applicationId,\n groupId: i.groupId,\n startAt: i.startAt,\n endAt: i.endAt,\n timezone: i.timezone,\n status: \"scheduled\",\n googleEventId: i.googleEventId,\n ...(i.meetUrl ? { meetUrl: i.meetUrl } : {}),\n ...(i.htmlLink ? { htmlLink: i.htmlLink } : {}),\n drift: \"none\",\n createdAt: i.createdAt,\n };\n}\n\n/** The `meetings`-row patch for a reschedule (same row id, moved in place). */\nexport type MeetingReschedulePatch = {\n startAt: number;\n endAt: number;\n drift: \"none\";\n};\n\n/** Patch to move an existing meeting to a new window. */\nexport function meetingRescheduleUpdate(startAt: number, endAt: number): MeetingReschedulePatch {\n return { startAt, endAt, drift: \"none\" };\n}\n\n/** The `applications`-row patch after a booking. */\nexport type ApplicationBookingPatch = {\n meetingAt: number;\n meetingLink?: string;\n status?: \"call_scheduled\";\n};\n\n/** Project the booking onto the application row: cache the time, adopt the\n * calendar link if any, and advance the status to `call_scheduled` unless it is\n * already there (never backward). */\nexport function applicationBookingUpdate(\n currentStatus: string,\n startAt: number,\n htmlLink?: string | null,\n): ApplicationBookingPatch {\n return {\n meetingAt: startAt,\n ...(htmlLink ? { meetingLink: htmlLink } : {}),\n ...(currentStatus !== \"call_scheduled\" ? { status: \"call_scheduled\" as const } : {}),\n };\n}\n","// The member session — what GET /api/me returns to a signed-in applicant/member.\n// Ported from Silver & Salt's applicationSummary + /api/me reconciliation. The\n// SHAPING is pure and package-enforced so no site re-derives it; the worker owns\n// only the I/O around it (locating the application by email, reconciling the\n// meeting against the calendar) and hands the resolved rows here.\n//\n// Two invariants live here, not in a site:\n// - `paid` is DERIVED, never a stored flag: a subscription exists and the\n// application wasn't refunded. Sites can't drift a stale boolean out of sync\n// with Stripe.\n// - the live meeting row wins: its startAt/meetUrl/timezone override whatever\n// the application row cached, and a non-scheduled meeting (a cancellation\n// adopted from the calendar) forces meetingAt back to null.\n\n/** An application row, as far as the session cares about it. */\nexport interface ApplicationRecord {\n id: string;\n firstName?: string | null;\n lastName?: string | null;\n email?: string | null;\n status: string;\n meetingAt?: number | null;\n meetingLink?: string | null;\n createdAt?: number | null;\n stripeSubscriptionId?: string | null;\n renewalAt?: number | null;\n canceled?: boolean;\n}\n\n/** The reconciled meeting row (already adopted against the calendar), or null. */\nexport interface MeetingRecord {\n status: string;\n startAt?: number | null;\n meetUrl?: string | null;\n timezone?: string | null;\n}\n\n/** The stable, non-meeting fields of an application (safe to expose to its own\n * owner). */\nexport interface ApplicationSummary {\n id: string;\n firstName: string | null;\n lastName: string | null;\n email: string | null;\n status: string;\n createdAt: number | null;\n meetingLink: string | null;\n paid: boolean;\n renewalAt: number | null;\n canceled: boolean;\n}\n\n/** A summary plus the reconciled meeting fields — the `application` the member\n * area renders. */\nexport interface MemberApplication extends ApplicationSummary {\n meetingAt: number | null;\n meetUrl: string | null;\n timezone: string;\n}\n\n/** The full GET /api/me payload for a signed-in user. */\nexport interface MemberSession {\n userId: string;\n email: string | null;\n role: string;\n superAdmin: boolean;\n application: MemberApplication | null;\n}\n\n/** Derive the summary fields from an application row. `paid` is computed, not\n * read, so it can never contradict Stripe. */\nexport function applicationSummary(app: ApplicationRecord): ApplicationSummary {\n return {\n id: app.id,\n firstName: app.firstName ?? null,\n lastName: app.lastName ?? null,\n email: app.email ?? null,\n status: app.status,\n createdAt: app.createdAt ?? null,\n meetingLink: app.meetingLink ?? null,\n paid: Boolean(app.stripeSubscriptionId) && app.status !== \"refunded\",\n renewalAt: app.renewalAt ?? null,\n canceled: app.canceled === true,\n };\n}\n\n/** Fold a (possibly absent, already-reconciled) meeting into the application the\n * member area renders. The live meeting overrides the application's cached\n * meeting fields; a non-`scheduled` meeting clears the booking. */\nexport function memberApplication(\n app: ApplicationRecord,\n meeting: MeetingRecord | null | undefined,\n defaultTimezone: string,\n): MemberApplication {\n const summary = applicationSummary(app);\n let meetingAt = app.meetingAt ?? null;\n let meetUrl: string | null = null;\n let timezone = defaultTimezone;\n if (meeting) {\n timezone = meeting.timezone ?? timezone;\n if (meeting.status === \"scheduled\") {\n meetingAt = meeting.startAt ?? null;\n meetUrl = meeting.meetUrl ?? null;\n } else {\n meetingAt = null;\n }\n }\n return { ...summary, meetingAt, meetUrl, timezone };\n}\n\n/** Identity of the signed-in user, from the verified session. */\nexport interface SessionUser {\n userId: string;\n email?: string | null;\n role: string;\n}\n\n/** Assemble the GET /api/me payload. `application` is null when the user has no\n * application on file (an admin who never applied, or a brand-new account). */\nexport function memberSession(\n user: SessionUser,\n opts: { application: MemberApplication | null; superAdmin: boolean },\n): MemberSession {\n return {\n userId: user.userId,\n email: user.email ?? null,\n role: user.role,\n superAdmin: opts.superAdmin,\n application: opts.application,\n };\n}\n","// Scheduling routes: GET /api/schedule/slots + POST /api/schedule/book. This\n// file owns the I/O — Google FreeBusy, the pure computeBookableSlots, the\n// calendar create/reschedule, and the db writes — and delegates every\n// correctness rule to ./scheduling. Calendar is RUNTIME-optional: a chapter with\n// no connected calendar has freeBusy throw, so /slots answers\n// { schedulingReady: false } (200) and the join flow degrades to \"we'll reach\n// out by email\" rather than erroring.\nimport { computeBookableSlots, initCalendar } from \"@odla-ai/calendar\";\nimport { json } from \"./worker-context\";\nimport type { ChapterEnv, WorkerContext } from \"./worker-context\";\nimport type { Route } from \"./worker-routes\";\nimport type { ChapterDb, ChapterScheduling, DbOp } from \"./types\";\nimport {\n applicationBookingUpdate,\n bookingDecision,\n canBookFrom,\n endForSlot,\n introIdempotencyKey,\n isSlotAvailable,\n meetingCreateRow,\n meetingRescheduleUpdate,\n renderSummary,\n resolveScheduling,\n slotWindow,\n} from \"./scheduling\";\nimport type { ExistingMeeting, ResolvedScheduling } from \"./scheduling\";\n\ntype Cal = ReturnType<typeof initCalendar>;\ntype Row = Record<string, unknown>;\n\n/** The stable `.code` off an OdlaError/provider error, else a safe default. */\nfunction errCode(err: unknown): string {\n if (err && typeof err === \"object\") {\n const code = (err as { code?: unknown }).code;\n if (typeof code === \"string\") return code;\n }\n return \"calendar_unavailable\";\n}\n\nfunction makeCalendar(env: ChapterEnv): Cal {\n // Calendar uses ODLA_APP_ID + ODLA_PLATFORM — distinct from the db client's\n // ODLA_TENANT + ODLA_ENDPOINT.\n return initCalendar({ appId: env.ODLA_APP_ID, env: env.ODLA_ENV, adminToken: env.ODLA_API_KEY, endpoint: env.ODLA_PLATFORM });\n}\n\nasync function firstRow(db: ChapterDb, ns: string, q: Record<string, unknown>): Promise<Row | undefined> {\n const res = await db.query({ [ns]: { $: q } });\n const rows = res[ns];\n return Array.isArray(rows) ? rows[0] : undefined;\n}\n\nasync function computeSlots(cal: Cal, cfg: ResolvedScheduling): Promise<Array<{ startAt: number; endAt: number }>> {\n const { from, to } = slotWindow(Date.now(), cfg.windowDays);\n const fb = await cal.availability.freeBusy({ timeMin: from, timeMax: to });\n return computeBookableSlots(fb.busy, {\n from: fb.timeMin,\n to: fb.timeMax,\n timezone: cfg.timezone,\n slotMinutes: cfg.slotMinutes,\n businessHours: { days: [...cfg.days], startHour: cfg.startHour, endHour: cfg.endHour },\n minNoticeMs: cfg.minNoticeHours * 3_600_000,\n });\n}\n\nasync function bookSlot(req: Request, env: ChapterEnv, ctx: WorkerContext): Promise<Response> {\n let body: Row;\n try {\n body = JSON.parse(await req.text()) as Row;\n } catch {\n return json({ error: \"invalid JSON body\" }, 400);\n }\n const applicationId = typeof body.applicationId === \"string\" ? body.applicationId : \"\";\n const startAt = Number(body.startAt);\n if (!applicationId || !Number.isFinite(startAt)) return json({ error: \"applicationId and startAt required\" }, 400);\n\n const db = ctx.makeDb(env) as unknown as ChapterDb;\n const app = await firstRow(db, \"applications\", { where: { id: applicationId }, limit: 1 });\n if (!app) return json({ error: \"not found\" }, 404);\n const status = String(app.status ?? \"\");\n if (!canBookFrom(status)) return json({ error: `cannot book from status \"${status}\"` }, 409);\n\n const group = await firstRow(db, \"groups\", { where: { id: String(app.groupId ?? ctx.chapter.id) }, limit: 1 });\n if (!group) return json({ error: \"group not found\" }, 500);\n const cfg = resolveScheduling(group.schedulingJson as ChapterScheduling | undefined);\n const endAt = endForSlot(startAt, cfg.slotMinutes);\n const cal = makeCalendar(env);\n\n // Double-book guard, layer 1: the requested start must still be a live slot.\n let slots: Array<{ startAt: number }>;\n try {\n slots = await computeSlots(cal, cfg);\n } catch (err) {\n return json({ error: \"scheduling unavailable\", code: errCode(err) }, 503);\n }\n if (!isSlotAvailable(slots, startAt)) return json({ error: \"slot no longer available\", code: \"calendar_slot_unavailable\" }, 409);\n\n const summary = renderSummary(cfg.summaryTemplate, { firstName: app.firstName as string, lastName: app.lastName as string });\n const existing = (await firstRow(db, \"meetings\", {\n where: { applicationId, status: \"scheduled\" },\n order: { createdAt: \"desc\" },\n limit: 1,\n })) as ExistingMeeting | undefined;\n const decision = bookingDecision(existing);\n\n let meetUrl: string | null = null;\n let htmlLink: string | null = null;\n let meetingOp: DbOp;\n try {\n if (decision.reschedule && decision.eventId) {\n // Reschedule the SAME event — the Meet link + invite thread survive.\n await cal.actions.reschedule(decision.eventId, { startAt, endAt });\n meetUrl = (existing?.meetUrl as string | undefined) ?? null;\n htmlLink = (existing?.htmlLink as string | undefined) ?? null;\n meetingOp = { t: \"update\", ns: \"meetings\", id: String(existing?.id), attrs: meetingRescheduleUpdate(startAt, endAt) };\n } else {\n const { booking } = await cal.actions.create(\n { summary, startAt, endAt, attendees: [String(app.email)], timezone: cfg.timezone, meet: true },\n { idempotencyKey: introIdempotencyKey(applicationId) },\n );\n meetUrl = booking.meetUrl ?? null;\n htmlLink = booking.htmlLink ?? null;\n const meetingId = crypto.randomUUID();\n meetingOp = {\n t: \"update\",\n ns: \"meetings\",\n id: meetingId,\n attrs: meetingCreateRow({\n meetingId,\n applicationId,\n groupId: String(group.id),\n startAt,\n endAt,\n timezone: cfg.timezone,\n googleEventId: booking.eventId,\n meetUrl: booking.meetUrl,\n htmlLink: booking.htmlLink,\n createdAt: Date.now(),\n }),\n };\n }\n } catch (err) {\n const code = errCode(err);\n // Double-book guard, layer 2 (authoritative): the provider rejected under lease.\n if (code === \"calendar_slot_unavailable\") return json({ error: \"slot no longer available\", code }, 409);\n return json({ error: \"booking failed\", code }, 502);\n }\n\n // meetings row is canonical; the application row is the projection.\n const appOp: DbOp = { t: \"update\", ns: \"applications\", id: applicationId, attrs: applicationBookingUpdate(status, startAt, htmlLink) };\n await db.transact([meetingOp, appOp]);\n\n return json({ ok: true, startAt, endAt, meetUrl, rescheduled: decision.reschedule });\n}\n\n/** GET /api/schedule/slots + POST /api/schedule/book (chapter mode only). */\nexport const handleSchedule: Route = async (req, url, env, ctx) => {\n if (ctx.chapter.mode !== \"chapter\") return null;\n\n if (req.method === \"GET\" && url.pathname === \"/api/schedule/slots\") {\n const db = ctx.makeDb(env) as unknown as ChapterDb;\n const group = await firstRow(db, \"groups\", { where: { id: url.searchParams.get(\"group\") ?? ctx.chapter.id }, limit: 1 });\n if (!group) return json({ error: \"not found\" }, 404);\n const cfg = resolveScheduling(group.schedulingJson as ChapterScheduling | undefined);\n try {\n const slots = await computeSlots(makeCalendar(env), cfg);\n return json({ schedulingReady: true, timezone: cfg.timezone, slotMinutes: cfg.slotMinutes, slots });\n } catch (err) {\n return json({ schedulingReady: false, code: errCode(err) });\n }\n }\n\n if (req.method === \"POST\" && url.pathname === \"/api/schedule/book\") {\n return bookSlot(req, env, ctx);\n }\n\n return null;\n};\n","// Payments primitives. The webhook-integrity check below is security-critical and\n// easy to get wrong, so it is pure and tested here; the worker wires a payments\n// provider (Stripe first — subscription create, webhook ingest, refund) around\n// it, and every resulting db write carries an event-derived mutationId for\n// exactly-once. Sites that don't charge omit payments entirely (paymentsReady:\n// false), so nothing here is imported unless a chapter runs the payment flow.\n\n/** Parse a Stripe-style `Stripe-Signature` header (`t=<unix>,v1=<hex>`). */\nfunction parseSigHeader(header: string): { t?: string; v1?: string } {\n const parts: Record<string, string> = {};\n for (const p of header.split(\",\")) {\n const [k, v] = p.split(\"=\", 2);\n if (k && v !== undefined) parts[k] = v;\n }\n return { t: parts.t, v1: parts.v1 };\n}\n\nfunction toHex(buf: ArrayBuffer): string {\n return [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\n/** Constant-time compare of two equal-length hex strings. */\nfunction timingSafeEqual(a: string, b: string): boolean {\n if (a.length !== b.length) return false;\n let diff = 0;\n for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);\n return diff === 0;\n}\n\n/**\n * Verify a Stripe webhook signature (C3): HMAC-SHA256 over `` `${t}.${payload}` ``\n * with the endpoint signing secret, a replay window (default 5 minutes), and a\n * constant-time compare. Package-enforced — never left to a site. Returns `false`\n * (never throws) on a malformed header, a non-numeric or stale timestamp, or a\n * signature mismatch. `now`/`toleranceSec` are injectable for tests.\n */\nexport async function verifyStripeSignature(\n payload: string,\n header: string,\n secret: string,\n opts: { now?: number; toleranceSec?: number } = {},\n): Promise<boolean> {\n const { t, v1 } = parseSigHeader(header);\n if (!t || !v1) return false;\n const ts = Number(t);\n if (!Number.isFinite(ts)) return false;\n const nowSec = (opts.now ?? Date.now()) / 1000;\n const tolerance = opts.toleranceSec ?? 300;\n if (Math.abs(nowSec - ts) > tolerance) return false;\n\n const enc = new TextEncoder();\n const key = await crypto.subtle.importKey(\"raw\", enc.encode(secret), { name: \"HMAC\", hash: \"SHA-256\" }, false, [\"sign\"]);\n const mac = await crypto.subtle.sign(\"HMAC\", key, enc.encode(`${t}.${payload}`));\n return timingSafeEqual(toHex(mac), v1);\n}\n\n// ── readiness + Stripe wire helpers ──\n\n/** A group row's payment configuration, as far as readiness cares. */\nexport interface PaymentsGroup {\n stripePublishableKey?: string | null;\n stripePriceId?: string | null;\n}\n\n/** Whether a group can take payment: a publishable key + a price id (both on the\n * group row) AND a secret key (the vault). Anything missing drops the join\n * flow's payment step (paymentsReady:false) rather than half-charging. */\nexport function paymentsReady(group: PaymentsGroup, hasSecretKey: boolean): boolean {\n return Boolean(group.stripePublishableKey && group.stripePriceId && hasSecretKey);\n}\n\n/** Form-encode params for Stripe's x-www-form-urlencoded API, expanding one level\n * of nested objects into bracket syntax (`metadata[applicationId]=...`). */\nexport function stripeForm(params: Record<string, unknown>): string {\n const out = new URLSearchParams();\n for (const [k, v] of Object.entries(params)) {\n if (v === undefined || v === null) continue;\n if (typeof v === \"object\") {\n for (const [k2, v2] of Object.entries(v as Record<string, unknown>)) {\n if (v2 !== undefined && v2 !== null) out.append(`${k}[${k2}]`, String(v2));\n }\n } else {\n out.append(k, String(v));\n }\n }\n return out.toString();\n}\n\n/** The Stripe idempotency key for creating an application's subscription — one\n * per application, so a client retry can't orphan a second subscription (S&S\n * lacked this; the package enforces it). */\nexport function subscriptionIdempotencyKey(applicationId: string): string {\n return `sub:${applicationId}`;\n}\n\n/** The db mutationId for a webhook-driven write — exactly-once per Stripe event,\n * so replays are deduped at the db layer. */\nexport function webhookMutationId(eventId: string): string {\n return `stripe:${eventId}`;\n}\n\n// ── webhook normalization (pure; the worker owns the db lookup + writes) ──\n\n/** A raw Stripe event, as far as normalization cares. */\nexport interface StripeEvent {\n id: string;\n type: string;\n data?: { object?: Record<string, unknown> };\n}\n\n/** A normalized, provider-agnostic webhook event. `kind` drives the db write;\n * the application is resolved from `applicationId` (metadata) or `customerId`. */\nexport type WebhookEvent =\n | { kind: \"first_payment\"; applicationId?: string; customerId?: string; renewalAt?: number }\n | { kind: \"renewal\"; applicationId?: string; customerId?: string; renewalAt?: number }\n | { kind: \"refunded\"; applicationId?: string; customerId?: string }\n | { kind: \"canceled\"; applicationId?: string; customerId?: string }\n | { kind: \"ignored\"; type: string };\n\n/** Resolve the application reference on a Stripe object: `applicationId` from\n * metadata (direct, then subscription_details, then nested\n * parent.subscription_details), plus the customer id for the db fallback. */\nexport function findApplicationRef(obj: Record<string, unknown>): { applicationId?: string; customerId?: string } {\n const metaOf = (v: unknown): Record<string, unknown> =>\n v && typeof v === \"object\" ? ((v as Record<string, unknown>).metadata as Record<string, unknown>) ?? {} : {};\n const pick = (m: Record<string, unknown>): string | undefined =>\n typeof m.applicationId === \"string\" ? m.applicationId : undefined;\n const applicationId =\n pick(metaOf(obj)) ?? pick(metaOf(obj.subscription_details)) ?? pick(metaOf((obj.parent as Record<string, unknown> | undefined)?.subscription_details));\n const customerId = typeof obj.customer === \"string\" ? obj.customer : undefined;\n return { ...(applicationId ? { applicationId } : {}), ...(customerId ? { customerId } : {}) };\n}\n\n/** Normalize a verified Stripe event into a {@link WebhookEvent}. `invoice.paid`\n * splits into first_payment vs renewal by `billing_reason`; refunds and\n * cancellations map directly; everything else is ignored (acked, not retried). */\nexport function normalizeWebhookEvent(event: StripeEvent): WebhookEvent {\n const obj = event.data?.object ?? {};\n const ref = findApplicationRef(obj);\n switch (event.type) {\n case \"invoice.paid\": {\n const lines = ((obj.lines as Record<string, unknown> | undefined)?.data as Array<Record<string, unknown>> | undefined) ?? [];\n const periodEnd = (lines[0]?.period as Record<string, unknown> | undefined)?.end;\n const renewalAt = typeof periodEnd === \"number\" ? periodEnd * 1000 : undefined;\n const kind = obj.billing_reason === \"subscription_create\" ? \"first_payment\" : \"renewal\";\n return { kind, ...ref, ...(renewalAt !== undefined ? { renewalAt } : {}) };\n }\n case \"charge.refunded\":\n return { kind: \"refunded\", ...ref };\n case \"customer.subscription.deleted\":\n return { kind: \"canceled\", ...ref };\n default:\n return { kind: \"ignored\", type: event.type };\n }\n}\n\n// ── webhook write-set builders (the authoritative writers of paid/refunded) ──\n\n/** First-payment patch: advance submitted→paid_pending_vetting (never any other\n * transition) and record the renewal date. Empty when nothing changed, so the\n * caller can skip the write. */\nexport function firstPaymentPatch(currentStatus: string, renewalAt?: number): { status?: \"paid_pending_vetting\"; renewalAt?: number } {\n return {\n ...(currentStatus === \"submitted\" ? { status: \"paid_pending_vetting\" as const } : {}),\n ...(renewalAt !== undefined ? { renewalAt } : {}),\n };\n}\n\n/** Renewal-invoice patch: just the new renewal date. */\nexport function renewalPatch(renewalAt: number): { renewalAt: number } {\n return { renewalAt };\n}\n\n/** Refund patch — the SOLE writer of status \"refunded\" (the admin refund route\n * issues the Stripe refund but never sets this; the webhook does). */\nexport function refundedPatch(): { status: \"refunded\" } {\n return { status: \"refunded\" };\n}\n\n/** Subscription-cancellation patch. */\nexport function canceledPatch(): { canceled: true } {\n return { canceled: true };\n}\n","// The Stripe payments provider — the only I/O half of payments. Talks to the\n// Stripe REST API over fetch (no SDK), and maps 1:1 onto the pure helpers in\n// ./payments (form encoding, signature verify, event normalization). The worker\n// resolves secrets from the vault per request and constructs this; the routes own\n// every db write so idempotency stays at the db layer.\nimport { normalizeWebhookEvent, stripeForm, verifyStripeSignature } from \"./payments\";\nimport type { StripeEvent, WebhookEvent } from \"./payments\";\n\n/** Inputs to create an application's founding-member subscription. */\nexport interface CreateSubscriptionInput {\n applicationId: string;\n groupId: string;\n email: string;\n name: string;\n priceId: string;\n existingCustomerId?: string;\n}\n\n/** The client secret to confirm card entry, plus the ids to persist. */\nexport interface CreateSubscriptionResult {\n customerId: string;\n subscriptionId: string;\n clientSecret: string;\n}\n\n/** The outcome of a full refund. */\nexport interface RefundResult {\n refundedCents: number | null;\n subscriptionCanceled: boolean;\n}\n\n/** The capabilities the payment routes depend on — Stripe is one impl. */\nexport interface PaymentsProvider {\n createSubscription(input: CreateSubscriptionInput): Promise<CreateSubscriptionResult>;\n ingestWebhook(\n rawBody: string,\n sigHeader: string,\n ): Promise<{ ok: true; eventId: string; event: WebhookEvent } | { ok: false; reason: \"bad_signature\" }>;\n refund(input: { customerId: string; subscriptionId: string }): Promise<RefundResult>;\n}\n\ntype StripeResult = { ok: boolean; status: number; body: Record<string, unknown> };\n\nasync function stripeCall(\n sk: string,\n method: \"GET\" | \"POST\" | \"DELETE\",\n path: string,\n params?: Record<string, unknown>,\n idempotencyKey?: string,\n): Promise<StripeResult> {\n const qs = method === \"GET\" && params ? `?${stripeForm(params)}` : \"\";\n const headers: Record<string, string> = { authorization: `Bearer ${sk}` };\n if (idempotencyKey) headers[\"idempotency-key\"] = idempotencyKey;\n const init: RequestInit = { method, headers };\n if (method === \"POST\" && params) {\n headers[\"content-type\"] = \"application/x-www-form-urlencoded\";\n init.body = stripeForm(params);\n }\n const res = await fetch(`https://api.stripe.com${path}${qs}`, init);\n const body = (await res.json().catch(() => ({}))) as Record<string, unknown>;\n return { ok: res.ok, status: res.status, body };\n}\n\nfunction fail(op: string, r: StripeResult): never {\n const err = new Error(`stripe ${op} failed: ${r.status}`) as Error & { code: string };\n err.code = \"stripe_error\";\n throw err;\n}\n\nfunction clientSecretOf(sub: Record<string, unknown>): string | undefined {\n const inv = sub.latest_invoice as Record<string, unknown> | undefined;\n const confirmation = inv?.confirmation_secret as Record<string, unknown> | undefined;\n const intent = inv?.payment_intent as Record<string, unknown> | undefined;\n const secret = confirmation?.client_secret ?? intent?.client_secret;\n return typeof secret === \"string\" ? secret : undefined;\n}\n\nfunction requireSecret(secretKey: string | undefined): string {\n if (!secretKey) {\n const err = new Error(\"stripe secret key missing\") as Error & { code: string };\n err.code = \"not_configured\";\n throw err;\n }\n return secretKey;\n}\n\n/** Build the Stripe provider. `secretKey` powers charging/refunds; `webhookSecret`\n * powers webhook ingest. Each is resolved from the vault per request, so the\n * webhook route can construct an ingest-only provider without the secret key. */\nexport function createStripeProvider(config: { secretKey?: string; webhookSecret?: string }): PaymentsProvider {\n const { secretKey, webhookSecret } = config;\n return {\n async createSubscription(input) {\n const sk = requireSecret(secretKey);\n const meta = { applicationId: input.applicationId, groupId: input.groupId, email: input.email };\n let customerId = input.existingCustomerId;\n if (!customerId) {\n const cust = await stripeCall(\n sk,\n \"POST\",\n \"/v1/customers\",\n { email: input.email, name: input.name, metadata: meta },\n `cus:${input.applicationId}`,\n );\n if (!cust.ok) fail(\"customer create\", cust);\n customerId = String(cust.body.id);\n }\n const sub = await stripeCall(\n sk,\n \"POST\",\n \"/v1/subscriptions\",\n {\n customer: customerId,\n \"items[0][price]\": input.priceId,\n payment_behavior: \"default_incomplete\",\n \"payment_settings[save_default_payment_method]\": \"on_subscription\",\n \"payment_settings[payment_method_types][0]\": \"card\",\n \"expand[0]\": \"latest_invoice.confirmation_secret\",\n metadata: meta,\n },\n // The hardening S&S lacked: one subscription per application, so a client\n // retry between create and the db write can't orphan a second one.\n `sub:${input.applicationId}`,\n );\n if (!sub.ok) fail(\"subscription create\", sub);\n const clientSecret = clientSecretOf(sub.body);\n if (!clientSecret) fail(\"subscription confirmation-secret missing\", sub);\n return { customerId, subscriptionId: String(sub.body.id), clientSecret };\n },\n\n async ingestWebhook(rawBody, sigHeader) {\n if (!webhookSecret || !(await verifyStripeSignature(rawBody, sigHeader, webhookSecret))) {\n return { ok: false, reason: \"bad_signature\" };\n }\n const event = JSON.parse(rawBody) as StripeEvent;\n return { ok: true, eventId: event.id, event: normalizeWebhookEvent(event) };\n },\n\n async refund(input) {\n const sk = requireSecret(secretKey);\n const charges = await stripeCall(sk, \"GET\", \"/v1/charges\", { customer: input.customerId, limit: 100 });\n if (!charges.ok) fail(\"charges list\", charges);\n const rows = (charges.body.data as Array<Record<string, unknown>> | undefined) ?? [];\n const paid = rows.filter((c) => c.status === \"succeeded\" && c.refunded !== true);\n const charge = paid[paid.length - 1]; // Stripe returns newest-first; refund the earliest.\n if (!charge) {\n const err = new Error(\"no paid charge to refund\") as Error & { code: string };\n err.code = \"no_charge\";\n throw err;\n }\n const refund = await stripeCall(sk, \"POST\", \"/v1/refunds\", { charge: String(charge.id) });\n if (!refund.ok) fail(\"refund\", refund);\n const cancel = await stripeCall(sk, \"DELETE\", `/v1/subscriptions/${input.subscriptionId}`);\n const amount = refund.body.amount;\n return { refundedCents: typeof amount === \"number\" ? amount : null, subscriptionCanceled: cancel.ok };\n },\n };\n}\n","// Payment routes: POST /api/payments/subscription (start a founding-member\n// subscription), POST /api/webhooks/stripe (the AUTHORITATIVE writer of\n// paid/refunded/canceled, exactly-once per event id), and POST\n// /api/admin/applications/:id/refund (admin-gated; issues the refund but never\n// writes status — the charge.refunded webhook does). I/O only; the correctness\n// rules live in ./payments and the provider in ./payments-stripe.\nimport { getVaultSecret } from \"./auth\";\nimport { canceledPatch, firstPaymentPatch, refundedPatch, renewalPatch, webhookMutationId } from \"./payments\";\nimport type { WebhookEvent } from \"./payments\";\nimport { createStripeProvider } from \"./payments-stripe\";\nimport { json } from \"./worker-context\";\nimport type { ChapterEnv, WorkerContext } from \"./worker-context\";\nimport type { Route } from \"./worker-routes\";\nimport type { ChapterDb } from \"./types\";\n\ntype Row = Record<string, unknown>;\n\nconst codeOf = (err: unknown): string =>\n err && typeof err === \"object\" && typeof (err as { code?: unknown }).code === \"string\" ? (err as { code: string }).code : \"unknown\";\n\nasync function firstRow(db: ChapterDb, ns: string, q: Record<string, unknown>): Promise<Row | undefined> {\n const res = await db.query({ [ns]: { $: q } });\n const rows = res[ns];\n return Array.isArray(rows) ? rows[0] : undefined;\n}\n\nfunction lineItems(group: Row): { standardCents: number; discountCents: number; dueTodayCents: number } {\n const standard = Number(group.standardPriceCents ?? 0);\n const discount = Number(group.foundingDiscountCents ?? 0);\n return { standardCents: standard, discountCents: discount, dueTodayCents: standard - discount };\n}\n\n// Resolve the application an event targets: by metadata applicationId, else the\n// newest row for the Stripe customer.\nasync function findApplication(db: ChapterDb, event: WebhookEvent): Promise<Row | undefined> {\n if (\"applicationId\" in event && event.applicationId) {\n return firstRow(db, \"applications\", { where: { id: event.applicationId }, limit: 1 });\n }\n if (\"customerId\" in event && event.customerId) {\n return firstRow(db, \"applications\", { where: { stripeCustomerId: event.customerId }, order: { createdAt: \"desc\" }, limit: 1 });\n }\n return undefined;\n}\n\n// The application-row patch for a resolved event (empty = skip the write).\nfunction webhookPatch(event: WebhookEvent, status: string): Record<string, unknown> {\n switch (event.kind) {\n case \"first_payment\":\n return firstPaymentPatch(status, event.renewalAt);\n case \"renewal\":\n return event.renewalAt !== undefined ? renewalPatch(event.renewalAt) : {};\n case \"refunded\":\n return refundedPatch();\n case \"canceled\":\n return canceledPatch();\n default:\n return {};\n }\n}\n\nasync function startSubscription(req: Request, env: ChapterEnv, ctx: WorkerContext): Promise<Response> {\n let body: Row;\n try {\n body = JSON.parse(await req.text()) as Row;\n } catch {\n return json({ error: \"invalid JSON body\" }, 400);\n }\n const applicationId = typeof body.applicationId === \"string\" ? body.applicationId : \"\";\n if (!applicationId) return json({ error: \"applicationId required\" }, 400);\n if (body.refundPolicyAck !== true) return json({ error: \"refundPolicyAck required\" }, 400);\n\n const db = ctx.makeDb(env) as unknown as ChapterDb;\n const app = await firstRow(db, \"applications\", { where: { id: applicationId }, limit: 1 });\n if (!app) return json({ error: \"not found\" }, 404);\n // Single-charge guard: only from the initial state, so a re-post can't double-subscribe.\n if (app.status !== \"submitted\") return json({ error: \"already processed\" }, 409);\n\n const group = await firstRow(db, \"groups\", { where: { id: String(app.groupId ?? ctx.chapter.id) }, limit: 1 });\n const priceId = group?.stripePriceId;\n const secretKey = await getVaultSecret(db, \"stripe_secret_key\");\n if (!group || !priceId || !secretKey) return json({ error: \"payments not configured\" }, 503);\n\n const provider = createStripeProvider({ secretKey });\n let result;\n try {\n result = await provider.createSubscription({\n applicationId,\n groupId: String(group.id),\n email: String(app.email ?? \"\"),\n name: `${app.firstName ?? \"\"} ${app.lastName ?? \"\"}`.trim(),\n priceId: String(priceId),\n existingCustomerId: typeof app.stripeCustomerId === \"string\" ? app.stripeCustomerId : undefined,\n });\n } catch (err) {\n return json({ error: \"payment setup failed\", code: codeOf(err) }, 502);\n }\n\n await db.transact([\n {\n t: \"update\",\n ns: \"applications\",\n id: applicationId,\n attrs: { stripeCustomerId: result.customerId, stripeSubscriptionId: result.subscriptionId, refundPolicyAckAt: Date.now() },\n },\n ]);\n return json({ clientSecret: result.clientSecret, publishableKey: group.stripePublishableKey ?? null, lineItems: lineItems(group) });\n}\n\nasync function ingestWebhook(req: Request, env: ChapterEnv, ctx: WorkerContext): Promise<Response> {\n const db = ctx.makeDb(env) as unknown as ChapterDb;\n const webhookSecret = await getVaultSecret(db, \"stripe_webhook_secret\");\n if (!webhookSecret) return json({ error: \"webhook not configured\" }, 503);\n\n const rawBody = await req.text();\n const ingest = await createStripeProvider({ webhookSecret }).ingestWebhook(rawBody, req.headers.get(\"stripe-signature\") ?? \"\");\n if (!ingest.ok) return json({ error: \"invalid signature\" }, 400);\n const { eventId, event } = ingest;\n if (event.kind === \"ignored\") return json({ ok: true, ignored: event.type });\n\n const app = await findApplication(db, event);\n if (!app) return json({ ok: true, matched: false });\n\n const patch = webhookPatch(event, String(app.status ?? \"\"));\n if (Object.keys(patch).length) {\n await db.transact([{ t: \"update\", ns: \"applications\", id: String(app.id), attrs: patch }], { mutationId: webhookMutationId(eventId) });\n }\n return json({ ok: true });\n}\n\nasync function refundApplication(req: Request, url: URL, env: ChapterEnv, ctx: WorkerContext): Promise<Response> {\n const rawDb = ctx.makeDb(env);\n const u = await ctx.verifyUser(req, env);\n if (!u || !(await ctx.isAdmin(rawDb, u))) return json({ error: \"forbidden\" }, 403);\n\n const id = url.pathname.split(\"/\")[4] ?? \"\";\n const db = rawDb as unknown as ChapterDb;\n const app = await firstRow(db, \"applications\", { where: { id }, limit: 1 });\n if (!app) return json({ error: \"not found\" }, 404);\n if (app.status === \"refunded\") return json({ error: \"already refunded\" }, 409);\n if (app.status === \"approved\") return json({ error: \"approved memberships are non-refundable\" }, 409);\n if (!app.stripeSubscriptionId) return json({ error: \"no subscription on file\" }, 409);\n if (!app.stripeCustomerId) return json({ error: \"no customer on file\" }, 409);\n\n const secretKey = await getVaultSecret(db, \"stripe_secret_key\");\n if (!secretKey) return json({ error: \"payments not configured\" }, 503);\n\n try {\n const result = await createStripeProvider({ secretKey }).refund({\n customerId: String(app.stripeCustomerId),\n subscriptionId: String(app.stripeSubscriptionId),\n });\n // Note: status \"refunded\" is NOT written here — the charge.refunded webhook is\n // the single writer, keeping Stripe the source of truth.\n return json({ ok: true, refundedCents: result.refundedCents, subscriptionCanceled: result.subscriptionCanceled });\n } catch (err) {\n const code = codeOf(err);\n return json({ error: \"refund failed\", code }, code === \"no_charge\" ? 409 : 502);\n }\n}\n\nconst REFUND_PATH = /^\\/api\\/admin\\/applications\\/[^/]+\\/refund$/;\n\n/** The payment routes (chapter mode). Webhook + subscription are public\n * (capability-guarded by the unguessable application id / the signed payload);\n * refund is admin-gated. */\nexport const handlePayments: Route = async (req, url, env, ctx) => {\n if (ctx.chapter.mode !== \"chapter\") return null;\n if (req.method === \"POST\" && url.pathname === \"/api/payments/subscription\") return startSubscription(req, env, ctx);\n if (req.method === \"POST\" && url.pathname === \"/api/webhooks/stripe\") return ingestWebhook(req, env, ctx);\n if (req.method === \"POST\" && REFUND_PATH.test(url.pathname)) return refundApplication(req, url, env, ctx);\n return null;\n};\n","// chapterWorker — the Cloudflare Worker for a chapter/hub site. This entry\n// (@odla-ai/chapter/worker) is separate from the core so the CLI can load\n// odla.config.mjs without pulling in worker-runtime deps.\n//\n// The worker is the ONLY thing that talks to odla-db, using the app key\n// (ODLA_API_KEY), which bypasses the deny-all rules. Browsers never receive a\n// db credential. Access is admin-only: a request is authorized when its Clerk\n// session JWT verifies AND the user's role (a JWT claim, or a row in the\n// Studio-seeded `admins` allowlist) is an admin rung.\n//\n// The env typing + auth plumbing live in ./worker-context; the route handlers in\n// ./worker-routes. This entry only builds the context and runs the routes in\n// order — first match wins, else the static site (ASSETS).\n//\n// hub mode routes: GET /api/config, GET /api/me, /api/crm/*, POST\n// /api/network/shared, else ASSETS. chapter mode adds the public member surface:\n// GET /api/join-config and POST /api/applications (idempotent).\nimport { createWorkerContext } from \"./worker-context\";\nimport type { ChapterEnv, ChapterWorkerOptions } from \"./worker-context\";\nimport { handleConfig, handleCrm, handleMe, handleMember, handleNetworkShared } from \"./worker-routes\";\nimport { handleSchedule } from \"./worker-routes-schedule\";\nimport { handlePayments } from \"./worker-routes-payments\";\nimport type { Route } from \"./worker-routes\";\n\nexport type { ChapterEnv, ChapterWorkerOptions } from \"./worker-context\";\n\n// First-match-wins order (preserved from the original single-file handler;\n// scheduling + payments appended after the member surface, all chapter-mode only).\nconst ROUTES: Route[] = [handleConfig, handleMe, handleCrm, handleNetworkShared, handleMember, handleSchedule, handlePayments];\n\n/**\n * Build the Cloudflare `ExportedHandler` for a chapter/hub site: Clerk-JWT\n * verification, the odla-db admins-allowlist gate, the mounted @odla-ai/crm\n * routes, the hub→chapter network projection, and the static-asset fallback. In\n * hub mode it serves /api/config, /api/me, /api/crm/*, /api/network/shared;\n * chapter mode adds the public member surface (join/apply; Stripe + booking\n * land in the member-experience release).\n *\n * Observability is a host concern, not a chapter dependency. To trace, wrap the\n * result in your worker entry — `export default withObservability(chapterWorker(\n * { chapter }))` — with `withObservability` from `@odla-ai/o11y`. Sites that\n * don't run o11y bundle `@odla-ai/chapter/worker` without installing it.\n */\nexport function chapterWorker(options: ChapterWorkerOptions) {\n const ctx = createWorkerContext(options);\n return {\n async fetch(req: Request, env: ChapterEnv): Promise<Response> {\n const url = new URL(req.url);\n for (const route of ROUTES) {\n const res = await route(req, url, env, ctx);\n if (res) return res;\n }\n // Everything else is the static site.\n return env.ASSETS.fetch(req);\n },\n };\n}\n"],"mappings":";AAMA,SAAS,iBAAiB;AAC1B,SAAS,oBAAoB,iBAAiB;;;AC2BvC,SAAS,cAAc,SAAkC,MAA4B;AAC1F,QAAM,MAAM,QAAQ,KAAK,KAAK;AAC9B,SAAO,OAAO,QAAQ,YAAY,KAAK,OAAO,SAAS,GAAG,IAAI,MAAO,KAAK,OAAO,CAAC;AACpF;AAGO,SAAS,YAAY,MAAc,MAA6B;AACrE,SAAO,SAAS,KAAK;AACvB;AAuDA,eAAsB,eAAe,IAAiB,MAA2C;AAC/F,MAAI;AACF,UAAM,QAAQ,MAAM,GAAG,QAAQ,IAAI,IAAI;AACvC,WAAO,OAAO,UAAU,YAAY,UAAU,KAAK,QAAQ;AAAA,EAC7D,QAAQ;AACN,WAAO;AAAA,EACT;AACF;;;AD/CO,IAAM,OAAO,CAAC,MAAe,SAAS,QAC3C,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG,EAAE,QAAQ,SAAS,EAAE,gBAAgB,mBAAmB,EAAE,CAAC;AAQzF,SAAS,oBAAoB,SAA+B;AACjE,QAAM,EAAE,QAAQ,IAAI;AACpB,QAAM,OAAO,QAAQ;AACrB,QAAM,UAAU,QAAQ,eAAe;AAEvC,MAAI,oBAAgE;AACpE,QAAM,eAAe,oBAAI,IAAmD;AAE5E,iBAAe,gBAAgB,KAAwC;AACrE,QAAI,qBAAqB,KAAK,IAAI,IAAI,kBAAkB,KAAK,IAAI,IAAQ,QAAO,kBAAkB;AAClG,UAAM,MAAM,MAAM,MAAM,GAAG,IAAI,aAAa,kBAAkB,IAAI,WAAW,sBAAsB,IAAI,QAAQ,EAAE;AACjH,QAAI,CAAC,IAAI,GAAI,OAAM,IAAI,MAAM,+BAA+B,IAAI,M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G;AAC1C,YAAI,IAAK,QAAO;AAAA,MAClB;AAEA,aAAO,IAAI,OAAO,MAAM,GAAG;AAAA,IAC7B;AAAA,EACF;AACF;","names":["fail","firstRow"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@odla-ai/chapter",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "A foundation for membership sites — one config (defineChapter) stands up a full member site (join, Stripe membership, Google booking, member area, admin, CRM) or an admin-only hub, on odla-db + Clerk + @odla-ai/crm + calendar + email.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://odla.ai/docs/packages/chapter",
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
68
|
"@odla-ai/auth-clerk": ">=0.2.0 <1.0.0",
|
|
69
|
+
"@odla-ai/calendar": ">=0.2.0 <1.0.0",
|
|
69
70
|
"@odla-ai/db": ">=0.6.0 <1.0.0",
|
|
70
71
|
"@odla-ai/ui": ">=0.7.0 <1.0.0",
|
|
71
72
|
"react": ">=18"
|
|
@@ -74,6 +75,9 @@
|
|
|
74
75
|
"@odla-ai/auth-clerk": {
|
|
75
76
|
"optional": true
|
|
76
77
|
},
|
|
78
|
+
"@odla-ai/calendar": {
|
|
79
|
+
"optional": true
|
|
80
|
+
},
|
|
77
81
|
"@odla-ai/db": {
|
|
78
82
|
"optional": true
|
|
79
83
|
},
|
|
@@ -86,6 +90,7 @@
|
|
|
86
90
|
},
|
|
87
91
|
"devDependencies": {
|
|
88
92
|
"@odla-ai/auth-clerk": "*",
|
|
93
|
+
"@odla-ai/calendar": "*",
|
|
89
94
|
"@odla-ai/db": "*",
|
|
90
95
|
"@odla-ai/ui": "*",
|
|
91
96
|
"@types/node": "^26.1.0",
|