@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,249 @@
|
|
|
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 UserBackupService {
|
|
8
|
+
// Create user backup
|
|
9
|
+
createBackup(data, tx) {
|
|
10
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
11
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
12
|
+
return yield client.userBackup.create({
|
|
13
|
+
data: {
|
|
14
|
+
originalUserId: data.originalUserId,
|
|
15
|
+
fingerprintId: data.fingerprintId,
|
|
16
|
+
clerkUserId: data.clerkUserId,
|
|
17
|
+
stripeCusId: data.stripeCusId,
|
|
18
|
+
email: data.email,
|
|
19
|
+
userName: data.userName,
|
|
20
|
+
status: data.status,
|
|
21
|
+
backupData: data.backupData,
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
// Backup user basic data
|
|
27
|
+
backupUserData(userId, tx) {
|
|
28
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
var _a, _b;
|
|
30
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
31
|
+
// Get user basic data
|
|
32
|
+
const userData = yield client.user.findUnique({
|
|
33
|
+
where: { userId },
|
|
34
|
+
});
|
|
35
|
+
if (!userData) {
|
|
36
|
+
throw new Error('User not found');
|
|
37
|
+
}
|
|
38
|
+
// Create backup record
|
|
39
|
+
return yield this.createBackup({
|
|
40
|
+
originalUserId: userData.userId,
|
|
41
|
+
fingerprintId: userData.fingerprintId || undefined,
|
|
42
|
+
clerkUserId: userData.clerkUserId || undefined,
|
|
43
|
+
email: userData.email || undefined,
|
|
44
|
+
status: userData.status,
|
|
45
|
+
backupData: {
|
|
46
|
+
id: userData.id.toString(),
|
|
47
|
+
userId: userData.userId,
|
|
48
|
+
fingerprintId: userData.fingerprintId,
|
|
49
|
+
clerkUserId: userData.clerkUserId,
|
|
50
|
+
email: userData.email,
|
|
51
|
+
status: userData.status,
|
|
52
|
+
createdAt: (_a = userData.createdAt) === null || _a === void 0 ? void 0 : _a.toISOString(),
|
|
53
|
+
updatedAt: (_b = userData.updatedAt) === null || _b === void 0 ? void 0 : _b.toISOString(),
|
|
54
|
+
},
|
|
55
|
+
}, tx);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
// Find backup by original user ID
|
|
59
|
+
findByOriginalUserId(originalUserId, tx) {
|
|
60
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
62
|
+
return yield client.userBackup.findMany({
|
|
63
|
+
where: { originalUserId, deleted: 0 },
|
|
64
|
+
orderBy: { deletedAt: 'desc' },
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// Find backup by email
|
|
69
|
+
findByEmail(email, tx) {
|
|
70
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
72
|
+
return yield client.userBackup.findMany({
|
|
73
|
+
where: { email, deleted: 0 },
|
|
74
|
+
orderBy: { deletedAt: 'desc' },
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
// Find backup by Fingerprint ID
|
|
79
|
+
findByFingerprintId(fingerprintId, tx) {
|
|
80
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
82
|
+
return yield client.userBackup.findMany({
|
|
83
|
+
where: { fingerprintId, deleted: 0 },
|
|
84
|
+
orderBy: { deletedAt: 'desc' },
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
// Find backup by Clerk user ID
|
|
89
|
+
findByClerkUserId(clerkUserId, tx) {
|
|
90
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
91
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
92
|
+
return yield client.userBackup.findMany({
|
|
93
|
+
where: { clerkUserId, deleted: 0 },
|
|
94
|
+
orderBy: { deletedAt: 'desc' },
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
// Find backup by backup ID
|
|
99
|
+
getBackupById(id, tx) {
|
|
100
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
101
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
102
|
+
return yield client.userBackup.findFirst({
|
|
103
|
+
where: { id, deleted: 0 },
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
// Restore user data from backup
|
|
108
|
+
restoreUserData(backupId, tx) {
|
|
109
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
110
|
+
const backup = yield this.getBackupById(backupId, tx);
|
|
111
|
+
if (!backup) {
|
|
112
|
+
throw new Error('Backup not found');
|
|
113
|
+
}
|
|
114
|
+
const backupData = backup.backupData;
|
|
115
|
+
if (!backupData) {
|
|
116
|
+
throw new Error('No backup data available');
|
|
117
|
+
}
|
|
118
|
+
const restore = (client) => tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
119
|
+
const user = yield client.user.create({
|
|
120
|
+
data: {
|
|
121
|
+
fingerprintId: backupData.fingerprintId,
|
|
122
|
+
clerkUserId: backupData.clerkUserId,
|
|
123
|
+
email: backupData.email,
|
|
124
|
+
status: backupData.status,
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
return {
|
|
128
|
+
user,
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
return restore(prisma.checkAndFallbackWithNonTCClient(tx));
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
// List backups
|
|
135
|
+
listBackups(params, tx) {
|
|
136
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
137
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
138
|
+
const where = { deleted: 0 };
|
|
139
|
+
if (params.startDate || params.endDate) {
|
|
140
|
+
where.deletedAt = {};
|
|
141
|
+
if (params.startDate)
|
|
142
|
+
where.deletedAt.gte = params.startDate;
|
|
143
|
+
if (params.endDate)
|
|
144
|
+
where.deletedAt.lte = params.endDate;
|
|
145
|
+
}
|
|
146
|
+
const [backups, total] = yield Promise.all([
|
|
147
|
+
client.userBackup.findMany({
|
|
148
|
+
where,
|
|
149
|
+
skip: params.skip || 0,
|
|
150
|
+
take: params.take || 20,
|
|
151
|
+
orderBy: params.orderBy || { deletedAt: 'desc' },
|
|
152
|
+
}),
|
|
153
|
+
client.userBackup.count({ where }),
|
|
154
|
+
]);
|
|
155
|
+
return { backups, total };
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
// Soft Delete old backups (data cleanup)
|
|
159
|
+
deleteOldBackups() {
|
|
160
|
+
return tslib_es6.__awaiter(this, arguments, void 0, function* (daysToKeep = 90, tx) {
|
|
161
|
+
const cutoffDate = new Date();
|
|
162
|
+
cutoffDate.setDate(cutoffDate.getDate() - daysToKeep);
|
|
163
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
164
|
+
const result = yield client.userBackup.updateMany({
|
|
165
|
+
where: {
|
|
166
|
+
deletedAt: {
|
|
167
|
+
lt: cutoffDate,
|
|
168
|
+
},
|
|
169
|
+
deleted: 0,
|
|
170
|
+
},
|
|
171
|
+
data: {
|
|
172
|
+
deleted: 1,
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
return result.count;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
// Get backup statistics
|
|
179
|
+
getBackupStats(tx) {
|
|
180
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
181
|
+
const client = prisma.checkAndFallbackWithNonTCClient(tx);
|
|
182
|
+
const now = new Date();
|
|
183
|
+
const oneDayAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
|
184
|
+
const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
|
|
185
|
+
const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
|
|
186
|
+
const [totalBackups, last24Hours, last7Days, last30Days,] = yield Promise.all([
|
|
187
|
+
client.userBackup.count({ where: { deleted: 0 } }),
|
|
188
|
+
client.userBackup.count({
|
|
189
|
+
where: { deletedAt: { gte: oneDayAgo }, deleted: 0 },
|
|
190
|
+
}),
|
|
191
|
+
client.userBackup.count({
|
|
192
|
+
where: { deletedAt: { gte: sevenDaysAgo }, deleted: 0 },
|
|
193
|
+
}),
|
|
194
|
+
client.userBackup.count({
|
|
195
|
+
where: { deletedAt: { gte: thirtyDaysAgo }, deleted: 0 },
|
|
196
|
+
}),
|
|
197
|
+
]);
|
|
198
|
+
// Calculate average backup size (simplified calculation)
|
|
199
|
+
const sampleBackups = yield client.userBackup.findMany({
|
|
200
|
+
where: { deleted: 0 },
|
|
201
|
+
take: 100,
|
|
202
|
+
select: { backupData: true },
|
|
203
|
+
});
|
|
204
|
+
const avgBackupSize = sampleBackups.length > 0
|
|
205
|
+
? sampleBackups.reduce((sum, backup) => {
|
|
206
|
+
const size = JSON.stringify(backup.backupData || {}).length;
|
|
207
|
+
return sum + size;
|
|
208
|
+
}, 0) / sampleBackups.length
|
|
209
|
+
: 0;
|
|
210
|
+
return {
|
|
211
|
+
totalBackups,
|
|
212
|
+
last24Hours,
|
|
213
|
+
last7Days,
|
|
214
|
+
last30Days,
|
|
215
|
+
avgBackupSize: Math.round(avgBackupSize),
|
|
216
|
+
};
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
// Export backup data as JSON
|
|
220
|
+
exportBackup(backupId, tx) {
|
|
221
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
const backup = yield this.getBackupById(backupId, tx);
|
|
223
|
+
if (!backup) {
|
|
224
|
+
throw new Error('Backup not found');
|
|
225
|
+
}
|
|
226
|
+
return JSON.stringify(backup, null, 2);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
// Batch backup users (for scheduled backup tasks)
|
|
230
|
+
batchBackupUsers(userIds, tx) {
|
|
231
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
232
|
+
let successCount = 0;
|
|
233
|
+
for (const userId of userIds) {
|
|
234
|
+
try {
|
|
235
|
+
yield this.backupUserData(userId, tx);
|
|
236
|
+
successCount++;
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
console.error(`Failed to backup user ${userId}:`, error);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return successCount;
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const userBackupService = new UserBackupService();
|
|
247
|
+
|
|
248
|
+
exports.UserBackupService = UserBackupService;
|
|
249
|
+
exports.userBackupService = userBackupService;
|
|
@@ -0,0 +1,246 @@
|
|
|
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 { checkAndFallbackWithNonTCClient } from '../../prisma/prisma.mjs';
|
|
3
|
+
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
|
+
class UserBackupService {
|
|
6
|
+
// Create user backup
|
|
7
|
+
createBackup(data, tx) {
|
|
8
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
9
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
10
|
+
return yield client.userBackup.create({
|
|
11
|
+
data: {
|
|
12
|
+
originalUserId: data.originalUserId,
|
|
13
|
+
fingerprintId: data.fingerprintId,
|
|
14
|
+
clerkUserId: data.clerkUserId,
|
|
15
|
+
stripeCusId: data.stripeCusId,
|
|
16
|
+
email: data.email,
|
|
17
|
+
userName: data.userName,
|
|
18
|
+
status: data.status,
|
|
19
|
+
backupData: data.backupData,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
// Backup user basic data
|
|
25
|
+
backupUserData(userId, tx) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
var _a, _b;
|
|
28
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
29
|
+
// Get user basic data
|
|
30
|
+
const userData = yield client.user.findUnique({
|
|
31
|
+
where: { userId },
|
|
32
|
+
});
|
|
33
|
+
if (!userData) {
|
|
34
|
+
throw new Error('User not found');
|
|
35
|
+
}
|
|
36
|
+
// Create backup record
|
|
37
|
+
return yield this.createBackup({
|
|
38
|
+
originalUserId: userData.userId,
|
|
39
|
+
fingerprintId: userData.fingerprintId || undefined,
|
|
40
|
+
clerkUserId: userData.clerkUserId || undefined,
|
|
41
|
+
email: userData.email || undefined,
|
|
42
|
+
status: userData.status,
|
|
43
|
+
backupData: {
|
|
44
|
+
id: userData.id.toString(),
|
|
45
|
+
userId: userData.userId,
|
|
46
|
+
fingerprintId: userData.fingerprintId,
|
|
47
|
+
clerkUserId: userData.clerkUserId,
|
|
48
|
+
email: userData.email,
|
|
49
|
+
status: userData.status,
|
|
50
|
+
createdAt: (_a = userData.createdAt) === null || _a === void 0 ? void 0 : _a.toISOString(),
|
|
51
|
+
updatedAt: (_b = userData.updatedAt) === null || _b === void 0 ? void 0 : _b.toISOString(),
|
|
52
|
+
},
|
|
53
|
+
}, tx);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
// Find backup by original user ID
|
|
57
|
+
findByOriginalUserId(originalUserId, tx) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
60
|
+
return yield client.userBackup.findMany({
|
|
61
|
+
where: { originalUserId, deleted: 0 },
|
|
62
|
+
orderBy: { deletedAt: 'desc' },
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
// Find backup by email
|
|
67
|
+
findByEmail(email, tx) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
70
|
+
return yield client.userBackup.findMany({
|
|
71
|
+
where: { email, deleted: 0 },
|
|
72
|
+
orderBy: { deletedAt: 'desc' },
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
// Find backup by Fingerprint ID
|
|
77
|
+
findByFingerprintId(fingerprintId, tx) {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
80
|
+
return yield client.userBackup.findMany({
|
|
81
|
+
where: { fingerprintId, deleted: 0 },
|
|
82
|
+
orderBy: { deletedAt: 'desc' },
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
// Find backup by Clerk user ID
|
|
87
|
+
findByClerkUserId(clerkUserId, tx) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
90
|
+
return yield client.userBackup.findMany({
|
|
91
|
+
where: { clerkUserId, deleted: 0 },
|
|
92
|
+
orderBy: { deletedAt: 'desc' },
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
// Find backup by backup ID
|
|
97
|
+
getBackupById(id, tx) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
100
|
+
return yield client.userBackup.findFirst({
|
|
101
|
+
where: { id, deleted: 0 },
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
// Restore user data from backup
|
|
106
|
+
restoreUserData(backupId, tx) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
const backup = yield this.getBackupById(backupId, tx);
|
|
109
|
+
if (!backup) {
|
|
110
|
+
throw new Error('Backup not found');
|
|
111
|
+
}
|
|
112
|
+
const backupData = backup.backupData;
|
|
113
|
+
if (!backupData) {
|
|
114
|
+
throw new Error('No backup data available');
|
|
115
|
+
}
|
|
116
|
+
const restore = (client) => __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
const user = yield client.user.create({
|
|
118
|
+
data: {
|
|
119
|
+
fingerprintId: backupData.fingerprintId,
|
|
120
|
+
clerkUserId: backupData.clerkUserId,
|
|
121
|
+
email: backupData.email,
|
|
122
|
+
status: backupData.status,
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
return {
|
|
126
|
+
user,
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
return restore(checkAndFallbackWithNonTCClient(tx));
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
// List backups
|
|
133
|
+
listBackups(params, tx) {
|
|
134
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
135
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
136
|
+
const where = { deleted: 0 };
|
|
137
|
+
if (params.startDate || params.endDate) {
|
|
138
|
+
where.deletedAt = {};
|
|
139
|
+
if (params.startDate)
|
|
140
|
+
where.deletedAt.gte = params.startDate;
|
|
141
|
+
if (params.endDate)
|
|
142
|
+
where.deletedAt.lte = params.endDate;
|
|
143
|
+
}
|
|
144
|
+
const [backups, total] = yield Promise.all([
|
|
145
|
+
client.userBackup.findMany({
|
|
146
|
+
where,
|
|
147
|
+
skip: params.skip || 0,
|
|
148
|
+
take: params.take || 20,
|
|
149
|
+
orderBy: params.orderBy || { deletedAt: 'desc' },
|
|
150
|
+
}),
|
|
151
|
+
client.userBackup.count({ where }),
|
|
152
|
+
]);
|
|
153
|
+
return { backups, total };
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
// Soft Delete old backups (data cleanup)
|
|
157
|
+
deleteOldBackups() {
|
|
158
|
+
return __awaiter(this, arguments, void 0, function* (daysToKeep = 90, tx) {
|
|
159
|
+
const cutoffDate = new Date();
|
|
160
|
+
cutoffDate.setDate(cutoffDate.getDate() - daysToKeep);
|
|
161
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
162
|
+
const result = yield client.userBackup.updateMany({
|
|
163
|
+
where: {
|
|
164
|
+
deletedAt: {
|
|
165
|
+
lt: cutoffDate,
|
|
166
|
+
},
|
|
167
|
+
deleted: 0,
|
|
168
|
+
},
|
|
169
|
+
data: {
|
|
170
|
+
deleted: 1,
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
return result.count;
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
// Get backup statistics
|
|
177
|
+
getBackupStats(tx) {
|
|
178
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
179
|
+
const client = checkAndFallbackWithNonTCClient(tx);
|
|
180
|
+
const now = new Date();
|
|
181
|
+
const oneDayAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
|
182
|
+
const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
|
|
183
|
+
const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
|
|
184
|
+
const [totalBackups, last24Hours, last7Days, last30Days,] = yield Promise.all([
|
|
185
|
+
client.userBackup.count({ where: { deleted: 0 } }),
|
|
186
|
+
client.userBackup.count({
|
|
187
|
+
where: { deletedAt: { gte: oneDayAgo }, deleted: 0 },
|
|
188
|
+
}),
|
|
189
|
+
client.userBackup.count({
|
|
190
|
+
where: { deletedAt: { gte: sevenDaysAgo }, deleted: 0 },
|
|
191
|
+
}),
|
|
192
|
+
client.userBackup.count({
|
|
193
|
+
where: { deletedAt: { gte: thirtyDaysAgo }, deleted: 0 },
|
|
194
|
+
}),
|
|
195
|
+
]);
|
|
196
|
+
// Calculate average backup size (simplified calculation)
|
|
197
|
+
const sampleBackups = yield client.userBackup.findMany({
|
|
198
|
+
where: { deleted: 0 },
|
|
199
|
+
take: 100,
|
|
200
|
+
select: { backupData: true },
|
|
201
|
+
});
|
|
202
|
+
const avgBackupSize = sampleBackups.length > 0
|
|
203
|
+
? sampleBackups.reduce((sum, backup) => {
|
|
204
|
+
const size = JSON.stringify(backup.backupData || {}).length;
|
|
205
|
+
return sum + size;
|
|
206
|
+
}, 0) / sampleBackups.length
|
|
207
|
+
: 0;
|
|
208
|
+
return {
|
|
209
|
+
totalBackups,
|
|
210
|
+
last24Hours,
|
|
211
|
+
last7Days,
|
|
212
|
+
last30Days,
|
|
213
|
+
avgBackupSize: Math.round(avgBackupSize),
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
// Export backup data as JSON
|
|
218
|
+
exportBackup(backupId, tx) {
|
|
219
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
220
|
+
const backup = yield this.getBackupById(backupId, tx);
|
|
221
|
+
if (!backup) {
|
|
222
|
+
throw new Error('Backup not found');
|
|
223
|
+
}
|
|
224
|
+
return JSON.stringify(backup, null, 2);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
// Batch backup users (for scheduled backup tasks)
|
|
228
|
+
batchBackupUsers(userIds, tx) {
|
|
229
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
230
|
+
let successCount = 0;
|
|
231
|
+
for (const userId of userIds) {
|
|
232
|
+
try {
|
|
233
|
+
yield this.backupUserData(userId, tx);
|
|
234
|
+
successCount++;
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
console.error(`Failed to backup user ${userId}:`, error);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return successCount;
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const userBackupService = new UserBackupService();
|
|
245
|
+
|
|
246
|
+
export { UserBackupService, userBackupService };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/stripe/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { handleStripeEvent } from './webhook-handler.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webhook-handler.d.ts","sourceRoot":"","sources":["../../../src/services/stripe/webhook-handler.ts"],"names":[],"mappings":"AAgBA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAoB5B;;GAEG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,iBAqD1D"}
|