@i4e/invest4edu-access-core 0.15.0 → 0.16.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@i4e/invest4edu-access-core",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "Shared access-control primitives for NeoFindesk: tenant keystone, role capabilities, reportee tree, feature flags, and the unified access engine (registry schema, snapshot resolver, visibleWhen).",
5
5
  "type": "module",
6
6
  "exports": {
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Type declarations for the entitlement store.
3
+ *
4
+ * The package is plain JavaScript with JSDoc, which is fine for the Node services. NFD AI is
5
+ * TypeScript, and without declarations every call through this module widens to `any` — which
6
+ * silently removes the type checking from the one place in the codebase that decides whether a
7
+ * customer is over quota. Declaring it here rather than in the consumer keeps one definition:
8
+ * a copy in each TypeScript service would be a second description of this contract, free to drift
9
+ * from the implementation without anything failing.
10
+ */
11
+
12
+ export interface EntitlementResult {
13
+ /** False only on a deliberate refusal — never on an infrastructure failure. */
14
+ allowed: boolean;
15
+ unlocked: boolean;
16
+ /** True when this feature is counted; consume() is a no-op otherwise. */
17
+ metered: boolean;
18
+ quota: number | null;
19
+ used: number;
20
+ remaining: number | null;
21
+ overage_policy: string;
22
+ reason?: string;
23
+ period?: string | null;
24
+ subject_key?: string | null;
25
+ /** Carried so consume() counts against the exact bucket that was just checked. */
26
+ carriedSubscriptionId?: unknown;
27
+ carriedPeriodStart?: Date | string | null;
28
+ carriedIdentity?: { accountId?: string | null; userId?: string | null };
29
+ carriedRow?: Record<string, unknown> | null;
30
+ }
31
+
32
+ export interface ConsumeResult {
33
+ consumed: boolean;
34
+ /** "not_metered" | "quota_exceeded" | "error" */
35
+ reason?: string;
36
+ }
37
+
38
+ export interface EntitlementStore {
39
+ findSubscription(args: {
40
+ subjectType: string;
41
+ subjectId: string;
42
+ }): Promise<Record<string, unknown> | null>;
43
+
44
+ entitlementFor(args: {
45
+ subjectType: string;
46
+ subjectId: string;
47
+ featureCode: string;
48
+ feature?: Record<string, unknown>;
49
+ identity?: { accountId?: string | null; userId?: string | null };
50
+ bypass?: boolean;
51
+ now?: Date;
52
+ }): Promise<EntitlementResult>;
53
+
54
+ consume(args: {
55
+ entitlement?: EntitlementResult;
56
+ featureCode: string;
57
+ n?: number;
58
+ now?: Date;
59
+ subscriptionId?: unknown;
60
+ row?: Record<string, unknown> | null;
61
+ identity?: { accountId?: string | null; userId?: string | null };
62
+ periodStart?: Date | string | null;
63
+ }): Promise<ConsumeResult>;
64
+
65
+ /** Idempotent; returns null when there is no default plan or the write failed. */
66
+ provisionDefault(args: {
67
+ subjectType: string;
68
+ subjectId: string;
69
+ accountId?: string | null;
70
+ now?: Date;
71
+ }): Promise<Record<string, unknown> | null>;
72
+ }
73
+
74
+ export function createEntitlementStore(opts: {
75
+ /** Returns a raw MongoDB `Db`, or null/undefined when the connection is not ready. */
76
+ getDb: () => unknown;
77
+ /** Cast an id the way the caller's driver expects. Defaults to pass-through. */
78
+ toObjectId?: (v: string) => unknown;
79
+ logger?: { warn: (msg: string) => void };
80
+ }): EntitlementStore;
81
+
82
+ declare const _default: { createEntitlementStore: typeof createEntitlementStore };
83
+ export default _default;
@@ -29,7 +29,10 @@ export const LIVE_STATUSES = Object.freeze(["trialing", "active", "past_due"]);
29
29
 
30
30
  export const TRANSITIONS = Object.freeze({
31
31
  trialing: ["active", "cancelled", "expired"],
32
- active: ["past_due", "blocked", "cancelled"],
32
+ // `expired` because a FIXED-TERM subscription that reaches its end has simply run out. Without
33
+ // it the only exits are `blocked`, which implies a deliberate cut-off, and `cancelled`, which
34
+ // implies the customer chose to leave — neither is true of a 90-day plan reaching day 91.
35
+ active: ["past_due", "blocked", "cancelled", "expired"],
33
36
  past_due: ["active", "blocked", "cancelled"],
34
37
  blocked: ["active", "cancelled"],
35
38
  cancelled: [],