@windrun-huaiin/backend-core 10.0.1
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/LICENSE +21 -0
- package/dist/app/api/stripe/checkout/route.d.ts +19 -0
- package/dist/app/api/stripe/checkout/route.d.ts.map +1 -0
- package/dist/app/api/stripe/checkout/route.js +120 -0
- package/dist/app/api/stripe/checkout/route.mjs +118 -0
- package/dist/app/api/stripe/customer-portal/route.d.ts +11 -0
- package/dist/app/api/stripe/customer-portal/route.d.ts.map +1 -0
- package/dist/app/api/stripe/customer-portal/route.js +73 -0
- package/dist/app/api/stripe/customer-portal/route.mjs +71 -0
- package/dist/app/api/user/anonymous/init/route.d.ts +7 -0
- package/dist/app/api/user/anonymous/init/route.d.ts.map +1 -0
- package/dist/app/api/user/anonymous/init/route.js +210 -0
- package/dist/app/api/user/anonymous/init/route.mjs +208 -0
- package/dist/app/api/webhook/clerk/user/route.d.ts +7 -0
- package/dist/app/api/webhook/clerk/user/route.d.ts.map +1 -0
- package/dist/app/api/webhook/clerk/user/route.js +202 -0
- package/dist/app/api/webhook/clerk/user/route.mjs +200 -0
- package/dist/app/api/webhook/stripe/route.d.ts +8 -0
- package/dist/app/api/webhook/stripe/route.d.ts.map +1 -0
- package/dist/app/api/webhook/stripe/route.js +70 -0
- package/dist/app/api/webhook/stripe/route.mjs +67 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +83 -0
- package/dist/index.mjs +18 -0
- package/dist/lib/auth-utils.d.ts +46 -0
- package/dist/lib/auth-utils.d.ts.map +1 -0
- package/dist/lib/auth-utils.js +107 -0
- package/dist/lib/auth-utils.mjs +102 -0
- package/dist/lib/credit-init.d.ts +8 -0
- package/dist/lib/credit-init.d.ts.map +1 -0
- package/dist/lib/credit-init.js +16 -0
- package/dist/lib/credit-init.mjs +10 -0
- package/dist/lib/index.d.ts +5 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/index.js +31 -0
- package/dist/lib/index.mjs +4 -0
- package/dist/lib/money-price-config.d.ts +51 -0
- package/dist/lib/money-price-config.d.ts.map +1 -0
- package/dist/lib/money-price-config.js +156 -0
- package/dist/lib/money-price-config.mjs +151 -0
- package/dist/lib/stripe-config.d.ts +31 -0
- package/dist/lib/stripe-config.d.ts.map +1 -0
- package/dist/lib/stripe-config.js +278 -0
- package/dist/lib/stripe-config.mjs +268 -0
- package/dist/node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.js +48 -0
- package/dist/node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.mjs +45 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/errors.js +54 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/errors.mjs +51 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/iso.js +44 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/iso.mjs +35 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/parse.js +31 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/parse.mjs +18 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/schemas.js +587 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/schemas.mjs +527 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/api.js +447 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/api.mjs +399 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/checks.js +245 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/checks.mjs +232 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/core.js +68 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/core.mjs +62 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/doc.js +39 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/doc.mjs +37 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/errors.js +80 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/errors.mjs +75 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/parse.js +101 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/parse.mjs +86 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/regexes.js +102 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/regexes.mjs +76 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/registries.js +56 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/registries.mjs +52 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/schemas.js +1205 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/schemas.mjs +1157 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/util.js +407 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/util.mjs +374 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/versions.js +9 -0
- package/dist/node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/core/versions.mjs +7 -0
- package/dist/prisma/client.d.ts +2 -0
- package/dist/prisma/client.d.ts.map +1 -0
- package/dist/prisma/client.js +12 -0
- package/dist/prisma/client.mjs +1 -0
- package/dist/prisma/index.d.ts +4 -0
- package/dist/prisma/index.d.ts.map +1 -0
- package/dist/prisma/index.js +10 -0
- package/dist/prisma/index.mjs +2 -0
- package/dist/prisma/prisma-transaction-util.d.ts +3 -0
- package/dist/prisma/prisma-transaction-util.d.ts.map +1 -0
- package/dist/prisma/prisma-transaction-util.js +29 -0
- package/dist/prisma/prisma-transaction-util.mjs +27 -0
- package/dist/prisma/prisma.d.ts +4 -0
- package/dist/prisma/prisma.d.ts.map +1 -0
- package/dist/prisma/prisma.js +109 -0
- package/dist/prisma/prisma.mjs +106 -0
- package/dist/services/aggregate/billing.aggregate.service.d.ts +83 -0
- package/dist/services/aggregate/billing.aggregate.service.d.ts.map +1 -0
- package/dist/services/aggregate/billing.aggregate.service.js +308 -0
- package/dist/services/aggregate/billing.aggregate.service.mjs +306 -0
- package/dist/services/aggregate/index.d.ts +3 -0
- package/dist/services/aggregate/index.d.ts.map +1 -0
- package/dist/services/aggregate/index.js +9 -0
- package/dist/services/aggregate/index.mjs +2 -0
- package/dist/services/aggregate/user.aggregate.service.d.ts +34 -0
- package/dist/services/aggregate/user.aggregate.service.d.ts.map +1 -0
- package/dist/services/aggregate/user.aggregate.service.js +136 -0
- package/dist/services/aggregate/user.aggregate.service.mjs +133 -0
- package/dist/services/context/index.d.ts +2 -0
- package/dist/services/context/index.d.ts.map +1 -0
- package/dist/services/context/index.js +13 -0
- package/dist/services/context/index.mjs +1 -0
- package/dist/services/context/user-context-service.d.ts +30 -0
- package/dist/services/context/user-context-service.d.ts.map +1 -0
- package/dist/services/context/user-context-service.js +170 -0
- package/dist/services/context/user-context-service.mjs +162 -0
- package/dist/services/database/apilog.service.d.ts +39 -0
- package/dist/services/database/apilog.service.d.ts.map +1 -0
- package/dist/services/database/apilog.service.js +174 -0
- package/dist/services/database/apilog.service.mjs +170 -0
- package/dist/services/database/constants.d.ts +73 -0
- package/dist/services/database/constants.d.ts.map +1 -0
- package/dist/services/database/constants.js +135 -0
- package/dist/services/database/constants.mjs +117 -0
- package/dist/services/database/credit.service.d.ts +107 -0
- package/dist/services/database/credit.service.d.ts.map +1 -0
- package/dist/services/database/credit.service.js +515 -0
- package/dist/services/database/credit.service.mjs +512 -0
- package/dist/services/database/creditAuditLog.service.d.ts +73 -0
- package/dist/services/database/creditAuditLog.service.d.ts.map +1 -0
- package/dist/services/database/creditAuditLog.service.js +305 -0
- package/dist/services/database/creditAuditLog.service.mjs +302 -0
- package/dist/services/database/index.d.ts +10 -0
- package/dist/services/database/index.d.ts.map +1 -0
- package/dist/services/database/index.js +38 -0
- package/dist/services/database/index.mjs +8 -0
- package/dist/services/database/prisma-model-type.d.ts +3 -0
- package/dist/services/database/prisma-model-type.d.ts.map +1 -0
- package/dist/services/database/subscription.service.d.ts +48 -0
- package/dist/services/database/subscription.service.d.ts.map +1 -0
- package/dist/services/database/subscription.service.js +267 -0
- package/dist/services/database/subscription.service.mjs +264 -0
- package/dist/services/database/transaction.service.d.ts +92 -0
- package/dist/services/database/transaction.service.d.ts.map +1 -0
- package/dist/services/database/transaction.service.js +326 -0
- package/dist/services/database/transaction.service.mjs +323 -0
- package/dist/services/database/user.service.d.ts +45 -0
- package/dist/services/database/user.service.d.ts.map +1 -0
- package/dist/services/database/user.service.js +180 -0
- package/dist/services/database/user.service.mjs +177 -0
- package/dist/services/database/userBackup.service.d.ts +45 -0
- package/dist/services/database/userBackup.service.d.ts.map +1 -0
- package/dist/services/database/userBackup.service.js +249 -0
- package/dist/services/database/userBackup.service.mjs +246 -0
- package/dist/services/stripe/index.d.ts +2 -0
- package/dist/services/stripe/index.d.ts.map +1 -0
- package/dist/services/stripe/index.js +7 -0
- package/dist/services/stripe/index.mjs +1 -0
- package/dist/services/stripe/webhook-handler.d.ts +6 -0
- package/dist/services/stripe/webhook-handler.d.ts.map +1 -0
- package/dist/services/stripe/webhook-handler.js +537 -0
- package/dist/services/stripe/webhook-handler.mjs +535 -0
- package/migrations/create.sql +176 -0
- package/migrations/db.init.sql +13 -0
- package/migrations/init-schema.sql +19 -0
- package/migrations/purge.sql +27 -0
- package/migrations/test-check.sql +167 -0
- package/package.json +123 -0
- package/prisma/schema.prisma +191 -0
- package/src/app/api/stripe/checkout/route.ts +145 -0
- package/src/app/api/stripe/customer-portal/route.ts +83 -0
- package/src/app/api/user/anonymous/init/route.ts +284 -0
- package/src/app/api/webhook/clerk/user/route.ts +249 -0
- package/src/app/api/webhook/stripe/route.ts +93 -0
- package/src/index.ts +6 -0
- package/src/lib/auth-utils.ts +101 -0
- package/src/lib/credit-init.ts +9 -0
- package/src/lib/index.ts +4 -0
- package/src/lib/money-price-config.ts +168 -0
- package/src/lib/stripe-config.ts +333 -0
- package/src/prisma/client.ts +2 -0
- package/src/prisma/index.ts +3 -0
- package/src/prisma/prisma-transaction-util.ts +24 -0
- package/src/prisma/prisma.ts +122 -0
- package/src/services/aggregate/billing.aggregate.service.ts +498 -0
- package/src/services/aggregate/index.ts +2 -0
- package/src/services/aggregate/user.aggregate.service.ts +168 -0
- package/src/services/context/index.ts +1 -0
- package/src/services/context/user-context-service.ts +200 -0
- package/src/services/database/apilog.service.ts +185 -0
- package/src/services/database/constants.ts +148 -0
- package/src/services/database/credit.service.ts +747 -0
- package/src/services/database/creditAuditLog.service.ts +402 -0
- package/src/services/database/index.ts +41 -0
- package/src/services/database/prisma-model-type.ts +13 -0
- package/src/services/database/subscription.service.ts +319 -0
- package/src/services/database/transaction.service.ts +447 -0
- package/src/services/database/user.service.ts +218 -0
- package/src/services/database/userBackup.service.ts +290 -0
- package/src/services/stripe/index.ts +1 -0
- package/src/services/stripe/webhook-handler.ts +648 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Credit, Subscription, User } from '../database/prisma-model-type';
|
|
2
|
+
import type { XCredit, XSubscription, XUser } from '@windrun-huaiin/third-ui/fingerprint';
|
|
3
|
+
import type { InitUserContext } from '@windrun-huaiin/third-ui/main/server';
|
|
4
|
+
export interface UserContextEntities {
|
|
5
|
+
user: User;
|
|
6
|
+
credit: Credit | null;
|
|
7
|
+
subscription: Subscription | null;
|
|
8
|
+
}
|
|
9
|
+
export interface FingerprintUserContext extends UserContextEntities {
|
|
10
|
+
totalUsersOnDevice: number;
|
|
11
|
+
hasAnonymousUser: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare function mapUserToXUser(user: User): XUser;
|
|
14
|
+
export declare function mapCreditToXCredit(credit: Credit): XCredit;
|
|
15
|
+
export declare function mapSubscriptionToXSubscription(subscription: Subscription | null): XSubscription | null;
|
|
16
|
+
export declare function buildInitUserContextFromEntities(params: {
|
|
17
|
+
user: User;
|
|
18
|
+
credit: Credit | null;
|
|
19
|
+
subscription: Subscription | null;
|
|
20
|
+
isClerkAuthenticated?: boolean;
|
|
21
|
+
}): InitUserContext;
|
|
22
|
+
export declare function fetchUserContextByClerkUserId(clerkUserId: string): Promise<UserContextEntities | null>;
|
|
23
|
+
export declare function fetchLatestUserContextByFingerprintId(fingerprintId: string): Promise<FingerprintUserContext | null>;
|
|
24
|
+
type MockableContext = {
|
|
25
|
+
xUser: XUser | null;
|
|
26
|
+
xSubscription: XSubscription | null;
|
|
27
|
+
};
|
|
28
|
+
export declare function applyUserMockContext<T extends MockableContext>(context: T): T;
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=user-context-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-context-service.d.ts","sourceRoot":"","sources":["../../../src/services/context/user-context-service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,+BAA+B,CAAC;AAEhF,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,sCAAsC,CAAC;AAC1F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sCAAsC,CAAC;AAE5E,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,IAAI,CAAC;IACX,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,YAAY,EAAE,YAAY,GAAG,IAAI,CAAC;CACnC;AAED,MAAM,WAAW,sBAAuB,SAAQ,mBAAmB;IACjE,kBAAkB,EAAE,MAAM,CAAC;IAC3B,gBAAgB,EAAE,OAAO,CAAC;CAC3B;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,KAAK,CAWhD;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAgB1D;AAED,wBAAgB,8BAA8B,CAC5C,YAAY,EAAE,YAAY,GAAG,IAAI,GAChC,aAAa,GAAG,IAAI,CAiBtB;AAED,wBAAgB,gCAAgC,CAAC,MAAM,EAAE;IACvD,IAAI,EAAE,IAAI,CAAC;IACX,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,YAAY,EAAE,YAAY,GAAG,IAAI,CAAC;IAClC,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC,GAAG,eAAe,CAQlB;AAED,wBAAsB,6BAA6B,CACjD,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAarC;AAED,wBAAsB,qCAAqC,CACzD,aAAa,EAAE,MAAM,GACpB,OAAO,CAAC,sBAAsB,GAAG,IAAI,CAAC,CAmBxC;AAED,KAAK,eAAe,GAAG;IACrB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,aAAa,EAAE,aAAa,GAAG,IAAI,CAAC;CACrC,CAAC;AAEF,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,eAAe,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,CAsE7E"}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var tslib_es6 = require('../../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.js');
|
|
4
|
+
var user_service = require('../database/user.service.js');
|
|
5
|
+
var subscription_service = require('../database/subscription.service.js');
|
|
6
|
+
var credit_service = require('../database/credit.service.js');
|
|
7
|
+
require('@prisma/client');
|
|
8
|
+
require('../../prisma/prisma.js');
|
|
9
|
+
var utils = require('@windrun-huaiin/lib/utils');
|
|
10
|
+
|
|
11
|
+
function mapUserToXUser(user) {
|
|
12
|
+
return {
|
|
13
|
+
userId: user.userId,
|
|
14
|
+
userName: user.userName || '',
|
|
15
|
+
fingerprintId: user.fingerprintId || '',
|
|
16
|
+
clerkUserId: user.clerkUserId || '',
|
|
17
|
+
stripeCusId: user.stripeCusId || '',
|
|
18
|
+
email: user.email || '',
|
|
19
|
+
status: user.status,
|
|
20
|
+
createdAt: utils.viewLocalTime(user.createdAt),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function mapCreditToXCredit(credit) {
|
|
24
|
+
return {
|
|
25
|
+
balanceFree: credit.balanceFree,
|
|
26
|
+
totalFreeLimit: credit.totalFreeLimit,
|
|
27
|
+
freeStart: utils.viewLocalTime(credit.freeStart),
|
|
28
|
+
freeEnd: utils.viewLocalTime(credit.freeEnd),
|
|
29
|
+
balancePaid: credit.balancePaid,
|
|
30
|
+
totalPaidLimit: credit.totalPaidLimit,
|
|
31
|
+
paidStart: utils.viewLocalTime(credit.paidStart),
|
|
32
|
+
paidEnd: utils.viewLocalTime(credit.paidEnd),
|
|
33
|
+
balanceOneTimePaid: credit.balanceOneTimePaid,
|
|
34
|
+
totalOneTimePaidLimit: credit.totalOneTimePaidLimit,
|
|
35
|
+
oneTimePaidStart: utils.viewLocalTime(credit.oneTimePaidStart),
|
|
36
|
+
oneTimePaidEnd: utils.viewLocalTime(credit.oneTimePaidEnd),
|
|
37
|
+
totalBalance: credit.balanceFree + credit.balancePaid + credit.balanceOneTimePaid,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function mapSubscriptionToXSubscription(subscription) {
|
|
41
|
+
if (!subscription) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
id: subscription.id,
|
|
46
|
+
userId: subscription.userId || '',
|
|
47
|
+
paySubscriptionId: subscription.paySubscriptionId || '',
|
|
48
|
+
orderId: subscription.orderId || '',
|
|
49
|
+
priceId: subscription.priceId || '',
|
|
50
|
+
priceName: subscription.priceName || '',
|
|
51
|
+
status: subscription.status || '',
|
|
52
|
+
creditsAllocated: subscription.creditsAllocated,
|
|
53
|
+
subPeriodStart: utils.viewLocalTime(subscription.subPeriodStart),
|
|
54
|
+
subPeriodEnd: utils.viewLocalTime(subscription.subPeriodEnd),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function buildInitUserContextFromEntities(params) {
|
|
58
|
+
var _a;
|
|
59
|
+
return {
|
|
60
|
+
fingerprintId: params.user.fingerprintId || null,
|
|
61
|
+
xUser: mapUserToXUser(params.user),
|
|
62
|
+
xCredit: params.credit ? mapCreditToXCredit(params.credit) : null,
|
|
63
|
+
xSubscription: mapSubscriptionToXSubscription(params.subscription),
|
|
64
|
+
isClerkAuthenticated: (_a = params.isClerkAuthenticated) !== null && _a !== void 0 ? _a : true,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function fetchUserContextByClerkUserId(clerkUserId) {
|
|
68
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
const user = yield user_service.userService.findByClerkUserId(clerkUserId);
|
|
70
|
+
if (!user) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const [credit, subscription] = yield Promise.all([
|
|
74
|
+
credit_service.creditService.getCredit(user.userId),
|
|
75
|
+
subscription_service.subscriptionService.getActiveSubscription(user.userId),
|
|
76
|
+
]);
|
|
77
|
+
return { user, credit, subscription };
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
function fetchLatestUserContextByFingerprintId(fingerprintId) {
|
|
81
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
const existingUsers = yield user_service.userService.findListByFingerprintId(fingerprintId);
|
|
83
|
+
if (existingUsers.length === 0) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
const latestAnonymousUser = existingUsers[0];
|
|
87
|
+
const [credit, subscription] = yield Promise.all([
|
|
88
|
+
credit_service.creditService.getCredit(latestAnonymousUser.userId),
|
|
89
|
+
subscription_service.subscriptionService.getActiveSubscription(latestAnonymousUser.userId),
|
|
90
|
+
]);
|
|
91
|
+
return {
|
|
92
|
+
user: latestAnonymousUser,
|
|
93
|
+
credit,
|
|
94
|
+
subscription,
|
|
95
|
+
totalUsersOnDevice: existingUsers.length,
|
|
96
|
+
hasAnonymousUser: true,
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
function applyUserMockContext(context) {
|
|
101
|
+
var _a;
|
|
102
|
+
const mockEnabled = process.env.MONEY_PRICE_MOCK_USER_ENABLED === 'true';
|
|
103
|
+
const mockType = Number((_a = process.env.MONEY_PRICE_MOCK_USER_TYPE) !== null && _a !== void 0 ? _a : NaN);
|
|
104
|
+
if (!context.xUser ||
|
|
105
|
+
!mockEnabled ||
|
|
106
|
+
!Number.isInteger(mockType) ||
|
|
107
|
+
mockType < 0 ||
|
|
108
|
+
mockType > 4) {
|
|
109
|
+
return context;
|
|
110
|
+
}
|
|
111
|
+
const ensureSubscription = () => {
|
|
112
|
+
if (!context.xSubscription) {
|
|
113
|
+
const now = new Date();
|
|
114
|
+
context.xSubscription = {
|
|
115
|
+
id: BigInt(99999),
|
|
116
|
+
userId: context.xUser.userId,
|
|
117
|
+
paySubscriptionId: 'MOCK-PAY-SUB-ID',
|
|
118
|
+
orderId: '',
|
|
119
|
+
priceId: '',
|
|
120
|
+
priceName: 'MOCK-TEST',
|
|
121
|
+
status: 'active',
|
|
122
|
+
creditsAllocated: 0,
|
|
123
|
+
subPeriodStart: utils.viewLocalTime(now),
|
|
124
|
+
subPeriodEnd: utils.viewLocalTime(now),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return context.xSubscription;
|
|
128
|
+
};
|
|
129
|
+
switch (mockType) {
|
|
130
|
+
case 0: {
|
|
131
|
+
const subscription = ensureSubscription();
|
|
132
|
+
subscription.status = '';
|
|
133
|
+
subscription.priceId = '';
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
case 1: {
|
|
137
|
+
const subscription = ensureSubscription();
|
|
138
|
+
subscription.priceId =
|
|
139
|
+
process.env.STRIPE_PRO_MONTHLY_PRICE_ID || subscription.priceId;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
case 2: {
|
|
143
|
+
const subscription = ensureSubscription();
|
|
144
|
+
subscription.priceId =
|
|
145
|
+
process.env.STRIPE_ULTRA_MONTHLY_PRICE_ID || subscription.priceId;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
case 3: {
|
|
149
|
+
const subscription = ensureSubscription();
|
|
150
|
+
subscription.priceId =
|
|
151
|
+
process.env.STRIPE_PRO_YEARLY_PRICE_ID || subscription.priceId;
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case 4: {
|
|
155
|
+
const subscription = ensureSubscription();
|
|
156
|
+
subscription.priceId =
|
|
157
|
+
process.env.STRIPE_ULTRA_YEARLY_PRICE_ID || subscription.priceId;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return context;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
exports.applyUserMockContext = applyUserMockContext;
|
|
165
|
+
exports.buildInitUserContextFromEntities = buildInitUserContextFromEntities;
|
|
166
|
+
exports.fetchLatestUserContextByFingerprintId = fetchLatestUserContextByFingerprintId;
|
|
167
|
+
exports.fetchUserContextByClerkUserId = fetchUserContextByClerkUserId;
|
|
168
|
+
exports.mapCreditToXCredit = mapCreditToXCredit;
|
|
169
|
+
exports.mapSubscriptionToXSubscription = mapSubscriptionToXSubscription;
|
|
170
|
+
exports.mapUserToXUser = mapUserToXUser;
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { __awaiter } from '../../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.mjs';
|
|
2
|
+
import { userService } from '../database/user.service.mjs';
|
|
3
|
+
import { subscriptionService } from '../database/subscription.service.mjs';
|
|
4
|
+
import { creditService } from '../database/credit.service.mjs';
|
|
5
|
+
import '@prisma/client';
|
|
6
|
+
import '../../prisma/prisma.mjs';
|
|
7
|
+
import { viewLocalTime } from '@windrun-huaiin/lib/utils';
|
|
8
|
+
|
|
9
|
+
function mapUserToXUser(user) {
|
|
10
|
+
return {
|
|
11
|
+
userId: user.userId,
|
|
12
|
+
userName: user.userName || '',
|
|
13
|
+
fingerprintId: user.fingerprintId || '',
|
|
14
|
+
clerkUserId: user.clerkUserId || '',
|
|
15
|
+
stripeCusId: user.stripeCusId || '',
|
|
16
|
+
email: user.email || '',
|
|
17
|
+
status: user.status,
|
|
18
|
+
createdAt: viewLocalTime(user.createdAt),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function mapCreditToXCredit(credit) {
|
|
22
|
+
return {
|
|
23
|
+
balanceFree: credit.balanceFree,
|
|
24
|
+
totalFreeLimit: credit.totalFreeLimit,
|
|
25
|
+
freeStart: viewLocalTime(credit.freeStart),
|
|
26
|
+
freeEnd: viewLocalTime(credit.freeEnd),
|
|
27
|
+
balancePaid: credit.balancePaid,
|
|
28
|
+
totalPaidLimit: credit.totalPaidLimit,
|
|
29
|
+
paidStart: viewLocalTime(credit.paidStart),
|
|
30
|
+
paidEnd: viewLocalTime(credit.paidEnd),
|
|
31
|
+
balanceOneTimePaid: credit.balanceOneTimePaid,
|
|
32
|
+
totalOneTimePaidLimit: credit.totalOneTimePaidLimit,
|
|
33
|
+
oneTimePaidStart: viewLocalTime(credit.oneTimePaidStart),
|
|
34
|
+
oneTimePaidEnd: viewLocalTime(credit.oneTimePaidEnd),
|
|
35
|
+
totalBalance: credit.balanceFree + credit.balancePaid + credit.balanceOneTimePaid,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function mapSubscriptionToXSubscription(subscription) {
|
|
39
|
+
if (!subscription) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
id: subscription.id,
|
|
44
|
+
userId: subscription.userId || '',
|
|
45
|
+
paySubscriptionId: subscription.paySubscriptionId || '',
|
|
46
|
+
orderId: subscription.orderId || '',
|
|
47
|
+
priceId: subscription.priceId || '',
|
|
48
|
+
priceName: subscription.priceName || '',
|
|
49
|
+
status: subscription.status || '',
|
|
50
|
+
creditsAllocated: subscription.creditsAllocated,
|
|
51
|
+
subPeriodStart: viewLocalTime(subscription.subPeriodStart),
|
|
52
|
+
subPeriodEnd: viewLocalTime(subscription.subPeriodEnd),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function buildInitUserContextFromEntities(params) {
|
|
56
|
+
var _a;
|
|
57
|
+
return {
|
|
58
|
+
fingerprintId: params.user.fingerprintId || null,
|
|
59
|
+
xUser: mapUserToXUser(params.user),
|
|
60
|
+
xCredit: params.credit ? mapCreditToXCredit(params.credit) : null,
|
|
61
|
+
xSubscription: mapSubscriptionToXSubscription(params.subscription),
|
|
62
|
+
isClerkAuthenticated: (_a = params.isClerkAuthenticated) !== null && _a !== void 0 ? _a : true,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function fetchUserContextByClerkUserId(clerkUserId) {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
const user = yield userService.findByClerkUserId(clerkUserId);
|
|
68
|
+
if (!user) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const [credit, subscription] = yield Promise.all([
|
|
72
|
+
creditService.getCredit(user.userId),
|
|
73
|
+
subscriptionService.getActiveSubscription(user.userId),
|
|
74
|
+
]);
|
|
75
|
+
return { user, credit, subscription };
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
function fetchLatestUserContextByFingerprintId(fingerprintId) {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
const existingUsers = yield userService.findListByFingerprintId(fingerprintId);
|
|
81
|
+
if (existingUsers.length === 0) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
const latestAnonymousUser = existingUsers[0];
|
|
85
|
+
const [credit, subscription] = yield Promise.all([
|
|
86
|
+
creditService.getCredit(latestAnonymousUser.userId),
|
|
87
|
+
subscriptionService.getActiveSubscription(latestAnonymousUser.userId),
|
|
88
|
+
]);
|
|
89
|
+
return {
|
|
90
|
+
user: latestAnonymousUser,
|
|
91
|
+
credit,
|
|
92
|
+
subscription,
|
|
93
|
+
totalUsersOnDevice: existingUsers.length,
|
|
94
|
+
hasAnonymousUser: true,
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
function applyUserMockContext(context) {
|
|
99
|
+
var _a;
|
|
100
|
+
const mockEnabled = process.env.MONEY_PRICE_MOCK_USER_ENABLED === 'true';
|
|
101
|
+
const mockType = Number((_a = process.env.MONEY_PRICE_MOCK_USER_TYPE) !== null && _a !== void 0 ? _a : NaN);
|
|
102
|
+
if (!context.xUser ||
|
|
103
|
+
!mockEnabled ||
|
|
104
|
+
!Number.isInteger(mockType) ||
|
|
105
|
+
mockType < 0 ||
|
|
106
|
+
mockType > 4) {
|
|
107
|
+
return context;
|
|
108
|
+
}
|
|
109
|
+
const ensureSubscription = () => {
|
|
110
|
+
if (!context.xSubscription) {
|
|
111
|
+
const now = new Date();
|
|
112
|
+
context.xSubscription = {
|
|
113
|
+
id: BigInt(99999),
|
|
114
|
+
userId: context.xUser.userId,
|
|
115
|
+
paySubscriptionId: 'MOCK-PAY-SUB-ID',
|
|
116
|
+
orderId: '',
|
|
117
|
+
priceId: '',
|
|
118
|
+
priceName: 'MOCK-TEST',
|
|
119
|
+
status: 'active',
|
|
120
|
+
creditsAllocated: 0,
|
|
121
|
+
subPeriodStart: viewLocalTime(now),
|
|
122
|
+
subPeriodEnd: viewLocalTime(now),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return context.xSubscription;
|
|
126
|
+
};
|
|
127
|
+
switch (mockType) {
|
|
128
|
+
case 0: {
|
|
129
|
+
const subscription = ensureSubscription();
|
|
130
|
+
subscription.status = '';
|
|
131
|
+
subscription.priceId = '';
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case 1: {
|
|
135
|
+
const subscription = ensureSubscription();
|
|
136
|
+
subscription.priceId =
|
|
137
|
+
process.env.STRIPE_PRO_MONTHLY_PRICE_ID || subscription.priceId;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case 2: {
|
|
141
|
+
const subscription = ensureSubscription();
|
|
142
|
+
subscription.priceId =
|
|
143
|
+
process.env.STRIPE_ULTRA_MONTHLY_PRICE_ID || subscription.priceId;
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
case 3: {
|
|
147
|
+
const subscription = ensureSubscription();
|
|
148
|
+
subscription.priceId =
|
|
149
|
+
process.env.STRIPE_PRO_YEARLY_PRICE_ID || subscription.priceId;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
case 4: {
|
|
153
|
+
const subscription = ensureSubscription();
|
|
154
|
+
subscription.priceId =
|
|
155
|
+
process.env.STRIPE_ULTRA_YEARLY_PRICE_ID || subscription.priceId;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return context;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export { applyUserMockContext, buildInitUserContextFromEntities, fetchLatestUserContextByFingerprintId, fetchUserContextByClerkUserId, mapCreditToXCredit, mapSubscriptionToXSubscription, mapUserToXUser };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Prisma } from './prisma-model-type';
|
|
2
|
+
import type { Apilog } from './prisma-model-type';
|
|
3
|
+
export type ApiType = 'from_clerk_in' | 'to_clerk_out' | 'from_stripe_in' | 'to_stripe_out';
|
|
4
|
+
export interface CreateApiLogData {
|
|
5
|
+
methodName: string;
|
|
6
|
+
request?: any;
|
|
7
|
+
summary?: any;
|
|
8
|
+
apiType: ApiType;
|
|
9
|
+
}
|
|
10
|
+
export declare class ApilogService {
|
|
11
|
+
createApilog(data: CreateApiLogData, tx?: Prisma.TransactionClient): Promise<string>;
|
|
12
|
+
updateApilogResponse(logId: string, response: any, tx?: Prisma.TransactionClient): Promise<void>;
|
|
13
|
+
getApilog(logId: string, tx?: Prisma.TransactionClient): Promise<Apilog | null>;
|
|
14
|
+
getApilogList(params: {
|
|
15
|
+
apiType?: ApiType;
|
|
16
|
+
methodName?: string;
|
|
17
|
+
limit?: number;
|
|
18
|
+
offset?: number;
|
|
19
|
+
}, tx?: Prisma.TransactionClient): Promise<Apilog[]>;
|
|
20
|
+
deleteOldLogList(daysOld?: number, tx?: Prisma.TransactionClient): Promise<number>;
|
|
21
|
+
getApilogStats(tx?: Prisma.TransactionClient): Promise<{
|
|
22
|
+
totalLogs: number;
|
|
23
|
+
clerkIncoming: number;
|
|
24
|
+
clerkOutgoing: number;
|
|
25
|
+
stripeIncoming: number;
|
|
26
|
+
stripeOutgoing: number;
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
29
|
+
export declare class Apilogger {
|
|
30
|
+
static createLogAsync(data: CreateApiLogData): Promise<string | null>;
|
|
31
|
+
static updateResponseAsync(logId: string, response: any): Promise<void>;
|
|
32
|
+
static logClerkIncoming(methodName: string, summary?: any, originalRequest?: any): Promise<string | null>;
|
|
33
|
+
static logClerkOutgoing(methodName: string, request?: any, summary?: any): Promise<string | null>;
|
|
34
|
+
static logStripeIncoming(methodName: string, summary?: any, originalRequest?: any): Promise<string | null>;
|
|
35
|
+
static logStripeOutgoing(methodName: string, request?: any, summary?: any): Promise<string | null>;
|
|
36
|
+
static updateResponse(logId: string | null, response: any): void;
|
|
37
|
+
}
|
|
38
|
+
export declare const apilogService: ApilogService;
|
|
39
|
+
//# sourceMappingURL=apilog.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apilog.service.d.ts","sourceRoot":"","sources":["../../../src/services/database/apilog.service.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAGlD,MAAM,MAAM,OAAO,GAAG,eAAe,GAAG,cAAc,GAAG,gBAAgB,GAAG,eAAe,CAAC;AAE5F,MAAM,WAAW,gBAAgB;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,qBAAa,aAAa;IAGlB,YAAY,CAAC,IAAI,EAAE,gBAAgB,EAAE,EAAE,CAAC,EAAE,MAAM,CAAC,iBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC;IAcpF,oBAAoB,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,EAAE,EAAE,CAAC,EAAE,MAAM,CAAC,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAWhG,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,CAAC,iBAAiB,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAQ/E,aAAa,CAAC,MAAM,EAAE;QAC1B,OAAO,CAAC,EAAE,OAAO,CAAC;QAClB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,EAAE,EAAE,CAAC,EAAE,MAAM,CAAC,iBAAiB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAiB9C,gBAAgB,CAAC,OAAO,GAAE,MAAW,EAAE,EAAE,CAAC,EAAE,MAAM,CAAC,iBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC;IAiBtF,cAAc,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,iBAAiB,GAAG,OAAO,CAAC;QAC3D,SAAS,EAAE,MAAM,CAAC;QAClB,aAAa,EAAE,MAAM,CAAC;QACtB,aAAa,EAAE,MAAM,CAAC;QACtB,cAAc,EAAE,MAAM,CAAC;QACvB,cAAc,EAAE,MAAM,CAAC;KACxB,CAAC;CAkBH;AAGD,qBAAa,SAAS;WACP,cAAc,CAAC,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;WAS9D,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;WAchE,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,EAAE,eAAe,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;WASlG,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;WAS1F,iBAAiB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,EAAE,eAAe,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;WASnG,iBAAiB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IASxG,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,EAAE,QAAQ,EAAE,GAAG,GAAG,IAAI;CAKjE;AAED,eAAO,MAAM,aAAa,eAAsB,CAAC"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var tslib_es6 = require('../../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.js');
|
|
4
|
+
var prisma = require('../../prisma/prisma.js');
|
|
5
|
+
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
7
|
+
class ApilogService {
|
|
8
|
+
// Create API log record with request
|
|
9
|
+
createApilog(data, tx) {
|
|
10
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
11
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
12
|
+
const log = yield client.apilog.create({
|
|
13
|
+
data: {
|
|
14
|
+
methodName: data.methodName,
|
|
15
|
+
request: data.request ? JSON.stringify(data.request) : null,
|
|
16
|
+
summary: data.summary ? JSON.stringify(data.summary) : null,
|
|
17
|
+
apiType: data.apiType,
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
return log.id.toString();
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
// Update API log record with response
|
|
24
|
+
updateApilogResponse(logId, response, tx) {
|
|
25
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
27
|
+
yield client.apilog.update({
|
|
28
|
+
where: { id: BigInt(logId) },
|
|
29
|
+
data: {
|
|
30
|
+
response: response ? JSON.stringify(response) : null,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
// Get API log by ID
|
|
36
|
+
getApilog(logId, tx) {
|
|
37
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
39
|
+
return yield client.apilog.findUnique({
|
|
40
|
+
where: { id: BigInt(logId) },
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
// Get API logs with filters
|
|
45
|
+
getApilogList(params, tx) {
|
|
46
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const { apiType, methodName, limit = 50, offset = 0 } = params;
|
|
48
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
49
|
+
return yield client.apilog.findMany({
|
|
50
|
+
where: Object.assign(Object.assign({}, (apiType && { apiType })), (methodName && { methodName: { contains: methodName } })),
|
|
51
|
+
orderBy: { createdAt: 'desc' },
|
|
52
|
+
take: limit,
|
|
53
|
+
skip: offset,
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
// Delete old API logs (cleanup)
|
|
58
|
+
deleteOldLogList() {
|
|
59
|
+
return tslib_es6.__awaiter(this, arguments, void 0, function* (daysOld = 30, tx) {
|
|
60
|
+
const cutoffDate = new Date();
|
|
61
|
+
cutoffDate.setDate(cutoffDate.getDate() - daysOld);
|
|
62
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
63
|
+
const result = yield client.apilog.deleteMany({
|
|
64
|
+
where: {
|
|
65
|
+
createdAt: {
|
|
66
|
+
lt: cutoffDate,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
return result.count;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
// Get API log statistics
|
|
74
|
+
getApilogStats(tx) {
|
|
75
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
77
|
+
const [total, clerkIn, clerkOut, stripeIn, stripeOut] = yield Promise.all([
|
|
78
|
+
client.apilog.count(),
|
|
79
|
+
client.apilog.count({ where: { apiType: 'from_clerk_in' } }),
|
|
80
|
+
client.apilog.count({ where: { apiType: 'to_clerk_out' } }),
|
|
81
|
+
client.apilog.count({ where: { apiType: 'from_stripe_in' } }),
|
|
82
|
+
client.apilog.count({ where: { apiType: 'to_stripe_out' } }),
|
|
83
|
+
]);
|
|
84
|
+
return {
|
|
85
|
+
totalLogs: total,
|
|
86
|
+
clerkIncoming: clerkIn,
|
|
87
|
+
clerkOutgoing: clerkOut,
|
|
88
|
+
stripeIncoming: stripeIn,
|
|
89
|
+
stripeOutgoing: stripeOut,
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// API Logger Helper Class for convenience
|
|
95
|
+
class Apilogger {
|
|
96
|
+
static createLogAsync(data) {
|
|
97
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
try {
|
|
99
|
+
return yield apilogService.createApilog(data);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
console.error('Failed to create API log:', error);
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
static updateResponseAsync(logId, response) {
|
|
108
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
try {
|
|
110
|
+
setImmediate(() => tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
try {
|
|
112
|
+
yield apilogService.updateApilogResponse(logId, response);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
console.error('Failed to update API log response:', error);
|
|
116
|
+
}
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
console.error('Failed to queue API log update:', error);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
static logClerkIncoming(methodName, summary, originalRequest) {
|
|
125
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
return yield this.createLogAsync({
|
|
127
|
+
methodName,
|
|
128
|
+
request: originalRequest,
|
|
129
|
+
summary,
|
|
130
|
+
apiType: 'from_clerk_in',
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
static logClerkOutgoing(methodName, request, summary) {
|
|
135
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
return yield this.createLogAsync({
|
|
137
|
+
methodName,
|
|
138
|
+
request,
|
|
139
|
+
summary,
|
|
140
|
+
apiType: 'to_clerk_out',
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
static logStripeIncoming(methodName, summary, originalRequest) {
|
|
145
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
146
|
+
return yield this.createLogAsync({
|
|
147
|
+
methodName,
|
|
148
|
+
request: originalRequest,
|
|
149
|
+
summary,
|
|
150
|
+
apiType: 'from_stripe_in',
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
static logStripeOutgoing(methodName, request, summary) {
|
|
155
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
156
|
+
return yield this.createLogAsync({
|
|
157
|
+
methodName,
|
|
158
|
+
request,
|
|
159
|
+
summary,
|
|
160
|
+
apiType: 'to_stripe_out',
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
static updateResponse(logId, response) {
|
|
165
|
+
if (logId) {
|
|
166
|
+
this.updateResponseAsync(logId, response);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const apilogService = new ApilogService();
|
|
171
|
+
|
|
172
|
+
exports.ApilogService = ApilogService;
|
|
173
|
+
exports.Apilogger = Apilogger;
|
|
174
|
+
exports.apilogService = apilogService;
|