@happyvertical/smrt-subscriptions 0.61.3 → 0.61.5
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/chunks/{SubscriptionPlanCollection-Dmkorx_Q.js → SubscriptionPlanCollection-BZnAy9Hh.js} +3 -3
- package/dist/chunks/{SubscriptionPlanCollection-Dmkorx_Q.js.map → SubscriptionPlanCollection-BZnAy9Hh.js.map} +1 -1
- package/dist/chunks/{SubscriptionPlanVersionCollection-yTE_Y-2z.js → SubscriptionPlanVersionCollection-UOKqST93.js} +2 -2
- package/dist/chunks/{SubscriptionPlanVersionCollection-yTE_Y-2z.js.map → SubscriptionPlanVersionCollection-UOKqST93.js.map} +1 -1
- package/dist/chunks/{plan-versioning-ChDyHPvn.js → plan-versioning-DSCBIici.js} +5 -4
- package/dist/chunks/plan-versioning-DSCBIici.js.map +1 -0
- package/dist/index.js +20 -7
- package/dist/index.js.map +1 -1
- package/dist/manifest.json +2253 -1428
- package/dist/models/reseller.d.ts.map +1 -1
- package/dist/plan-versioning.d.ts.map +1 -1
- package/dist/smrt-knowledge.json +858 -858
- package/package.json +9 -9
- package/dist/chunks/plan-versioning-ChDyHPvn.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@happyvertical/smrt-subscriptions",
|
|
3
|
-
"version": "0.61.
|
|
3
|
+
"version": "0.61.5",
|
|
4
4
|
"description": "Tenant subscriptions, entitlement resolution, usage thresholds, and subscription UI for SMRT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"smrtRawPrimitives": "strict",
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@happyvertical/smrt-core": "0.61.
|
|
30
|
-
"@happyvertical/smrt-tenancy": "0.61.
|
|
31
|
-
"@happyvertical/smrt-ui": "0.61.
|
|
32
|
-
"@happyvertical/smrt-users": "0.61.
|
|
33
|
-
"@happyvertical/sql": "^0.
|
|
29
|
+
"@happyvertical/smrt-core": "0.61.5",
|
|
30
|
+
"@happyvertical/smrt-tenancy": "0.61.5",
|
|
31
|
+
"@happyvertical/smrt-ui": "0.61.5",
|
|
32
|
+
"@happyvertical/smrt-users": "0.61.5",
|
|
33
|
+
"@happyvertical/sql": "^0.113.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"svelte": "^5.57.0"
|
|
@@ -41,9 +41,9 @@
|
|
|
41
41
|
}
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@happyvertical/accounting": "^0.
|
|
45
|
-
"@happyvertical/smrt-features": "0.61.
|
|
46
|
-
"@happyvertical/smrt-vitest": "0.61.
|
|
44
|
+
"@happyvertical/accounting": "^0.113.0",
|
|
45
|
+
"@happyvertical/smrt-features": "0.61.5",
|
|
46
|
+
"@happyvertical/smrt-vitest": "0.61.5",
|
|
47
47
|
"@sveltejs/package": "^2.5.8",
|
|
48
48
|
"@types/node": "24.13.6",
|
|
49
49
|
"svelte": "^5.57.1",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plan-versioning-ChDyHPvn.js","names":["frozen","target"],"sources":["../../src/plan-versioning.ts"],"sourcesContent":["/**\n * Publishing plan versions and pinning subscriptions to them (#4032).\n *\n * Kept out of the model files so `SubscriptionPlan` and `TenantSubscription`\n * share one implementation of the pin rules without importing each other.\n */\nimport { withSystemContext } from '@happyvertical/smrt-tenancy';\nimport { SubscriptionPlanCollection } from './collections/SubscriptionPlanCollection.js';\nimport { SubscriptionPlanVersionCollection } from './collections/SubscriptionPlanVersionCollection.js';\nimport type { SubscriptionPlan } from './models/SubscriptionPlan.js';\nimport { SubscriptionPlanVersion } from './models/SubscriptionPlanVersion.js';\nimport type { TenantSubscription } from './models/TenantSubscription.js';\nimport {\n extractPlanTerms,\n isEntitlementUpgrade,\n samePlanTerms,\n} from './plan-terms.js';\nimport type { PlanTerms, PlanVersionPinReason } from './types.js';\n\n/** An in-period replacement requested through `replacePlanVersion()`. */\nexport interface PlanVersionReplacement {\n policy: 'upgrade' | 'authorized';\n reason?: string;\n planVersionId?: string;\n}\n\n/**\n * Version 1 for a plan that predates versioning: its current (persisted)\n * terms, effective from the plan's creation. Returns false when the plan\n * already has versions.\n */\nexport async function snapshotLegacyPlanVersion(\n plan: SubscriptionPlan,\n): Promise<boolean> {\n if (!plan.id) return false;\n const versions = await SubscriptionPlanVersionCollection.create({\n db: plan.db,\n });\n const owner = plan.tenantId ?? null;\n const latest = await withSystemContext(() =>\n versions.findLatest(String(plan.id), owner),\n );\n if (latest) return false;\n await createVersion(plan, plan, 1, persistedCreatedAt(plan));\n return true;\n}\n\n/**\n * After a plan save, publish its terms as the next version when they differ\n * from the latest one (or when none exists). `prior` is the row the save\n * overwrote: when the plan had no versions yet, its terms become version 1,\n * effective from the plan's creation.\n */\nexport async function publishPlanVersion(\n plan: SubscriptionPlan,\n effectiveFrom: Date,\n prior?: SubscriptionPlan,\n): Promise<SubscriptionPlanVersion> {\n const versions = await SubscriptionPlanVersionCollection.create({\n db: plan.db,\n });\n const owner = plan.tenantId ?? null;\n let latest = await withSystemContext(() =>\n versions.findLatest(String(plan.id), owner),\n );\n if (!latest && prior && String(prior.id) === String(plan.id)) {\n latest = await createVersion(plan, prior, 1, persistedCreatedAt(prior));\n }\n if (latest && samePlanTerms(latest, plan)) return latest;\n return createVersion(plan, plan, (latest?.version ?? 0) + 1, effectiveFrom);\n}\n\nasync function createVersion(\n plan: SubscriptionPlan,\n terms: PlanTerms,\n version: number,\n effectiveFrom: Date,\n): Promise<SubscriptionPlanVersion> {\n const record = new SubscriptionPlanVersion({\n db: plan.db,\n tenantId: plan.tenantId ?? null,\n planId: String(plan.id),\n planKey: plan.planKey,\n name: plan.name,\n version,\n effectiveFrom,\n ...extractPlanTerms(terms),\n });\n await record.initialize();\n await record.save();\n return record;\n}\n\nfunction persistedCreatedAt(plan: SubscriptionPlan): Date {\n const created = plan.created_at ? new Date(plan.created_at) : null;\n return created && !Number.isNaN(created.getTime()) ? created : new Date(0);\n}\n\n/**\n * Bind a subscription to the plan version its current period paid for.\n * Called from `TenantSubscription.validateBeforeSave()`.\n *\n * - A new subscription, or a legacy row without a pin, pins the version in\n * force at its period start (`currentPeriodStart ?? startedAt`).\n * - A renewal re-pins the version in force at the new boundary, for the\n * (possibly changed) plan. A save is a renewal only when the new period\n * starts at or after the stored `currentPeriodEnd` (or, without one, after\n * the stored start); moving the start within a paid period is not.\n * - At creation and renewal the boundary decides the version: a caller may\n * name only that version, unless the save is an authorized replacement.\n * - Within a paid period, a different plan or version is accepted only when\n * it is an entitlement upgrade of the frozen terms, when the period no\n * longer entitles, or through an explicit authorized replacement.\n *\n * A plan without versions (an unmigrated legacy plan) leaves the pin empty;\n * resolution then falls back to the live plan.\n */\nexport async function pinSubscriptionPlanVersion(\n subscription: TenantSubscription,\n persisted: TenantSubscription | null,\n replacement: PlanVersionReplacement | undefined,\n now = new Date(),\n): Promise<void> {\n const versions = await SubscriptionPlanVersionCollection.create({\n db: subscription.db,\n });\n const boundary = periodBoundary(subscription);\n const requested =\n subscription.planVersionId &&\n subscription.planVersionId !== (persisted?.planVersionId ?? null)\n ? subscription.planVersionId\n : replacement?.planVersionId;\n const authorizedReason =\n replacement?.policy === 'authorized' ? replacement.reason?.trim() : '';\n if (replacement?.policy === 'authorized' && !authorizedReason) {\n throw new Error('An authorized plan version replacement needs a reason');\n }\n const pin = (\n version: SubscriptionPlanVersion | null,\n reason: PlanVersionPinReason,\n note = '',\n ) => {\n subscription.planVersionId = version?.id ? String(version.id) : null;\n subscription.planVersionPinnedAt = version ? now : null;\n subscription.planVersionPinReason = version ? reason : null;\n subscription.planVersionPinNote = version ? note : '';\n };\n const load = async (id: string) => {\n const version = await loadVersion(versions, id);\n if (!version) throw new Error('Plan version is unavailable');\n if (version.planId !== subscription.planId) {\n throw new Error('Plan version does not belong to the subscription plan');\n }\n return version;\n };\n // Creation and renewal: the version in force at the boundary.\n const atBoundary = async (reason: PlanVersionPinReason) => {\n const inForce = await versions.findInForce(subscription.planId, boundary);\n if (requested && requested !== inForce?.id) {\n const chosen = await load(requested);\n if (!authorizedReason) {\n throw new Error(\n 'A period pins the plan version in force at its start; pinning ' +\n \"another version needs replacePlanVersion({ policy: 'authorized', reason }).\",\n );\n }\n pin(chosen, 'authorized', authorizedReason);\n return;\n }\n if (authorizedReason && inForce) {\n pin(inForce, 'authorized', authorizedReason);\n return;\n }\n pin(inForce, reason);\n };\n\n if (!persisted) {\n await atBoundary('period_start');\n return;\n }\n if (isRenewal(persisted, boundary)) {\n await atBoundary('renewal');\n // Saved ahead of its start, a renewal must not change what the still\n // running paid period resolves to: allow it only when the pin is\n // unchanged, the change is an upgrade, or it was authorized.\n if (\n boundary.getTime() > now.getTime() &&\n !authorizedReason &&\n persisted.isEntitled(now) &&\n (subscription.planVersionId !== persisted.planVersionId ||\n subscription.planId !== persisted.planId)\n ) {\n const frozen = await frozenTerms(\n versions,\n persisted,\n periodBoundary(persisted),\n );\n const target: PlanTerms | null = subscription.planVersionId\n ? await loadVersion(versions, subscription.planVersionId)\n : await (\n await SubscriptionPlanCollection.create({ db: subscription.db })\n ).get({ id: subscription.planId }, { cache: false });\n if (frozen && (!target || !isEntitlementUpgrade(frozen, target))) {\n throw new Error(\n 'A renewal that changes plan terms can be saved once its period ' +\n 'has started; the current paid period keeps its terms until then.',\n );\n }\n }\n return;\n }\n const planChanged = persisted.planId !== subscription.planId;\n if (!planChanged && !requested && !replacement) {\n if (!persisted.planVersionId) {\n // A row written before versioning: pin what its period was sold under.\n pin(\n await versions.findInForce(subscription.planId, boundary),\n 'migration',\n );\n return;\n }\n // Nothing moved: keep the pin and its audit trail exactly as stored.\n subscription.planVersionId = persisted.planVersionId;\n subscription.planVersionPinnedAt = persisted.planVersionPinnedAt;\n subscription.planVersionPinReason = persisted.planVersionPinReason;\n subscription.planVersionPinNote = persisted.planVersionPinNote;\n return;\n }\n\n // An in-period change: the target is the named version, else the version\n // of the (new) plan in force now, else that plan's live terms.\n const next = requested\n ? await load(requested)\n : await versions.findInForce(subscription.planId, now);\n if (authorizedReason) {\n pin(next, 'authorized', authorizedReason);\n return;\n }\n if (!persisted.isEntitled(now)) {\n pin(next, 'period_start');\n return;\n }\n const target: PlanTerms | null =\n next ??\n (await (\n await SubscriptionPlanCollection.create({ db: subscription.db })\n ).get({ id: subscription.planId }, { cache: false }));\n const frozen = await frozenTerms(versions, persisted, boundary);\n if (frozen && (!target || !isEntitlementUpgrade(frozen, target))) {\n throw new Error(\n 'This plan change would remove entitlements from a paid period. It ' +\n 'takes effect at renewal (a new period starting at or after ' +\n \"currentPeriodEnd), or through replacePlanVersion({ policy: 'authorized', reason }).\",\n );\n }\n pin(next, 'upgrade', replacement?.reason?.trim() ?? '');\n}\n\n/**\n * Whether moving to `boundary` starts a new period: at or after the stored\n * period end, or — for an open-ended row — after the stored start.\n */\nfunction isRenewal(persisted: TenantSubscription, boundary: Date): boolean {\n const previous = periodBoundary(persisted).getTime();\n if (boundary.getTime() === previous) return false;\n const end = persisted.currentPeriodEnd\n ? new Date(persisted.currentPeriodEnd).getTime()\n : null;\n return end === null\n ? boundary.getTime() > previous\n : boundary.getTime() >= end;\n}\n\n/** The terms the persisted subscription's current period paid for. */\nasync function frozenTerms(\n versions: SubscriptionPlanVersionCollection,\n persisted: TenantSubscription,\n boundary: Date,\n): Promise<PlanTerms | null> {\n if (persisted.planVersionId) {\n const pinned = await loadVersion(versions, persisted.planVersionId);\n if (pinned) return pinned;\n }\n const inForce = await versions.findInForce(persisted.planId, boundary);\n if (inForce) return inForce;\n const plans = await SubscriptionPlanCollection.create({\n db: persisted.db,\n });\n return plans.get({ id: persisted.planId }, { cache: false });\n}\n\nasync function loadVersion(\n versions: SubscriptionPlanVersionCollection,\n id: string,\n): Promise<SubscriptionPlanVersion | null> {\n return versions.findVisibleById(id);\n}\n\n/** Start of a subscription's current period. */\nexport function periodBoundary(\n subscription: Pick<TenantSubscription, 'currentPeriodStart' | 'startedAt'>,\n): Date {\n return new Date(subscription.currentPeriodStart ?? subscription.startedAt);\n}\n"],"mappings":";;;;;;;;;;;;AA+BA,eAAsB,0BACpB,MACkB;CAClB,IAAI,CAAC,KAAK,IAAI,OAAO;CACrB,MAAM,WAAW,MAAM,kCAAkC,OAAO,EAC9D,IAAI,KAAK,GACX,CAAC;CACD,MAAM,QAAQ,KAAK,YAAY;CAI/B,IAAI,MAHiB,wBACnB,SAAS,WAAW,OAAO,KAAK,EAAE,GAAG,KAAK,CAC5C,GACY,OAAO;CACnB,MAAM,cAAc,MAAM,MAAM,GAAG,mBAAmB,IAAI,CAAC;CAC3D,OAAO;AACT;AAQA,eAAsB,mBACpB,MACA,eACA,OACkC;CAClC,MAAM,WAAW,MAAM,kCAAkC,OAAO,EAC9D,IAAI,KAAK,GACX,CAAC;CACD,MAAM,QAAQ,KAAK,YAAY;CAC/B,IAAI,SAAS,MAAM,wBACjB,SAAS,WAAW,OAAO,KAAK,EAAE,GAAG,KAAK,CAC5C;CACA,IAAI,CAAC,UAAU,SAAS,OAAO,MAAM,EAAE,MAAM,OAAO,KAAK,EAAE,GACzD,SAAS,MAAM,cAAc,MAAM,OAAO,GAAG,mBAAmB,KAAK,CAAC;CAExE,IAAI,UAAU,cAAc,QAAQ,IAAI,GAAG,OAAO;CAClD,OAAO,cAAc,MAAM,OAAO,QAAQ,WAAW,KAAK,GAAG,aAAa;AAC5E;AAEA,eAAe,cACb,MACA,OACA,SACA,eACkC;CAClC,MAAM,SAAS,IAAI,wBAAwB;EACzC,IAAI,KAAK;EACT,UAAU,KAAK,YAAY;EAC3B,QAAQ,OAAO,KAAK,EAAE;EACtB,SAAS,KAAK;EACd,MAAM,KAAK;EACX;EACA;EACA,GAAG,iBAAiB,KAAK;CAC3B,CAAC;CACD,MAAM,OAAO,WAAW;CACxB,MAAM,OAAO,KAAK;CAClB,OAAO;AACT;AAEA,SAAS,m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