@onesub/server 0.14.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/dist/__tests__/google-subscriptions-v2.test.d.ts +1 -1
- package/dist/__tests__/receipt-age.test.d.ts +7 -0
- package/dist/__tests__/receipt-age.test.d.ts.map +1 -0
- package/dist/__tests__/tracing.test.d.ts +2 -0
- package/dist/__tests__/tracing.test.d.ts.map +1 -0
- package/dist/cache.d.ts +5 -0
- package/dist/cache.d.ts.map +1 -1
- package/dist/index.cjs +178 -55
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +178 -55
- package/dist/index.js.map +1 -1
- package/dist/providers/apple.d.ts.map +1 -1
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/mock.d.ts.map +1 -1
- package/dist/routes/admin.d.ts.map +1 -1
- package/dist/routes/purchase.d.ts.map +1 -1
- package/dist/routes/validate.d.ts.map +1 -1
- package/dist/routes/webhook-apple.d.ts.map +1 -1
- package/dist/routes/webhook-google.d.ts +1 -1
- package/dist/routes/webhook-google.d.ts.map +1 -1
- package/dist/store.d.ts.map +1 -1
- package/dist/stores/postgres.d.ts.map +1 -1
- package/dist/stores/redis.d.ts +1 -0
- package/dist/stores/redis.d.ts.map +1 -1
- package/dist/tracing.d.ts.map +1 -1
- package/dist/webhook-events.d.ts +10 -0
- package/dist/webhook-events.d.ts.map +1 -1
- package/package.json +5 -3
- package/dist/__tests__/admin-customer-detail.test.js +0 -146
- package/dist/__tests__/admin-customer-detail.test.js.map +0 -1
- package/dist/__tests__/admin-list-subscriptions.test.js +0 -192
- package/dist/__tests__/admin-list-subscriptions.test.js.map +0 -1
- package/dist/__tests__/admin-subscription-detail.test.js +0 -112
- package/dist/__tests__/admin-subscription-detail.test.js.map +0 -1
- package/dist/__tests__/apple-root-ca.test.js +0 -41
- package/dist/__tests__/apple-root-ca.test.js.map +0 -1
- package/dist/__tests__/apple-status-api.test.js +0 -345
- package/dist/__tests__/apple-status-api.test.js.map +0 -1
- package/dist/__tests__/cache.test.js +0 -34
- package/dist/__tests__/cache.test.js.map +0 -1
- package/dist/__tests__/entitlements.test.js +0 -268
- package/dist/__tests__/entitlements.test.js.map +0 -1
- package/dist/__tests__/error-codes.test.js +0 -173
- package/dist/__tests__/error-codes.test.js.map +0 -1
- package/dist/__tests__/fetch-hardening.test.js +0 -192
- package/dist/__tests__/fetch-hardening.test.js.map +0 -1
- package/dist/__tests__/google-paused.test.js +0 -126
- package/dist/__tests__/google-paused.test.js.map +0 -1
- package/dist/__tests__/google-price-change.test.js +0 -186
- package/dist/__tests__/google-price-change.test.js.map +0 -1
- package/dist/__tests__/google-subscriptions-v2.test.js +0 -206
- package/dist/__tests__/google-subscriptions-v2.test.js.map +0 -1
- package/dist/__tests__/lifecycle-scenarios.test.js +0 -579
- package/dist/__tests__/lifecycle-scenarios.test.js.map +0 -1
- package/dist/__tests__/lifecycle-states.test.js +0 -423
- package/dist/__tests__/lifecycle-states.test.js.map +0 -1
- package/dist/__tests__/linked-purchase-token.test.js +0 -227
- package/dist/__tests__/linked-purchase-token.test.js.map +0 -1
- package/dist/__tests__/metrics.test.js +0 -340
- package/dist/__tests__/metrics.test.js.map +0 -1
- package/dist/__tests__/mock-provider.test.js +0 -188
- package/dist/__tests__/mock-provider.test.js.map +0 -1
- package/dist/__tests__/openapi.test.js +0 -32
- package/dist/__tests__/openapi.test.js.map +0 -1
- package/dist/__tests__/paused-auto-resume.test.js +0 -162
- package/dist/__tests__/paused-auto-resume.test.js.map +0 -1
- package/dist/__tests__/providers.test.js +0 -321
- package/dist/__tests__/providers.test.js.map +0 -1
- package/dist/__tests__/redis-store.test.js +0 -166
- package/dist/__tests__/redis-store.test.js.map +0 -1
- package/dist/__tests__/refund-policy.test.js +0 -317
- package/dist/__tests__/refund-policy.test.js.map +0 -1
- package/dist/__tests__/schema.test.js +0 -46
- package/dist/__tests__/schema.test.js.map +0 -1
- package/dist/__tests__/store.test.js +0 -172
- package/dist/__tests__/store.test.js.map +0 -1
- package/dist/__tests__/test-utils.js +0 -30
- package/dist/__tests__/test-utils.js.map +0 -1
- package/dist/__tests__/webhook-apple.test.js +0 -247
- package/dist/__tests__/webhook-apple.test.js.map +0 -1
- package/dist/__tests__/webhook-events.test.js +0 -36
- package/dist/__tests__/webhook-events.test.js.map +0 -1
- package/dist/__tests__/webhook-google.test.js +0 -255
- package/dist/__tests__/webhook-google.test.js.map +0 -1
- package/dist/__tests__/webhook-queue.test.js +0 -26
- package/dist/__tests__/webhook-queue.test.js.map +0 -1
- package/dist/__tests__/webhook-refund.test.js +0 -313
- package/dist/__tests__/webhook-refund.test.js.map +0 -1
- package/dist/cache.js +0 -39
- package/dist/cache.js.map +0 -1
- package/dist/errors.js +0 -19
- package/dist/errors.js.map +0 -1
- package/dist/http.js +0 -36
- package/dist/http.js.map +0 -1
- package/dist/logger.js +0 -20
- package/dist/logger.js.map +0 -1
- package/dist/openapi.js +0 -196
- package/dist/openapi.js.map +0 -1
- package/dist/providers/apple-root-ca.js +0 -38
- package/dist/providers/apple-root-ca.js.map +0 -1
- package/dist/providers/apple.js +0 -630
- package/dist/providers/apple.js.map +0 -1
- package/dist/providers/google.js +0 -564
- package/dist/providers/google.js.map +0 -1
- package/dist/providers/mock.js +0 -93
- package/dist/providers/mock.js.map +0 -1
- package/dist/routes/admin.js +0 -327
- package/dist/routes/admin.js.map +0 -1
- package/dist/routes/apple-offer.js +0 -72
- package/dist/routes/apple-offer.js.map +0 -1
- package/dist/routes/entitlements.js +0 -130
- package/dist/routes/entitlements.js.map +0 -1
- package/dist/routes/metrics.js +0 -281
- package/dist/routes/metrics.js.map +0 -1
- package/dist/routes/purchase.js +0 -204
- package/dist/routes/purchase.js.map +0 -1
- package/dist/routes/status.js +0 -55
- package/dist/routes/status.js.map +0 -1
- package/dist/routes/validate.js +0 -70
- package/dist/routes/validate.js.map +0 -1
- package/dist/routes/webhook-apple.js +0 -145
- package/dist/routes/webhook-apple.js.map +0 -1
- package/dist/routes/webhook-google.js +0 -204
- package/dist/routes/webhook-google.js.map +0 -1
- package/dist/routes/webhook.js +0 -19
- package/dist/routes/webhook.js.map +0 -1
- package/dist/store.js +0 -145
- package/dist/store.js.map +0 -1
- package/dist/stores/postgres.js +0 -329
- package/dist/stores/postgres.js.map +0 -1
- package/dist/stores/redis.js +0 -297
- package/dist/stores/redis.js.map +0 -1
- package/dist/stores/schema.js +0 -67
- package/dist/stores/schema.js.map +0 -1
- package/dist/tracing.js +0 -74
- package/dist/tracing.js.map +0 -1
- package/dist/webhook-events.js +0 -58
- package/dist/webhook-events.js.map +0 -1
- package/dist/webhook-queue.js +0 -108
- package/dist/webhook-queue.js.map +0 -1
package/dist/providers/mock.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { createHash } from 'node:crypto';
|
|
2
|
-
import { SUBSCRIPTION_STATUS, MOCK_RECEIPT_PREFIX } from '@onesub/shared';
|
|
3
|
-
import { log } from '../logger.js';
|
|
4
|
-
export function classifyMockReceipt(receipt) {
|
|
5
|
-
if (receipt.startsWith(MOCK_RECEIPT_PREFIX.NETWORK_ERROR))
|
|
6
|
-
return { kind: 'network-error' };
|
|
7
|
-
if (receipt.startsWith(MOCK_RECEIPT_PREFIX.REVOKED))
|
|
8
|
-
return { kind: 'revoked' };
|
|
9
|
-
if (receipt.startsWith(MOCK_RECEIPT_PREFIX.EXPIRED))
|
|
10
|
-
return { kind: 'expired' };
|
|
11
|
-
if (receipt.startsWith(MOCK_RECEIPT_PREFIX.INVALID) ||
|
|
12
|
-
receipt.startsWith(MOCK_RECEIPT_PREFIX.BAD_SIG)) {
|
|
13
|
-
return { kind: 'invalid' };
|
|
14
|
-
}
|
|
15
|
-
if (receipt.startsWith(MOCK_RECEIPT_PREFIX.SANDBOX))
|
|
16
|
-
return { kind: 'sandbox' };
|
|
17
|
-
return { kind: 'valid' };
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Gate the outcome against `valid` / `sandbox`. Throws for network-error,
|
|
21
|
-
* logs + returns false for rejection cases, returns true for pass-through.
|
|
22
|
-
* Every mock validator uses this so their rejection behavior stays in sync.
|
|
23
|
-
*/
|
|
24
|
-
function outcomePasses(outcome, tag) {
|
|
25
|
-
if (outcome.kind === 'network-error') {
|
|
26
|
-
throw new Error(`[onesub/mock/${tag}] simulated upstream network error`);
|
|
27
|
-
}
|
|
28
|
-
if (outcome.kind === 'valid' || outcome.kind === 'sandbox')
|
|
29
|
-
return true;
|
|
30
|
-
log.warn(`[onesub/mock/${tag}] receipt rejected`, { outcome: outcome.kind });
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
function deterministicTransactionId(prefix, receipt) {
|
|
34
|
-
const digest = createHash('sha256').update(receipt).digest('hex').slice(0, 24);
|
|
35
|
-
return `${prefix}_${digest}`;
|
|
36
|
-
}
|
|
37
|
-
const HOURS = 60 * 60 * 1000;
|
|
38
|
-
const DAYS = 24 * HOURS;
|
|
39
|
-
export function mockValidateAppleSubscription(receipt) {
|
|
40
|
-
const outcome = classifyMockReceipt(receipt);
|
|
41
|
-
if (!outcomePasses(outcome, 'apple'))
|
|
42
|
-
return null;
|
|
43
|
-
const now = Date.now();
|
|
44
|
-
const expiresAt = outcome.kind === 'sandbox' ? now + 1 * HOURS : now + 30 * DAYS;
|
|
45
|
-
return {
|
|
46
|
-
userId: '',
|
|
47
|
-
productId: 'mock_subscription',
|
|
48
|
-
platform: 'apple',
|
|
49
|
-
status: SUBSCRIPTION_STATUS.ACTIVE,
|
|
50
|
-
expiresAt: new Date(expiresAt).toISOString(),
|
|
51
|
-
originalTransactionId: deterministicTransactionId('mock_apple_orig', receipt),
|
|
52
|
-
purchasedAt: new Date(now).toISOString(),
|
|
53
|
-
willRenew: true,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
export function mockValidateAppleProduct(receipt, expectedProductId) {
|
|
57
|
-
const outcome = classifyMockReceipt(receipt);
|
|
58
|
-
if (!outcomePasses(outcome, 'apple'))
|
|
59
|
-
return null;
|
|
60
|
-
const productId = expectedProductId ?? 'mock_product';
|
|
61
|
-
return {
|
|
62
|
-
transactionId: deterministicTransactionId(`mock_apple_${productId}`, receipt),
|
|
63
|
-
productId,
|
|
64
|
-
purchasedAt: new Date().toISOString(),
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
export function mockValidateGoogleSubscription(receipt, productId) {
|
|
68
|
-
const outcome = classifyMockReceipt(receipt);
|
|
69
|
-
if (!outcomePasses(outcome, 'google'))
|
|
70
|
-
return null;
|
|
71
|
-
const now = Date.now();
|
|
72
|
-
const expiresAt = outcome.kind === 'sandbox' ? now + 1 * HOURS : now + 30 * DAYS;
|
|
73
|
-
return {
|
|
74
|
-
userId: '',
|
|
75
|
-
productId,
|
|
76
|
-
platform: 'google',
|
|
77
|
-
status: SUBSCRIPTION_STATUS.ACTIVE,
|
|
78
|
-
expiresAt: new Date(expiresAt).toISOString(),
|
|
79
|
-
originalTransactionId: deterministicTransactionId('mock_google_orig', receipt),
|
|
80
|
-
purchasedAt: new Date(now).toISOString(),
|
|
81
|
-
willRenew: true,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
export function mockValidateGoogleProduct(receipt, productId) {
|
|
85
|
-
const outcome = classifyMockReceipt(receipt);
|
|
86
|
-
if (!outcomePasses(outcome, 'google'))
|
|
87
|
-
return null;
|
|
88
|
-
return {
|
|
89
|
-
transactionId: deterministicTransactionId(`mock_google_${productId}`, receipt),
|
|
90
|
-
purchasedAt: new Date().toISOString(),
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
//# sourceMappingURL=mock.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mock.js","sourceRoot":"","sources":["../../src/providers/mock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAC1E,OA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nD,OAAO;QACL,aAAa,EAAE,0BAA0B,CAAC,eAAe,SAAS,EAAE,EAAE,OAAO,CAAC;QAC9E,WAAW,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KACtC,CAAC;AACJ,CAAC"}
|
package/dist/routes/admin.js
DELETED
|
@@ -1,327 +0,0 @@
|
|
|
1
|
-
import { Router } from 'express';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { PURCHASE_TYPE, ONESUB_ERROR_CODE, ROUTES, SUBSCRIPTION_STATUS } from '@onesub/shared';
|
|
4
|
-
import { evaluateEntitlement } from './entitlements.js';
|
|
5
|
-
import { sendError, sendZodError } from '../errors.js';
|
|
6
|
-
import { fetchAppleSubscriptionStatus } from '../providers/apple.js';
|
|
7
|
-
const ADMIN_SECRET_HEADER = 'x-admin-secret';
|
|
8
|
-
/**
|
|
9
|
-
* Admin routes for testing / operational tasks. Require `X-Admin-Secret`
|
|
10
|
-
* header matching config.adminSecret. If config.adminSecret is not set, the
|
|
11
|
-
* entire admin router is not mounted (returns 404 from the parent router).
|
|
12
|
-
*
|
|
13
|
-
* Endpoints:
|
|
14
|
-
* DELETE /onesub/purchase/admin/:userId/:productId
|
|
15
|
-
* → reset a non-consumable so the user can test the purchase flow again
|
|
16
|
-
*
|
|
17
|
-
* POST /onesub/purchase/admin/grant
|
|
18
|
-
* → manually insert a purchase record (skips store verification)
|
|
19
|
-
* body: { userId, productId, platform, type?, transactionId? }
|
|
20
|
-
*
|
|
21
|
-
* POST /onesub/purchase/admin/transfer
|
|
22
|
-
* → reassign a transactionId to a new userId (device migration)
|
|
23
|
-
*
|
|
24
|
-
* GET /onesub/admin/subscriptions?userId=&status=&productId=&platform=&limit=&offset=
|
|
25
|
-
* → filtered/paginated subscription list (used by dashboard + scripts)
|
|
26
|
-
*
|
|
27
|
-
* GET /onesub/admin/subscriptions/:transactionId
|
|
28
|
-
* → single subscription record by originalTransactionId (dashboard detail page)
|
|
29
|
-
*
|
|
30
|
-
* GET /onesub/admin/customers/:userId
|
|
31
|
-
* → bundle of every record onesub knows for one user (subs + purchases +
|
|
32
|
-
* entitlements when configured). Backs the dashboard customer detail page.
|
|
33
|
-
*/
|
|
34
|
-
export function createAdminRouter(config, purchaseStore, store, webhookQueue) {
|
|
35
|
-
if (!config.adminSecret)
|
|
36
|
-
return null;
|
|
37
|
-
const router = Router();
|
|
38
|
-
const adminSecret = config.adminSecret;
|
|
39
|
-
// Auth middleware — applied to both admin scopes. Without this guard, a
|
|
40
|
-
// misconfigured mount on the parent root would expose admin endpoints
|
|
41
|
-
// alongside host-app routes (e.g. /health).
|
|
42
|
-
const adminAuth = (req, res, next) => {
|
|
43
|
-
const provided = req.headers[ADMIN_SECRET_HEADER];
|
|
44
|
-
if (typeof provided !== 'string' || provided !== adminSecret) {
|
|
45
|
-
sendError(res, 401, ONESUB_ERROR_CODE.INVALID_ADMIN_SECRET, 'INVALID_ADMIN_SECRET');
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
next();
|
|
49
|
-
};
|
|
50
|
-
router.use('/onesub/purchase/admin', adminAuth);
|
|
51
|
-
router.use('/onesub/admin', adminAuth);
|
|
52
|
-
// DELETE /onesub/purchase/admin/:userId/:productId
|
|
53
|
-
// Express 5 types route params as `string | string[]` — narrow via zod.
|
|
54
|
-
const resetParamsSchema = z.object({
|
|
55
|
-
userId: z.string().min(1).max(256),
|
|
56
|
-
productId: z.string().min(1).max(256),
|
|
57
|
-
});
|
|
58
|
-
router.delete('/onesub/purchase/admin/:userId/:productId', async (req, res) => {
|
|
59
|
-
let params;
|
|
60
|
-
try {
|
|
61
|
-
params = resetParamsSchema.parse(req.params);
|
|
62
|
-
}
|
|
63
|
-
catch {
|
|
64
|
-
sendError(res, 400, ONESUB_ERROR_CODE.INVALID_INPUT, 'userId and productId required');
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
const deleted = await purchaseStore.deletePurchases(params.userId, params.productId);
|
|
68
|
-
res.json({ ok: true, deleted });
|
|
69
|
-
});
|
|
70
|
-
// POST /onesub/purchase/admin/transfer — reassign transactionId to a new userId
|
|
71
|
-
// (legitimate device/account migration)
|
|
72
|
-
const transferSchema = z.object({
|
|
73
|
-
transactionId: z.string().min(1).max(256),
|
|
74
|
-
newUserId: z.string().min(1).max(256),
|
|
75
|
-
});
|
|
76
|
-
router.post('/onesub/purchase/admin/transfer', async (req, res) => {
|
|
77
|
-
let body;
|
|
78
|
-
try {
|
|
79
|
-
body = transferSchema.parse(req.body);
|
|
80
|
-
}
|
|
81
|
-
catch (err) {
|
|
82
|
-
if (err instanceof z.ZodError) {
|
|
83
|
-
sendZodError(res, err);
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
throw err;
|
|
87
|
-
}
|
|
88
|
-
const existing = await purchaseStore.getPurchaseByTransactionId(body.transactionId);
|
|
89
|
-
if (!existing) {
|
|
90
|
-
sendError(res, 404, ONESUB_ERROR_CODE.TRANSACTION_NOT_FOUND, 'TRANSACTION_NOT_FOUND');
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
// Delete the old row, then save under new userId
|
|
94
|
-
await purchaseStore.deletePurchases(existing.userId, existing.productId);
|
|
95
|
-
const migrated = { ...existing, userId: body.newUserId };
|
|
96
|
-
await purchaseStore.savePurchase(migrated);
|
|
97
|
-
res.json({ ok: true, purchase: migrated });
|
|
98
|
-
});
|
|
99
|
-
// POST /onesub/purchase/admin/grant
|
|
100
|
-
const grantSchema = z.object({
|
|
101
|
-
userId: z.string().min(1).max(256),
|
|
102
|
-
productId: z.string().min(1).max(256),
|
|
103
|
-
platform: z.enum(['apple', 'google']),
|
|
104
|
-
type: z.enum([PURCHASE_TYPE.CONSUMABLE, PURCHASE_TYPE.NON_CONSUMABLE]).default(PURCHASE_TYPE.NON_CONSUMABLE),
|
|
105
|
-
transactionId: z.string().min(1).max(256).optional(),
|
|
106
|
-
});
|
|
107
|
-
router.post('/onesub/purchase/admin/grant', async (req, res) => {
|
|
108
|
-
let body;
|
|
109
|
-
try {
|
|
110
|
-
body = grantSchema.parse(req.body);
|
|
111
|
-
}
|
|
112
|
-
catch (err) {
|
|
113
|
-
if (err instanceof z.ZodError) {
|
|
114
|
-
sendZodError(res, err);
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
throw err;
|
|
118
|
-
}
|
|
119
|
-
const transactionId = body.transactionId ?? `admin_grant_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
|
|
120
|
-
const purchase = {
|
|
121
|
-
transactionId,
|
|
122
|
-
userId: body.userId,
|
|
123
|
-
productId: body.productId,
|
|
124
|
-
platform: body.platform,
|
|
125
|
-
type: body.type,
|
|
126
|
-
quantity: 1,
|
|
127
|
-
purchasedAt: new Date().toISOString(),
|
|
128
|
-
};
|
|
129
|
-
await purchaseStore.savePurchase(purchase);
|
|
130
|
-
res.json({ ok: true, purchase });
|
|
131
|
-
});
|
|
132
|
-
// GET /onesub/admin/subscriptions?userId=&status=&productId=&platform=&limit=&offset=
|
|
133
|
-
// Filtered + paginated subscription list. Backs the dashboard's
|
|
134
|
-
// subscriptions page and ad-hoc operational scripts.
|
|
135
|
-
const listQuerySchema = z.object({
|
|
136
|
-
userId: z.string().min(1).max(256).optional(),
|
|
137
|
-
status: z.enum([
|
|
138
|
-
SUBSCRIPTION_STATUS.ACTIVE,
|
|
139
|
-
SUBSCRIPTION_STATUS.GRACE_PERIOD,
|
|
140
|
-
SUBSCRIPTION_STATUS.ON_HOLD,
|
|
141
|
-
SUBSCRIPTION_STATUS.PAUSED,
|
|
142
|
-
SUBSCRIPTION_STATUS.EXPIRED,
|
|
143
|
-
SUBSCRIPTION_STATUS.CANCELED,
|
|
144
|
-
SUBSCRIPTION_STATUS.NONE,
|
|
145
|
-
]).optional(),
|
|
146
|
-
productId: z.string().min(1).max(256).optional(),
|
|
147
|
-
platform: z.enum(['apple', 'google']).optional(),
|
|
148
|
-
// Cap at 200 server-side so a runaway dashboard query can't tip the DB.
|
|
149
|
-
limit: z.coerce.number().int().positive().max(200).optional(),
|
|
150
|
-
offset: z.coerce.number().int().nonnegative().optional(),
|
|
151
|
-
});
|
|
152
|
-
router.get(ROUTES.ADMIN_SUBSCRIPTIONS, async (req, res) => {
|
|
153
|
-
let query;
|
|
154
|
-
try {
|
|
155
|
-
query = listQuerySchema.parse(req.query);
|
|
156
|
-
}
|
|
157
|
-
catch (err) {
|
|
158
|
-
if (err instanceof z.ZodError) {
|
|
159
|
-
sendZodError(res, err);
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
throw err;
|
|
163
|
-
}
|
|
164
|
-
try {
|
|
165
|
-
const result = await store.listFiltered(query);
|
|
166
|
-
const response = {
|
|
167
|
-
items: result.items,
|
|
168
|
-
total: result.total,
|
|
169
|
-
limit: result.limit,
|
|
170
|
-
offset: result.offset,
|
|
171
|
-
};
|
|
172
|
-
res.status(200).json(response);
|
|
173
|
-
}
|
|
174
|
-
catch (err) {
|
|
175
|
-
// Bubble the message up but not the stack — admin clients log status code
|
|
176
|
-
sendError(res, 500, ONESUB_ERROR_CODE.STORE_ERROR, err.message ?? 'list error');
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
// GET /onesub/admin/subscriptions/:transactionId — single record by
|
|
180
|
-
// originalTransactionId. Backs the dashboard's subscription detail page.
|
|
181
|
-
const detailParamsSchema = z.object({
|
|
182
|
-
transactionId: z.string().min(1).max(256),
|
|
183
|
-
});
|
|
184
|
-
router.get('/onesub/admin/subscriptions/:transactionId', async (req, res) => {
|
|
185
|
-
let params;
|
|
186
|
-
try {
|
|
187
|
-
params = detailParamsSchema.parse(req.params);
|
|
188
|
-
}
|
|
189
|
-
catch {
|
|
190
|
-
sendError(res, 400, ONESUB_ERROR_CODE.INVALID_INPUT, 'transactionId required');
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
try {
|
|
194
|
-
const sub = await store.getByTransactionId(params.transactionId);
|
|
195
|
-
if (!sub) {
|
|
196
|
-
sendError(res, 404, ONESUB_ERROR_CODE.TRANSACTION_NOT_FOUND, 'TRANSACTION_NOT_FOUND');
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
res.status(200).json(sub);
|
|
200
|
-
}
|
|
201
|
-
catch (err) {
|
|
202
|
-
sendError(res, 500, ONESUB_ERROR_CODE.STORE_ERROR, err.message ?? 'detail error');
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
// GET /onesub/admin/customers/:userId — full per-user profile. Combines the
|
|
206
|
-
// store calls a CS dashboard makes for "show me everything about this user"
|
|
207
|
-
// into a single round-trip.
|
|
208
|
-
//
|
|
209
|
-
// Always returns 200 (even for unknown userIds) — empty arrays + no
|
|
210
|
-
// entitlements means "we have no record of this user", which is a valid
|
|
211
|
-
// signal for the dashboard to render rather than an error condition.
|
|
212
|
-
const customerParamsSchema = z.object({
|
|
213
|
-
userId: z.string().min(1).max(256),
|
|
214
|
-
});
|
|
215
|
-
router.get('/onesub/admin/customers/:userId', async (req, res) => {
|
|
216
|
-
let params;
|
|
217
|
-
try {
|
|
218
|
-
params = customerParamsSchema.parse(req.params);
|
|
219
|
-
}
|
|
220
|
-
catch {
|
|
221
|
-
sendError(res, 400, ONESUB_ERROR_CODE.INVALID_INPUT, 'userId required');
|
|
222
|
-
return;
|
|
223
|
-
}
|
|
224
|
-
try {
|
|
225
|
-
const [subscriptions, purchases] = await Promise.all([
|
|
226
|
-
store.getAllByUserId(params.userId),
|
|
227
|
-
purchaseStore.getPurchasesByUserId(params.userId),
|
|
228
|
-
]);
|
|
229
|
-
// Evaluate entitlements only when configured. Hosts that don't use the
|
|
230
|
-
// entitlement abstraction get the field omitted (dashboard hides panel).
|
|
231
|
-
let entitlements;
|
|
232
|
-
if (config.entitlements && Object.keys(config.entitlements).length > 0) {
|
|
233
|
-
entitlements = {};
|
|
234
|
-
for (const [id, def] of Object.entries(config.entitlements)) {
|
|
235
|
-
entitlements[id] = await evaluateEntitlement(params.userId, def, store, purchaseStore);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
const response = {
|
|
239
|
-
userId: params.userId,
|
|
240
|
-
subscriptions,
|
|
241
|
-
purchases,
|
|
242
|
-
...(entitlements ? { entitlements } : {}),
|
|
243
|
-
};
|
|
244
|
-
res.status(200).json(response);
|
|
245
|
-
}
|
|
246
|
-
catch (err) {
|
|
247
|
-
sendError(res, 500, ONESUB_ERROR_CODE.STORE_ERROR, err.message ?? 'customer error');
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
// POST /onesub/admin/sync-apple/:originalTransactionId
|
|
251
|
-
// Fetch the current subscription state directly from Apple's Status API and
|
|
252
|
-
// upsert into the local store. Useful for reconciliation / missed-webhook recovery.
|
|
253
|
-
const syncAppleParamsSchema = z.object({
|
|
254
|
-
originalTransactionId: z.string().min(1).max(256),
|
|
255
|
-
});
|
|
256
|
-
router.post(ROUTES.ADMIN_SYNC_APPLE, async (req, res) => {
|
|
257
|
-
let params;
|
|
258
|
-
try {
|
|
259
|
-
params = syncAppleParamsSchema.parse(req.params);
|
|
260
|
-
}
|
|
261
|
-
catch {
|
|
262
|
-
sendError(res, 400, ONESUB_ERROR_CODE.INVALID_INPUT, 'originalTransactionId required');
|
|
263
|
-
return;
|
|
264
|
-
}
|
|
265
|
-
if (!config.apple?.issuerId || !config.apple?.keyId || !config.apple?.privateKey) {
|
|
266
|
-
sendError(res, 400, ONESUB_ERROR_CODE.APPLE_CONFIG_MISSING, 'Apple API credentials not configured');
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
try {
|
|
270
|
-
const { originalTransactionId } = params;
|
|
271
|
-
// Allow callers to force sandbox mode; otherwise infer from the stored record.
|
|
272
|
-
const existingForEnv = await store.getByTransactionId(originalTransactionId);
|
|
273
|
-
const isSandbox = req.query['sandbox'] === 'true' ||
|
|
274
|
-
existingForEnv?.environment === 'Sandbox';
|
|
275
|
-
const fresh = await fetchAppleSubscriptionStatus(originalTransactionId, config.apple, { sandbox: isSandbox });
|
|
276
|
-
if (!fresh) {
|
|
277
|
-
sendError(res, 404, ONESUB_ERROR_CODE.TRANSACTION_NOT_FOUND, 'Apple Status API returned no record');
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
const existing = existingForEnv;
|
|
281
|
-
fresh.userId = existing?.userId ?? originalTransactionId;
|
|
282
|
-
await store.save(fresh);
|
|
283
|
-
res.status(200).json({ ok: true, subscription: fresh });
|
|
284
|
-
}
|
|
285
|
-
catch (err) {
|
|
286
|
-
sendError(res, 500, ONESUB_ERROR_CODE.INTERNAL_ERROR, err.message ?? 'sync error');
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
// GET /onesub/admin/webhook-deadletters
|
|
290
|
-
// POST /onesub/admin/webhook-replay/:id
|
|
291
|
-
// Only mounted when the configured queue supports dead-letter inspection
|
|
292
|
-
// (the default in-process queue does not — Apple/Google retries are the
|
|
293
|
-
// durability layer for that mode).
|
|
294
|
-
if (webhookQueue?.listDeadLetters) {
|
|
295
|
-
router.get('/onesub/admin/webhook-deadletters', async (_req, res) => {
|
|
296
|
-
try {
|
|
297
|
-
const items = await webhookQueue.listDeadLetters();
|
|
298
|
-
res.status(200).json({ items });
|
|
299
|
-
}
|
|
300
|
-
catch (err) {
|
|
301
|
-
sendError(res, 500, ONESUB_ERROR_CODE.STORE_ERROR, err.message ?? 'list error');
|
|
302
|
-
}
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
if (webhookQueue?.replayDeadLetter) {
|
|
306
|
-
const replayParamsSchema = z.object({ id: z.string().min(1).max(256) });
|
|
307
|
-
router.post('/onesub/admin/webhook-replay/:id', async (req, res) => {
|
|
308
|
-
let params;
|
|
309
|
-
try {
|
|
310
|
-
params = replayParamsSchema.parse(req.params);
|
|
311
|
-
}
|
|
312
|
-
catch {
|
|
313
|
-
sendError(res, 400, ONESUB_ERROR_CODE.INVALID_INPUT, 'id required');
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
try {
|
|
317
|
-
await webhookQueue.replayDeadLetter(params.id);
|
|
318
|
-
res.status(200).json({ ok: true });
|
|
319
|
-
}
|
|
320
|
-
catch (err) {
|
|
321
|
-
sendError(res, 500, ONESUB_ERROR_CODE.STORE_ERROR, err.message ?? 'replay error');
|
|
322
|
-
}
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
return router;
|
|
326
|
-
}
|
|
327
|
-
//# sourceMappingURL=admin.js.map
|
package/dist/routes/admin.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"admin.js","sourceRoot":"","sources":["../../src/routes/admin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAUxB,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAE/F,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEvD,OAAO,EAAE,4BAA4B,EAAE,MAAM,uBAAuB,CAAC;AAErE,MAAM,mBAAmB,GAAG,gBAAgB,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,iBAAiB,CAC/B,MAA0B,EAC1B,aAA4B,EAC5B,KAAwB,EACxB,YAA2B;IAE3B,IAAI,CAAC,MAAM,CAAC,WAAW;QAAE,OAAO,IAAI,CAAC;IAErC,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC;IACxB,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;IAEvC,wEAAwE;IACxE,sEAAsE;IACtE,4CAA4C;IAC5C,MAAM,SAAS,GAAG,CAAC,GAAY,EAAE,GAAa,EAAE,IAAgB,EAAE,EAAE;QAClE,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,mBAAmB,C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,IAAI,cAAc,CAAC,CAAC;YAC/F,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { Router } from 'express';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { ONESUB_ERROR_CODE } from '@onesub/shared';
|
|
4
|
-
import { signApplePromotionalOffer } from '../providers/apple.js';
|
|
5
|
-
import { sendError, sendZodError } from '../errors.js';
|
|
6
|
-
const OFFER_SECRET_HEADER = 'x-onesub-offer-secret';
|
|
7
|
-
const offerBodySchema = z.object({
|
|
8
|
-
productId: z.string().min(1).max(256),
|
|
9
|
-
offerId: z.string().min(1).max(256),
|
|
10
|
-
applicationUsername: z.string().min(1).max(256),
|
|
11
|
-
});
|
|
12
|
-
/**
|
|
13
|
-
* POST /onesub/apple/offer-signature
|
|
14
|
-
*
|
|
15
|
-
* Sign an Apple Promotional Offer payload server-side.
|
|
16
|
-
* Requires config.apple.offerKeyId and config.apple.offerPrivateKey.
|
|
17
|
-
*
|
|
18
|
-
* Authentication: if config.adminSecret is set, the request must carry
|
|
19
|
-
* `X-Onesub-Offer-Secret: <adminSecret>` (same value). Hosts that mount
|
|
20
|
-
* onesub without adminSecret are responsible for securing this endpoint
|
|
21
|
-
* themselves (e.g. behind their own auth middleware).
|
|
22
|
-
*
|
|
23
|
-
* Body: { productId, offerId, applicationUsername }
|
|
24
|
-
* Response: { keyId, nonce, timestamp, signature }
|
|
25
|
-
*
|
|
26
|
-
* The client passes these four values to StoreKit's
|
|
27
|
-
* Product.SubscriptionOffer.Signature to redeem the offer.
|
|
28
|
-
*/
|
|
29
|
-
export function createAppleOfferRouter(config) {
|
|
30
|
-
const apple = config.apple;
|
|
31
|
-
if (!apple?.offerKeyId || !apple?.offerPrivateKey)
|
|
32
|
-
return null;
|
|
33
|
-
const router = Router();
|
|
34
|
-
router.post('/onesub/apple/offer-signature', async (req, res) => {
|
|
35
|
-
if (config.adminSecret) {
|
|
36
|
-
const provided = req.headers[OFFER_SECRET_HEADER];
|
|
37
|
-
if (typeof provided !== 'string' || provided !== config.adminSecret) {
|
|
38
|
-
sendError(res, 401, ONESUB_ERROR_CODE.UNAUTHORIZED, 'Unauthorized');
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
let body;
|
|
43
|
-
try {
|
|
44
|
-
body = offerBodySchema.parse(req.body);
|
|
45
|
-
}
|
|
46
|
-
catch (err) {
|
|
47
|
-
if (err instanceof z.ZodError) {
|
|
48
|
-
sendZodError(res, err);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
throw err;
|
|
52
|
-
}
|
|
53
|
-
if (!apple.bundleId) {
|
|
54
|
-
sendError(res, 400, ONESUB_ERROR_CODE.APPLE_CONFIG_MISSING, 'config.apple.bundleId is required for offer signing');
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
try {
|
|
58
|
-
const result = await signApplePromotionalOffer({
|
|
59
|
-
bundleId: apple.bundleId,
|
|
60
|
-
productId: body.productId,
|
|
61
|
-
offerId: body.offerId,
|
|
62
|
-
applicationUsername: body.applicationUsername,
|
|
63
|
-
}, apple);
|
|
64
|
-
res.status(200).json(result);
|
|
65
|
-
}
|
|
66
|
-
catch (err) {
|
|
67
|
-
sendError(res, 500, ONESUB_ERROR_CODE.INTERNAL_ERROR, err.message ?? 'Offer signing failed');
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
return router;
|
|
71
|
-
}
|
|
72
|
-
//# sourceMappingURL=apple-offer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"apple-offer.js","sourceRoot":"","sources":["../../src/routes/apple-offer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,iBAAiB,EAAE,MAAM,gB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;QAC1C,IAAI,CAAC;YACH,IAAI,GAAG,eAAe,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACzC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,GAAG,YAAY,CAAC,CAAC,QAAQ,EAAE,CAAC;gBAC9B,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;gBACvB,OAAO;YACT,CAAC;YACD,MAAM,GAAG,CAAC;QACZ,CAAC;QAED,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;YACpB,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,iBAAiB,CAAC,oBAAoB,EAAE,qDAAqD,CAAC,CAAC;YACnH,OAAO;QACT,CAAC;QAED,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,yBAAyB,CAC5C;gBACE,QAAQ,EAAE,KAAK,CAAC,QAAQ;gBACxB,SAAS,EAAE,IAAI,CAAC,SAAS;gBACzB,OAAO,EAAE,IAAI,CAAC,OAAO;gBACrB,mBAAmB,EAAE,IAAI,CAAC,mBAAmB;aAC9C,EACD,KAAK,CACN,CAAC;YACF,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC/B,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,iBAAiB,CAAC,cAAc,EAAG,GAAa,CAAC,OAAO,IAAI,sBAAsB,CAAC,CAAC;QAC1G,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import { Router } from 'express';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { ROUTES, SUBSCRIPTION_STATUS, ONESUB_ERROR_CODE, PURCHASE_TYPE } from '@onesub/shared';
|
|
4
|
-
import { log } from '../logger.js';
|
|
5
|
-
import { sendError } from '../errors.js';
|
|
6
|
-
/**
|
|
7
|
-
* Evaluate a single entitlement for a user.
|
|
8
|
-
*
|
|
9
|
-
* A user is entitled when EITHER condition holds for any productId in
|
|
10
|
-
* `entitlement.productIds`:
|
|
11
|
-
* 1. an active subscription (status === active|grace_period AND
|
|
12
|
-
* expiresAt > now) for that productId exists
|
|
13
|
-
* 2. a non-consumable purchase for that productId exists
|
|
14
|
-
*
|
|
15
|
-
* Consumables are intentionally excluded — they grant a one-time resource,
|
|
16
|
-
* not an ongoing right.
|
|
17
|
-
*
|
|
18
|
-
* Subscription is preferred over purchase when both match (subs carry an
|
|
19
|
-
* expiry; non-consumables are forever) so the source field skews toward the
|
|
20
|
-
* more "interesting" signal for ops/analytics.
|
|
21
|
-
*/
|
|
22
|
-
export async function evaluateEntitlement(userId, entitlement, store, purchaseStore) {
|
|
23
|
-
const productIdSet = new Set(entitlement.productIds);
|
|
24
|
-
const now = Date.now();
|
|
25
|
-
// 1. Check subscriptions first (richer signal — has expiry).
|
|
26
|
-
const subs = await store.getAllByUserId(userId);
|
|
27
|
-
for (const sub of subs) {
|
|
28
|
-
if (!productIdSet.has(sub.productId))
|
|
29
|
-
continue;
|
|
30
|
-
const statusAllows = sub.status === SUBSCRIPTION_STATUS.ACTIVE ||
|
|
31
|
-
sub.status === SUBSCRIPTION_STATUS.GRACE_PERIOD;
|
|
32
|
-
if (!statusAllows)
|
|
33
|
-
continue;
|
|
34
|
-
if (new Date(sub.expiresAt).getTime() <= now)
|
|
35
|
-
continue;
|
|
36
|
-
return {
|
|
37
|
-
active: true,
|
|
38
|
-
source: 'subscription',
|
|
39
|
-
productId: sub.productId,
|
|
40
|
-
expiresAt: sub.expiresAt,
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
// 2. Check non-consumable purchases.
|
|
44
|
-
const purchases = await purchaseStore.getPurchasesByUserId(userId);
|
|
45
|
-
for (const p of purchases) {
|
|
46
|
-
if (p.type !== PURCHASE_TYPE.NON_CONSUMABLE)
|
|
47
|
-
continue;
|
|
48
|
-
if (!productIdSet.has(p.productId))
|
|
49
|
-
continue;
|
|
50
|
-
return {
|
|
51
|
-
active: true,
|
|
52
|
-
source: 'purchase',
|
|
53
|
-
productId: p.productId,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
return { active: false, source: null };
|
|
57
|
-
}
|
|
58
|
-
const userIdSchema = z.string().min(1).max(256);
|
|
59
|
-
const entitlementIdSchema = z.string().min(1).max(128);
|
|
60
|
-
export function createEntitlementRouter(config, store, purchaseStore) {
|
|
61
|
-
// Only mount when entitlements are configured. Without this guard, the
|
|
62
|
-
// routes would always 503 — better to make the absence loud (404 from
|
|
63
|
-
// express) so misconfiguration is obvious during dev.
|
|
64
|
-
if (!config.entitlements || Object.keys(config.entitlements).length === 0) {
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
const entitlements = config.entitlements;
|
|
68
|
-
const router = Router();
|
|
69
|
-
/**
|
|
70
|
-
* GET /onesub/entitlement?userId=&id=premium
|
|
71
|
-
* Single entitlement check.
|
|
72
|
-
*/
|
|
73
|
-
router.get(ROUTES.ENTITLEMENT, async (req, res) => {
|
|
74
|
-
let userId;
|
|
75
|
-
let id;
|
|
76
|
-
try {
|
|
77
|
-
userId = userIdSchema.parse(req.query['userId']);
|
|
78
|
-
id = entitlementIdSchema.parse(req.query['id']);
|
|
79
|
-
}
|
|
80
|
-
catch {
|
|
81
|
-
sendError(res, 400, ONESUB_ERROR_CODE.INVALID_INPUT, 'userId and id are required');
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
const entitlement = entitlements[id];
|
|
85
|
-
if (!entitlement) {
|
|
86
|
-
sendError(res, 404, ONESUB_ERROR_CODE.ENTITLEMENT_NOT_FOUND, `Unknown entitlement: ${id}`);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
try {
|
|
90
|
-
const status = await evaluateEntitlement(userId, entitlement, store, purchaseStore);
|
|
91
|
-
const response = { id, ...status };
|
|
92
|
-
res.status(200).json(response);
|
|
93
|
-
}
|
|
94
|
-
catch (err) {
|
|
95
|
-
log.error('[onesub/entitlement] evaluation error:', err);
|
|
96
|
-
sendError(res, 500, ONESUB_ERROR_CODE.STORE_ERROR, 'Internal server error');
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
/**
|
|
100
|
-
* GET /onesub/entitlements?userId=
|
|
101
|
-
* Evaluate every configured entitlement in one round-trip — useful on
|
|
102
|
-
* app launch / login when the host wants the full entitlement map.
|
|
103
|
-
*/
|
|
104
|
-
router.get(ROUTES.ENTITLEMENTS, async (req, res) => {
|
|
105
|
-
let userId;
|
|
106
|
-
try {
|
|
107
|
-
userId = userIdSchema.parse(req.query['userId']);
|
|
108
|
-
}
|
|
109
|
-
catch {
|
|
110
|
-
sendError(res, 400, ONESUB_ERROR_CODE.INVALID_INPUT, 'userId is required');
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
try {
|
|
114
|
-
const entries = await Promise.all(Object.entries(entitlements).map(async ([id, entitlement]) => {
|
|
115
|
-
const status = await evaluateEntitlement(userId, entitlement, store, purchaseStore);
|
|
116
|
-
return [id, status];
|
|
117
|
-
}));
|
|
118
|
-
const response = {
|
|
119
|
-
entitlements: Object.fromEntries(entries),
|
|
120
|
-
};
|
|
121
|
-
res.status(200).json(response);
|
|
122
|
-
}
|
|
123
|
-
catch (err) {
|
|
124
|
-
log.error('[onesub/entitlements] evaluation error:', err);
|
|
125
|
-
sendError(res, 500, ONESUB_ERROR_CODE.STORE_ERROR, 'Internal server error', { entitlements: {} });
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
return router;
|
|
129
|
-
}
|
|
130
|
-
//# sourceMappingURL=entitlements.js.map
|