@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 @@
|
|
|
1
|
+
{"version":3,"file":"transaction.service.d.ts","sourceRoot":"","sources":["../../../src/services/database/transaction.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAgC,WAAW,EAAiB,MAAM,aAAa,CAAC;AAGvF,qBAAa,kBAAkB;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,CAAC,EAAE,IAAI,EAChB,OAAO,CAAC,EAAE,IAAI,EACd,EAAE,CAAC,EAAE,MAAM,CAAC,iBAAiB,GAC5B,OAAO,CAAC;QACT,YAAY,EAAE,MAAM,CAAC;QACrB,iBAAiB,EAAE,MAAM,CAAC;QAC1B,iBAAiB,EAAE,MAAM,CAAC;QAC1B,mBAAmB,EAAE,MAAM,CAAC;QAC5B,cAAc,EAAE,MAAM,CAAC;QACvB,cAAc,EAAE,MAAM,CAAC;KACxB,CAAC;IAuEI,eAAe,CAAC,IAAI,GAAE,MAAW,EAAE,EAAE,CAAC,EAAE,MAAM,CAAC,iBAAiB,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;IA2BjF,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,CAAC,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAUhF,uBAAuB,CAC3B,YAAY,EAAE,MAAM,CAAC,0BAA0B,EAAE,EACjD,EAAE,CAAC,EAAE,MAAM,CAAC,iBAAiB,GAC5B,OAAO,CAAC,MAAM,CAAC;CASnB;AAED,eAAO,MAAM,kBAAkB,oBAA2B,CAAC"}
|
|
@@ -0,0 +1,326 @@
|
|
|
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 client = require('@prisma/client');
|
|
5
|
+
var constants = require('./constants.js');
|
|
6
|
+
var prisma = require('../../prisma/prisma.js');
|
|
7
|
+
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
|
+
class TransactionService {
|
|
10
|
+
// Create transaction order
|
|
11
|
+
createTransaction(data, tx) {
|
|
12
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
13
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
14
|
+
return yield client.transaction.create({
|
|
15
|
+
data: {
|
|
16
|
+
userId: data.userId,
|
|
17
|
+
orderId: data.orderId,
|
|
18
|
+
orderStatus: data.orderStatus || constants.OrderStatus.CREATED,
|
|
19
|
+
paymentStatus: data.paymentStatus || constants.PaymentStatus.UN_PAID,
|
|
20
|
+
orderExpiredAt: data.orderExpiredAt || new Date(Date.now() + 30 * 60 * 1000), // 默认30分钟过期
|
|
21
|
+
paySupplier: data.paySupplier,
|
|
22
|
+
payTransactionId: data.payTransactionId,
|
|
23
|
+
paySubscriptionId: data.paySubscriptionId,
|
|
24
|
+
paySessionId: data.paySessionId,
|
|
25
|
+
payInvoiceId: data.payInvoiceId,
|
|
26
|
+
hostedInvoiceUrl: data.hostedInvoiceUrl,
|
|
27
|
+
invoicePdf: data.invoicePdf,
|
|
28
|
+
billingReason: data.billingReason,
|
|
29
|
+
priceId: data.priceId,
|
|
30
|
+
priceName: data.priceName,
|
|
31
|
+
amount: data.amount,
|
|
32
|
+
currency: data.currency,
|
|
33
|
+
type: data.type,
|
|
34
|
+
creditsGranted: data.creditsGranted || 0,
|
|
35
|
+
orderDetail: data.orderDetail,
|
|
36
|
+
paidAt: data.paidAt,
|
|
37
|
+
paidEmail: data.paidEmail || undefined,
|
|
38
|
+
payUpdatedAt: data.payUpdatedAt,
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
// Find transaction by order ID
|
|
44
|
+
findByOrderId(orderId, tx) {
|
|
45
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
47
|
+
return yield client.transaction.findUnique({
|
|
48
|
+
where: { orderId, deleted: 0 }
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
// Find transaction by pay session ID
|
|
53
|
+
findByPaySessionId(paySessionId, tx) {
|
|
54
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
56
|
+
return yield client.transaction.findFirst({
|
|
57
|
+
where: { paySessionId, deleted: 0 }
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
// Find transaction by pay transaction ID
|
|
62
|
+
findByPayTransactionId(payTransactionId, tx) {
|
|
63
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
65
|
+
return yield client.transaction.findFirst({
|
|
66
|
+
where: { payTransactionId, deleted: 0 },
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
// Find transactions by user ID
|
|
71
|
+
findByUserId(userId, params, tx) {
|
|
72
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
74
|
+
const where = { userId, deleted: 0 };
|
|
75
|
+
if (params === null || params === void 0 ? void 0 : params.orderStatus) {
|
|
76
|
+
where.orderStatus = params.orderStatus;
|
|
77
|
+
}
|
|
78
|
+
if (params === null || params === void 0 ? void 0 : params.type) {
|
|
79
|
+
where.type = params.type;
|
|
80
|
+
}
|
|
81
|
+
const [transactions, total] = yield Promise.all([
|
|
82
|
+
client.transaction.findMany({
|
|
83
|
+
where,
|
|
84
|
+
skip: (params === null || params === void 0 ? void 0 : params.skip) || 0,
|
|
85
|
+
take: (params === null || params === void 0 ? void 0 : params.take) || 10,
|
|
86
|
+
orderBy: (params === null || params === void 0 ? void 0 : params.orderBy) || { orderCreatedAt: 'desc' },
|
|
87
|
+
}),
|
|
88
|
+
client.transaction.count({ where }),
|
|
89
|
+
]);
|
|
90
|
+
return { transactions, total };
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
// Update transaction status
|
|
94
|
+
updateStatus(orderId, orderStatus, additionalData, tx) {
|
|
95
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
const updateData = Object.assign({ orderStatus }, additionalData);
|
|
97
|
+
console.log(`orderId: ${orderId}\n updateData: ${JSON.stringify(updateData)}`);
|
|
98
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
99
|
+
return yield client.transaction.update({
|
|
100
|
+
where: { orderId },
|
|
101
|
+
data: updateData,
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
update(orderId, data, tx) {
|
|
106
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
108
|
+
return yield client.transaction.update({
|
|
109
|
+
where: { orderId },
|
|
110
|
+
data,
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// Complete payment
|
|
115
|
+
completePayment(orderId, paymentData, tx) {
|
|
116
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
118
|
+
return yield client.transaction.update({
|
|
119
|
+
where: { orderId },
|
|
120
|
+
data: {
|
|
121
|
+
orderStatus: constants.OrderStatus.SUCCESS,
|
|
122
|
+
payTransactionId: paymentData.payTransactionId,
|
|
123
|
+
paidAt: paymentData.paidAt,
|
|
124
|
+
paidEmail: paymentData.paidEmail,
|
|
125
|
+
paidDetail: paymentData.paidDetail,
|
|
126
|
+
creditsGranted: paymentData.creditsGranted,
|
|
127
|
+
payUpdatedAt: paymentData.payUpdatedAt,
|
|
128
|
+
paymentStatus: paymentData.paymentStatus || constants.PaymentStatus.PAID
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
// Process refund
|
|
134
|
+
processRefund(orderId, refundData, tx) {
|
|
135
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
137
|
+
const transaction = yield this.findByOrderId(orderId, tx);
|
|
138
|
+
if (!transaction) {
|
|
139
|
+
throw new Error('Transaction not found');
|
|
140
|
+
}
|
|
141
|
+
return yield client.transaction.update({
|
|
142
|
+
where: { orderId },
|
|
143
|
+
data: {
|
|
144
|
+
orderStatus: constants.OrderStatus.REFUNDED,
|
|
145
|
+
orderDetail: JSON.stringify({
|
|
146
|
+
original: transaction.orderDetail,
|
|
147
|
+
refund: {
|
|
148
|
+
amount: refundData.refundAmount,
|
|
149
|
+
reason: refundData.refundReason,
|
|
150
|
+
refundedAt: refundData.refundedAt || new Date(),
|
|
151
|
+
},
|
|
152
|
+
}),
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
// Cancel order
|
|
158
|
+
cancelOrder(orderId, reason, tx) {
|
|
159
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
160
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
161
|
+
return yield client.transaction.update({
|
|
162
|
+
where: { orderId },
|
|
163
|
+
data: {
|
|
164
|
+
orderStatus: constants.OrderStatus.CANCELED,
|
|
165
|
+
orderDetail: reason || 'User canceled',
|
|
166
|
+
paymentStatus: constants.PaymentStatus.UN_PAID,
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
// Get expired orders
|
|
172
|
+
getExpiredOrders(tx) {
|
|
173
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
175
|
+
return yield client.transaction.findMany({
|
|
176
|
+
where: {
|
|
177
|
+
orderStatus: constants.OrderStatus.CREATED,
|
|
178
|
+
deleted: 0,
|
|
179
|
+
orderExpiredAt: {
|
|
180
|
+
lt: new Date(),
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
// Update expired orders status
|
|
187
|
+
updateExpiredOrders(tx) {
|
|
188
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
189
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
190
|
+
const result = yield client.transaction.updateMany({
|
|
191
|
+
where: {
|
|
192
|
+
orderStatus: constants.OrderStatus.CREATED,
|
|
193
|
+
deleted: 0,
|
|
194
|
+
orderExpiredAt: {
|
|
195
|
+
lt: new Date(),
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
data: {
|
|
199
|
+
orderStatus: constants.OrderStatus.FAILED,
|
|
200
|
+
orderDetail: 'Order expired',
|
|
201
|
+
paymentStatus: constants.PaymentStatus.UN_PAID,
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
return result.count;
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
// Get subscription related transactions
|
|
208
|
+
getSubscriptionTransactions(paySubscriptionId, tx) {
|
|
209
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
210
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
211
|
+
return yield client.transaction.findMany({
|
|
212
|
+
where: { paySubscriptionId, deleted: 0 },
|
|
213
|
+
orderBy: { orderCreatedAt: 'desc' },
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
// Get revenue statistics
|
|
218
|
+
getRevenueStats(startDate, endDate, tx) {
|
|
219
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
220
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
221
|
+
const where = {
|
|
222
|
+
orderStatus: constants.OrderStatus.SUCCESS,
|
|
223
|
+
deleted: 0,
|
|
224
|
+
};
|
|
225
|
+
if (startDate || endDate) {
|
|
226
|
+
where.paidAt = {};
|
|
227
|
+
if (startDate)
|
|
228
|
+
where.paidAt.gte = startDate;
|
|
229
|
+
if (endDate)
|
|
230
|
+
where.paidAt.lte = endDate;
|
|
231
|
+
}
|
|
232
|
+
// Get successful transactions
|
|
233
|
+
const successfulTransactions = yield client.transaction.findMany({
|
|
234
|
+
where,
|
|
235
|
+
select: {
|
|
236
|
+
amount: true,
|
|
237
|
+
type: true,
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
// Get refund transactions
|
|
241
|
+
const refundWhere = {
|
|
242
|
+
orderStatus: constants.OrderStatus.REFUNDED,
|
|
243
|
+
deleted: 0,
|
|
244
|
+
};
|
|
245
|
+
if (startDate || endDate) {
|
|
246
|
+
refundWhere.orderUpdatedAt = {};
|
|
247
|
+
if (startDate)
|
|
248
|
+
refundWhere.orderUpdatedAt.gte = startDate;
|
|
249
|
+
if (endDate)
|
|
250
|
+
refundWhere.orderUpdatedAt.lte = endDate;
|
|
251
|
+
}
|
|
252
|
+
const refundedTransactions = yield client.transaction.findMany({
|
|
253
|
+
where: refundWhere,
|
|
254
|
+
select: { amount: true },
|
|
255
|
+
});
|
|
256
|
+
// Calculate statistics
|
|
257
|
+
const totalRevenue = successfulTransactions.reduce((sum, t) => sum + (t.amount ? parseFloat(t.amount.toString()) : 0), 0);
|
|
258
|
+
const subscriptionRevenue = successfulTransactions
|
|
259
|
+
.filter(t => t.type === constants.TransactionType.SUBSCRIPTION)
|
|
260
|
+
.reduce((sum, t) => sum + (t.amount ? parseFloat(t.amount.toString()) : 0), 0);
|
|
261
|
+
const oneTimeRevenue = successfulTransactions
|
|
262
|
+
.filter(t => t.type === constants.TransactionType.ONE_TIME)
|
|
263
|
+
.reduce((sum, t) => sum + (t.amount ? parseFloat(t.amount.toString()) : 0), 0);
|
|
264
|
+
const refundedAmount = refundedTransactions.reduce((sum, t) => sum + (t.amount ? parseFloat(t.amount.toString()) : 0), 0);
|
|
265
|
+
return {
|
|
266
|
+
totalRevenue,
|
|
267
|
+
totalTransactions: successfulTransactions.length,
|
|
268
|
+
averageOrderValue: successfulTransactions.length > 0
|
|
269
|
+
? totalRevenue / successfulTransactions.length
|
|
270
|
+
: 0,
|
|
271
|
+
subscriptionRevenue,
|
|
272
|
+
oneTimeRevenue,
|
|
273
|
+
refundedAmount,
|
|
274
|
+
};
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
// Get daily revenue
|
|
278
|
+
getDailyRevenue() {
|
|
279
|
+
return tslib_es6.__awaiter(this, arguments, void 0, function* (days = 30, tx) {
|
|
280
|
+
const startDate = new Date();
|
|
281
|
+
startDate.setDate(startDate.getDate() - days);
|
|
282
|
+
const client$1 = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
283
|
+
const query = client.Prisma.sql `
|
|
284
|
+
SELECT
|
|
285
|
+
DATE(paid_at) as date,
|
|
286
|
+
COUNT(*) as transactions,
|
|
287
|
+
SUM(amount) as revenue,
|
|
288
|
+
SUM(CASE WHEN type = 'subscription' THEN amount ELSE 0 END) as subscription_revenue,
|
|
289
|
+
SUM(CASE WHEN type = 'one_time' THEN amount ELSE 0 END) as onetime_revenue
|
|
290
|
+
FROM transactions
|
|
291
|
+
WHERE order_status = 'success'
|
|
292
|
+
AND deleted = 0
|
|
293
|
+
AND paid_at >= ${startDate}
|
|
294
|
+
GROUP BY DATE(paid_at)
|
|
295
|
+
ORDER BY date DESC
|
|
296
|
+
`;
|
|
297
|
+
const result = yield client$1.$queryRaw(query);
|
|
298
|
+
return result;
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
// Soft Delete transaction
|
|
302
|
+
deleteTransaction(orderId, tx) {
|
|
303
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
304
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
305
|
+
yield client.transaction.update({
|
|
306
|
+
where: { orderId },
|
|
307
|
+
data: { deleted: 1 },
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
// Create batch transactions
|
|
312
|
+
createBatchTransactions(transactions, tx) {
|
|
313
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
314
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
315
|
+
const result = yield client.transaction.createMany({
|
|
316
|
+
data: transactions,
|
|
317
|
+
skipDuplicates: true,
|
|
318
|
+
});
|
|
319
|
+
return result.count;
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
const transactionService = new TransactionService();
|
|
324
|
+
|
|
325
|
+
exports.TransactionService = TransactionService;
|
|
326
|
+
exports.transactionService = transactionService;
|
|
@@ -0,0 +1,323 @@
|
|
|
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 { Prisma } from '@prisma/client';
|
|
3
|
+
import { PaymentStatus, OrderStatus, TransactionType } from './constants.mjs';
|
|
4
|
+
import { checkAndFallbackWithNonTCClient } from '../../prisma/prisma.mjs';
|
|
5
|
+
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
7
|
+
class TransactionService {
|
|
8
|
+
// Create transaction order
|
|
9
|
+
createTransaction(data, tx) {
|
|
10
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
11
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
12
|
+
return yield client.transaction.create({
|
|
13
|
+
data: {
|
|
14
|
+
userId: data.userId,
|
|
15
|
+
orderId: data.orderId,
|
|
16
|
+
orderStatus: data.orderStatus || OrderStatus.CREATED,
|
|
17
|
+
paymentStatus: data.paymentStatus || PaymentStatus.UN_PAID,
|
|
18
|
+
orderExpiredAt: data.orderExpiredAt || new Date(Date.now() + 30 * 60 * 1000), // 默认30分钟过期
|
|
19
|
+
paySupplier: data.paySupplier,
|
|
20
|
+
payTransactionId: data.payTransactionId,
|
|
21
|
+
paySubscriptionId: data.paySubscriptionId,
|
|
22
|
+
paySessionId: data.paySessionId,
|
|
23
|
+
payInvoiceId: data.payInvoiceId,
|
|
24
|
+
hostedInvoiceUrl: data.hostedInvoiceUrl,
|
|
25
|
+
invoicePdf: data.invoicePdf,
|
|
26
|
+
billingReason: data.billingReason,
|
|
27
|
+
priceId: data.priceId,
|
|
28
|
+
priceName: data.priceName,
|
|
29
|
+
amount: data.amount,
|
|
30
|
+
currency: data.currency,
|
|
31
|
+
type: data.type,
|
|
32
|
+
creditsGranted: data.creditsGranted || 0,
|
|
33
|
+
orderDetail: data.orderDetail,
|
|
34
|
+
paidAt: data.paidAt,
|
|
35
|
+
paidEmail: data.paidEmail || undefined,
|
|
36
|
+
payUpdatedAt: data.payUpdatedAt,
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
// Find transaction by order ID
|
|
42
|
+
findByOrderId(orderId, tx) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
45
|
+
return yield client.transaction.findUnique({
|
|
46
|
+
where: { orderId, deleted: 0 }
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
// Find transaction by pay session ID
|
|
51
|
+
findByPaySessionId(paySessionId, tx) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
54
|
+
return yield client.transaction.findFirst({
|
|
55
|
+
where: { paySessionId, deleted: 0 }
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
// Find transaction by pay transaction ID
|
|
60
|
+
findByPayTransactionId(payTransactionId, tx) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
63
|
+
return yield client.transaction.findFirst({
|
|
64
|
+
where: { payTransactionId, deleted: 0 },
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// Find transactions by user ID
|
|
69
|
+
findByUserId(userId, params, tx) {
|
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
72
|
+
const where = { userId, deleted: 0 };
|
|
73
|
+
if (params === null || params === void 0 ? void 0 : params.orderStatus) {
|
|
74
|
+
where.orderStatus = params.orderStatus;
|
|
75
|
+
}
|
|
76
|
+
if (params === null || params === void 0 ? void 0 : params.type) {
|
|
77
|
+
where.type = params.type;
|
|
78
|
+
}
|
|
79
|
+
const [transactions, total] = yield Promise.all([
|
|
80
|
+
client.transaction.findMany({
|
|
81
|
+
where,
|
|
82
|
+
skip: (params === null || params === void 0 ? void 0 : params.skip) || 0,
|
|
83
|
+
take: (params === null || params === void 0 ? void 0 : params.take) || 10,
|
|
84
|
+
orderBy: (params === null || params === void 0 ? void 0 : params.orderBy) || { orderCreatedAt: 'desc' },
|
|
85
|
+
}),
|
|
86
|
+
client.transaction.count({ where }),
|
|
87
|
+
]);
|
|
88
|
+
return { transactions, total };
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
// Update transaction status
|
|
92
|
+
updateStatus(orderId, orderStatus, additionalData, tx) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
const updateData = Object.assign({ orderStatus }, additionalData);
|
|
95
|
+
console.log(`orderId: ${orderId}\n updateData: ${JSON.stringify(updateData)}`);
|
|
96
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
97
|
+
return yield client.transaction.update({
|
|
98
|
+
where: { orderId },
|
|
99
|
+
data: updateData,
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
update(orderId, data, tx) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
106
|
+
return yield client.transaction.update({
|
|
107
|
+
where: { orderId },
|
|
108
|
+
data,
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
// Complete payment
|
|
113
|
+
completePayment(orderId, paymentData, tx) {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
116
|
+
return yield client.transaction.update({
|
|
117
|
+
where: { orderId },
|
|
118
|
+
data: {
|
|
119
|
+
orderStatus: OrderStatus.SUCCESS,
|
|
120
|
+
payTransactionId: paymentData.payTransactionId,
|
|
121
|
+
paidAt: paymentData.paidAt,
|
|
122
|
+
paidEmail: paymentData.paidEmail,
|
|
123
|
+
paidDetail: paymentData.paidDetail,
|
|
124
|
+
creditsGranted: paymentData.creditsGranted,
|
|
125
|
+
payUpdatedAt: paymentData.payUpdatedAt,
|
|
126
|
+
paymentStatus: paymentData.paymentStatus || PaymentStatus.PAID
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
// Process refund
|
|
132
|
+
processRefund(orderId, refundData, tx) {
|
|
133
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
134
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
135
|
+
const transaction = yield this.findByOrderId(orderId, tx);
|
|
136
|
+
if (!transaction) {
|
|
137
|
+
throw new Error('Transaction not found');
|
|
138
|
+
}
|
|
139
|
+
return yield client.transaction.update({
|
|
140
|
+
where: { orderId },
|
|
141
|
+
data: {
|
|
142
|
+
orderStatus: OrderStatus.REFUNDED,
|
|
143
|
+
orderDetail: JSON.stringify({
|
|
144
|
+
original: transaction.orderDetail,
|
|
145
|
+
refund: {
|
|
146
|
+
amount: refundData.refundAmount,
|
|
147
|
+
reason: refundData.refundReason,
|
|
148
|
+
refundedAt: refundData.refundedAt || new Date(),
|
|
149
|
+
},
|
|
150
|
+
}),
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
// Cancel order
|
|
156
|
+
cancelOrder(orderId, reason, tx) {
|
|
157
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
159
|
+
return yield client.transaction.update({
|
|
160
|
+
where: { orderId },
|
|
161
|
+
data: {
|
|
162
|
+
orderStatus: OrderStatus.CANCELED,
|
|
163
|
+
orderDetail: reason || 'User canceled',
|
|
164
|
+
paymentStatus: PaymentStatus.UN_PAID,
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
// Get expired orders
|
|
170
|
+
getExpiredOrders(tx) {
|
|
171
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
172
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
173
|
+
return yield client.transaction.findMany({
|
|
174
|
+
where: {
|
|
175
|
+
orderStatus: OrderStatus.CREATED,
|
|
176
|
+
deleted: 0,
|
|
177
|
+
orderExpiredAt: {
|
|
178
|
+
lt: new Date(),
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
// Update expired orders status
|
|
185
|
+
updateExpiredOrders(tx) {
|
|
186
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
187
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
188
|
+
const result = yield client.transaction.updateMany({
|
|
189
|
+
where: {
|
|
190
|
+
orderStatus: OrderStatus.CREATED,
|
|
191
|
+
deleted: 0,
|
|
192
|
+
orderExpiredAt: {
|
|
193
|
+
lt: new Date(),
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
data: {
|
|
197
|
+
orderStatus: OrderStatus.FAILED,
|
|
198
|
+
orderDetail: 'Order expired',
|
|
199
|
+
paymentStatus: PaymentStatus.UN_PAID,
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
return result.count;
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
// Get subscription related transactions
|
|
206
|
+
getSubscriptionTransactions(paySubscriptionId, tx) {
|
|
207
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
208
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
209
|
+
return yield client.transaction.findMany({
|
|
210
|
+
where: { paySubscriptionId, deleted: 0 },
|
|
211
|
+
orderBy: { orderCreatedAt: 'desc' },
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
// Get revenue statistics
|
|
216
|
+
getRevenueStats(startDate, endDate, tx) {
|
|
217
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
218
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
219
|
+
const where = {
|
|
220
|
+
orderStatus: OrderStatus.SUCCESS,
|
|
221
|
+
deleted: 0,
|
|
222
|
+
};
|
|
223
|
+
if (startDate || endDate) {
|
|
224
|
+
where.paidAt = {};
|
|
225
|
+
if (startDate)
|
|
226
|
+
where.paidAt.gte = startDate;
|
|
227
|
+
if (endDate)
|
|
228
|
+
where.paidAt.lte = endDate;
|
|
229
|
+
}
|
|
230
|
+
// Get successful transactions
|
|
231
|
+
const successfulTransactions = yield client.transaction.findMany({
|
|
232
|
+
where,
|
|
233
|
+
select: {
|
|
234
|
+
amount: true,
|
|
235
|
+
type: true,
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
// Get refund transactions
|
|
239
|
+
const refundWhere = {
|
|
240
|
+
orderStatus: OrderStatus.REFUNDED,
|
|
241
|
+
deleted: 0,
|
|
242
|
+
};
|
|
243
|
+
if (startDate || endDate) {
|
|
244
|
+
refundWhere.orderUpdatedAt = {};
|
|
245
|
+
if (startDate)
|
|
246
|
+
refundWhere.orderUpdatedAt.gte = startDate;
|
|
247
|
+
if (endDate)
|
|
248
|
+
refundWhere.orderUpdatedAt.lte = endDate;
|
|
249
|
+
}
|
|
250
|
+
const refundedTransactions = yield client.transaction.findMany({
|
|
251
|
+
where: refundWhere,
|
|
252
|
+
select: { amount: true },
|
|
253
|
+
});
|
|
254
|
+
// Calculate statistics
|
|
255
|
+
const totalRevenue = successfulTransactions.reduce((sum, t) => sum + (t.amount ? parseFloat(t.amount.toString()) : 0), 0);
|
|
256
|
+
const subscriptionRevenue = successfulTransactions
|
|
257
|
+
.filter(t => t.type === TransactionType.SUBSCRIPTION)
|
|
258
|
+
.reduce((sum, t) => sum + (t.amount ? parseFloat(t.amount.toString()) : 0), 0);
|
|
259
|
+
const oneTimeRevenue = successfulTransactions
|
|
260
|
+
.filter(t => t.type === TransactionType.ONE_TIME)
|
|
261
|
+
.reduce((sum, t) => sum + (t.amount ? parseFloat(t.amount.toString()) : 0), 0);
|
|
262
|
+
const refundedAmount = refundedTransactions.reduce((sum, t) => sum + (t.amount ? parseFloat(t.amount.toString()) : 0), 0);
|
|
263
|
+
return {
|
|
264
|
+
totalRevenue,
|
|
265
|
+
totalTransactions: successfulTransactions.length,
|
|
266
|
+
averageOrderValue: successfulTransactions.length > 0
|
|
267
|
+
? totalRevenue / successfulTransactions.length
|
|
268
|
+
: 0,
|
|
269
|
+
subscriptionRevenue,
|
|
270
|
+
oneTimeRevenue,
|
|
271
|
+
refundedAmount,
|
|
272
|
+
};
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
// Get daily revenue
|
|
276
|
+
getDailyRevenue() {
|
|
277
|
+
return __awaiter(this, arguments, void 0, function* (days = 30, tx) {
|
|
278
|
+
const startDate = new Date();
|
|
279
|
+
startDate.setDate(startDate.getDate() - days);
|
|
280
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
281
|
+
const query = Prisma.sql `
|
|
282
|
+
SELECT
|
|
283
|
+
DATE(paid_at) as date,
|
|
284
|
+
COUNT(*) as transactions,
|
|
285
|
+
SUM(amount) as revenue,
|
|
286
|
+
SUM(CASE WHEN type = 'subscription' THEN amount ELSE 0 END) as subscription_revenue,
|
|
287
|
+
SUM(CASE WHEN type = 'one_time' THEN amount ELSE 0 END) as onetime_revenue
|
|
288
|
+
FROM transactions
|
|
289
|
+
WHERE order_status = 'success'
|
|
290
|
+
AND deleted = 0
|
|
291
|
+
AND paid_at >= ${startDate}
|
|
292
|
+
GROUP BY DATE(paid_at)
|
|
293
|
+
ORDER BY date DESC
|
|
294
|
+
`;
|
|
295
|
+
const result = yield client.$queryRaw(query);
|
|
296
|
+
return result;
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
// Soft Delete transaction
|
|
300
|
+
deleteTransaction(orderId, tx) {
|
|
301
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
302
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
303
|
+
yield client.transaction.update({
|
|
304
|
+
where: { orderId },
|
|
305
|
+
data: { deleted: 1 },
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
// Create batch transactions
|
|
310
|
+
createBatchTransactions(transactions, tx) {
|
|
311
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
312
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
313
|
+
const result = yield client.transaction.createMany({
|
|
314
|
+
data: transactions,
|
|
315
|
+
skipDuplicates: true,
|
|
316
|
+
});
|
|
317
|
+
return result.count;
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
const transactionService = new TransactionService();
|
|
322
|
+
|
|
323
|
+
export { TransactionService, transactionService };
|