@venturekit-pro/billing 0.0.45 → 0.0.46
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/README.md +52 -0
- package/dist/entitlements/entitlements.d.ts +24 -3
- package/dist/entitlements/entitlements.d.ts.map +1 -1
- package/dist/entitlements/entitlements.js +26 -5
- package/dist/entitlements/entitlements.js.map +1 -1
- package/dist/grants/grants.d.ts +140 -0
- package/dist/grants/grants.d.ts.map +1 -0
- package/dist/grants/grants.js +135 -0
- package/dist/grants/grants.js.map +1 -0
- package/dist/grants/index.d.ts +7 -0
- package/dist/grants/index.d.ts.map +1 -0
- package/dist/grants/index.js +4 -0
- package/dist/grants/index.js.map +1 -0
- package/dist/grants/postgres.d.ts +12 -0
- package/dist/grants/postgres.d.ts.map +1 -0
- package/dist/grants/postgres.js +111 -0
- package/dist/grants/postgres.js.map +1 -0
- package/dist/grants/store.d.ts +33 -0
- package/dist/grants/store.d.ts.map +1 -0
- package/dist/grants/store.js +84 -0
- package/dist/grants/store.js.map +1 -0
- package/dist/index.d.ts +15 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -2
- package/dist/index.js.map +1 -1
- package/dist/invoices/generator.d.ts +5 -2
- package/dist/invoices/generator.d.ts.map +1 -1
- package/dist/invoices/generator.js +4 -2
- package/dist/invoices/generator.js.map +1 -1
- package/dist/invoices/postgres.d.ts +27 -0
- package/dist/invoices/postgres.d.ts.map +1 -0
- package/dist/invoices/postgres.js +136 -0
- package/dist/invoices/postgres.js.map +1 -0
- package/dist/migrations/vk_billing_0003_catalogue_numbering.sql +20 -0
- package/dist/migrations/vk_billing_0004_grants.sql +43 -0
- package/dist/plans/index.d.ts +12 -0
- package/dist/plans/index.d.ts.map +1 -1
- package/dist/plans/index.js +19 -0
- package/dist/plans/index.js.map +1 -1
- package/dist/plans/postgres.d.ts +14 -0
- package/dist/plans/postgres.d.ts.map +1 -0
- package/dist/plans/postgres.js +89 -0
- package/dist/plans/postgres.js.map +1 -0
- package/dist/plans/store.d.ts +17 -0
- package/dist/plans/store.d.ts.map +1 -0
- package/dist/plans/store.js +73 -0
- package/dist/plans/store.js.map +1 -0
- package/dist/sql.d.ts +23 -0
- package/dist/sql.d.ts.map +1 -0
- package/dist/sql.js +37 -0
- package/dist/sql.js.map +1 -0
- package/dist/subscriptions/manager.d.ts +4 -1
- package/dist/subscriptions/manager.d.ts.map +1 -1
- package/dist/subscriptions/manager.js +11 -8
- package/dist/subscriptions/manager.js.map +1 -1
- package/dist/subscriptions/postgres.d.ts +16 -0
- package/dist/subscriptions/postgres.d.ts.map +1 -0
- package/dist/subscriptions/postgres.js +101 -0
- package/dist/subscriptions/postgres.js.map +1 -0
- package/dist/types/invoice.d.ts +2 -0
- package/dist/types/invoice.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/migrations/vk_billing_0003_catalogue_numbering.sql +20 -0
- package/src/migrations/vk_billing_0004_grants.sql +43 -0
package/README.md
CHANGED
|
@@ -19,6 +19,8 @@ npm install @venturekit-pro/billing@dev
|
|
|
19
19
|
- **Invoice generation** — from plan + usage, with tax calculation
|
|
20
20
|
- **Usage tracking** — record and meter feature usage
|
|
21
21
|
- **Metered allowances** — an included quantity per period, credits that roll over, exempt consumptions on debt, automatic top-ups
|
|
22
|
+
- **Stacked grants** — entitlements from everything a subject holds (subscription + packs + trials + manual deals), each with the features it was sold with, frozen
|
|
23
|
+
- **Postgres stores** — a plan catalogue edited at runtime, subscriptions, invoices numbered without gaps, grants
|
|
22
24
|
- **Feature gating** — plan-based and usage-based access control
|
|
23
25
|
- **Auto-migrations** — billing tables created automatically via `vk migrate`
|
|
24
26
|
|
|
@@ -163,6 +165,56 @@ row-level security, implements `MeterStore` — eight small queries — and keep
|
|
|
163
165
|
here; the pure rules (`meterGate`, `splitOf`, `openingCredits`, `alertsCrossed`,
|
|
164
166
|
`topUpFor`, `packsFor`, `periodKey`, `shiftPeriod`) are exported too.
|
|
165
167
|
|
|
168
|
+
## Stacked grants
|
|
169
|
+
|
|
170
|
+
One plan per customer is rarely the whole story: a subscription AND two
|
|
171
|
+
one-off packs, a trial overlapping a purchase, a partner deal granted by hand.
|
|
172
|
+
Each is a **grant** — features, a window, a quantity — holding the features it
|
|
173
|
+
was sold with, frozen, so editing the catalogue never changes what a customer
|
|
174
|
+
already paid for.
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import { createGrantEntitlementsResolver, createPostgresGrantStore, grantFromPlan } from '@venturekit-pro/billing';
|
|
178
|
+
|
|
179
|
+
// when an order is paid (in its transaction): one grant per line, idempotent on the reference
|
|
180
|
+
const grants = createPostgresGrantStore(tx.query);
|
|
181
|
+
await grants.grant(grantFromPlan(plan, { subjectId: companyId, source: 'purchase', reference: `${orderId}:${line}`, quantity }));
|
|
182
|
+
|
|
183
|
+
// in a route
|
|
184
|
+
const entitlements = createGrantEntitlementsResolver({
|
|
185
|
+
getGrants: (subjectId, at) => createPostgresGrantStore(query).active(subjectId, at),
|
|
186
|
+
combine: { communities_per_listing: 'max' }, // the rest add up (limit × quantity)
|
|
187
|
+
});
|
|
188
|
+
const ent = await entitlements.forSubject(companyId);
|
|
189
|
+
ent.require('talent_pool'); // 402 unless a running grant has it
|
|
190
|
+
await ent.requireWithinLimit('listings', () => listings.countSince(companyId, ent.since)); // 403 at the cap
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Per feature, over the grants active now: enabled if any grant enables it,
|
|
194
|
+
unlimited if any enabling grant gives no limit, otherwise `sum` (default) or
|
|
195
|
+
`max`. `ent.since` is the earliest running grant's start (count usage "for the
|
|
196
|
+
life of the plan" from there), `ent.until` when the last one ends. Grants are
|
|
197
|
+
never deleted: `revoke(id, { reason, by })` ends one early, `setEnd(id, date)`
|
|
198
|
+
extends or shortens it; `grantedBy` / `revokedBy` record who. An app whose grants are its own rows (orders, deals)
|
|
199
|
+
maps them to `EntitlementGrant` and passes them to `resolveGrantEntitlements`.
|
|
200
|
+
|
|
201
|
+
## Postgres stores
|
|
202
|
+
|
|
203
|
+
The tables `vk migrate` creates have stores:
|
|
204
|
+
|
|
205
|
+
- `createPostgresPlanStore(q)` — the catalogue at runtime: `list`, `get`,
|
|
206
|
+
`save` (plan + exactly these features), `setActive` (retire, never delete).
|
|
207
|
+
Feed `createEntitlementsResolver({ getPlan: store.get })` and
|
|
208
|
+
`createSubscriptionManager({ getPlan: store.get })`.
|
|
209
|
+
- `createPostgresSubscriptionStore(q)` for `createSubscriptionManager`.
|
|
210
|
+
- `createPostgresInvoiceStore(q)` for `createInvoiceGenerator`: `getNextNumber`
|
|
211
|
+
bumps a counter per prefix under a row lock held to the end of the
|
|
212
|
+
transaction — write the invoice in the same transaction and the series has
|
|
213
|
+
no gaps. `prefix: (d) => \`INV-${d.getUTCFullYear()}-\`` restarts it yearly.
|
|
214
|
+
- `createPostgresGrantStore(q)` (above).
|
|
215
|
+
|
|
216
|
+
Pass the transaction's `query` so each write commits with the rest of the request.
|
|
217
|
+
|
|
166
218
|
## Usage-to-Invoice Mapping
|
|
167
219
|
|
|
168
220
|
```typescript
|
|
@@ -67,8 +67,8 @@ export interface ResolvedFeature {
|
|
|
67
67
|
enabled: boolean;
|
|
68
68
|
/** `null` = unlimited; `undefined` when the feature is disabled. */
|
|
69
69
|
limit: number | null | undefined;
|
|
70
|
-
/** Where the value came from. */
|
|
71
|
-
source: 'plan' | 'override' | 'absent';
|
|
70
|
+
/** Where the value came from (`grants`: combined from stacked grants — grants/). */
|
|
71
|
+
source: 'plan' | 'override' | 'absent' | 'grants';
|
|
72
72
|
}
|
|
73
73
|
export interface LimitCheck {
|
|
74
74
|
allowed: boolean;
|
|
@@ -107,8 +107,29 @@ export declare function resolveEntitlements(input: {
|
|
|
107
107
|
plan: PlanDefinition | null;
|
|
108
108
|
overrides?: EntitlementOverrides;
|
|
109
109
|
}): Entitlements;
|
|
110
|
+
/**
|
|
111
|
+
* The {@link Entitlements} API over already-resolved features — what
|
|
112
|
+
* {@link resolveEntitlements} (one plan + overrides) and
|
|
113
|
+
* `resolveGrantEntitlements` (stacked grants) both answer with. `planName`
|
|
114
|
+
* names what the refusal says the feature is missing from; `null` with no
|
|
115
|
+
* `planId` means "nothing active" (`PLAN_UNKNOWN`).
|
|
116
|
+
*/
|
|
117
|
+
export declare function entitlementsFromFeatures(input: {
|
|
118
|
+
subjectId: string;
|
|
119
|
+
planId: string | null;
|
|
120
|
+
plan: PlanDefinition | null;
|
|
121
|
+
planName: string | null;
|
|
122
|
+
features: Record<string, ResolvedFeature>;
|
|
123
|
+
}): Entitlements;
|
|
110
124
|
export interface EntitlementsResolverOptions {
|
|
111
|
-
|
|
125
|
+
/** The catalogue, when it lives in code. */
|
|
126
|
+
plans?: PlanDefinition[];
|
|
127
|
+
/**
|
|
128
|
+
* The catalogue, when it lives in a store (`createPostgresPlanStore(q).get`):
|
|
129
|
+
* looked up per subject resolution, so an edit applies at the next one.
|
|
130
|
+
* Wins over {@link plans}.
|
|
131
|
+
*/
|
|
132
|
+
getPlan?: (planId: string) => Promise<PlanDefinition | null> | PlanDefinition | null;
|
|
112
133
|
/** The subject's current plan id, or `null` when none (free tier / trial expired). */
|
|
113
134
|
getPlanId: (subjectId: string) => Promise<string | null> | string | null;
|
|
114
135
|
getOverrides?: (subjectId: string) => Promise<EntitlementOverrides | null | undefined> | EntitlementOverrides | null | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entitlements.d.ts","sourceRoot":"","sources":["../../src/entitlements/entitlements.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAe,MAAM,mBAAmB,CAAC;AAErE,yGAAyG;AACzG,QAAA,MAAM,oBAAoB,EAAE,OAAO,MAAuC,CAAC;AAE3E,MAAM,MAAM,oBAAoB,GAAG,uBAAuB,GAAG,oBAAoB,GAAG,cAAc,CAAC;AAEnG;;;GAGG;AACH,qBAAa,gBAAiB,SAAQ,K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EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;CAC3C,GAAG,YAAY,CAwDf;AAeD,MAAM,WAAW,2BAA2B;IAC1C,4CAA4C;IAC5C,KAAK,CAAC,EAAE,cAAc,EAAE,CAAC;IACzB;;;;OAIG;IACH,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,GAAG,cAAc,GAAG,IAAI,CAAC;IACrF,sFAAsF;IACtF,SAAS,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,MAAM,GAAG,IAAI,CAAC;IACzE,YAAY,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC,oBAAoB,GAAG,IAAI,GAAG,SAAS,CAAC,GAAG,oBAAoB,GAAG,IAAI,GAAG,SAAS,CAAC;IACjI,8EAA8E;IAC9E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,gCAAgC;IAChC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,oBAAoB;IACnC,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IACrD,4DAA4D;IAC5D,UAAU,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACtC;AAED,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,2BAA2B,GAAG,oBAAoB,CA6BrG"}
|
|
@@ -67,7 +67,24 @@ export function resolveEntitlements(input) {
|
|
|
67
67
|
source: 'override',
|
|
68
68
|
};
|
|
69
69
|
}
|
|
70
|
-
|
|
70
|
+
return entitlementsFromFeatures({
|
|
71
|
+
subjectId: input.subjectId,
|
|
72
|
+
planId: input.plan?.id ?? null,
|
|
73
|
+
plan: input.plan,
|
|
74
|
+
planName: input.plan?.name ?? null,
|
|
75
|
+
features,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The {@link Entitlements} API over already-resolved features — what
|
|
80
|
+
* {@link resolveEntitlements} (one plan + overrides) and
|
|
81
|
+
* `resolveGrantEntitlements` (stacked grants) both answer with. `planName`
|
|
82
|
+
* names what the refusal says the feature is missing from; `null` with no
|
|
83
|
+
* `planId` means "nothing active" (`PLAN_UNKNOWN`).
|
|
84
|
+
*/
|
|
85
|
+
export function entitlementsFromFeatures(input) {
|
|
86
|
+
const { features, planId } = input;
|
|
87
|
+
const active = planId !== null || input.planName !== null;
|
|
71
88
|
const has = (key) => features[key]?.enabled === true;
|
|
72
89
|
const limitOf = (key) => (has(key) ? features[key].limit : undefined);
|
|
73
90
|
const checkLimit = (key, usage) => {
|
|
@@ -81,8 +98,8 @@ export function resolveEntitlements(input) {
|
|
|
81
98
|
const require = (key) => {
|
|
82
99
|
if (has(key))
|
|
83
100
|
return;
|
|
84
|
-
throw new EntitlementError(
|
|
85
|
-
? `'${key}' is not included in the ${input.
|
|
101
|
+
throw new EntitlementError(active ? 'PLAN_UPGRADE_REQUIRED' : 'PLAN_UNKNOWN', active
|
|
102
|
+
? `'${key}' is not included in the ${input.planName ?? planId} plan`
|
|
86
103
|
: `No plan is active for this account, so '${key}' is not available`, { planId, featureKey: key });
|
|
87
104
|
};
|
|
88
105
|
return {
|
|
@@ -132,7 +149,11 @@ function fromPlanFeature(f) {
|
|
|
132
149
|
export function createEntitlementsResolver(options) {
|
|
133
150
|
const ttlMs = options.ttlMs ?? 60_000;
|
|
134
151
|
const now = options.now ?? Date.now;
|
|
135
|
-
|
|
152
|
+
if (!options.plans && !options.getPlan) {
|
|
153
|
+
throw new Error('createEntitlementsResolver: pass `plans` or `getPlan`');
|
|
154
|
+
}
|
|
155
|
+
const plansById = new Map((options.plans ?? []).map((p) => [p.id, p]));
|
|
156
|
+
const lookup = async (planId) => options.getPlan ? ((await options.getPlan(planId)) ?? null) : (plansById.get(planId) ?? null);
|
|
136
157
|
const cache = new Map();
|
|
137
158
|
return {
|
|
138
159
|
async forSubject(subjectId) {
|
|
@@ -143,7 +164,7 @@ export function createEntitlementsResolver(options) {
|
|
|
143
164
|
options.getPlanId(subjectId),
|
|
144
165
|
options.getOverrides ? options.getOverrides(subjectId) : undefined,
|
|
145
166
|
]);
|
|
146
|
-
const plan = planId ? (
|
|
167
|
+
const plan = planId ? await lookup(planId) : null;
|
|
147
168
|
const value = resolveEntitlements({ subjectId, plan, overrides: overrides ?? undefined });
|
|
148
169
|
if (ttlMs > 0)
|
|
149
170
|
cache.set(subjectId, { value, at: now() });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entitlements.js","sourceRoot":"","sources":["../../src/entitlements/entitlements.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAIH,yGAAyG;AACzG,MAAM,oBAAoB,GAAkB,MAAM,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;AAI3E;;;GAGG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAI9B;IAEA;IALF,CAAC,oBAAoB,CAAC,GAAG,IAAa,CAAC;IACvC,UAAU,CAAS;IAC5B,YACW,IAA0B,EACnC,OAAe,EACN,OAA6F;QAEtG,KAAK,CAAC,OAAO,CAAC,CAAC;QAJN,SAAI,GAAJ,IAAI,CAAsB;QAE1B,YAAO,GAAP,OAAO,CAAsF;QAGtG,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;QAC/B,IAAI,CAAC,UAAU,GAAG,IAAI,KAAK,oBAAoB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,cAAc,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;IAC9F,CAAC;CACF;AA+CD,6DAA6D;AAC7D,MAAM,UAAU,mBAAmB,CAAC,KAInC;IACC,MAAM,QAAQ,GAAoC,EAAE,CAAC;IACrD,KAAK,MAAM,CAAC,IAAI,KAAK,CAAC,I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AS,CAAC,CAAC;QAC/B,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stacked grants — entitlements built from EVERYTHING a subject holds right
|
|
3
|
+
* now, not from one plan.
|
|
4
|
+
*
|
|
5
|
+
* `resolveEntitlements` answers for one plan (plus overrides). Real
|
|
6
|
+
* products sell more than that: a subscription AND two one-off packs, a
|
|
7
|
+
* trial that overlaps a purchase, a partner deal granted by hand, an
|
|
8
|
+
* order line bought twice. Each of those is a GRANT: some features, for a
|
|
9
|
+
* window, with a quantity — and, crucially, the features it was SOLD with,
|
|
10
|
+
* frozen when granted (`grantFromPlan`), so editing the catalogue later
|
|
11
|
+
* never changes what a customer already paid for.
|
|
12
|
+
*
|
|
13
|
+
* Resolution, per feature key, over the grants active at `at`
|
|
14
|
+
* (`startsAt <= at < endsAt`, not revoked):
|
|
15
|
+
* - enabled when any grant enables it;
|
|
16
|
+
* - unlimited when any enabling grant gives no limit (`null` / absent);
|
|
17
|
+
* - otherwise combined by its rule: `sum` (default — credits, seats,
|
|
18
|
+
* monthly allowances: `limit × quantity` added up) or `max` (a reach,
|
|
19
|
+
* a tier, a size cap: the widest wins, quantity ignored).
|
|
20
|
+
*
|
|
21
|
+
* The result is the same {@link Entitlements} API (`has`, `require`,
|
|
22
|
+
* `requireWithinLimit`, `toQuotas`, …) plus the grants it came from and
|
|
23
|
+
* its window — `since` (the earliest running grant's start: count
|
|
24
|
+
* "for the life of the plan" usage from there) and `until` (when the last
|
|
25
|
+
* one ends, `null` = open-ended).
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* const ent = await grants.forSubject(companyId); // createGrantEntitlementsResolver
|
|
29
|
+
* ent.require('talent_pool'); // 402 unless a running grant has it
|
|
30
|
+
* await ent.requireWithinLimit('listings', () => listings.countSince(companyId, ent.since));
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
import type { PlanDefinition } from '../plans/index.js';
|
|
34
|
+
import { type Entitlements } from '../entitlements/entitlements.js';
|
|
35
|
+
/** A feature as a grant gives it — `limit` `null` / absent = unlimited. */
|
|
36
|
+
export interface GrantFeature {
|
|
37
|
+
featureKey: string;
|
|
38
|
+
enabled: boolean;
|
|
39
|
+
limit?: number | null;
|
|
40
|
+
}
|
|
41
|
+
export interface EntitlementGrant {
|
|
42
|
+
id: string;
|
|
43
|
+
subjectId: string;
|
|
44
|
+
/** What gave it: `purchase`, `subscription`, `trial`, `manual`, … (`[a-z][a-z0-9_]*`). */
|
|
45
|
+
source: string;
|
|
46
|
+
/** The order line, subscription, ticket… it stands for. Unique per (subject, source) when set. */
|
|
47
|
+
reference: string | null;
|
|
48
|
+
/** The catalogue plan its features were frozen from, if any (informational). */
|
|
49
|
+
planId: string | null;
|
|
50
|
+
/** What to call it in a UI ("Pro — yearly", "Partner deal"). */
|
|
51
|
+
label: string | null;
|
|
52
|
+
/** The features it gives, frozen when granted. */
|
|
53
|
+
features: GrantFeature[];
|
|
54
|
+
/** Multiplies the limits combined by `sum`. ≥ 1. */
|
|
55
|
+
quantity: number;
|
|
56
|
+
startsAt: Date;
|
|
57
|
+
/** `null` = open-ended. */
|
|
58
|
+
endsAt: Date | null;
|
|
59
|
+
/** Ended early (or scheduled to): no longer active from this instant. */
|
|
60
|
+
revokedAt: Date | null;
|
|
61
|
+
revokedReason: string | null;
|
|
62
|
+
/** Who granted it (a staff user id, `system`, …) — `null` when not recorded. */
|
|
63
|
+
grantedBy: string | null;
|
|
64
|
+
/** Who ended it early. */
|
|
65
|
+
revokedBy: string | null;
|
|
66
|
+
metadata: Record<string, unknown> | null;
|
|
67
|
+
createdAt: Date;
|
|
68
|
+
}
|
|
69
|
+
export interface GrantInput {
|
|
70
|
+
subjectId: string;
|
|
71
|
+
source: string;
|
|
72
|
+
reference?: string | null;
|
|
73
|
+
planId?: string | null;
|
|
74
|
+
label?: string | null;
|
|
75
|
+
features: GrantFeature[];
|
|
76
|
+
quantity?: number;
|
|
77
|
+
/** Default: now. */
|
|
78
|
+
startsAt?: Date;
|
|
79
|
+
/** Default `null` (open-ended). */
|
|
80
|
+
endsAt?: Date | null;
|
|
81
|
+
/** Who grants it (a staff user id, `system`, …). */
|
|
82
|
+
grantedBy?: string | null;
|
|
83
|
+
metadata?: Record<string, unknown> | null;
|
|
84
|
+
}
|
|
85
|
+
/** How one feature's limits add up across grants. */
|
|
86
|
+
export type CombineRule = 'sum' | 'max';
|
|
87
|
+
export interface CombineOptions {
|
|
88
|
+
/** Per feature key. Unlisted keys use {@link defaultCombine}. */
|
|
89
|
+
combine?: Record<string, CombineRule>;
|
|
90
|
+
/** Default `sum`. */
|
|
91
|
+
defaultCombine?: CombineRule;
|
|
92
|
+
}
|
|
93
|
+
export interface GrantEntitlements extends Entitlements {
|
|
94
|
+
/** The grants active at `at`, oldest first — what the entitlements came from. */
|
|
95
|
+
grants: EntitlementGrant[];
|
|
96
|
+
/** The earliest active grant's start — count usage "since the plan began" from here. `null` when none is active. */
|
|
97
|
+
since: Date | null;
|
|
98
|
+
/** When the last active grant ends; `null` when one is open-ended or none is active (see `grants.length`). */
|
|
99
|
+
until: Date | null;
|
|
100
|
+
}
|
|
101
|
+
/** The instant a grant stops: its end, or its revocation when earlier. */
|
|
102
|
+
export declare function grantEnd(grant: Pick<EntitlementGrant, 'endsAt' | 'revokedAt'>): Date | null;
|
|
103
|
+
export declare function isGrantActive(grant: Pick<EntitlementGrant, 'startsAt' | 'endsAt' | 'revokedAt'>, at?: Date): boolean;
|
|
104
|
+
/** Pure resolution over a subject's grants (any grants — inactive ones are skipped). */
|
|
105
|
+
export declare function resolveGrantEntitlements(input: {
|
|
106
|
+
subjectId: string;
|
|
107
|
+
grants: readonly EntitlementGrant[];
|
|
108
|
+
/** Default: now. */
|
|
109
|
+
at?: Date;
|
|
110
|
+
} & CombineOptions): GrantEntitlements;
|
|
111
|
+
/**
|
|
112
|
+
* A grant of what `plan` includes TODAY, frozen — the features are copied,
|
|
113
|
+
* so a later edit of the plan changes nothing for this grant. Lasts
|
|
114
|
+
* `intervals` × the plan's interval from `startsAt` unless `endsAt` is
|
|
115
|
+
* given (`null` = open-ended).
|
|
116
|
+
*/
|
|
117
|
+
export declare function grantFromPlan(plan: PlanDefinition, input: Omit<GrantInput, 'features' | 'planId' | 'label'> & {
|
|
118
|
+
label?: string | null;
|
|
119
|
+
intervals?: number;
|
|
120
|
+
}): GrantInput;
|
|
121
|
+
/** Throws on a grant no store should keep. */
|
|
122
|
+
export declare function assertGrantInput(input: GrantInput): void;
|
|
123
|
+
export interface GrantEntitlementsResolverOptions extends CombineOptions {
|
|
124
|
+
/**
|
|
125
|
+
* The subject's grants active at `at` — `store.active` (store.ts / postgres.ts),
|
|
126
|
+
* or the app's own rows mapped to {@link EntitlementGrant} (orders, deals).
|
|
127
|
+
*/
|
|
128
|
+
getGrants: (subjectId: string, at: Date) => Promise<readonly EntitlementGrant[]> | readonly EntitlementGrant[];
|
|
129
|
+
/** Per-subject cache lifetime in ms. Default 60 000. `0` disables caching. */
|
|
130
|
+
ttlMs?: number;
|
|
131
|
+
/** Clock override for tests. */
|
|
132
|
+
now?: () => Date;
|
|
133
|
+
}
|
|
134
|
+
export interface GrantEntitlementsResolver {
|
|
135
|
+
forSubject(subjectId: string): Promise<GrantEntitlements>;
|
|
136
|
+
/** Drop one subject (after a grant changed) or everything. */
|
|
137
|
+
invalidate(subjectId?: string): void;
|
|
138
|
+
}
|
|
139
|
+
export declare function createGrantEntitlementsResolver(options: GrantEntitlementsResolverOptions): GrantEntitlementsResolver;
|
|
140
|
+
//# sourceMappingURL=grants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grants.d.ts","sourceRoot":"","sources":["../../src/grants/grants.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,EAA4B,KA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IAAI,CAAC;CACX,GAAG,cAAc,GAAG,iBAAiB,CAsCrC;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,cAAc,EACpB,KAAK,EAAE,IAAI,CAAC,UAAU,EAAE,UAAU,GAAG,QAAQ,GAAG,OAAO,CAAC,GAAG;IAAE,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,GACvG,UAAU,CAWZ;AAED,8CAA8C;AAC9C,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAmBxD;AAMD,MAAM,WAAW,gCAAiC,SAAQ,cAAc;IACtE;;;OAGG;IACH,SAAS,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,KAAK,OAAO,CAAC,SAAS,gBAAgB,EAAE,CAAC,GAAG,SAAS,gBAAgB,EAAE,CAAC;IAC/G,8EAA8E;IAC9E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,gCAAgC;IAChC,GAAG,CAAC,EAAE,MAAM,IAAI,CAAC;CAClB;AAED,MAAM,WAAW,yBAAyB;IACxC,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAC1D,8DAA8D;IAC9D,UAAU,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACtC;AAED,wBAAgB,+BAA+B,CAAC,OAAO,EAAE,gCAAgC,GAAG,yBAAyB,CA0BpH"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { addBillingInterval } from '../plans/index.js';
|
|
2
|
+
import { entitlementsFromFeatures } from '../entitlements/entitlements.js';
|
|
3
|
+
/** The instant a grant stops: its end, or its revocation when earlier. */
|
|
4
|
+
export function grantEnd(grant) {
|
|
5
|
+
if (grant.revokedAt && (!grant.endsAt || grant.revokedAt < grant.endsAt))
|
|
6
|
+
return grant.revokedAt;
|
|
7
|
+
return grant.endsAt;
|
|
8
|
+
}
|
|
9
|
+
export function isGrantActive(grant, at = new Date()) {
|
|
10
|
+
const end = grantEnd(grant);
|
|
11
|
+
return grant.startsAt <= at && (end === null || at < end);
|
|
12
|
+
}
|
|
13
|
+
/** Pure resolution over a subject's grants (any grants — inactive ones are skipped). */
|
|
14
|
+
export function resolveGrantEntitlements(input) {
|
|
15
|
+
const at = input.at ?? new Date();
|
|
16
|
+
const active = input.grants.filter((g) => g.subjectId === input.subjectId && isGrantActive(g, at)).sort((a, b) => a.startsAt.getTime() - b.startsAt.getTime());
|
|
17
|
+
const ruleOf = (key) => input.combine?.[key] ?? input.defaultCombine ?? 'sum';
|
|
18
|
+
const features = {};
|
|
19
|
+
for (const grant of active) {
|
|
20
|
+
for (const f of grant.features) {
|
|
21
|
+
const prev = features[f.featureKey];
|
|
22
|
+
if (!f.enabled) {
|
|
23
|
+
if (!prev)
|
|
24
|
+
features[f.featureKey] = { featureKey: f.featureKey, enabled: false, limit: undefined, source: 'grants' };
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const limit = f.limit ?? null;
|
|
28
|
+
const own = limit === null ? null : ruleOf(f.featureKey) === 'sum' ? limit * grant.quantity : limit;
|
|
29
|
+
let next;
|
|
30
|
+
if (!prev?.enabled)
|
|
31
|
+
next = own;
|
|
32
|
+
else if (prev.limit === null || own === null)
|
|
33
|
+
next = null;
|
|
34
|
+
else
|
|
35
|
+
next = ruleOf(f.featureKey) === 'sum' ? prev.limit + own : Math.max(prev.limit, own);
|
|
36
|
+
features[f.featureKey] = { featureKey: f.featureKey, enabled: true, limit: next, source: 'grants' };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const ends = active.map(grantEnd);
|
|
40
|
+
const only = active.length === 1 ? active[0] : null;
|
|
41
|
+
const entitlements = entitlementsFromFeatures({
|
|
42
|
+
subjectId: input.subjectId,
|
|
43
|
+
planId: only?.planId ?? null,
|
|
44
|
+
plan: null,
|
|
45
|
+
planName: active.length === 0 ? null : (only?.label ?? only?.planId ?? 'current'),
|
|
46
|
+
features,
|
|
47
|
+
});
|
|
48
|
+
return {
|
|
49
|
+
...entitlements,
|
|
50
|
+
grants: active,
|
|
51
|
+
since: active[0]?.startsAt ?? null,
|
|
52
|
+
until: active.length === 0 || ends.some((e) => e === null) ? null : new Date(Math.max(...ends.map((e) => e.getTime()))),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A grant of what `plan` includes TODAY, frozen — the features are copied,
|
|
57
|
+
* so a later edit of the plan changes nothing for this grant. Lasts
|
|
58
|
+
* `intervals` × the plan's interval from `startsAt` unless `endsAt` is
|
|
59
|
+
* given (`null` = open-ended).
|
|
60
|
+
*/
|
|
61
|
+
export function grantFromPlan(plan, input) {
|
|
62
|
+
const startsAt = input.startsAt ?? new Date();
|
|
63
|
+
const { intervals, ...rest } = input;
|
|
64
|
+
return {
|
|
65
|
+
...rest,
|
|
66
|
+
planId: plan.id,
|
|
67
|
+
label: input.label ?? plan.name,
|
|
68
|
+
features: plan.features.map((f) => ({ featureKey: f.featureKey, enabled: f.enabled, limit: f.limit ?? null })),
|
|
69
|
+
startsAt,
|
|
70
|
+
endsAt: input.endsAt !== undefined ? input.endsAt : addBillingInterval(startsAt, plan.interval, (plan.intervalCount ?? 1) * (intervals ?? 1)),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/** Throws on a grant no store should keep. */
|
|
74
|
+
export function assertGrantInput(input) {
|
|
75
|
+
const fail = (why) => {
|
|
76
|
+
throw new Error(`grant for '${input.subjectId}': ${why}`);
|
|
77
|
+
};
|
|
78
|
+
if (!input.subjectId?.trim())
|
|
79
|
+
fail('subjectId is required');
|
|
80
|
+
if (!/^[a-z][a-z0-9_]{0,39}$/.test(input.source ?? ''))
|
|
81
|
+
fail(`source '${input.source}' must be 1–40 chars of a-z, 0-9, _`);
|
|
82
|
+
if (!Array.isArray(input.features))
|
|
83
|
+
fail('features must be an array');
|
|
84
|
+
const seen = new Set();
|
|
85
|
+
for (const f of input.features) {
|
|
86
|
+
if (!f || typeof f.featureKey !== 'string' || !f.featureKey)
|
|
87
|
+
fail('every feature needs a featureKey');
|
|
88
|
+
if (seen.has(f.featureKey))
|
|
89
|
+
fail(`feature '${f.featureKey}' is listed twice`);
|
|
90
|
+
seen.add(f.featureKey);
|
|
91
|
+
if (typeof f.enabled !== 'boolean')
|
|
92
|
+
fail(`feature '${f.featureKey}': enabled must be a boolean`);
|
|
93
|
+
if (f.limit !== undefined && f.limit !== null && (!Number.isInteger(f.limit) || f.limit < 0))
|
|
94
|
+
fail(`feature '${f.featureKey}': limit must be null or a whole number ≥ 0`);
|
|
95
|
+
}
|
|
96
|
+
if (input.quantity !== undefined && (!Number.isInteger(input.quantity) || input.quantity < 1))
|
|
97
|
+
fail('quantity must be a whole number ≥ 1');
|
|
98
|
+
const startsAt = input.startsAt ?? new Date();
|
|
99
|
+
if (Number.isNaN(startsAt.getTime()))
|
|
100
|
+
fail('startsAt is not a date');
|
|
101
|
+
if (input.endsAt && (Number.isNaN(input.endsAt.getTime()) || input.endsAt <= startsAt))
|
|
102
|
+
fail('endsAt must be after startsAt');
|
|
103
|
+
}
|
|
104
|
+
export function createGrantEntitlementsResolver(options) {
|
|
105
|
+
const ttlMs = options.ttlMs ?? 60_000;
|
|
106
|
+
const now = options.now ?? (() => new Date());
|
|
107
|
+
const cache = new Map();
|
|
108
|
+
return {
|
|
109
|
+
async forSubject(subjectId) {
|
|
110
|
+
const at = now();
|
|
111
|
+
const hit = cache.get(subjectId);
|
|
112
|
+
// A cached answer also expires when one of its grants does.
|
|
113
|
+
if (hit && at.getTime() - hit.at < ttlMs && (hit.value.until === null || at < hit.value.until))
|
|
114
|
+
return hit.value;
|
|
115
|
+
const grants = await options.getGrants(subjectId, at);
|
|
116
|
+
const value = resolveGrantEntitlements({
|
|
117
|
+
subjectId,
|
|
118
|
+
grants,
|
|
119
|
+
at,
|
|
120
|
+
...(options.combine ? { combine: options.combine } : {}),
|
|
121
|
+
...(options.defaultCombine ? { defaultCombine: options.defaultCombine } : {}),
|
|
122
|
+
});
|
|
123
|
+
if (ttlMs > 0)
|
|
124
|
+
cache.set(subjectId, { value, at: at.getTime() });
|
|
125
|
+
return value;
|
|
126
|
+
},
|
|
127
|
+
invalidate(subjectId) {
|
|
128
|
+
if (subjectId === undefined)
|
|
129
|
+
cache.clear();
|
|
130
|
+
else
|
|
131
|
+
cache.delete(subjectId);
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=grants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grants.js","sourceRoot":"","sources":["../../src/grants/grants.ts"],"names":[],"mappings":"AAiCA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,wBAAwB,EAA2C,MAAM,iCAAiC,CAAC;AA0EpH,0EAA0E;AAC1E,MAAM,UAAU,QAAQ,CAAC,KAAqD;IAC5E,IAAI,KAAK,CAAC,SAAS,IAAI,CAAC,CAAC,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC;QAAE,OAAO,KAAK,CAAC,SAAS,CAAC;IACjG,OAAO,KAAK,CAAC,MAAM,CAAC;AACtB,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,KAAkE,EAAE,KAAW,IAAI,IAAI,EAAE;IACrH,MAAM,GAAG,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC5B,OAAO,KAAK,CAAC,QAAQ,IAAI,EAAE,IAAI,CAAC,GAAG,KAAK,IAAI,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC;AAC5D,CAAC;AAED,wFAAwF;AACxF,MAAM,UAAU,wBAAwB,CAAC,KAKvB;IAChB,MAAM,EAAE,GAAG,KAAK,CAAC,EAAE,IAAI,IAAI,IAAI,EAAE,CAAC;IAClC,MAAM,MAAM,GA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CAAC,KAAK,CAAC;gBAAE,OAAO,GAAG,CAAC,KAAK,CAAC;YACjH,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,SAAS,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;YACtD,MAAM,KAAK,GAAG,wBAAwB,CAAC;gBACrC,SAAS;gBACT,MAAM;gBACN,EAAE;gBACF,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACxD,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAC9E,CAAC,CAAC;YACH,IAAI,KAAK,GAAG,CAAC;gBAAE,KAAK,CAAC,GAAG,CAAC,SAAS,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;YACjE,OAAO,KAAK,CAAC;QACf,CAAC;QACD,UAAU,CAAC,SAAS;YAClB,IAAI,SAAS,KAAK,SAAS;gBAAE,KAAK,CAAC,KAAK,EAAE,CAAC;;gBACtC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAC/B,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { assertGrantInput, createGrantEntitlementsResolver, grantEnd, grantFromPlan, isGrantActive, resolveGrantEntitlements, } from './grants.js';
|
|
2
|
+
export type { CombineOptions, CombineRule, EntitlementGrant, GrantEntitlements, GrantEntitlementsResolver, GrantEntitlementsResolverOptions, GrantFeature, GrantInput, } from './grants.js';
|
|
3
|
+
export { createMemoryGrantStore } from './store.js';
|
|
4
|
+
export type { GrantStore } from './store.js';
|
|
5
|
+
export { createPostgresGrantStore } from './postgres.js';
|
|
6
|
+
export type { PostgresGrantStoreOptions } from './postgres.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/grants/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,+BAA+B,EAC/B,QAAQ,EACR,aAAa,EACb,aAAa,EACb,wBAAwB,GACzB,MAAM,aAAa,CAAC;AACrB,YAAY,EACV,cAAc,EACd,WAAW,EACX,gBAAgB,EAChB,iBAAiB,EACjB,yBAAyB,EACzB,gCAAgC,EAChC,YAAY,EACZ,UAAU,GACX,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AACpD,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AACzD,YAAY,EAAE,yBAAyB,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { assertGrantInput, createGrantEntitlementsResolver, grantEnd, grantFromPlan, isGrantActive, resolveGrantEntitlements, } from './grants.js';
|
|
2
|
+
export { createMemoryGrantStore } from './store.js';
|
|
3
|
+
export { createPostgresGrantStore } from './postgres.js';
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/grants/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,+BAA+B,EAC/B,QAAQ,EACR,aAAa,EACb,aAAa,EACb,wBAAwB,GACzB,MAAM,aAAa,CAAC;AAWrB,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAEpD,OAAO,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {@link GrantStore} over `vk_billing_grants` (`vk_billing_0004_grants.sql`).
|
|
3
|
+
* Pass the transaction's `query` so a grant commits with what caused it (the
|
|
4
|
+
* payment recorded, the audit entry written).
|
|
5
|
+
*/
|
|
6
|
+
import type { BillingQuerier } from '../sql.js';
|
|
7
|
+
import type { GrantStore } from './store.js';
|
|
8
|
+
export interface PostgresGrantStoreOptions {
|
|
9
|
+
table?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function createPostgresGrantStore(querier: BillingQuerier, options?: PostgresGrantStoreOptions): GrantStore;
|
|
12
|
+
//# sourceMappingURL=postgres.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../src/grants/postgres.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGhD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,MAAM,WAAW,yBAAyB;IACxC,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AA4BD,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,cAAc,EAAE,OAAO,GAAE,yBAA8B,GAAG,UAAU,CAmGrH"}
|