@things-factory/marketplace-base 4.1.27 → 4.1.28

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,18 +1,10 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.syncAllMarketplaceOrder = void 0;
7
4
  const typeorm_1 = require("typeorm");
8
- const v4_1 = __importDefault(require("uuid/v4"));
9
- const biz_base_1 = require("@things-factory/biz-base");
10
- const integration_fulfillment_1 = require("@things-factory/integration-fulfillment");
11
5
  const integration_marketplace_1 = require("@things-factory/integration-marketplace");
12
- const product_base_1 = require("@things-factory/product-base");
13
6
  const shell_1 = require("@things-factory/shell");
14
- const entities_1 = require("../../../entities");
15
- const utils_1 = require("../../../utils");
7
+ const sync_marketplace_order_1 = require("./sync-marketplace-order");
16
8
  exports.syncAllMarketplaceOrder = {
17
9
  async syncAllMarketplaceOrder(_, { companyDomainId, fromDate, toDate, blackListStores }, context) {
18
10
  const { user } = context.state;
@@ -29,627 +21,9 @@ exports.syncAllMarketplaceOrder = {
29
21
  }
30
22
  for (var i = 0; i < (whiteListStores === null || whiteListStores === void 0 ? void 0 : whiteListStores.length); i++) {
31
23
  const marketplaceStore = whiteListStores[i];
32
- const domain = marketplaceStore.domain;
33
- let page = 0;
34
- let hasMorePage = true;
35
- let lastOrderId;
36
- var limit = 100;
37
- const cancelStatuses = ['cancelled', 'canceled'];
38
- const disallowCancelStatuses = ['CANCELLED', 'PENDING_CANCEL'];
39
- while (hasMorePage) {
40
- const { results: marketplaceOrders, more } = await integration_marketplace_1.StoreAPI.getStoreOrders(marketplaceStore, {
41
- fromDate,
42
- toDate,
43
- pagination: { page, limit },
44
- lastOrderId
45
- });
46
- if ((marketplaceOrders === null || marketplaceOrders === void 0 ? void 0 : marketplaceOrders.length) > 0) {
47
- for (var m = 0; m < marketplaceOrders.length; m++) {
48
- try {
49
- var order = marketplaceOrders[m];
50
- let marketplaceOrderItems = [];
51
- let tracking_code = '';
52
- let deliveryProvider;
53
- let dropPickProvider;
54
- if (marketplaceStore.platform == 'lazada') {
55
- if (order.trackingNo != '') {
56
- tracking_code = JSON.parse(order.trackingNo)
57
- .reduce((newItem, item) => {
58
- if (newItem.indexOf(item.tracking_code) < 0) {
59
- newItem.push(item.tracking_code);
60
- }
61
- return newItem;
62
- }, [])
63
- .join(',');
64
- }
65
- const shippingProviderInfo = {};
66
- order.shippingProvider.split(', ').forEach(s => {
67
- var [key, value] = s.split(': ');
68
- shippingProviderInfo[key] = value;
69
- });
70
- dropPickProvider = (shippingProviderInfo === null || shippingProviderInfo === void 0 ? void 0 : shippingProviderInfo.Pickup)
71
- ? shippingProviderInfo['Pickup']
72
- : shippingProviderInfo['Drop-off'];
73
- deliveryProvider = shippingProviderInfo['Delivery'];
74
- }
75
- let marketplaceOrder = new entities_1.MarketplaceOrder();
76
- marketplaceOrder.name = order.name;
77
- marketplaceOrder.orderNo = order.orderNo;
78
- marketplaceOrder.itemCount = order.itemCount;
79
- marketplaceOrder.totalAmount = order.totalAmount;
80
- marketplaceOrder.status = order.status;
81
- marketplaceOrder.remark = order === null || order === void 0 ? void 0 : order.remark;
82
- marketplaceOrder.trackingNo = tracking_code ? tracking_code : order.trackingNo;
83
- marketplaceOrder.shippingProvider = order.shippingProvider;
84
- marketplaceOrder.orderCreatedAt = order.orderCreatedAt;
85
- marketplaceOrder.orderUpdatedAt = order.orderUpdatedAt;
86
- marketplaceOrder.marketplaceStore = marketplaceStore;
87
- marketplaceOrder.domain = marketplaceStore.domain;
88
- marketplaceOrder.buyerUsername = order.buyerUsername;
89
- marketplaceOrder.payTime = order.payTime;
90
- marketplaceOrder.serviceFee = order.serviceFee;
91
- marketplaceOrder.commissionFee = order.commissionFee;
92
- marketplaceOrder.transactionFee = order.transactionFee;
93
- marketplaceOrder.shippingMode = order.shippingMode;
94
- marketplaceOrder.voucherAmount = order.voucherAmount;
95
- marketplaceOrder.totalRealeasedAmount = order.totalRealeasedAmount;
96
- marketplaceOrder.voucherCode = order.voucherCode;
97
- marketplaceOrder.shippingRebate = order.shippingRebate;
98
- const existingMarketplaceOrder = await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrder).findOne({
99
- where: { domain: marketplaceStore.domain, name: order.name },
100
- relations: [
101
- 'domain',
102
- 'fulfillmentCenter',
103
- 'marketplaceOrderItems',
104
- 'marketplaceOrderItems.marketplaceOrderShippingItems',
105
- 'marketplaceOrderItems.marketplaceOrderShippingItems.marketplaceOrderShipping',
106
- 'marketplaceOrderItems.marketplaceProductVariation'
107
- ]
108
- });
109
- if (existingMarketplaceOrder) {
110
- let otherInfoJSON = {};
111
- if ((existingMarketplaceOrder === null || existingMarketplaceOrder === void 0 ? void 0 : existingMarketplaceOrder.otherInfoJSON) && deliveryProvider) {
112
- otherInfoJSON = JSON.parse(existingMarketplaceOrder.otherInfoJSON);
113
- otherInfoJSON.shippingProvider = deliveryProvider;
114
- }
115
- marketplaceOrder = Object.assign(Object.assign(Object.assign({}, existingMarketplaceOrder), marketplaceOrder), { otherInfoJSON: otherInfoJSON ? JSON.stringify(otherInfoJSON) : null, updater: user });
116
- if (!(marketplaceOrder === null || marketplaceOrder === void 0 ? void 0 : marketplaceOrder.releaseOrderId) &&
117
- cancelStatuses.includes(marketplaceOrder.status) &&
118
- !cancelStatuses.includes(existingMarketplaceOrder.status)) {
119
- for (let item of marketplaceOrder.marketplaceOrderItems) {
120
- let foundVariations = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).find({
121
- where: {
122
- domain: marketplaceOrder.domain,
123
- sku: item.marketplaceProductVariation.sku
124
- },
125
- relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
126
- });
127
- let activeVariations = foundVariations.filter(variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED' && variation.sku != null);
128
- await Promise.all(activeVariations.map(async (variation) => {
129
- await calculateReserveQtyForBundle(variation, marketplaceOrder.domain, item.qty, '-');
130
- variation.reserveQty -= item.qty;
131
- variation.qty += item.qty;
132
- await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).save(variation);
133
- }));
134
- }
135
- }
136
- if (marketplaceOrder === null || marketplaceOrder === void 0 ? void 0 : marketplaceOrder.releaseOrderId) {
137
- const fulfillmentCenter = marketplaceOrder.fulfillmentCenter;
138
- const centerId = fulfillmentCenter.centerId;
139
- const warehouseDomain = await (0, typeorm_1.getRepository)(shell_1.Domain).findOne({
140
- where: { subdomain: centerId }
141
- });
142
- const customerBizplaces = await (0, biz_base_1.getCustomerBizplaces)(warehouseDomain);
143
- const customerBizplaceId = customerBizplaces.find(customerBizplace => customerBizplace.company.domain.id == domain.id).id;
144
- let { items: releaseOrders } = await integration_fulfillment_1.FulfillmentAPI.getOutboundOrders(fulfillmentCenter, {
145
- customerBizplaceId,
146
- refNo: marketplaceOrder.orderNo
147
- });
148
- if (existingMarketplaceOrder.isSplitted) {
149
- for (let a = 0; a < releaseOrders.length; a++) {
150
- const releaseOrder = releaseOrders[a];
151
- const foundMarketplaceOrderShipping = await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderShipping).findOne({
152
- where: { domain: marketplaceStore.domain, subOrderNoRef: releaseOrder.refNo2 }
153
- });
154
- let patch = {
155
- id: releaseOrder.id,
156
- marketplaceOrderStatus: marketplaceOrder.status,
157
- trackingNo: (foundMarketplaceOrderShipping === null || foundMarketplaceOrderShipping === void 0 ? void 0 : foundMarketplaceOrderShipping.trackingNo) == '[]'
158
- ? foundMarketplaceOrderShipping.ownTrackingNo
159
- : (foundMarketplaceOrderShipping === null || foundMarketplaceOrderShipping === void 0 ? void 0 : foundMarketplaceOrderShipping.trackingNo)
160
- ? foundMarketplaceOrderShipping.trackingNo
161
- : foundMarketplaceOrderShipping.ownTrackingNo,
162
- transporter: dropPickProvider ? dropPickProvider : marketplaceOrder.shippingProvider
163
- };
164
- await integration_fulfillment_1.FulfillmentAPI.updateReleaseGoodDetails(fulfillmentCenter, {
165
- customerBizplaceId,
166
- releaseOrder: Object.assign({}, patch),
167
- shippingOrder: null
168
- });
169
- }
170
- }
171
- else {
172
- let releaseOrder = Object.assign({}, releaseOrders[0]);
173
- const foundMarketplaceOrderShipping = await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderShipping).findOne({
174
- where: { domain: marketplaceStore.domain, orderNoRef: releaseOrder.refNo }
175
- });
176
- let patch = {
177
- id: releaseOrder.id,
178
- marketplaceOrderStatus: marketplaceOrder.status,
179
- trackingNo: (foundMarketplaceOrderShipping === null || foundMarketplaceOrderShipping === void 0 ? void 0 : foundMarketplaceOrderShipping.trackingNo) == '[]'
180
- ? foundMarketplaceOrderShipping.ownTrackingNo
181
- : (foundMarketplaceOrderShipping === null || foundMarketplaceOrderShipping === void 0 ? void 0 : foundMarketplaceOrderShipping.trackingNo)
182
- ? foundMarketplaceOrderShipping.trackingNo
183
- : foundMarketplaceOrderShipping.ownTrackingNo,
184
- transporter: dropPickProvider ? dropPickProvider : marketplaceOrder.shippingProvider
185
- };
186
- await integration_fulfillment_1.FulfillmentAPI.updateReleaseGoodDetails(fulfillmentCenter, {
187
- customerBizplaceId,
188
- releaseOrder: Object.assign({}, patch),
189
- shippingOrder: null
190
- });
191
- }
192
- if (cancelStatuses.includes(marketplaceOrder.status)) {
193
- if (existingMarketplaceOrder.isSplitted) {
194
- releaseOrders = releaseOrders.filter(function (order) {
195
- return !disallowCancelStatuses.includes(order.status);
196
- });
197
- if (releaseOrders === null || releaseOrders === void 0 ? void 0 : releaseOrders.length) {
198
- for (let a = 0; a < releaseOrders.length; a++) {
199
- const releaseOrderId = releaseOrders[a].id;
200
- await integration_fulfillment_1.FulfillmentAPI.cancelReleaseOrder(fulfillmentCenter, {
201
- customerBizplaceId,
202
- releaseOrderId
203
- });
204
- }
205
- }
206
- }
207
- else {
208
- const fulfillmentCenter = marketplaceOrder.fulfillmentCenter;
209
- const centerId = fulfillmentCenter.centerId;
210
- const warehouseDomain = await (0, typeorm_1.getRepository)(shell_1.Domain).findOne({
211
- where: { subdomain: centerId }
212
- });
213
- const customerBizplaces = await (0, biz_base_1.getCustomerBizplaces)(warehouseDomain);
214
- const customerBizplaceId = customerBizplaces[0].id;
215
- const { items: releaseOrders } = await integration_fulfillment_1.FulfillmentAPI.getOutboundOrders(fulfillmentCenter, {
216
- customerBizplaceId,
217
- refNo: marketplaceOrder.orderNo
218
- });
219
- const releaseOrderStatus = releaseOrders[0].status;
220
- if (!disallowCancelStatuses.includes(releaseOrderStatus)) {
221
- await integration_fulfillment_1.FulfillmentAPI.cancelReleaseOrder(fulfillmentCenter, {
222
- customerBizplaceId,
223
- releaseOrderId: releaseOrders[0].id
224
- });
225
- }
226
- }
227
- }
228
- }
229
- }
230
- let savedMarketplaceOrderShipping = new entities_1.MarketplaceOrderShipping();
231
- let savedMarketplaceOrderShippings = [];
232
- if (order === null || order === void 0 ? void 0 : order.orderShipping) {
233
- const orderShipping = order.orderShipping;
234
- let marketplaceOrderShipping = new entities_1.MarketplaceOrderShipping();
235
- marketplaceOrderShipping.name = utils_1.NoGenerator.orderShipping();
236
- marketplaceOrderShipping.address1 = orderShipping.address1;
237
- marketplaceOrderShipping.address2 = orderShipping.address2;
238
- marketplaceOrderShipping.address3 = orderShipping.address3;
239
- marketplaceOrderShipping.address4 = orderShipping.address4;
240
- marketplaceOrderShipping.address5 = orderShipping.address5;
241
- marketplaceOrderShipping.attentionTo = orderShipping.attentionTo;
242
- marketplaceOrderShipping.city = orderShipping.city;
243
- marketplaceOrderShipping.country = orderShipping.country;
244
- marketplaceOrderShipping.state = (orderShipping === null || orderShipping === void 0 ? void 0 : orderShipping.state) || null;
245
- marketplaceOrderShipping.postCode = orderShipping.postCode;
246
- marketplaceOrderShipping.phone1 = orderShipping.phone1;
247
- marketplaceOrderShipping.phone2 = orderShipping.phone2;
248
- marketplaceOrderShipping.trackingNo = (order === null || order === void 0 ? void 0 : order.trackingNo) || null;
249
- marketplaceOrderShipping.orderNoRef = marketplaceOrder.orderNo;
250
- marketplaceOrderShipping.subOrderNoRef = marketplaceOrder.orderNo;
251
- marketplaceOrderShipping.marketplaceStore = marketplaceStore;
252
- marketplaceOrderShipping.creator = user;
253
- marketplaceOrderShipping.domain = marketplaceStore.domain;
254
- marketplaceOrderShipping.shippingFee = orderShipping.shippingFee;
255
- marketplaceOrderShipping.actualShippingFee = orderShipping.actualShippingFee;
256
- marketplaceOrderShipping.marketplaceShippingFeeVoucher = orderShipping.marketplaceShippingFeeVoucher;
257
- marketplaceOrderShipping.sellerShippingFeeVoucher = orderShipping.sellerShippingFeeVoucher;
258
- if (existingMarketplaceOrder) {
259
- if (existingMarketplaceOrder.isSplitted) {
260
- let existingOrderShippings = await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderShipping).find({
261
- where: { domain, marketplaceStore, orderNoRef: marketplaceOrder.orderNo }
262
- });
263
- existingOrderShippings = existingOrderShippings.map(item => {
264
- return Object.assign(Object.assign({}, item), { address1: orderShipping.address1, address2: orderShipping.address2, address3: orderShipping.address3, address4: orderShipping.address4, address5: orderShipping.address5, attentionTo: orderShipping.attentionTo, city: orderShipping.city, country: orderShipping.country, state: orderShipping === null || orderShipping === void 0 ? void 0 : orderShipping.state, postCode: orderShipping.postCode, phone1: orderShipping.phone1, phone2: orderShipping.phone2, email: orderShipping === null || orderShipping === void 0 ? void 0 : orderShipping.email, trackingNo: marketplaceStore.eTrax ? item.trackingNo : order === null || order === void 0 ? void 0 : order.trackingNo, shippingFee: orderShipping.shippingFee, actualShippingFee: orderShipping.actualShippingFee, marketplaceShippingFeeVoucher: orderShipping.marketplaceShippingFeeVoucher, sellerShippingFeeVoucher: orderShipping.sellerShippingFeeVoucher });
265
- });
266
- savedMarketplaceOrderShippings = await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderShipping).save(existingOrderShippings);
267
- }
268
- else {
269
- if (existingMarketplaceOrder === null || existingMarketplaceOrder === void 0 ? void 0 : existingMarketplaceOrder.marketplaceOrderItems) {
270
- let existingOrderShipping;
271
- existingOrderShipping =
272
- existingMarketplaceOrder.marketplaceOrderItems[0].marketplaceOrderShippingItems[0]
273
- .marketplaceOrderShipping;
274
- marketplaceOrderShipping = Object.assign(Object.assign(Object.assign({}, existingOrderShipping), marketplaceOrderShipping), { trackingNo: marketplaceStore.eTrax
275
- ? existingOrderShipping.trackingNo
276
- : (marketplaceOrderShipping === null || marketplaceOrderShipping === void 0 ? void 0 : marketplaceOrderShipping.trackingNo)
277
- ? marketplaceOrderShipping.trackingNo
278
- : (existingOrderShipping === null || existingOrderShipping === void 0 ? void 0 : existingOrderShipping.trackingNo)
279
- ? existingOrderShipping.trackingNo
280
- : marketplaceOrderShipping.trackingNo, updater: user });
281
- savedMarketplaceOrderShipping = await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderShipping).save(marketplaceOrderShipping);
282
- }
283
- }
284
- }
285
- else {
286
- if (marketplaceStore.eTrax)
287
- delete marketplaceOrderShipping.trackingNo;
288
- savedMarketplaceOrderShipping = await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderShipping).save(marketplaceOrderShipping);
289
- }
290
- }
291
- let orderTotalWeight = 0;
292
- let foundVariations = [];
293
- for (var j = 0; j < order.orderItems.length; j++) {
294
- var item = order.orderItems[j];
295
- let foundVariationsQuery = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).find({
296
- where: { domain: marketplaceStore.domain, variationId: item.variationId },
297
- relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
298
- });
299
- let foundVariation = foundVariationsQuery.filter(fv => fv.marketplaceProduct.marketplaceStore.id == marketplaceStore.id)[0];
300
- if (foundVariation) {
301
- foundVariations.push(foundVariation);
302
- }
303
- if (!foundVariation) {
304
- let newVariation = {
305
- variationId: item.variationId,
306
- name: item.variationName,
307
- variationSku: item.variationSku,
308
- channelSku: item.sku,
309
- status: 'INACTIVE',
310
- domain: marketplaceStore.domain
311
- };
312
- await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).save(newVariation);
313
- foundVariation = newVariation;
314
- }
315
- let marketplaceOrderItem = new entities_1.MarketplaceOrderItem();
316
- marketplaceOrderItem.domain = marketplaceStore.domain;
317
- marketplaceOrderItem.name = item.name;
318
- marketplaceOrderItem.qty = item.qty;
319
- marketplaceOrderItem.paidPrice = item.paidPrice;
320
- marketplaceOrderItem.status = marketplaceOrder.status;
321
- marketplaceOrderItem.docRefNo = item.docRefNo;
322
- marketplaceOrderItem.marketplaceProductVariation = foundVariation;
323
- marketplaceOrderItem.trackingCode = order.trackingNo;
324
- marketplaceOrderItem.marketplaceOrder = marketplaceOrder;
325
- marketplaceOrderItem.originalPrice = item.originalPrice;
326
- marketplaceOrderItem.discount = item.discount;
327
- marketplaceOrderItem.promotionId = item.promotionId;
328
- marketplaceOrderItem.creator = user;
329
- marketplaceOrderItem.paymentFee = item.paymentFee || null;
330
- marketplaceOrderItem.commissionFee = item.commissionFee || null;
331
- marketplaceOrderItem.shippingFeePaidByCustomer = item.shippingFeePaidByCustomer || null;
332
- marketplaceOrderItem.itemPriceCredit = item.itemPriceCredit || null;
333
- marketplaceOrderItem.promotionalCharges = item.promotionalCharges || null;
334
- marketplaceOrderItem.ordersMarketplaceFeesTotal = item.ordersMarketplaceFeesTotal || null;
335
- marketplaceOrderItem.ordersSalesTotal = item.ordersSalesTotal || null;
336
- marketplaceOrderItem.ordersMarketingFeesTotal = item.ordersMarketingFeesTotal || null;
337
- marketplaceOrderItem.ordersLogisticsTotal = item.ordersLogisticsTotal || null;
338
- marketplaceOrderItem.marketplaceBonus = item.marketplaceBonus;
339
- marketplaceOrderItem.marketplaceBonusSeller = item.marketplaceBonusSeller;
340
- marketplaceOrderItem.shippingFeeDiscountMarketplace = item.shippingFeeDiscountMarketplace;
341
- marketplaceOrderItem.shippingFeeDiscountSeller = item.shippingFeeDiscountSeller;
342
- marketplaceOrderItem.promotionalChargesFlexiCombo = item.promotionalChargesFlexiCombo;
343
- marketplaceOrderItem.autoShippingSubsidyMarketplace = item.autoShippingSubsidyMarketplace;
344
- marketplaceOrderItem.originalShippingFee = item.originalShippingFee;
345
- marketplaceOrderItem.taxAmount = item.taxAmount;
346
- marketplaceOrderItems.push(Object.assign({}, marketplaceOrderItem));
347
- if (foundVariation === null || foundVariation === void 0 ? void 0 : foundVariation.primaryUnitValue)
348
- orderTotalWeight += parseFloat(foundVariation.primaryUnitValue.toFixed(3)) * item.qty || 0;
349
- }
350
- if (existingMarketplaceOrder) {
351
- let existingOrderItems = [];
352
- existingOrderItems = existingMarketplaceOrder.marketplaceOrderItems;
353
- existingOrderItems = await checkUpdateItemId(existingOrderItems, marketplaceOrderItems);
354
- if (existingMarketplaceOrder.isSplitted) {
355
- marketplaceOrderItems = existingOrderItems.filter(itm => itm.name == item.name && itm.marketplaceProductVariation.variationId == item.variationId);
356
- marketplaceOrderItems = marketplaceOrderItems.map(marketplaceOrderitem => {
357
- return Object.assign(Object.assign({}, marketplaceOrderitem), { status: order.status, trackingCode: order.trackingNo });
358
- });
359
- }
360
- else {
361
- if (existingOrderItems.length > marketplaceOrderItems.length) {
362
- let nonMatchedOrderItems = [];
363
- existingOrderItems.map(item => {
364
- const matchedOrderItem = marketplaceOrderItems.find(itm => itm.name === item.name &&
365
- itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId);
366
- if (!matchedOrderItem) {
367
- nonMatchedOrderItems.push(Object.assign({}, item));
368
- }
369
- });
370
- if (nonMatchedOrderItems.length > 0 && !cancelStatuses.includes(marketplaceOrder.status)) {
371
- await Promise.all(nonMatchedOrderItems.map(async (item) => {
372
- for (var foundVariation of foundVariations) {
373
- if (foundVariation.sku) {
374
- let allVariations = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).find({
375
- where: { domain: foundVariation.domain, sku: foundVariation.sku },
376
- relations: [
377
- 'domain',
378
- 'marketplaceProduct',
379
- 'marketplaceProduct.marketplaceStore',
380
- 'marketplaceProduct.marketplaceStore.marketplaceDistributors'
381
- ]
382
- });
383
- let activeVariations = allVariations.filter(variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED');
384
- await Promise.all(activeVariations.map(async (variation) => {
385
- await calculateReserveQtyForBundle(variation, marketplaceOrder.domain, item.qty, '-');
386
- variation.reserveQty -= item.qty;
387
- variation.qty += item.qty;
388
- variation = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).save(variation);
389
- }));
390
- }
391
- }
392
- }));
393
- await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderItem).delete(nonMatchedOrderItems);
394
- }
395
- if (!cancelStatuses.includes(marketplaceOrder.status)) {
396
- existingOrderItems = await Promise.all(marketplaceOrderItems.map(async (item) => {
397
- const matchedOrderItem = existingOrderItems.find(itm => itm.name == item.name &&
398
- itm.marketplaceProductVariation.variationId ==
399
- item.marketplaceProductVariation.variationId);
400
- if (matchedOrderItem) {
401
- for (var foundVariation of foundVariations) {
402
- if (foundVariation.sku) {
403
- let allVariations = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).find({
404
- where: { domain: foundVariation.domain, sku: foundVariation.sku },
405
- relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
406
- });
407
- let activeVariations = allVariations.filter(variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED');
408
- await Promise.all(activeVariations.map(async (variation) => {
409
- if (!(existingMarketplaceOrder === null || existingMarketplaceOrder === void 0 ? void 0 : existingMarketplaceOrder.releaseOrderId) &&
410
- matchedOrderItem.qty - item.qty != 0) {
411
- await calculateReserveQtyForBundle(variation, marketplaceOrder.domain, matchedOrderItem.qty - item.qty);
412
- }
413
- variation.reserveQty = (existingMarketplaceOrder === null || existingMarketplaceOrder === void 0 ? void 0 : existingMarketplaceOrder.releaseOrderId)
414
- ? variation.reserveQty
415
- : variation.reserveQty - item.qty + matchedOrderItem.qty;
416
- variation.qty = (existingMarketplaceOrder === null || existingMarketplaceOrder === void 0 ? void 0 : existingMarketplaceOrder.releaseOrderId)
417
- ? variation.qty
418
- : variation.qty + item.qty - matchedOrderItem.qty;
419
- variation = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).save(variation);
420
- }));
421
- }
422
- }
423
- return Object.assign(Object.assign(Object.assign({}, matchedOrderItem), item), { updater: user });
424
- }
425
- }));
426
- }
427
- }
428
- else {
429
- if (!cancelStatuses.includes(marketplaceOrder.status)) {
430
- existingOrderItems = await Promise.all(marketplaceOrderItems.map(async (item) => {
431
- const matchedOrderItem = existingOrderItems.find(itm => itm.name == item.name &&
432
- itm.marketplaceProductVariation.variationId ==
433
- item.marketplaceProductVariation.variationId);
434
- if (matchedOrderItem) {
435
- for (var foundVariation of foundVariations) {
436
- if (foundVariation.sku) {
437
- let allVariations = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).find({
438
- where: { domain: foundVariation.domain, sku: foundVariation.sku },
439
- relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
440
- });
441
- let activeVariations = allVariations.filter(variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED');
442
- await Promise.all(activeVariations.map(async (variation) => {
443
- if (!(existingMarketplaceOrder === null || existingMarketplaceOrder === void 0 ? void 0 : existingMarketplaceOrder.releaseOrderId) &&
444
- matchedOrderItem.qty - item.qty != 0) {
445
- await calculateReserveQtyForBundle(variation, marketplaceOrder.domain, matchedOrderItem.qty - item.qty);
446
- }
447
- variation.reserveQty = (existingMarketplaceOrder === null || existingMarketplaceOrder === void 0 ? void 0 : existingMarketplaceOrder.releaseOrderId)
448
- ? variation.reserveQty
449
- : variation.reserveQty - item.qty + matchedOrderItem.qty;
450
- variation.qty = (existingMarketplaceOrder === null || existingMarketplaceOrder === void 0 ? void 0 : existingMarketplaceOrder.releaseOrderId)
451
- ? variation.qty
452
- : variation.qty + item.qty - matchedOrderItem.qty;
453
- variation = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).save(variation);
454
- }));
455
- }
456
- }
457
- return Object.assign(Object.assign(Object.assign({}, matchedOrderItem), item), { updater: user });
458
- }
459
- else {
460
- for (var foundVariation of foundVariations) {
461
- if (foundVariation.sku) {
462
- let allVariations = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).find({
463
- where: { domain: foundVariation.domain, sku: foundVariation.sku },
464
- relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
465
- });
466
- let activeVariations = allVariations.filter(variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED');
467
- await Promise.all(activeVariations.map(async (variation) => {
468
- await calculateReserveQtyForBundle(variation, marketplaceOrder.domain, item.qty);
469
- variation.reserveQty += item.qty;
470
- if (variation.qty - item.qty >= 0) {
471
- variation.qty -= item.qty;
472
- }
473
- variation = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).save(variation);
474
- }));
475
- }
476
- }
477
- return Object.assign(Object.assign({}, item), { updater: user });
478
- }
479
- }));
480
- }
481
- }
482
- marketplaceOrderItems = existingOrderItems;
483
- if (order === null || order === void 0 ? void 0 : order.orderShipping) {
484
- savedMarketplaceOrderShipping.totalWeight = orderTotalWeight;
485
- if (marketplaceStore.eTrax)
486
- delete savedMarketplaceOrderShipping.trackingNo;
487
- savedMarketplaceOrderShipping = await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderShipping).save(savedMarketplaceOrderShipping);
488
- }
489
- }
490
- }
491
- else {
492
- if (!cancelStatuses.includes(marketplaceOrder.status)) {
493
- for (var foundVariation of foundVariations) {
494
- if (foundVariation.sku) {
495
- let allVariations = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).find({
496
- where: { domain: foundVariation.domain, sku: foundVariation.sku },
497
- relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
498
- });
499
- let activeVariations = allVariations.filter(variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED');
500
- let item = order.orderItems.find(e => e.variationId == foundVariation.variationId);
501
- await Promise.all(activeVariations.map(async (variation) => {
502
- await calculateReserveQtyForBundle(variation, marketplaceOrder.domain, item.qty);
503
- variation.reserveQty += item.qty;
504
- if (variation.qty - item.qty >= 0) {
505
- variation.qty -= item.qty;
506
- }
507
- variation = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).save(variation);
508
- }));
509
- }
510
- if (order === null || order === void 0 ? void 0 : order.orderShipping) {
511
- savedMarketplaceOrderShipping.totalWeight = orderTotalWeight;
512
- if (marketplaceStore.eTrax)
513
- delete savedMarketplaceOrderShipping.trackingNo;
514
- savedMarketplaceOrderShipping = await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderShipping).save(savedMarketplaceOrderShipping);
515
- }
516
- }
517
- }
518
- }
519
- await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrder).save(marketplaceOrder);
520
- await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderItem).save(marketplaceOrderItems);
521
- for (let i = 0; i < marketplaceOrderItems.length; i++) {
522
- const marketplaceOrderItem = marketplaceOrderItems[i];
523
- const marketplaceProductVariation = marketplaceOrderItem.marketplaceProductVariation;
524
- let existingMarketplaceOrderShippingItems = await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderShippingItem).find({
525
- where: { domain: marketplaceStore.domain, marketplaceOrderItem }
526
- });
527
- let product = await (0, typeorm_1.getRepository)(product_base_1.Product).findOne({
528
- where: { domain: marketplaceStore.domain, sku: marketplaceProductVariation.sku, deletedAt: (0, typeorm_1.IsNull)() },
529
- relations: ['productDetails']
530
- });
531
- let productBundle;
532
- if (!product) {
533
- productBundle = await (0, typeorm_1.getRepository)(product_base_1.ProductBundle).findOne({
534
- where: { domain: marketplaceStore.domain, sku: marketplaceProductVariation.sku },
535
- relations: ['productBundleSettings', 'productBundleSettings.product']
536
- });
537
- }
538
- if (existingMarketplaceOrderShippingItems && (existingMarketplaceOrderShippingItems === null || existingMarketplaceOrderShippingItems === void 0 ? void 0 : existingMarketplaceOrderShippingItems.length)) {
539
- }
540
- else {
541
- if (productBundle) {
542
- const productBundleSettings = productBundle.productBundleSettings;
543
- let marketplaceOrderShippingItems = [];
544
- productBundleSettings.map(productBundleSetting => {
545
- let marketplaceOrderShippingItem = new entities_1.MarketplaceOrderShippingItem();
546
- marketplaceOrderShippingItem.name = (0, v4_1.default)();
547
- marketplaceOrderShippingItem.qty = marketplaceOrderItem.qty * productBundleSetting.bundleQty;
548
- marketplaceOrderShippingItem.domain = marketplaceStore.domain;
549
- marketplaceOrderShippingItem.marketplaceOrderItem = marketplaceOrderItem;
550
- marketplaceOrderShippingItem.marketplaceOrderShipping = savedMarketplaceOrderShipping;
551
- marketplaceOrderShippingItem.product = productBundleSetting.product;
552
- marketplaceOrderShippingItems.push(marketplaceOrderShippingItem);
553
- });
554
- await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderShippingItem).save(marketplaceOrderShippingItems);
555
- }
556
- else {
557
- let marketplaceOrderShippingItem = new entities_1.MarketplaceOrderShippingItem();
558
- marketplaceOrderShippingItem.name = (0, v4_1.default)();
559
- marketplaceOrderShippingItem.qty = marketplaceOrderItem.qty;
560
- marketplaceOrderShippingItem.domain = marketplaceStore.domain;
561
- marketplaceOrderShippingItem.marketplaceOrderItem = marketplaceOrderItem;
562
- marketplaceOrderShippingItem.marketplaceOrderShipping = savedMarketplaceOrderShipping;
563
- marketplaceOrderShippingItem.product = product ? product : null;
564
- await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrderShippingItem).save(marketplaceOrderShippingItem);
565
- }
566
- }
567
- }
568
- if (m == marketplaceOrders.length - 1) {
569
- lastOrderId = order.name;
570
- }
571
- }
572
- catch (e) { }
573
- }
574
- if (more)
575
- page++;
576
- hasMorePage = more;
577
- }
578
- else {
579
- hasMorePage = false;
580
- }
581
- }
582
- if (marketplaceStore.platform === 'shopee') {
583
- let pageNo = 0;
584
- let hasMore = true;
585
- let perPage = 100;
586
- var payoutDateList = [];
587
- while (hasMore) {
588
- const { result: payoutDates, more } = await integration_marketplace_1.StoreAPI.getStoreOrderPayoutDates(marketplaceStore, {
589
- fromDate,
590
- toDate,
591
- pagination: { page: pageNo, limit: perPage }
592
- });
593
- payoutDateList.push(...payoutDates);
594
- if (more)
595
- pageNo++;
596
- hasMore = more;
597
- }
598
- await Promise.all(payoutDateList.map(async (item) => {
599
- await (0, typeorm_1.getRepository)(entities_1.MarketplaceOrder).update({
600
- orderNo: item.orderNo
601
- }, {
602
- payoutDate: item.payoutDate
603
- });
604
- }));
605
- }
24
+ sync_marketplace_order_1.syncMarketplaceOrder.syncMarketplaceOrder(_, { storeId: marketplaceStore.id, fromDate, toDate }, context);
606
25
  }
607
26
  return true;
608
27
  }
609
28
  };
610
- async function calculateReserveQtyForBundle(variation, domain, qty, operator = '+') {
611
- const productBundle = await (0, typeorm_1.getRepository)(product_base_1.ProductBundle).findOne({
612
- where: { sku: variation.sku, domain }
613
- });
614
- if (productBundle) {
615
- const productBundleSettings = await (0, typeorm_1.getRepository)(product_base_1.ProductBundleSetting).find({
616
- where: { productBundle },
617
- relations: ['productBundle', 'product']
618
- });
619
- for (let setting of productBundleSettings) {
620
- const product = await (0, typeorm_1.getRepository)(product_base_1.Product).findOne({
621
- where: { id: setting.product.id }
622
- });
623
- const mpvs = await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).find({
624
- where: { domain, sku: product.sku },
625
- relations: ['domain']
626
- });
627
- for (let mpv of mpvs) {
628
- if (operator === '+') {
629
- mpv.reserveQty += setting.bundleQty * qty;
630
- if (mpv.qty - setting.bundleQty * qty >= 0) {
631
- mpv.qty -= setting.bundleQty * qty;
632
- }
633
- }
634
- if (operator === '-') {
635
- mpv.reserveQty -= setting.bundleQty * qty;
636
- mpv.qty += setting.bundleQty * qty;
637
- }
638
- await (0, typeorm_1.getRepository)(entities_1.MarketplaceProductVariation).save(mpv);
639
- }
640
- }
641
- }
642
- }
643
- function checkUpdateItemId(existingOrderItems, marketplaceOrderItems) {
644
- for (let item of marketplaceOrderItems) {
645
- let existingItem = existingOrderItems.find(i => i.name == item.name);
646
- if (!existingItem) {
647
- let existingItemByVariation = existingOrderItems.find(i => i.marketplaceProductVariation.id == item.marketplaceProductVariation.id);
648
- existingOrderItems = existingOrderItems.filter(i => i != existingItemByVariation);
649
- existingItemByVariation.name = item.name;
650
- existingOrderItems.push(existingItemByVariation);
651
- }
652
- }
653
- return existingOrderItems;
654
- }
655
29
  //# sourceMappingURL=sync-all-marketplace-order.js.map