@windrun-huaiin/backend-core 14.2.0 → 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.
@@ -1 +1 @@
1
- {"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../../../../src/app/api/user/anonymous/init/route.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AA2rBxD;;;GAGG;AACH,wBAAsB,IAAI,CAAC,OAAO,EAAE,WAAW,kCAE9C"}
1
+ {"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../../../../src/app/api/user/anonymous/init/route.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AA4rBxD;;;GAGG;AACH,wBAAsB,IAAI,CAAC,OAAO,EAAE,WAAW,kCAE9C"}
@@ -6,6 +6,7 @@ var server = require('@windrun-huaiin/third-ui/fingerprint/server');
6
6
  var server$1 = require('@clerk/nextjs/server');
7
7
  var server$2 = require('next/server');
8
8
  var userContextService = require('../../../../../services/context/user-context-service.js');
9
+ var userContextFinalizer = require('../../../../../services/context/user-context-finalizer.js');
9
10
 
10
11
  /* eslint-disable @typescript-eslint/no-explicit-any */
11
12
  // Fix BigInt serialization issue
@@ -16,7 +17,7 @@ BigInt.prototype.toJSON = function () {
16
17
  /** 创建成功响应对象 */
17
18
  function createSuccessResponse(params) {
18
19
  const response = Object.assign({ success: true, xUser: userContextService.mapUserToXUser(params.entities.user), xCredit: params.entities.credit ? userContextService.mapCreditToXCredit(params.entities.credit) : null, xSubscription: userContextService.mapSubscriptionToXSubscription(params.entities.subscription), isNewUser: params.isNewUser }, params.options);
19
- return userContextService.applyUserMockContext(response);
20
+ return userContextFinalizer.finalizeUserContext(response);
20
21
  }
21
22
  /** 创建错误响应 */
22
23
  function createErrorResponse(message, status = 400) {
@@ -3,7 +3,8 @@ import { anonymousAggregateService } from '../../../../../services/aggregate/ano
3
3
  import { extractFingerprintFromNextRequest } from '@windrun-huaiin/third-ui/fingerprint/server';
4
4
  import { auth } from '@clerk/nextjs/server';
5
5
  import { NextResponse } from 'next/server';
6
- import { fetchUserContextByClerkUserId, fetchLatestUserContextByFingerprintId, mapSubscriptionToXSubscription, mapCreditToXCredit, mapUserToXUser, applyUserMockContext } from '../../../../../services/context/user-context-service.mjs';
6
+ import { fetchUserContextByClerkUserId, fetchLatestUserContextByFingerprintId, mapSubscriptionToXSubscription, mapCreditToXCredit, mapUserToXUser } from '../../../../../services/context/user-context-service.mjs';
7
+ import { finalizeUserContext } from '../../../../../services/context/user-context-finalizer.mjs';
7
8
 
8
9
  /* eslint-disable @typescript-eslint/no-explicit-any */
9
10
  // Fix BigInt serialization issue
@@ -14,7 +15,7 @@ BigInt.prototype.toJSON = function () {
14
15
  /** 创建成功响应对象 */
15
16
  function createSuccessResponse(params) {
16
17
  const response = Object.assign({ success: true, xUser: mapUserToXUser(params.entities.user), xCredit: params.entities.credit ? mapCreditToXCredit(params.entities.credit) : null, xSubscription: mapSubscriptionToXSubscription(params.entities.subscription), isNewUser: params.isNewUser }, params.options);
17
- return applyUserMockContext(response);
18
+ return finalizeUserContext(response);
18
19
  }
19
20
  /** 创建错误响应 */
20
21
  function createErrorResponse(message, status = 400) {
package/dist/index.js CHANGED
@@ -14,6 +14,7 @@ var user_aggregate_service = require('./services/aggregate/user.aggregate.servic
14
14
  var billing_aggregate_service = require('./services/aggregate/billing.aggregate.service.js');
15
15
  var anonymous_aggregate_service = require('./services/aggregate/anonymous.aggregate.service.js');
16
16
  var userContextService = require('./services/context/user-context-service.js');
17
+ var userContextFinalizer = require('./services/context/user-context-finalizer.js');
17
18
  var webhookHandler = require('./services/stripe/webhook-handler.js');
18
19
  var moneyPriceConfig = require('./lib/money-price-config.js');
19
20
  var moneyPriceHelper = require('./lib/money-price-helper.js');
@@ -62,13 +63,13 @@ exports.isValidUserStatus = constants.isValidUserStatus;
62
63
  exports.userAggregateService = user_aggregate_service.userAggregateService;
63
64
  exports.billingAggregateService = billing_aggregate_service.billingAggregateService;
64
65
  exports.anonymousAggregateService = anonymous_aggregate_service.anonymousAggregateService;
65
- exports.applyUserMockContext = userContextService.applyUserMockContext;
66
66
  exports.buildInitUserContextFromEntities = userContextService.buildInitUserContextFromEntities;
67
67
  exports.fetchLatestUserContextByFingerprintId = userContextService.fetchLatestUserContextByFingerprintId;
68
68
  exports.fetchUserContextByClerkUserId = userContextService.fetchUserContextByClerkUserId;
69
69
  exports.mapCreditToXCredit = userContextService.mapCreditToXCredit;
70
70
  exports.mapSubscriptionToXSubscription = userContextService.mapSubscriptionToXSubscription;
71
71
  exports.mapUserToXUser = userContextService.mapUserToXUser;
72
+ exports.finalizeUserContext = userContextFinalizer.finalizeUserContext;
72
73
  exports.handleStripeEvent = webhookHandler.handleStripeEvent;
73
74
  exports.getActiveProviderConfig = moneyPriceConfig.getActiveProviderConfig;
74
75
  exports.getCreditsFromPriceId = moneyPriceConfig.getCreditsFromPriceId;
package/dist/index.mjs CHANGED
@@ -11,7 +11,8 @@ export { BillingReason, CreditType, OperationType, OrderStatus, PaySupplier, Pay
11
11
  export { userAggregateService } from './services/aggregate/user.aggregate.service.mjs';
12
12
  export { billingAggregateService } from './services/aggregate/billing.aggregate.service.mjs';
13
13
  export { anonymousAggregateService } from './services/aggregate/anonymous.aggregate.service.mjs';
14
- export { applyUserMockContext, buildInitUserContextFromEntities, fetchLatestUserContextByFingerprintId, fetchUserContextByClerkUserId, mapCreditToXCredit, mapSubscriptionToXSubscription, mapUserToXUser } from './services/context/user-context-service.mjs';
14
+ export { buildInitUserContextFromEntities, fetchLatestUserContextByFingerprintId, fetchUserContextByClerkUserId, mapCreditToXCredit, mapSubscriptionToXSubscription, mapUserToXUser } from './services/context/user-context-service.mjs';
15
+ export { finalizeUserContext } from './services/context/user-context-finalizer.mjs';
15
16
  export { handleStripeEvent } from './services/stripe/webhook-handler.mjs';
16
17
  export { getActiveProviderConfig, getCreditsFromPriceId, getPriceConfig, moneyPriceConfig } from './lib/money-price-config.mjs';
17
18
  export { getMoneyPriceInitUserContext } from './lib/money-price-helper.mjs';
@@ -5,6 +5,7 @@ var server = require('@clerk/nextjs/server');
5
5
  var headers = require('next/headers');
6
6
  var server$1 = require('@windrun-huaiin/third-ui/fingerprint/server');
7
7
  var userContextService = require('../services/context/user-context-service.js');
8
+ var userContextFinalizer = require('../services/context/user-context-finalizer.js');
8
9
 
9
10
  function readFingerprintIdFromRequest() {
10
11
  return tslib_es6.__awaiter(this, void 0, void 0, function* () {
@@ -36,7 +37,7 @@ function getMoneyPriceInitUserContext() {
36
37
  subscription: userContext.subscription,
37
38
  isClerkAuthenticated: true,
38
39
  });
39
- return userContextService.applyUserMockContext(initUserContext);
40
+ return userContextFinalizer.finalizeUserContext(initUserContext);
40
41
  }
41
42
  const fingerprintId = yield readFingerprintIdFromRequest();
42
43
  if (!fingerprintId) {
@@ -2,7 +2,8 @@ import { __awaiter } from '../node_modules/.pnpm/@rollup_plugin-typescript@12.1.
2
2
  import { auth } from '@clerk/nextjs/server';
3
3
  import { cookies, headers } from 'next/headers';
4
4
  import { extractFingerprintFromNextStores } from '@windrun-huaiin/third-ui/fingerprint/server';
5
- import { fetchUserContextByClerkUserId, buildInitUserContextFromEntities, applyUserMockContext } from '../services/context/user-context-service.mjs';
5
+ import { fetchUserContextByClerkUserId, buildInitUserContextFromEntities } from '../services/context/user-context-service.mjs';
6
+ import { finalizeUserContext } from '../services/context/user-context-finalizer.mjs';
6
7
 
7
8
  function readFingerprintIdFromRequest() {
8
9
  return __awaiter(this, void 0, void 0, function* () {
@@ -34,7 +35,7 @@ function getMoneyPriceInitUserContext() {
34
35
  subscription: userContext.subscription,
35
36
  isClerkAuthenticated: true,
36
37
  });
37
- return applyUserMockContext(initUserContext);
38
+ return finalizeUserContext(initUserContext);
38
39
  }
39
40
  const fingerprintId = yield readFingerprintIdFromRequest();
40
41
  if (!fingerprintId) {
@@ -1,2 +1,3 @@
1
1
  export * from './user-context-service';
2
+ export * from './user-context-finalizer';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/context/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/context/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC"}
@@ -1,13 +1,14 @@
1
1
  'use strict';
2
2
 
3
3
  var userContextService = require('./user-context-service.js');
4
+ var userContextFinalizer = require('./user-context-finalizer.js');
4
5
 
5
6
 
6
7
 
7
- exports.applyUserMockContext = userContextService.applyUserMockContext;
8
8
  exports.buildInitUserContextFromEntities = userContextService.buildInitUserContextFromEntities;
9
9
  exports.fetchLatestUserContextByFingerprintId = userContextService.fetchLatestUserContextByFingerprintId;
10
10
  exports.fetchUserContextByClerkUserId = userContextService.fetchUserContextByClerkUserId;
11
11
  exports.mapCreditToXCredit = userContextService.mapCreditToXCredit;
12
12
  exports.mapSubscriptionToXSubscription = userContextService.mapSubscriptionToXSubscription;
13
13
  exports.mapUserToXUser = userContextService.mapUserToXUser;
14
+ exports.finalizeUserContext = userContextFinalizer.finalizeUserContext;
@@ -1 +1,2 @@
1
- export { applyUserMockContext, buildInitUserContextFromEntities, fetchLatestUserContextByFingerprintId, fetchUserContextByClerkUserId, mapCreditToXCredit, mapSubscriptionToXSubscription, mapUserToXUser } from './user-context-service.mjs';
1
+ export { buildInitUserContextFromEntities, fetchLatestUserContextByFingerprintId, fetchUserContextByClerkUserId, mapCreditToXCredit, mapSubscriptionToXSubscription, mapUserToXUser } from './user-context-service.mjs';
2
+ export { finalizeUserContext } from './user-context-finalizer.mjs';
@@ -0,0 +1,13 @@
1
+ import type { XSubscription, XUser } from '@windrun-huaiin/third-ui/fingerprint';
2
+ type FinalizableUserContext = {
3
+ xUser: XUser | null;
4
+ xSubscription: XSubscription | null;
5
+ };
6
+ /**
7
+ * Output finalizer for user-context payloads.
8
+ * Real data assembly should stay in user-context-service; any optional test-only
9
+ * shaping is isolated here so production services do not depend on mock code.
10
+ */
11
+ export declare function finalizeUserContext<T extends FinalizableUserContext>(context: T): T;
12
+ export {};
13
+ //# sourceMappingURL=user-context-finalizer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-context-finalizer.d.ts","sourceRoot":"","sources":["../../../src/services/context/user-context-finalizer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,sCAAsC,CAAC;AAEjF,KAAK,sBAAsB,GAAG;IAC5B,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,aAAa,EAAE,aAAa,GAAG,IAAI,CAAC;CACrC,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,sBAAsB,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,CAsEnF"}
@@ -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;AAED,KAAK,eAAe,GAAG;IACrB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,aAAa,EAAE,aAAa,GAAG,IAAI,CAAC;CACrC,CAAC;AAEF,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,eAAe,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,CAsE7E"}
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 { applyUserMockContext, buildInitUserContextFromEntities, fetchLatestUserContextByFingerprintId, fetchUserContextByClerkUserId, mapCreditToXCredit, mapSubscriptionToXSubscription, mapUserToXUser };
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.2.0",
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.17.1",
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.17.1",
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.0",
98
- "@windrun-huaiin/third-ui": "^14.1.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.17.1",
111
+ "@prisma/client": "^6.19.0",
112
112
  "next": "16.1.6",
113
- "prisma": "^6.17.1",
113
+ "prisma": "^6.19.0",
114
114
  "stripe": "20.0.0",
115
115
  "svix": "^1.86.0"
116
116
  },
@@ -11,7 +11,6 @@ import { extractFingerprintFromNextRequest } from '@windrun-huaiin/third-ui/fing
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 applyUserMockContext(response);
64
+ return finalizeUserContext(response);
64
65
  }
65
66
 
66
67
  /** 创建错误响应 */
@@ -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 applyUserMockContext(initUserContext);
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
+ }
@@ -1 +1,2 @@
1
- export * from './user-context-service';
1
+ export * from './user-context-service';
2
+ export * from './user-context-finalizer';
@@ -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
- }