@pisell/pisellos 2.1.173 → 2.1.174
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/model/strategy/adapter/promotion/index.js +9 -0
- package/dist/model/strategy/adapter/walletPass/evaluator.d.ts +2 -0
- package/dist/model/strategy/adapter/walletPass/evaluator.js +4 -2
- package/dist/modules/Discount/availability.d.ts +7 -0
- package/dist/modules/Discount/availability.js +52 -0
- package/dist/modules/Discount/entitlementPass.d.ts +34 -0
- package/dist/modules/Discount/entitlementPass.js +147 -0
- package/dist/modules/Discount/entitlementState.d.ts +8 -0
- package/dist/modules/Discount/entitlementState.js +205 -0
- package/dist/modules/Discount/index.d.ts +6 -3
- package/dist/modules/Discount/index.js +125 -23
- package/dist/modules/Discount/types.d.ts +34 -5
- package/dist/modules/Holder/index.d.ts +48 -0
- package/dist/modules/Holder/index.js +640 -0
- package/dist/modules/Holder/types.d.ts +123 -0
- package/dist/modules/Holder/types.js +1 -0
- package/dist/modules/Holder/utils.d.ts +13 -0
- package/dist/modules/Holder/utils.js +34 -0
- package/dist/modules/Order/index.d.ts +1 -0
- package/dist/modules/Order/index.js +18 -1
- package/dist/modules/Order/types.d.ts +2 -0
- package/dist/modules/Order/utils.d.ts +10 -0
- package/dist/modules/Order/utils.js +35 -3
- package/dist/modules/Product/types.d.ts +7 -0
- package/dist/modules/Rules/entitlementLines.d.ts +8 -0
- package/dist/modules/Rules/entitlementLines.js +157 -0
- package/dist/modules/Rules/index.d.ts +12 -2
- package/dist/modules/Rules/index.js +377 -112
- package/dist/modules/Rules/types.d.ts +2 -2
- package/dist/modules/index.d.ts +1 -0
- package/dist/modules/index.js +2 -1
- package/dist/plugins/window.d.ts +3 -0
- package/dist/solution/BookingByStep/index.d.ts +17 -2
- package/dist/solution/BookingByStep/index.js +276 -204
- package/dist/solution/BookingByStep/types.d.ts +2 -1
- package/dist/solution/BookingByStep/types.js +3 -1
- package/dist/solution/ScanOrder/index.js +1 -1
- package/dist/solution/ScanOrder/types.d.ts +1 -0
- package/dist/solution/ShopDiscount/index.d.ts +3 -0
- package/dist/solution/ShopDiscount/index.js +161 -78
- package/dist/solution/ShopDiscount/utils.d.ts +1 -0
- package/dist/solution/ShopDiscount/utils.js +40 -1
- package/dist/solution/VenueBooking/index.d.ts +19 -0
- package/dist/solution/VenueBooking/index.js +978 -748
- package/dist/solution/VenueBooking/types.d.ts +2 -0
- package/dist/types/index.d.ts +1 -0
- package/lib/model/strategy/adapter/promotion/index.js +49 -0
- package/lib/model/strategy/adapter/walletPass/evaluator.d.ts +2 -0
- package/lib/model/strategy/adapter/walletPass/evaluator.js +4 -2
- package/lib/modules/Discount/availability.d.ts +7 -0
- package/lib/modules/Discount/availability.js +91 -0
- package/lib/modules/Discount/entitlementPass.d.ts +34 -0
- package/lib/modules/Discount/entitlementPass.js +184 -0
- package/lib/modules/Discount/entitlementState.d.ts +8 -0
- package/lib/modules/Discount/entitlementState.js +206 -0
- package/lib/modules/Discount/index.d.ts +6 -3
- package/lib/modules/Discount/index.js +66 -16
- package/lib/modules/Discount/types.d.ts +34 -5
- package/lib/modules/Holder/index.d.ts +48 -0
- package/lib/modules/Holder/index.js +402 -0
- package/lib/modules/Holder/types.d.ts +123 -0
- package/lib/modules/Holder/types.js +17 -0
- package/lib/modules/Holder/utils.d.ts +13 -0
- package/lib/modules/Holder/utils.js +71 -0
- package/lib/modules/Order/index.d.ts +1 -0
- package/lib/modules/Order/index.js +14 -0
- package/lib/modules/Order/types.d.ts +2 -0
- package/lib/modules/Order/utils.d.ts +10 -0
- package/lib/modules/Order/utils.js +36 -5
- package/lib/modules/Product/types.d.ts +7 -0
- package/lib/modules/Rules/entitlementLines.d.ts +8 -0
- package/lib/modules/Rules/entitlementLines.js +158 -0
- package/lib/modules/Rules/index.d.ts +12 -2
- package/lib/modules/Rules/index.js +304 -70
- package/lib/modules/Rules/types.d.ts +2 -2
- package/lib/modules/index.d.ts +1 -0
- package/lib/modules/index.js +3 -1
- package/lib/plugins/window.d.ts +3 -0
- package/lib/solution/BookingByStep/index.d.ts +17 -2
- package/lib/solution/BookingByStep/index.js +39 -0
- package/lib/solution/BookingByStep/types.d.ts +2 -1
- package/lib/solution/BookingByStep/types.js +5 -0
- package/lib/solution/ScanOrder/index.js +1 -1
- package/lib/solution/ScanOrder/types.d.ts +1 -0
- package/lib/solution/ShopDiscount/index.d.ts +3 -0
- package/lib/solution/ShopDiscount/index.js +78 -28
- package/lib/solution/ShopDiscount/utils.d.ts +1 -0
- package/lib/solution/ShopDiscount/utils.js +42 -1
- package/lib/solution/VenueBooking/index.d.ts +19 -0
- package/lib/solution/VenueBooking/index.js +134 -16
- package/lib/solution/VenueBooking/types.d.ts +2 -0
- package/lib/types/index.d.ts +1 -0
- package/package.json +1 -1
|
@@ -36,6 +36,8 @@ var import_utils = require("../../solution/ShopDiscount/utils");
|
|
|
36
36
|
var import_BaseModule = require("../BaseModule");
|
|
37
37
|
var import_types = require("./types");
|
|
38
38
|
var import_decimal = __toESM(require("decimal.js"));
|
|
39
|
+
var import_entitlementPass = require("./entitlementPass");
|
|
40
|
+
var import_availability = require("./availability");
|
|
39
41
|
var DiscountModule = class extends import_BaseModule.BaseModule {
|
|
40
42
|
constructor(name, version) {
|
|
41
43
|
super(name, version);
|
|
@@ -92,23 +94,34 @@ var DiscountModule = class extends import_BaseModule.BaseModule {
|
|
|
92
94
|
return this.store.originalDiscountList || [];
|
|
93
95
|
}
|
|
94
96
|
async loadPrepareConfig(params) {
|
|
95
|
-
var _a, _b;
|
|
97
|
+
var _a, _b, _c, _d, _e;
|
|
96
98
|
const prepareConfig = await this.request.post(
|
|
97
99
|
`/order/prepare/config`,
|
|
98
100
|
params
|
|
99
101
|
);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
102
|
+
if (!params.with_entitlement_pass) {
|
|
103
|
+
return this.filterEnabledDiscountList([
|
|
104
|
+
...((_a = prepareConfig == null ? void 0 : prepareConfig.data) == null ? void 0 : _a.good_pass_list) || [],
|
|
105
|
+
...((_b = prepareConfig == null ? void 0 : prepareConfig.data) == null ? void 0 : _b.discount_card_list) || []
|
|
106
|
+
].filter((item) => !(0, import_entitlementPass.isEntitlementPass)(item)));
|
|
107
|
+
}
|
|
108
|
+
const entitlementPassList = ((_c = prepareConfig == null ? void 0 : prepareConfig.data) == null ? void 0 : _c.entitlement_pass_list) || [];
|
|
109
|
+
const ids = new Set(entitlementPassList.map((item) => String(item.id)));
|
|
110
|
+
const rawDiscountList = [
|
|
111
|
+
...entitlementPassList,
|
|
112
|
+
...(((_d = prepareConfig == null ? void 0 : prepareConfig.data) == null ? void 0 : _d.good_pass_list) || []).filter((item) => !ids.has(String(item.id))),
|
|
113
|
+
...(((_e = prepareConfig == null ? void 0 : prepareConfig.data) == null ? void 0 : _e.discount_card_list) || []).filter((item) => !ids.has(String(item.id)))
|
|
114
|
+
];
|
|
115
|
+
await this.setOriginalDiscountList(rawDiscountList);
|
|
116
|
+
const goodPassList = this.filterEnabledDiscountList(rawDiscountList);
|
|
103
117
|
return goodPassList;
|
|
104
118
|
}
|
|
105
|
-
async batchSearch(code,
|
|
106
|
-
|
|
107
|
-
const
|
|
119
|
+
async batchSearch(code, customerIdOrOptions) {
|
|
120
|
+
var _a, _b;
|
|
121
|
+
const { customerId, orderId, withEntitlementPass } = typeof customerIdOrOptions === "number" ? { customerId: customerIdOrOptions, orderId: void 0, withEntitlementPass: false } : customerIdOrOptions || {};
|
|
122
|
+
const params = {
|
|
108
123
|
code,
|
|
109
124
|
translate_flag: 1,
|
|
110
|
-
tags: ["good_pass", "product_discount_card"],
|
|
111
|
-
// available: 1,
|
|
112
125
|
relation_product: 1,
|
|
113
126
|
with: ["extensionData", "customScheduleSnapshot", "holder.detail"],
|
|
114
127
|
order_behavior_count: 1,
|
|
@@ -116,9 +129,45 @@ var DiscountModule = class extends import_BaseModule.BaseModule {
|
|
|
116
129
|
...orderId ? { order_behavior_count_order_id: orderId } : {},
|
|
117
130
|
unified_status_info_flag: 1,
|
|
118
131
|
request_timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
132
|
+
};
|
|
133
|
+
if (!withEntitlementPass) {
|
|
134
|
+
const result2 = await this.request.get(`/machinecode/batch-search`, {
|
|
135
|
+
...params,
|
|
136
|
+
tags: ["good_pass", "product_discount_card"]
|
|
137
|
+
});
|
|
138
|
+
return this.filterEnabledDiscountListWithReason(((result2 == null ? void 0 : result2.data) || []).filter((item) => !(0, import_entitlementPass.isEntitlementPass)(item)));
|
|
139
|
+
}
|
|
140
|
+
const search = (query) => this.request.get(`/machinecode/batch-search`, query).then(
|
|
141
|
+
(value) => ({ status: "fulfilled", value }),
|
|
142
|
+
(reason) => ({ status: "rejected", reason })
|
|
143
|
+
);
|
|
144
|
+
const [legacy, entitlement] = await Promise.all([
|
|
145
|
+
search({
|
|
146
|
+
...params,
|
|
147
|
+
tags: ["good_pass", "product_discount_card"]
|
|
148
|
+
}),
|
|
149
|
+
search({
|
|
150
|
+
...params,
|
|
151
|
+
tags: ["entitlement_pass"],
|
|
152
|
+
with_franchisor_data: 1,
|
|
153
|
+
unified_status_info_flag: 1,
|
|
154
|
+
with: [...params.with, "franchiseeWalletPass:shop_id,source_shop_id,product_id,use"]
|
|
155
|
+
})
|
|
156
|
+
]);
|
|
157
|
+
const entitlementList = (entitlement.status === "fulfilled" ? ((_a = entitlement.value) == null ? void 0 : _a.data) || [] : []).filter(import_entitlementPass.isEntitlementPass);
|
|
158
|
+
const legacyList = legacy.status === "fulfilled" ? ((_b = legacy.value) == null ? void 0 : _b.data) || [] : [];
|
|
159
|
+
const ids = new Set(entitlementList.map((item) => String(item.id)));
|
|
160
|
+
const result = this.filterEnabledDiscountListWithReason([
|
|
161
|
+
...entitlementList,
|
|
162
|
+
...legacyList.filter((item) => !(0, import_entitlementPass.isEntitlementPass)(item) && !ids.has(String(item.id)))
|
|
163
|
+
]);
|
|
164
|
+
if (!result.discountList.length) {
|
|
165
|
+
if (legacy.status === "rejected")
|
|
166
|
+
throw legacy.reason;
|
|
167
|
+
if (entitlement.status === "rejected")
|
|
168
|
+
throw entitlement.reason;
|
|
169
|
+
}
|
|
170
|
+
return result;
|
|
122
171
|
}
|
|
123
172
|
async batchSearchByProductIds(productIds) {
|
|
124
173
|
const result = await this.request.get(`/machinecode/batch-search`, {
|
|
@@ -131,7 +180,7 @@ var DiscountModule = class extends import_BaseModule.BaseModule {
|
|
|
131
180
|
}
|
|
132
181
|
filterEnabledDiscountList(discountList) {
|
|
133
182
|
return discountList.filter(
|
|
134
|
-
(discount) => discount.limit_status === "enable" && new import_decimal.default((discount == null ? void 0 : discount.par_value) || 0).minus(new import_decimal.default((discount == null ? void 0 : discount.used_par_value) || 0)).greaterThan(0) && this.checkUsageCreditsLimit(discount)
|
|
183
|
+
(discount) => (0, import_entitlementPass.isEntitlementPass)(discount) ? !(0, import_availability.getDiscountLifecycleReason)(discount) : discount.limit_status === "enable" && new import_decimal.default((discount == null ? void 0 : discount.par_value) || 0).minus(new import_decimal.default((discount == null ? void 0 : discount.used_par_value) || 0)).greaterThan(0) && this.checkUsageCreditsLimit(discount)
|
|
135
184
|
);
|
|
136
185
|
}
|
|
137
186
|
// 检查使用次数限制
|
|
@@ -175,7 +224,8 @@ var DiscountModule = class extends import_BaseModule.BaseModule {
|
|
|
175
224
|
}
|
|
176
225
|
// 过滤启用且可用的优惠券,并返回校验失败原因
|
|
177
226
|
filterEnabledDiscountListWithReason(discountList) {
|
|
178
|
-
|
|
227
|
+
const entitlementPasses = discountList.filter((item) => (0, import_entitlementPass.isEntitlementPass)(item) && !(0, import_availability.getDiscountLifecycleReason)(item));
|
|
228
|
+
let lists = discountList.filter((item) => !(0, import_entitlementPass.isEntitlementPass)(item)).filter((discount) => discount.limit_status === "enable").filter((discount) => {
|
|
179
229
|
const remaining = new import_decimal.default((discount == null ? void 0 : discount.par_value) || 0).minus(new import_decimal.default((discount == null ? void 0 : discount.used_par_value) || 0)).greaterThan(0);
|
|
180
230
|
return remaining;
|
|
181
231
|
}).map((discount) => {
|
|
@@ -188,8 +238,8 @@ var DiscountModule = class extends import_BaseModule.BaseModule {
|
|
|
188
238
|
const availableLists = lists.filter((item) => !item.usageLimitFailKey && item.unified_available_status !== 0);
|
|
189
239
|
const currentFailDiscount = lists.find((item) => item.usageLimitFailKey);
|
|
190
240
|
return {
|
|
191
|
-
discountList: availableLists,
|
|
192
|
-
unavailableReasonKey: currentFailDiscount == null ? void 0 : currentFailDiscount.usageLimitFailKey
|
|
241
|
+
discountList: [...entitlementPasses.map((item) => ({ ...item, usageLimitFailKey: (0, import_availability.getDiscountUsageLimitFailKey)(item) })), ...availableLists],
|
|
242
|
+
unavailableReasonKey: (currentFailDiscount == null ? void 0 : currentFailDiscount.usageLimitFailKey) || discountList.filter(import_entitlementPass.isEntitlementPass).map(import_availability.getDiscountLifecycleReason).find(Boolean) || null
|
|
193
243
|
};
|
|
194
244
|
}
|
|
195
245
|
// 检查使用次数限制,并返回校验失败原因
|
|
@@ -45,6 +45,8 @@ interface ApplicableProductDetails {
|
|
|
45
45
|
num: number;
|
|
46
46
|
metadata?: {
|
|
47
47
|
product_discount_difference?: number;
|
|
48
|
+
num?: number;
|
|
49
|
+
custom_product_bundle_map_id?: string;
|
|
48
50
|
};
|
|
49
51
|
discount?: {
|
|
50
52
|
fixed_amount?: number;
|
|
@@ -63,10 +65,17 @@ interface UsageCreditsValue {
|
|
|
63
65
|
max_per_week: number;
|
|
64
66
|
max_per_month: number;
|
|
65
67
|
}
|
|
68
|
+
export type IncludedUses = {
|
|
69
|
+
type: 'limited';
|
|
70
|
+
value: number;
|
|
71
|
+
} | {
|
|
72
|
+
type: 'unlimited';
|
|
73
|
+
value: null;
|
|
74
|
+
};
|
|
66
75
|
export type DiscountFilterRejectKey = 'total_credits' | 'per_user_limit' | 'max_per_day' | 'max_per_week' | 'max_per_month';
|
|
67
76
|
export interface DiscountWithReason {
|
|
68
77
|
discountList: Discount[];
|
|
69
|
-
unavailableReasonKey: DiscountFilterRejectKey | null;
|
|
78
|
+
unavailableReasonKey: DiscountFilterRejectKey | string | null;
|
|
70
79
|
}
|
|
71
80
|
interface ExtensionData {
|
|
72
81
|
id: number;
|
|
@@ -93,19 +102,39 @@ export interface Discount {
|
|
|
93
102
|
used_par_value: string;
|
|
94
103
|
limit_status: string;
|
|
95
104
|
limited_relation_product_data: Limitedrelationproductdata;
|
|
96
|
-
balance
|
|
105
|
+
balance?: string | number | null;
|
|
97
106
|
format_title: Formattitle;
|
|
98
107
|
metadata?: {
|
|
99
108
|
discount_card_type?: 'fixed_amount' | 'percent';
|
|
100
109
|
custom_product_bundle_map_id?: string;
|
|
101
110
|
validity_type?: "custom_schedule_validity" | "fixed_validity";
|
|
102
|
-
|
|
103
|
-
|
|
111
|
+
included_uses?: IncludedUses;
|
|
112
|
+
discount_calculation_mode?: 'item_level' | 'order_level';
|
|
113
|
+
holder?: {
|
|
104
114
|
type: "customer" | "custom";
|
|
105
115
|
};
|
|
106
116
|
};
|
|
107
117
|
product: Product;
|
|
108
|
-
type
|
|
118
|
+
type?: 'product' | 'good_pass' | 'entitlement_pass' | 'discount_card' | 'product_discount_card';
|
|
119
|
+
shop_id?: number;
|
|
120
|
+
customer_id?: number | string | null;
|
|
121
|
+
holder?: {
|
|
122
|
+
holder_type?: string;
|
|
123
|
+
holder_id?: number | string | Array<number | string>;
|
|
124
|
+
[key: string]: any;
|
|
125
|
+
};
|
|
126
|
+
config?: {
|
|
127
|
+
isAvailable?: boolean;
|
|
128
|
+
maxUsagePerOrder?: number;
|
|
129
|
+
[key: string]: any;
|
|
130
|
+
};
|
|
131
|
+
reason?: string;
|
|
132
|
+
reasonCode?: string;
|
|
133
|
+
is_franchisor_pass?: number;
|
|
134
|
+
franchisee_wallet_pass?: {
|
|
135
|
+
use?: string;
|
|
136
|
+
};
|
|
137
|
+
committedAppliedProductDetails?: ApplicableProductDetails[];
|
|
109
138
|
resource_id?: number;
|
|
110
139
|
savedAmount?: number;
|
|
111
140
|
isEditMode?: boolean;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Module, PisellCore, ModuleOptions } from '../../types';
|
|
2
|
+
import { BaseModule } from '../BaseModule';
|
|
3
|
+
import { IFormInfo, IFormRecord, IFormItemData, IFetchFormInfoParams, IFetchFormRecordsParams, IAddFormRecordParams, IAppendFormRecordParams, IEnsureFormDataParams, IEnsureFormByProductParams, IRefreshAllFormRecordsParams, HolderModuleAPI, HolderFormMap } from './types';
|
|
4
|
+
export * from './types';
|
|
5
|
+
export * from './utils';
|
|
6
|
+
export declare class HolderModule extends BaseModule implements Module, HolderModuleAPI {
|
|
7
|
+
protected defaultName: string;
|
|
8
|
+
protected defaultVersion: string;
|
|
9
|
+
private store;
|
|
10
|
+
private request;
|
|
11
|
+
private cacheId;
|
|
12
|
+
private openCache;
|
|
13
|
+
private fatherModule;
|
|
14
|
+
private isLoading;
|
|
15
|
+
private error;
|
|
16
|
+
/** 已加载过表单数据的 form_id 缓存标记 */
|
|
17
|
+
private loadedFormIds;
|
|
18
|
+
private window;
|
|
19
|
+
constructor(name?: string, version?: string);
|
|
20
|
+
initialize(core: PisellCore, options: ModuleOptions): Promise<void>;
|
|
21
|
+
/** 统一提交 formMap,仅此处触发 store changed */
|
|
22
|
+
private commitFormMap;
|
|
23
|
+
/** 通知 UI 刷新(数据未变但需要 re-read 的场景) */
|
|
24
|
+
private notifyFormMapChanged;
|
|
25
|
+
getHolderFormMap(): HolderFormMap;
|
|
26
|
+
getHolderFormData(formId: number | string): IFormItemData | undefined;
|
|
27
|
+
setFormData(formId: number | string, data: Partial<IFormItemData>): void;
|
|
28
|
+
private ensureFormItem;
|
|
29
|
+
private hasFormData;
|
|
30
|
+
private patchFormMap;
|
|
31
|
+
fetchFormInfo(params: IFetchFormInfoParams): Promise<IFormInfo | IFormInfo[]>;
|
|
32
|
+
fetchRecords(params: IFetchFormRecordsParams): Promise<any>;
|
|
33
|
+
ensureFormData(params: IEnsureFormDataParams): Promise<IFormItemData>;
|
|
34
|
+
ensureFormByProduct(params: IEnsureFormByProductParams): Promise<IFormItemData | null>;
|
|
35
|
+
addFormRecord(params: IAddFormRecordParams): Promise<any>;
|
|
36
|
+
/**
|
|
37
|
+
* UI 层自行请求添加接口后,将返回记录合并到 holderMap 对应 form_id 的 records 中
|
|
38
|
+
*/
|
|
39
|
+
appendFormRecord(params: IAppendFormRecordParams): IFormRecord;
|
|
40
|
+
removeFormRecord(formId: number | string, formRecordId: number): void;
|
|
41
|
+
clearFormData(formId?: number | string): void;
|
|
42
|
+
/**
|
|
43
|
+
* 登录后按当前 holderMap 批量刷新所有表单的 records。
|
|
44
|
+
* 遍历 holderMap,从 form.form 中读取 form_id、shop_id,结合 customer_id 重新拉取记录。
|
|
45
|
+
*/
|
|
46
|
+
refreshAllFormRecords(params?: IRefreshAllFormRecordsParams): Promise<HolderFormMap>;
|
|
47
|
+
storeChange(path?: string): void;
|
|
48
|
+
}
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/modules/Holder/index.ts
|
|
21
|
+
var Holder_exports = {};
|
|
22
|
+
__export(Holder_exports, {
|
|
23
|
+
HolderModule: () => HolderModule
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(Holder_exports);
|
|
26
|
+
var import_lodash_es = require("lodash-es");
|
|
27
|
+
var import_BaseModule = require("../BaseModule");
|
|
28
|
+
var import_utils = require("./utils");
|
|
29
|
+
__reExport(Holder_exports, require("./types"), module.exports);
|
|
30
|
+
__reExport(Holder_exports, require("./utils"), module.exports);
|
|
31
|
+
var HolderModule = class extends import_BaseModule.BaseModule {
|
|
32
|
+
constructor(name, version) {
|
|
33
|
+
super(name, version);
|
|
34
|
+
this.defaultName = "holder";
|
|
35
|
+
this.defaultVersion = "1.0.0";
|
|
36
|
+
this.openCache = false;
|
|
37
|
+
this.isLoading = false;
|
|
38
|
+
this.error = null;
|
|
39
|
+
/** 已加载过表单数据的 form_id 缓存标记 */
|
|
40
|
+
this.loadedFormIds = /* @__PURE__ */ new Set();
|
|
41
|
+
}
|
|
42
|
+
async initialize(core, options) {
|
|
43
|
+
var _a, _b, _c;
|
|
44
|
+
this.core = core;
|
|
45
|
+
this.store = options.store;
|
|
46
|
+
this.request = this.core.getPlugin("request");
|
|
47
|
+
this.window = this.core.getPlugin("window");
|
|
48
|
+
if ((_a = options.initialState) == null ? void 0 : _a.holderMap) {
|
|
49
|
+
this.commitFormMap((0, import_lodash_es.cloneDeep)(options.initialState.holderMap));
|
|
50
|
+
Object.keys(this.store.holderMap).forEach((formId) => {
|
|
51
|
+
if (this.hasFormData(formId)) {
|
|
52
|
+
this.loadedFormIds.add(formId);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
if (!this.store.holderMap || typeof this.store.holderMap !== "object") {
|
|
57
|
+
this.store.holderMap = {};
|
|
58
|
+
}
|
|
59
|
+
if ((_b = options.otherParams) == null ? void 0 : _b.cacheId) {
|
|
60
|
+
this.openCache = options.otherParams.openCache;
|
|
61
|
+
this.cacheId = options.otherParams.cacheId;
|
|
62
|
+
}
|
|
63
|
+
if ((_c = options.otherParams) == null ? void 0 : _c.fatherModule) {
|
|
64
|
+
this.fatherModule = options.otherParams.fatherModule;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** 统一提交 formMap,仅此处触发 store changed */
|
|
68
|
+
commitFormMap(holderMap) {
|
|
69
|
+
this.store.holderMap = holderMap;
|
|
70
|
+
}
|
|
71
|
+
/** 通知 UI 刷新(数据未变但需要 re-read 的场景) */
|
|
72
|
+
notifyFormMapChanged() {
|
|
73
|
+
this.commitFormMap({ ...this.store.holderMap });
|
|
74
|
+
}
|
|
75
|
+
getHolderFormMap() {
|
|
76
|
+
return (0, import_lodash_es.cloneDeep)(this.store.holderMap);
|
|
77
|
+
}
|
|
78
|
+
getHolderFormData(formId) {
|
|
79
|
+
const formData = this.store.holderMap[(0, import_utils.normalizeFormId)(formId)];
|
|
80
|
+
return formData ? (0, import_lodash_es.cloneDeep)(formData) : void 0;
|
|
81
|
+
}
|
|
82
|
+
setFormData(formId, data) {
|
|
83
|
+
const key = (0, import_utils.normalizeFormId)(formId);
|
|
84
|
+
const current = this.store.holderMap[key] || (0, import_utils.createEmptyFormItemData)();
|
|
85
|
+
this.commitFormMap({
|
|
86
|
+
...this.store.holderMap,
|
|
87
|
+
[key]: {
|
|
88
|
+
records: data.records ?? current.records,
|
|
89
|
+
form: data.form ?? current.form
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
ensureFormItem(formId, holderMap = this.store.holderMap) {
|
|
94
|
+
const key = (0, import_utils.normalizeFormId)(formId);
|
|
95
|
+
if (!holderMap[key]) {
|
|
96
|
+
holderMap[key] = (0, import_utils.createEmptyFormItemData)();
|
|
97
|
+
}
|
|
98
|
+
return holderMap[key];
|
|
99
|
+
}
|
|
100
|
+
hasFormData(formId) {
|
|
101
|
+
const item = this.store.holderMap[formId];
|
|
102
|
+
return !!item && (Object.keys(item.form || {}).length > 0 || item.records.length > 0);
|
|
103
|
+
}
|
|
104
|
+
patchFormMap(holderMap, formId, patch) {
|
|
105
|
+
const current = this.ensureFormItem(formId, holderMap);
|
|
106
|
+
holderMap[formId] = {
|
|
107
|
+
records: patch.records ?? current.records,
|
|
108
|
+
form: patch.form ?? current.form
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
async fetchFormInfo(params) {
|
|
112
|
+
const { form_id } = params.query || {};
|
|
113
|
+
const url = params.url || `/form/${form_id}`;
|
|
114
|
+
const useCache = params.useCache !== false;
|
|
115
|
+
this.isLoading = true;
|
|
116
|
+
this.error = null;
|
|
117
|
+
try {
|
|
118
|
+
const res = await this.request.get(url, {}, { useCache });
|
|
119
|
+
const formData = res == null ? void 0 : res.data;
|
|
120
|
+
const formList = Array.isArray(formData == null ? void 0 : formData.list) ? formData.list : formData ? [formData] : [];
|
|
121
|
+
const nextFormMap = { ...this.store.holderMap };
|
|
122
|
+
formList.forEach((form) => {
|
|
123
|
+
const key = (0, import_utils.normalizeFormId)(form.form_id ?? form_id ?? form.id);
|
|
124
|
+
this.patchFormMap(nextFormMap, key, { form });
|
|
125
|
+
});
|
|
126
|
+
if (!formList.length && form_id !== void 0) {
|
|
127
|
+
const key = (0, import_utils.normalizeFormId)(form_id);
|
|
128
|
+
this.patchFormMap(nextFormMap, key, { form: formData || {} });
|
|
129
|
+
}
|
|
130
|
+
this.commitFormMap(nextFormMap);
|
|
131
|
+
return formList.length === 1 ? formList[0] : formList;
|
|
132
|
+
} catch (error) {
|
|
133
|
+
this.error = error instanceof Error ? error.message : String(error);
|
|
134
|
+
throw error;
|
|
135
|
+
} finally {
|
|
136
|
+
this.isLoading = false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
async fetchRecords(params) {
|
|
140
|
+
var _a, _b, _c;
|
|
141
|
+
const url = params.url || "/form/data/v1";
|
|
142
|
+
const { form_id, shop_id, num, skip, customer_id, ...rest } = params.query || {};
|
|
143
|
+
const formId = (0, import_utils.normalizeFormId)(form_id);
|
|
144
|
+
const useCache = params.useCache !== false;
|
|
145
|
+
const customer = JSON.parse(((_b = (_a = this.window) == null ? void 0 : _a.getLocalStorage) == null ? void 0 : _b.call(_a, "customer")) || "{}");
|
|
146
|
+
const customerId = customer_id ?? customer.id;
|
|
147
|
+
if (!customerId)
|
|
148
|
+
return [];
|
|
149
|
+
this.isLoading = true;
|
|
150
|
+
this.error = null;
|
|
151
|
+
try {
|
|
152
|
+
const res = await this.request.get(
|
|
153
|
+
url,
|
|
154
|
+
{
|
|
155
|
+
customer_id: customerId,
|
|
156
|
+
form_id,
|
|
157
|
+
shop_id,
|
|
158
|
+
num: num || 100,
|
|
159
|
+
skip: skip || 1,
|
|
160
|
+
front_end_cache_id: useCache && this.cacheId,
|
|
161
|
+
...rest
|
|
162
|
+
},
|
|
163
|
+
{ useCache }
|
|
164
|
+
);
|
|
165
|
+
const records = ((_c = res == null ? void 0 : res.data) == null ? void 0 : _c.list) || [];
|
|
166
|
+
const nextFormMap = { ...this.store.holderMap };
|
|
167
|
+
this.patchFormMap(nextFormMap, formId, { records });
|
|
168
|
+
this.commitFormMap(nextFormMap);
|
|
169
|
+
return records;
|
|
170
|
+
} catch (error) {
|
|
171
|
+
this.error = error instanceof Error ? error.message : String(error);
|
|
172
|
+
throw error;
|
|
173
|
+
} finally {
|
|
174
|
+
this.isLoading = false;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async ensureFormData(params) {
|
|
178
|
+
const formId = (0, import_utils.normalizeFormId)(params.form_id);
|
|
179
|
+
const useCache = params.useCache !== false;
|
|
180
|
+
if (useCache && !params.forceRefresh && this.loadedFormIds.has(formId)) {
|
|
181
|
+
this.notifyFormMapChanged();
|
|
182
|
+
return this.ensureFormItem(formId);
|
|
183
|
+
}
|
|
184
|
+
const current = this.ensureFormItem(formId);
|
|
185
|
+
const tasks = [];
|
|
186
|
+
if (params.forceRefresh || !Object.keys(current.form || {}).length) {
|
|
187
|
+
tasks.push(
|
|
188
|
+
this.fetchFormInfo({
|
|
189
|
+
url: params.formInfoUrl,
|
|
190
|
+
query: {
|
|
191
|
+
form_id: params.form_id
|
|
192
|
+
},
|
|
193
|
+
useCache
|
|
194
|
+
})
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
if (params.forceRefresh || !current.records.length) {
|
|
198
|
+
tasks.push(
|
|
199
|
+
this.fetchRecords({
|
|
200
|
+
url: params.recordsUrl,
|
|
201
|
+
query: {
|
|
202
|
+
form_id: params.form_id,
|
|
203
|
+
shop_id: params.shop_id,
|
|
204
|
+
customer_id: params.customer_id
|
|
205
|
+
},
|
|
206
|
+
useCache
|
|
207
|
+
})
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
if (!tasks.length) {
|
|
211
|
+
this.notifyFormMapChanged();
|
|
212
|
+
} else {
|
|
213
|
+
await Promise.all(tasks);
|
|
214
|
+
}
|
|
215
|
+
this.loadedFormIds.add(formId);
|
|
216
|
+
return this.ensureFormItem(formId);
|
|
217
|
+
}
|
|
218
|
+
async ensureFormByProduct(params) {
|
|
219
|
+
const holderConfig = (0, import_utils.getProductHolderConfig)(params.product);
|
|
220
|
+
if (!holderConfig)
|
|
221
|
+
return null;
|
|
222
|
+
const formId = (0, import_utils.getFormIdFromHolderConfig)(holderConfig);
|
|
223
|
+
const shopId = params.shop_id ?? params.product.shop_id;
|
|
224
|
+
return this.ensureFormData({
|
|
225
|
+
form_id: formId,
|
|
226
|
+
shop_id: shopId,
|
|
227
|
+
customer_id: params.customer_id,
|
|
228
|
+
useCache: params.useCache,
|
|
229
|
+
skip: 1,
|
|
230
|
+
num: 100
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
async addFormRecord(params) {
|
|
234
|
+
const url = params.url || "/form/record";
|
|
235
|
+
const formId = (0, import_utils.normalizeFormId)(params.body.form_id);
|
|
236
|
+
this.isLoading = true;
|
|
237
|
+
this.error = null;
|
|
238
|
+
try {
|
|
239
|
+
await this.request.post(url, params.body);
|
|
240
|
+
const records = await this.fetchRecords({
|
|
241
|
+
query: {
|
|
242
|
+
form_id: params.body.form_id,
|
|
243
|
+
shop_id: params.body.shop_id,
|
|
244
|
+
customer_id: params.body.customer_id
|
|
245
|
+
},
|
|
246
|
+
useCache: false
|
|
247
|
+
});
|
|
248
|
+
this.loadedFormIds.add(formId);
|
|
249
|
+
return records[records.length - 1];
|
|
250
|
+
} catch (error) {
|
|
251
|
+
this.error = error instanceof Error ? error.message : String(error);
|
|
252
|
+
throw error;
|
|
253
|
+
} finally {
|
|
254
|
+
this.isLoading = false;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* UI 层自行请求添加接口后,将返回记录合并到 holderMap 对应 form_id 的 records 中
|
|
259
|
+
*/
|
|
260
|
+
appendFormRecord(params) {
|
|
261
|
+
let record = params.record;
|
|
262
|
+
if ((record == null ? void 0 : record.data) && record.form_record_id === void 0) {
|
|
263
|
+
record = record.data;
|
|
264
|
+
}
|
|
265
|
+
const normalizedRecord = (0, import_lodash_es.cloneDeep)(record);
|
|
266
|
+
const formId = (0, import_utils.normalizeFormId)(
|
|
267
|
+
params.form_id ?? normalizedRecord.form_id
|
|
268
|
+
);
|
|
269
|
+
if (!formId) {
|
|
270
|
+
throw new Error("[HolderModule] appendFormRecord: form_id is required");
|
|
271
|
+
}
|
|
272
|
+
if (!normalizedRecord.form_record_id) {
|
|
273
|
+
throw new Error(
|
|
274
|
+
"[HolderModule] appendFormRecord: form_record_id is required"
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
normalizedRecord.form_id = Number(formId) || normalizedRecord.form_id;
|
|
278
|
+
const nextFormMap = { ...this.store.holderMap };
|
|
279
|
+
const current = this.ensureFormItem(formId, nextFormMap);
|
|
280
|
+
const recordIndex = current.records.findIndex(
|
|
281
|
+
(item) => item.form_record_id === normalizedRecord.form_record_id
|
|
282
|
+
);
|
|
283
|
+
const records = recordIndex >= 0 ? current.records.map(
|
|
284
|
+
(item, index) => index === recordIndex ? { ...item, ...normalizedRecord } : item
|
|
285
|
+
) : [...current.records, normalizedRecord];
|
|
286
|
+
this.patchFormMap(nextFormMap, formId, { records });
|
|
287
|
+
this.commitFormMap(nextFormMap);
|
|
288
|
+
this.loadedFormIds.add(formId);
|
|
289
|
+
return normalizedRecord;
|
|
290
|
+
}
|
|
291
|
+
removeFormRecord(formId, formRecordId) {
|
|
292
|
+
const key = (0, import_utils.normalizeFormId)(formId);
|
|
293
|
+
const current = this.store.holderMap[key] || (0, import_utils.createEmptyFormItemData)();
|
|
294
|
+
const nextFormMap = { ...this.store.holderMap };
|
|
295
|
+
this.patchFormMap(nextFormMap, key, {
|
|
296
|
+
records: current.records.filter(
|
|
297
|
+
(record) => record.form_record_id !== formRecordId
|
|
298
|
+
)
|
|
299
|
+
});
|
|
300
|
+
this.commitFormMap(nextFormMap);
|
|
301
|
+
}
|
|
302
|
+
clearFormData(formId) {
|
|
303
|
+
if (formId !== void 0) {
|
|
304
|
+
const key = (0, import_utils.normalizeFormId)(formId);
|
|
305
|
+
const nextFormMap = { ...this.store.holderMap };
|
|
306
|
+
delete nextFormMap[key];
|
|
307
|
+
this.loadedFormIds.delete(key);
|
|
308
|
+
this.commitFormMap(nextFormMap);
|
|
309
|
+
} else {
|
|
310
|
+
this.loadedFormIds.clear();
|
|
311
|
+
this.commitFormMap({});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* 登录后按当前 holderMap 批量刷新所有表单的 records。
|
|
316
|
+
* 遍历 holderMap,从 form.form 中读取 form_id、shop_id,结合 customer_id 重新拉取记录。
|
|
317
|
+
*/
|
|
318
|
+
async refreshAllFormRecords(params = {}) {
|
|
319
|
+
const { customer_id, recordsUrl, num, skip } = params;
|
|
320
|
+
const useCache = params.useCache ?? false;
|
|
321
|
+
if (!customer_id) {
|
|
322
|
+
return this.getHolderFormMap();
|
|
323
|
+
}
|
|
324
|
+
const entries = Object.entries(this.store.holderMap);
|
|
325
|
+
if (!entries.length) {
|
|
326
|
+
return this.getHolderFormMap();
|
|
327
|
+
}
|
|
328
|
+
const url = recordsUrl || "/form/data/v1";
|
|
329
|
+
this.isLoading = true;
|
|
330
|
+
this.error = null;
|
|
331
|
+
try {
|
|
332
|
+
const results = await Promise.all(
|
|
333
|
+
entries.map(async ([mapKey, item]) => {
|
|
334
|
+
var _a;
|
|
335
|
+
const meta = (0, import_utils.resolveFormMetaFromHolderItem)(mapKey, item);
|
|
336
|
+
if (!meta)
|
|
337
|
+
return null;
|
|
338
|
+
try {
|
|
339
|
+
const res = await this.request.get(
|
|
340
|
+
url,
|
|
341
|
+
{
|
|
342
|
+
customer_id,
|
|
343
|
+
form_id: meta.form_id,
|
|
344
|
+
shop_id: meta.shop_id,
|
|
345
|
+
num: num || 100,
|
|
346
|
+
skip: skip || 1,
|
|
347
|
+
front_end_cache_id: useCache && this.cacheId
|
|
348
|
+
},
|
|
349
|
+
{ useCache }
|
|
350
|
+
);
|
|
351
|
+
return {
|
|
352
|
+
formId: meta.form_id,
|
|
353
|
+
records: ((_a = res == null ? void 0 : res.data) == null ? void 0 : _a.list) || []
|
|
354
|
+
};
|
|
355
|
+
} catch (error) {
|
|
356
|
+
console.error(
|
|
357
|
+
`[HolderModule] refreshAllFormRecords failed for form ${meta.form_id}`,
|
|
358
|
+
error
|
|
359
|
+
);
|
|
360
|
+
return null;
|
|
361
|
+
}
|
|
362
|
+
})
|
|
363
|
+
);
|
|
364
|
+
const nextFormMap = { ...this.store.holderMap };
|
|
365
|
+
results.forEach((result) => {
|
|
366
|
+
if (!result)
|
|
367
|
+
return;
|
|
368
|
+
this.patchFormMap(nextFormMap, result.formId, {
|
|
369
|
+
records: result.records
|
|
370
|
+
});
|
|
371
|
+
this.loadedFormIds.add(result.formId);
|
|
372
|
+
});
|
|
373
|
+
this.commitFormMap(nextFormMap);
|
|
374
|
+
return (0, import_lodash_es.cloneDeep)(nextFormMap);
|
|
375
|
+
} catch (error) {
|
|
376
|
+
this.error = error instanceof Error ? error.message : String(error);
|
|
377
|
+
throw error;
|
|
378
|
+
} finally {
|
|
379
|
+
this.isLoading = false;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
storeChange(path) {
|
|
383
|
+
if (path !== "holderMap" && path !== void 0) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (this.openCache) {
|
|
387
|
+
const store = (0, import_lodash_es.cloneDeep)(this.store);
|
|
388
|
+
this.checkSaveCache({
|
|
389
|
+
cacheId: this.cacheId,
|
|
390
|
+
fatherModule: this.fatherModule,
|
|
391
|
+
store,
|
|
392
|
+
cacheKey: ["holderMap"]
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
398
|
+
0 && (module.exports = {
|
|
399
|
+
HolderModule,
|
|
400
|
+
...require("./types"),
|
|
401
|
+
...require("./utils")
|
|
402
|
+
});
|