@windrun-huaiin/backend-core 14.1.1 → 14.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app/api/user/anonymous/init/route.d.ts.map +1 -1
- package/dist/app/api/user/anonymous/init/route.js +60 -13
- package/dist/app/api/user/anonymous/init/route.mjs +61 -14
- package/dist/index.js +4 -1
- package/dist/index.mjs +3 -1
- package/dist/lib/money-price-helper.js +2 -1
- package/dist/lib/money-price-helper.mjs +3 -2
- package/dist/services/aggregate/anonymous.aggregate.service.d.ts +21 -0
- package/dist/services/aggregate/anonymous.aggregate.service.d.ts.map +1 -0
- package/dist/services/aggregate/anonymous.aggregate.service.js +87 -0
- package/dist/services/aggregate/anonymous.aggregate.service.mjs +85 -0
- package/dist/services/aggregate/index.d.ts +1 -0
- package/dist/services/aggregate/index.d.ts.map +1 -1
- package/dist/services/aggregate/index.js +2 -0
- package/dist/services/aggregate/index.mjs +1 -0
- package/dist/services/context/index.d.ts +1 -0
- package/dist/services/context/index.d.ts.map +1 -1
- package/dist/services/context/index.js +2 -1
- package/dist/services/context/index.mjs +2 -1
- package/dist/services/context/user-context-finalizer.d.ts +13 -0
- package/dist/services/context/user-context-finalizer.d.ts.map +1 -0
- package/dist/services/context/user-context-finalizer.js +74 -0
- package/dist/services/context/user-context-finalizer.mjs +72 -0
- package/dist/services/context/user-context-service.d.ts +0 -6
- package/dist/services/context/user-context-service.d.ts.map +1 -1
- package/dist/services/context/user-context-service.js +0 -64
- package/dist/services/context/user-context-service.mjs +1 -64
- package/package.json +7 -7
- package/src/app/api/user/anonymous/init/route.ts +72 -12
- package/src/lib/money-price-helper.ts +3 -3
- package/src/services/aggregate/anonymous.aggregate.service.ts +113 -0
- package/src/services/aggregate/index.ts +1 -0
- package/src/services/context/index.ts +2 -1
- package/src/services/context/user-context-finalizer.ts +84 -0
- package/src/services/context/user-context-service.ts +0 -77
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('@windrun-huaiin/lib/utils');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Output finalizer for user-context payloads.
|
|
7
|
+
* Real data assembly should stay in user-context-service; any optional test-only
|
|
8
|
+
* shaping is isolated here so production services do not depend on mock code.
|
|
9
|
+
*/
|
|
10
|
+
function finalizeUserContext(context) {
|
|
11
|
+
var _a;
|
|
12
|
+
const mockEnabled = process.env.MONEY_PRICE_MOCK_USER_ENABLED === 'true';
|
|
13
|
+
const mockType = Number((_a = process.env.MONEY_PRICE_MOCK_USER_TYPE) !== null && _a !== void 0 ? _a : NaN);
|
|
14
|
+
if (!context.xUser ||
|
|
15
|
+
!mockEnabled ||
|
|
16
|
+
!Number.isInteger(mockType) ||
|
|
17
|
+
mockType < 0 ||
|
|
18
|
+
mockType > 4) {
|
|
19
|
+
return context;
|
|
20
|
+
}
|
|
21
|
+
const ensureSubscription = () => {
|
|
22
|
+
if (!context.xSubscription) {
|
|
23
|
+
const now = new Date();
|
|
24
|
+
context.xSubscription = {
|
|
25
|
+
id: BigInt(99999),
|
|
26
|
+
userId: context.xUser.userId,
|
|
27
|
+
paySubscriptionId: 'MOCK-PAY-SUB-ID',
|
|
28
|
+
orderId: '',
|
|
29
|
+
priceId: '',
|
|
30
|
+
priceName: 'MOCK-TEST',
|
|
31
|
+
status: 'active',
|
|
32
|
+
creditsAllocated: 0,
|
|
33
|
+
subPeriodStart: utils.viewLocalTime(now),
|
|
34
|
+
subPeriodEnd: utils.viewLocalTime(now),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return context.xSubscription;
|
|
38
|
+
};
|
|
39
|
+
switch (mockType) {
|
|
40
|
+
case 0: {
|
|
41
|
+
const subscription = ensureSubscription();
|
|
42
|
+
subscription.status = '';
|
|
43
|
+
subscription.priceId = '';
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case 1: {
|
|
47
|
+
const subscription = ensureSubscription();
|
|
48
|
+
subscription.priceId =
|
|
49
|
+
process.env.STRIPE_PRO_MONTHLY_PRICE_ID || subscription.priceId;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
case 2: {
|
|
53
|
+
const subscription = ensureSubscription();
|
|
54
|
+
subscription.priceId =
|
|
55
|
+
process.env.STRIPE_ULTRA_MONTHLY_PRICE_ID || subscription.priceId;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
case 3: {
|
|
59
|
+
const subscription = ensureSubscription();
|
|
60
|
+
subscription.priceId =
|
|
61
|
+
process.env.STRIPE_PRO_YEARLY_PRICE_ID || subscription.priceId;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
case 4: {
|
|
65
|
+
const subscription = ensureSubscription();
|
|
66
|
+
subscription.priceId =
|
|
67
|
+
process.env.STRIPE_ULTRA_YEARLY_PRICE_ID || subscription.priceId;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return context;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
exports.finalizeUserContext = finalizeUserContext;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { viewLocalTime } from '@windrun-huaiin/lib/utils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Output finalizer for user-context payloads.
|
|
5
|
+
* Real data assembly should stay in user-context-service; any optional test-only
|
|
6
|
+
* shaping is isolated here so production services do not depend on mock code.
|
|
7
|
+
*/
|
|
8
|
+
function finalizeUserContext(context) {
|
|
9
|
+
var _a;
|
|
10
|
+
const mockEnabled = process.env.MONEY_PRICE_MOCK_USER_ENABLED === 'true';
|
|
11
|
+
const mockType = Number((_a = process.env.MONEY_PRICE_MOCK_USER_TYPE) !== null && _a !== void 0 ? _a : NaN);
|
|
12
|
+
if (!context.xUser ||
|
|
13
|
+
!mockEnabled ||
|
|
14
|
+
!Number.isInteger(mockType) ||
|
|
15
|
+
mockType < 0 ||
|
|
16
|
+
mockType > 4) {
|
|
17
|
+
return context;
|
|
18
|
+
}
|
|
19
|
+
const ensureSubscription = () => {
|
|
20
|
+
if (!context.xSubscription) {
|
|
21
|
+
const now = new Date();
|
|
22
|
+
context.xSubscription = {
|
|
23
|
+
id: BigInt(99999),
|
|
24
|
+
userId: context.xUser.userId,
|
|
25
|
+
paySubscriptionId: 'MOCK-PAY-SUB-ID',
|
|
26
|
+
orderId: '',
|
|
27
|
+
priceId: '',
|
|
28
|
+
priceName: 'MOCK-TEST',
|
|
29
|
+
status: 'active',
|
|
30
|
+
creditsAllocated: 0,
|
|
31
|
+
subPeriodStart: viewLocalTime(now),
|
|
32
|
+
subPeriodEnd: viewLocalTime(now),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return context.xSubscription;
|
|
36
|
+
};
|
|
37
|
+
switch (mockType) {
|
|
38
|
+
case 0: {
|
|
39
|
+
const subscription = ensureSubscription();
|
|
40
|
+
subscription.status = '';
|
|
41
|
+
subscription.priceId = '';
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
case 1: {
|
|
45
|
+
const subscription = ensureSubscription();
|
|
46
|
+
subscription.priceId =
|
|
47
|
+
process.env.STRIPE_PRO_MONTHLY_PRICE_ID || subscription.priceId;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case 2: {
|
|
51
|
+
const subscription = ensureSubscription();
|
|
52
|
+
subscription.priceId =
|
|
53
|
+
process.env.STRIPE_ULTRA_MONTHLY_PRICE_ID || subscription.priceId;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case 3: {
|
|
57
|
+
const subscription = ensureSubscription();
|
|
58
|
+
subscription.priceId =
|
|
59
|
+
process.env.STRIPE_PRO_YEARLY_PRICE_ID || subscription.priceId;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case 4: {
|
|
63
|
+
const subscription = ensureSubscription();
|
|
64
|
+
subscription.priceId =
|
|
65
|
+
process.env.STRIPE_ULTRA_YEARLY_PRICE_ID || subscription.priceId;
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return context;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export { finalizeUserContext };
|
|
@@ -21,10 +21,4 @@ export declare function buildInitUserContextFromEntities(params: {
|
|
|
21
21
|
}): InitUserContext;
|
|
22
22
|
export declare function fetchUserContextByClerkUserId(clerkUserId: string): Promise<UserContextEntities | null>;
|
|
23
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
24
|
//# sourceMappingURL=user-context-service.d.ts.map
|
|
@@ -1 +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
|
|
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"}
|
|
@@ -97,71 +97,7 @@ function fetchLatestUserContextByFingerprintId(fingerprintId) {
|
|
|
97
97
|
};
|
|
98
98
|
});
|
|
99
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
100
|
|
|
164
|
-
exports.applyUserMockContext = applyUserMockContext;
|
|
165
101
|
exports.buildInitUserContextFromEntities = buildInitUserContextFromEntities;
|
|
166
102
|
exports.fetchLatestUserContextByFingerprintId = fetchLatestUserContextByFingerprintId;
|
|
167
103
|
exports.fetchUserContextByClerkUserId = fetchUserContextByClerkUserId;
|
|
@@ -95,68 +95,5 @@ function fetchLatestUserContextByFingerprintId(fingerprintId) {
|
|
|
95
95
|
};
|
|
96
96
|
});
|
|
97
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
98
|
|
|
162
|
-
export {
|
|
99
|
+
export { buildInitUserContextFromEntities, fetchLatestUserContextByFingerprintId, fetchUserContextByClerkUserId, mapCreditToXCredit, mapSubscriptionToXSubscription, mapUserToXUser };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@windrun-huaiin/backend-core",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.3.0",
|
|
4
4
|
"description": "Shared backend primitives: Prisma schema/client, database services, routing helpers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -85,17 +85,17 @@
|
|
|
85
85
|
],
|
|
86
86
|
"dependencies": {
|
|
87
87
|
"@clerk/nextjs": "^7.0.5",
|
|
88
|
-
"@prisma/client": "^6.
|
|
88
|
+
"@prisma/client": "^6.19.0",
|
|
89
89
|
"@upstash/redis": "^1.34.0",
|
|
90
90
|
"@upstash/qstash": "^2.7.0",
|
|
91
91
|
"@upstash/lock": "^0.2.1",
|
|
92
92
|
"next": "16.1.6",
|
|
93
|
-
"prisma": "^6.
|
|
93
|
+
"prisma": "^6.19.0",
|
|
94
94
|
"stripe": "20.0.0",
|
|
95
95
|
"svix": "^1.86.0",
|
|
96
96
|
"zod": "^4.3.6",
|
|
97
|
-
"@windrun-huaiin/lib": "^14.0.
|
|
98
|
-
"@windrun-huaiin/third-ui": "^14.0
|
|
97
|
+
"@windrun-huaiin/lib": "^14.0.1",
|
|
98
|
+
"@windrun-huaiin/third-ui": "^14.2.0"
|
|
99
99
|
},
|
|
100
100
|
"devDependencies": {
|
|
101
101
|
"@rollup/plugin-alias": "^5.1.1",
|
|
@@ -108,9 +108,9 @@
|
|
|
108
108
|
},
|
|
109
109
|
"peerDependencies": {
|
|
110
110
|
"@clerk/nextjs": "^7.0.5",
|
|
111
|
-
"@prisma/client": "^6.
|
|
111
|
+
"@prisma/client": "^6.19.0",
|
|
112
112
|
"next": "16.1.6",
|
|
113
|
-
"prisma": "^6.
|
|
113
|
+
"prisma": "^6.19.0",
|
|
114
114
|
"stripe": "20.0.0",
|
|
115
115
|
"svix": "^1.86.0"
|
|
116
116
|
},
|
|
@@ -5,13 +5,12 @@
|
|
|
5
5
|
return this.toString();
|
|
6
6
|
};
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import { anonymousAggregateService } from '@/aggregate/anonymous.aggregate.service';
|
|
9
9
|
import type { XCredit, XSubscription, XUser } from '@windrun-huaiin/third-ui/fingerprint';
|
|
10
10
|
import { extractFingerprintFromNextRequest } from '@windrun-huaiin/third-ui/fingerprint/server';
|
|
11
11
|
import { auth } from '@clerk/nextjs/server';
|
|
12
12
|
import { NextRequest, NextResponse } from 'next/server';
|
|
13
13
|
import {
|
|
14
|
-
applyUserMockContext,
|
|
15
14
|
fetchLatestUserContextByFingerprintId,
|
|
16
15
|
fetchUserContextByClerkUserId,
|
|
17
16
|
mapCreditToXCredit,
|
|
@@ -19,6 +18,8 @@ import {
|
|
|
19
18
|
mapUserToXUser,
|
|
20
19
|
type UserContextEntities,
|
|
21
20
|
} from '@/context/user-context-service';
|
|
21
|
+
import { finalizeUserContext } from '@/context/user-context-finalizer';
|
|
22
|
+
|
|
22
23
|
import type { Prisma } from '@/db/prisma-model-type';
|
|
23
24
|
|
|
24
25
|
|
|
@@ -60,7 +61,7 @@ function createSuccessResponse(params: {
|
|
|
60
61
|
...params.options,
|
|
61
62
|
};
|
|
62
63
|
|
|
63
|
-
return
|
|
64
|
+
return finalizeUserContext(response);
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
/** 创建错误响应 */
|
|
@@ -306,6 +307,35 @@ function detectChannelFromPlatform(platform: string | null | undefined): string
|
|
|
306
307
|
}
|
|
307
308
|
}
|
|
308
309
|
|
|
310
|
+
function detectChannelFromUtmMedium(value: string | null | undefined): string | null {
|
|
311
|
+
const normalized = value?.trim().toLowerCase();
|
|
312
|
+
if (!normalized) {
|
|
313
|
+
return null;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (/^(cpc|ppc|paid|paid_search|display|banner|affiliate|email|newsletter|push|sms)$/.test(normalized)) {
|
|
317
|
+
return 'campaign';
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (/^(social|social_paid|social-organic|social_organic)$/.test(normalized)) {
|
|
321
|
+
return 'social';
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (/^(organic|seo|search)$/.test(normalized)) {
|
|
325
|
+
return 'search';
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (/^(referral|partner)$/.test(normalized)) {
|
|
329
|
+
return 'referral';
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (/^(ai|llm)$/.test(normalized)) {
|
|
333
|
+
return 'ai';
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return 'campaign';
|
|
337
|
+
}
|
|
338
|
+
|
|
309
339
|
function parseUserAgent(request: NextRequest): Pick<SourceRefData, 'userAgent' | 'deviceType' | 'os' | 'browser' | 'secChUaMobile' | 'secChUaPlatform'> {
|
|
310
340
|
const userAgentHeader = request.headers.get('user-agent');
|
|
311
341
|
const secChUaMobile = normalizeQueryParam(request.headers.get('sec-ch-ua-mobile')) ?? undefined;
|
|
@@ -430,6 +460,21 @@ function finalizeAttribution(sourceRef: SourceRefData) {
|
|
|
430
460
|
const landingHost = normalizeHost(sourceRef.landingHost);
|
|
431
461
|
const refererHost = normalizeHost(sourceRef.refererHost);
|
|
432
462
|
const internal = isInternalReferer(landingHost, refererHost);
|
|
463
|
+
const hasCampaignMarker = Boolean(
|
|
464
|
+
sourceRef.utmSource
|
|
465
|
+
|| sourceRef.utmMedium
|
|
466
|
+
|| sourceRef.utmCampaign
|
|
467
|
+
|| sourceRef.utmTerm
|
|
468
|
+
|| sourceRef.utmContent
|
|
469
|
+
|| sourceRef.utmId
|
|
470
|
+
|| sourceRef.ref
|
|
471
|
+
|| sourceRef.gclid
|
|
472
|
+
|| sourceRef.fbclid
|
|
473
|
+
|| sourceRef.msclkid
|
|
474
|
+
|| sourceRef.ttclid
|
|
475
|
+
|| sourceRef.twclid
|
|
476
|
+
|| sourceRef.liFatId
|
|
477
|
+
);
|
|
433
478
|
if (internal) {
|
|
434
479
|
sourceRef.isInternalReferer = true;
|
|
435
480
|
}
|
|
@@ -437,7 +482,10 @@ function finalizeAttribution(sourceRef: SourceRefData) {
|
|
|
437
482
|
const utmPlatform = detectPlatform(sourceRef.utmSource) || detectPlatform(sourceRef.ref);
|
|
438
483
|
if (utmPlatform) {
|
|
439
484
|
sourceRef.sourcePlatform = utmPlatform;
|
|
440
|
-
sourceRef.sourceChannel = detectChannelFromPlatform(utmPlatform)
|
|
485
|
+
sourceRef.sourceChannel = detectChannelFromPlatform(utmPlatform)
|
|
486
|
+
?? detectChannelFromUtmMedium(sourceRef.utmMedium)
|
|
487
|
+
?? sourceRef.sourceChannel
|
|
488
|
+
?? 'campaign';
|
|
441
489
|
sourceRef.sourceType = 'campaign';
|
|
442
490
|
return;
|
|
443
491
|
}
|
|
@@ -484,9 +532,16 @@ function finalizeAttribution(sourceRef: SourceRefData) {
|
|
|
484
532
|
return;
|
|
485
533
|
}
|
|
486
534
|
|
|
535
|
+
if (hasCampaignMarker) {
|
|
536
|
+
sourceRef.sourcePlatform = 'other';
|
|
537
|
+
sourceRef.sourceChannel = detectChannelFromUtmMedium(sourceRef.utmMedium) ?? 'campaign';
|
|
538
|
+
sourceRef.sourceType = 'campaign';
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
|
|
487
542
|
if (!internal && refererHost) {
|
|
488
543
|
const refererPlatform = detectPlatform(refererHost) || detectPlatform(sourceRef.httpRefer);
|
|
489
|
-
sourceRef.sourcePlatform = refererPlatform ??
|
|
544
|
+
sourceRef.sourcePlatform = refererPlatform ?? 'other';
|
|
490
545
|
sourceRef.sourceChannel = detectChannelFromPlatform(refererPlatform) ?? 'referral';
|
|
491
546
|
sourceRef.sourceType = 'referer';
|
|
492
547
|
return;
|
|
@@ -624,22 +679,27 @@ async function handleFingerprintRequest(request: NextRequest, options: { createI
|
|
|
624
679
|
|
|
625
680
|
const sourceRef = extractSourceRef(request);
|
|
626
681
|
|
|
627
|
-
|
|
628
|
-
const { newUser, credit } = await userAggregateService.initAnonymousUser(
|
|
682
|
+
const anonymousInitResult = await anonymousAggregateService.getOrCreateByFingerprintId(
|
|
629
683
|
fingerprintId,
|
|
630
684
|
{ sourceRef: sourceRef?? undefined}
|
|
631
685
|
);
|
|
632
686
|
|
|
633
|
-
|
|
687
|
+
if (anonymousInitResult.isNewUser) {
|
|
688
|
+
console.log(`Created new anonymous user ${anonymousInitResult.user.userId} with fingerprint ${fingerprintId}`);
|
|
689
|
+
}
|
|
634
690
|
|
|
635
691
|
// 返回创建结果
|
|
636
692
|
const response = createSuccessResponse({
|
|
637
693
|
entities: {
|
|
638
|
-
user:
|
|
639
|
-
credit,
|
|
640
|
-
subscription:
|
|
694
|
+
user: anonymousInitResult.user,
|
|
695
|
+
credit: anonymousInitResult.credit,
|
|
696
|
+
subscription: anonymousInitResult.subscription,
|
|
697
|
+
},
|
|
698
|
+
isNewUser: anonymousInitResult.isNewUser,
|
|
699
|
+
options: {
|
|
700
|
+
totalUsersOnDevice: anonymousInitResult.totalUsersOnDevice,
|
|
701
|
+
hasAnonymousUser: anonymousInitResult.hasAnonymousUser,
|
|
641
702
|
},
|
|
642
|
-
isNewUser: true,
|
|
643
703
|
});
|
|
644
704
|
return NextResponse.json(response);
|
|
645
705
|
|
|
@@ -5,9 +5,9 @@ import {
|
|
|
5
5
|
} from '@windrun-huaiin/third-ui/fingerprint/server';
|
|
6
6
|
import type { InitUserContext } from '@windrun-huaiin/third-ui/main/server';
|
|
7
7
|
import {
|
|
8
|
-
applyUserMockContext,
|
|
9
8
|
buildInitUserContextFromEntities,
|
|
10
9
|
fetchUserContextByClerkUserId,
|
|
10
|
+
finalizeUserContext,
|
|
11
11
|
} from '../services/context';
|
|
12
12
|
|
|
13
13
|
async function readFingerprintIdFromRequest(): Promise<string | null> {
|
|
@@ -42,7 +42,7 @@ export async function getMoneyPriceInitUserContext(): Promise<InitUserContext> {
|
|
|
42
42
|
isClerkAuthenticated: true,
|
|
43
43
|
});
|
|
44
44
|
|
|
45
|
-
return
|
|
45
|
+
return finalizeUserContext(initUserContext);
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
const fingerprintId = await readFingerprintIdFromRequest();
|
|
@@ -58,4 +58,4 @@ export async function getMoneyPriceInitUserContext(): Promise<InitUserContext> {
|
|
|
58
58
|
xSubscription: null,
|
|
59
59
|
isClerkAuthenticated: false,
|
|
60
60
|
};
|
|
61
|
-
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { creditService, subscriptionService, userService } from '@/db';
|
|
2
|
+
import { UserStatus } from '@/db/constants';
|
|
3
|
+
import type { Credit, Subscription, User } from '@/db/prisma-model-type';
|
|
4
|
+
import { freeAmount } from '@/lib/credit-init';
|
|
5
|
+
import { runInTransaction } from '@/prisma/prisma-transaction-util';
|
|
6
|
+
import { CreditType, OperationType } from '@/db/constants';
|
|
7
|
+
import { Prisma } from '@/db/prisma-model-type';
|
|
8
|
+
|
|
9
|
+
const ANONYMOUS_INIT_LOCK_NAMESPACE = 92831;
|
|
10
|
+
|
|
11
|
+
type AnonymousInitContext = {
|
|
12
|
+
user: User;
|
|
13
|
+
credit: Credit | null;
|
|
14
|
+
subscription: Subscription | null;
|
|
15
|
+
isNewUser: boolean;
|
|
16
|
+
totalUsersOnDevice: number;
|
|
17
|
+
hasAnonymousUser: boolean;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
class AnonymousAggregateService {
|
|
21
|
+
private async lockFingerprintInit(
|
|
22
|
+
tx: Prisma.TransactionClient,
|
|
23
|
+
fingerprintId: string,
|
|
24
|
+
): Promise<void> {
|
|
25
|
+
await tx.$executeRaw`
|
|
26
|
+
SELECT pg_advisory_xact_lock(
|
|
27
|
+
${Prisma.raw(String(ANONYMOUS_INIT_LOCK_NAMESPACE))},
|
|
28
|
+
hashtext(${fingerprintId})
|
|
29
|
+
)
|
|
30
|
+
`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private async findLatestUserContextByFingerprintId(
|
|
34
|
+
fingerprintId: string,
|
|
35
|
+
tx: Prisma.TransactionClient,
|
|
36
|
+
): Promise<AnonymousInitContext | null> {
|
|
37
|
+
const existingUsers = await userService.findListByFingerprintId(fingerprintId, tx);
|
|
38
|
+
if (existingUsers.length === 0) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const latestUser = existingUsers[0];
|
|
43
|
+
const [credit, subscription] = await Promise.all([
|
|
44
|
+
creditService.getCredit(latestUser.userId, tx),
|
|
45
|
+
subscriptionService.getActiveSubscription(latestUser.userId, tx),
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
user: latestUser,
|
|
50
|
+
credit,
|
|
51
|
+
subscription,
|
|
52
|
+
isNewUser: false,
|
|
53
|
+
totalUsersOnDevice: existingUsers.length,
|
|
54
|
+
hasAnonymousUser: true,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private async createAnonymousUser(
|
|
59
|
+
fingerprintId: string,
|
|
60
|
+
tx: Prisma.TransactionClient,
|
|
61
|
+
options?: { sourceRef?: Prisma.InputJsonValue; },
|
|
62
|
+
): Promise<AnonymousInitContext> {
|
|
63
|
+
const newUser = await userService.createUser(
|
|
64
|
+
{
|
|
65
|
+
fingerprintId,
|
|
66
|
+
sourceRef: options?.sourceRef,
|
|
67
|
+
status: UserStatus.ANONYMOUS,
|
|
68
|
+
},
|
|
69
|
+
tx,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const credit = await creditService.initializeCreditWithFree(
|
|
73
|
+
{
|
|
74
|
+
userId: newUser.userId,
|
|
75
|
+
feature: 'anonymous_user_init',
|
|
76
|
+
creditType: CreditType.FREE,
|
|
77
|
+
operationType: OperationType.SYS_GIFT,
|
|
78
|
+
operationReferId: newUser.userId,
|
|
79
|
+
creditsChange: freeAmount,
|
|
80
|
+
},
|
|
81
|
+
tx,
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
await subscriptionService.initializeSubscription(newUser.userId, tx);
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
user: newUser,
|
|
88
|
+
credit,
|
|
89
|
+
subscription: null,
|
|
90
|
+
isNewUser: true,
|
|
91
|
+
totalUsersOnDevice: 1,
|
|
92
|
+
hasAnonymousUser: true,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async getOrCreateByFingerprintId(
|
|
97
|
+
fingerprintId: string,
|
|
98
|
+
options?: { sourceRef?: Prisma.InputJsonValue; },
|
|
99
|
+
): Promise<AnonymousInitContext> {
|
|
100
|
+
return runInTransaction(async (tx) => {
|
|
101
|
+
await this.lockFingerprintInit(tx, fingerprintId);
|
|
102
|
+
|
|
103
|
+
const existingContext = await this.findLatestUserContextByFingerprintId(fingerprintId, tx);
|
|
104
|
+
if (existingContext) {
|
|
105
|
+
return existingContext;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return this.createAnonymousUser(fingerprintId, tx, options);
|
|
109
|
+
}, 'anonymous_get_or_create_by_fingerprint_id');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export const anonymousAggregateService = new AnonymousAggregateService();
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from './user-context-service';
|
|
1
|
+
export * from './user-context-service';
|
|
2
|
+
export * from './user-context-finalizer';
|