@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.
Files changed (35) hide show
  1. package/dist/app/api/user/anonymous/init/route.d.ts.map +1 -1
  2. package/dist/app/api/user/anonymous/init/route.js +60 -13
  3. package/dist/app/api/user/anonymous/init/route.mjs +61 -14
  4. package/dist/index.js +4 -1
  5. package/dist/index.mjs +3 -1
  6. package/dist/lib/money-price-helper.js +2 -1
  7. package/dist/lib/money-price-helper.mjs +3 -2
  8. package/dist/services/aggregate/anonymous.aggregate.service.d.ts +21 -0
  9. package/dist/services/aggregate/anonymous.aggregate.service.d.ts.map +1 -0
  10. package/dist/services/aggregate/anonymous.aggregate.service.js +87 -0
  11. package/dist/services/aggregate/anonymous.aggregate.service.mjs +85 -0
  12. package/dist/services/aggregate/index.d.ts +1 -0
  13. package/dist/services/aggregate/index.d.ts.map +1 -1
  14. package/dist/services/aggregate/index.js +2 -0
  15. package/dist/services/aggregate/index.mjs +1 -0
  16. package/dist/services/context/index.d.ts +1 -0
  17. package/dist/services/context/index.d.ts.map +1 -1
  18. package/dist/services/context/index.js +2 -1
  19. package/dist/services/context/index.mjs +2 -1
  20. package/dist/services/context/user-context-finalizer.d.ts +13 -0
  21. package/dist/services/context/user-context-finalizer.d.ts.map +1 -0
  22. package/dist/services/context/user-context-finalizer.js +74 -0
  23. package/dist/services/context/user-context-finalizer.mjs +72 -0
  24. package/dist/services/context/user-context-service.d.ts +0 -6
  25. package/dist/services/context/user-context-service.d.ts.map +1 -1
  26. package/dist/services/context/user-context-service.js +0 -64
  27. package/dist/services/context/user-context-service.mjs +1 -64
  28. package/package.json +7 -7
  29. package/src/app/api/user/anonymous/init/route.ts +72 -12
  30. package/src/lib/money-price-helper.ts +3 -3
  31. package/src/services/aggregate/anonymous.aggregate.service.ts +113 -0
  32. package/src/services/aggregate/index.ts +1 -0
  33. package/src/services/context/index.ts +2 -1
  34. package/src/services/context/user-context-finalizer.ts +84 -0
  35. package/src/services/context/user-context-service.ts +0 -77
@@ -0,0 +1,84 @@
1
+ import { viewLocalTime } from '@windrun-huaiin/lib/utils';
2
+ import type { XSubscription, XUser } from '@windrun-huaiin/third-ui/fingerprint';
3
+
4
+ type FinalizableUserContext = {
5
+ xUser: XUser | null;
6
+ xSubscription: XSubscription | null;
7
+ };
8
+
9
+ /**
10
+ * Output finalizer for user-context payloads.
11
+ * Real data assembly should stay in user-context-service; any optional test-only
12
+ * shaping is isolated here so production services do not depend on mock code.
13
+ */
14
+ export function finalizeUserContext<T extends FinalizableUserContext>(context: T): T {
15
+ const mockEnabled = process.env.MONEY_PRICE_MOCK_USER_ENABLED === 'true';
16
+ const mockType = Number(process.env.MONEY_PRICE_MOCK_USER_TYPE ?? NaN);
17
+
18
+ if (
19
+ !context.xUser ||
20
+ !mockEnabled ||
21
+ !Number.isInteger(mockType) ||
22
+ mockType < 0 ||
23
+ mockType > 4
24
+ ) {
25
+ return context;
26
+ }
27
+
28
+ const ensureSubscription = () => {
29
+ if (!context.xSubscription) {
30
+ const now = new Date();
31
+ context.xSubscription = {
32
+ id: BigInt(99999),
33
+ userId: context.xUser!.userId,
34
+ paySubscriptionId: 'MOCK-PAY-SUB-ID',
35
+ orderId: '',
36
+ priceId: '',
37
+ priceName: 'MOCK-TEST',
38
+ status: 'active',
39
+ creditsAllocated: 0,
40
+ subPeriodStart: viewLocalTime(now),
41
+ subPeriodEnd: viewLocalTime(now),
42
+ };
43
+ }
44
+
45
+ return context.xSubscription!;
46
+ };
47
+
48
+ switch (mockType) {
49
+ case 0: {
50
+ const subscription = ensureSubscription();
51
+ subscription.status = '';
52
+ subscription.priceId = '';
53
+ break;
54
+ }
55
+ case 1: {
56
+ const subscription = ensureSubscription();
57
+ subscription.priceId =
58
+ process.env.STRIPE_PRO_MONTHLY_PRICE_ID || subscription.priceId;
59
+ break;
60
+ }
61
+ case 2: {
62
+ const subscription = ensureSubscription();
63
+ subscription.priceId =
64
+ process.env.STRIPE_ULTRA_MONTHLY_PRICE_ID || subscription.priceId;
65
+ break;
66
+ }
67
+ case 3: {
68
+ const subscription = ensureSubscription();
69
+ subscription.priceId =
70
+ process.env.STRIPE_PRO_YEARLY_PRICE_ID || subscription.priceId;
71
+ break;
72
+ }
73
+ case 4: {
74
+ const subscription = ensureSubscription();
75
+ subscription.priceId =
76
+ process.env.STRIPE_ULTRA_YEARLY_PRICE_ID || subscription.priceId;
77
+ break;
78
+ }
79
+ default:
80
+ break;
81
+ }
82
+
83
+ return context;
84
+ }
@@ -121,80 +121,3 @@ export async function fetchLatestUserContextByFingerprintId(
121
121
  hasAnonymousUser: true,
122
122
  };
123
123
  }
124
-
125
- type MockableContext = {
126
- xUser: XUser | null;
127
- xSubscription: XSubscription | null;
128
- };
129
-
130
- export function applyUserMockContext<T extends MockableContext>(context: T): T {
131
- const mockEnabled = process.env.MONEY_PRICE_MOCK_USER_ENABLED === 'true';
132
- const mockType = Number(process.env.MONEY_PRICE_MOCK_USER_TYPE ?? NaN);
133
-
134
- if (
135
- !context.xUser ||
136
- !mockEnabled ||
137
- !Number.isInteger(mockType) ||
138
- mockType < 0 ||
139
- mockType > 4
140
- ) {
141
- return context;
142
- }
143
-
144
- const ensureSubscription = () => {
145
- if (!context.xSubscription) {
146
- const now = new Date();
147
- context.xSubscription = {
148
- id: BigInt(99999),
149
- userId: context.xUser!.userId,
150
- paySubscriptionId: 'MOCK-PAY-SUB-ID',
151
- orderId: '',
152
- priceId: '',
153
- priceName: 'MOCK-TEST',
154
- status: 'active',
155
- creditsAllocated: 0,
156
- subPeriodStart: viewLocalTime(now),
157
- subPeriodEnd: viewLocalTime(now),
158
- };
159
- }
160
-
161
- return context.xSubscription!;
162
- };
163
-
164
- switch (mockType) {
165
- case 0: {
166
- const subscription = ensureSubscription();
167
- subscription.status = '';
168
- subscription.priceId = '';
169
- break;
170
- }
171
- case 1: {
172
- const subscription = ensureSubscription();
173
- subscription.priceId =
174
- process.env.STRIPE_PRO_MONTHLY_PRICE_ID || subscription.priceId;
175
- break;
176
- }
177
- case 2: {
178
- const subscription = ensureSubscription();
179
- subscription.priceId =
180
- process.env.STRIPE_ULTRA_MONTHLY_PRICE_ID || subscription.priceId;
181
- break;
182
- }
183
- case 3: {
184
- const subscription = ensureSubscription();
185
- subscription.priceId =
186
- process.env.STRIPE_PRO_YEARLY_PRICE_ID || subscription.priceId;
187
- break;
188
- }
189
- case 4: {
190
- const subscription = ensureSubscription();
191
- subscription.priceId =
192
- process.env.STRIPE_ULTRA_YEARLY_PRICE_ID || subscription.priceId;
193
- break;
194
- }
195
- default:
196
- break;
197
- }
198
-
199
- return context;
200
- }