@things-factory/integration-sellercraft 4.2.7 → 4.2.8

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.
@@ -4,6 +4,7 @@ import { getRepository } from 'typeorm'
4
4
  import { config } from '@things-factory/env'
5
5
  import { StoreAPI } from '@things-factory/integration-marketplace'
6
6
 
7
+ import { SHIPPING_TYPE } from '../../constants'
7
8
  import { SellercraftChannelIntegrationAPI } from '../../controllers/sellercraft-channel-integration-api'
8
9
  import { MarketplaceChannel } from './marketplace-channel'
9
10
 
@@ -22,7 +23,8 @@ export class MarketplaceChannelOrderMutation {
22
23
 
23
24
  for (var i = 0; i < channels.length; i++) {
24
25
  try {
25
- var channelsFullPath = getShopsTokenCraftUrl + '?channel_id=' + channels[i].channelId
26
+ const channel: MarketplaceChannel = channels[i]
27
+ var channelsFullPath = getShopsTokenCraftUrl + '?channel_id=' + channel.channelId
26
28
  const channelResponse: any = await fetch(channelsFullPath, {
27
29
  method: 'get',
28
30
  headers: {
@@ -38,311 +40,348 @@ export class MarketplaceChannelOrderMutation {
38
40
  var shops = shopsResponse.shops
39
41
 
40
42
  for (var j = 0; j < shops.length; j++) {
41
- var store = {
42
- accessKey: shops[j]?.credential?.consumer_key || '',
43
- accessSecret: shops[j]?.credential?.consumer_secret || '',
44
- storeURL: shops[j]?.credential?.store_url || '',
45
- platform: channels[i].name,
46
- accessToken: shops[j]?.credential?.access_token, // Magento+, Tiktok
47
- channelShopId: shops[j]?.channel_shop_id
48
- }
49
-
50
- // let countryCode = shops[j].country_code
51
- // let channelCode = shops[j].org_prefix
52
- let organisationId = shops[j].account_id
53
- let channelShopId = shops[j].channel_shop_id
43
+ try {
44
+ var store = {
45
+ accessKey: shops[j]?.credential?.consumer_key || '',
46
+ accessSecret: shops[j]?.credential?.consumer_secret || '',
47
+ storeURL: shops[j]?.credential?.store_url || '',
48
+ platform: channel.name,
49
+ accessToken: shops[j]?.credential?.access_token, // Magento+, Tiktok
50
+ channelShopId: shops[j]?.channel_shop_id
51
+ }
54
52
 
55
- let orderResult = []
56
- let page: number = 1
57
- let hasMorePage: boolean = true
58
- let lastOrderId: string
59
- let nextCursor: string
60
- var limit: number = 50
53
+ // let countryCode = shops[j].country_code
54
+ // let channelCode = shops[j].org_prefix
55
+ let organisationId = shops[j].account_id
56
+ let channelShopId = shops[j].channel_shop_id
61
57
 
62
- while (hasMorePage) {
63
- const {
64
- results: marketplaceOrders,
65
- more,
66
- nextCursor
67
- } = await StoreAPI.getStoreOrders(store, {
68
- fromDate,
69
- toDate,
70
- pagination: { page, limit },
71
- lastOrderId,
72
- nextCursor: lastOrderId
73
- })
58
+ let orderResult = []
59
+ let page: number = 1
60
+ let hasMorePage: boolean = true
61
+ let lastOrderId: string
62
+ let cursor: string
63
+ var limit: number = 50
74
64
 
75
- orderResult.push(...marketplaceOrders)
65
+ while (hasMorePage) {
66
+ const {
67
+ results: marketplaceOrders,
68
+ more,
69
+ nextCursor
70
+ } = await StoreAPI.getStoreOrders(store, {
71
+ fromDate,
72
+ toDate,
73
+ pagination: { page, limit },
74
+ lastOrderId,
75
+ nextCursor: cursor
76
+ })
76
77
 
77
- if (more) page++
78
- hasMorePage = more
79
- lastOrderId = nextCursor
80
- }
78
+ orderResult.push(...marketplaceOrders)
81
79
 
82
- var sellercraftStore = { ...store, platform: 'sellercraftChannelIntegration' }
80
+ if (more) page++
81
+ hasMorePage = more
82
+ cursor = nextCursor
83
+ }
83
84
 
84
- let mappedOrderResult = orderResult.map(order => {
85
- let {
86
- firstName: custFirstName,
87
- lastName: custLastName,
88
- orderCreatedAt: createdAt,
89
- orderUpdatedAt: updatedAt,
90
- orderNo: id,
91
- status
92
- } = order
85
+ var sellercraftStore = { ...store, platform: 'sellercraftChannelIntegration' }
93
86
 
94
- let {
95
- first_name: billFirstName,
96
- last_name: billLastName,
97
- address_1: billAddress1,
98
- address_2: billAddress2,
99
- address_3: billAddress3,
100
- address_4: billAddress4,
101
- address_5: billAddress5,
102
- city: billCity,
103
- postcode: billPostalCode,
104
- country: billCountry,
105
- phone: billPhone1,
106
- phone_2: billPhone2
107
- } = order?.billing
87
+ let mappedOrderResult = orderResult.map(order => {
88
+ let id = store.platform == 'magento' ? order.name : order.orderNo
89
+ let {
90
+ firstName: custFirstName,
91
+ lastName: custLastName,
92
+ orderCreatedAt: createdAt,
93
+ orderUpdatedAt: updatedAt,
94
+ status,
95
+ isSOF
96
+ } = order
108
97
 
109
- let {
110
- first_name: shipFirstName,
111
- last_name: shipLastName,
112
- address_1: shipAddress1,
113
- address_2: shipAddress2,
114
- address_3: shipAddress3,
115
- address_4: shipAddress4,
116
- address_5: shipAddress5,
117
- city: shipCity,
118
- postcode: shipPostalCode,
119
- country: shipCountry,
120
- phone: shipPhone1,
121
- phone_2: shipPhone2
122
- } = order.shipping
98
+ let {
99
+ first_name: billFirstName,
100
+ last_name: billLastName,
101
+ address_1: billAddress1,
102
+ address_2: billAddress2,
103
+ address_3: billAddress3,
104
+ address_4: billAddress4,
105
+ address_5: billAddress5,
106
+ city: billCity,
107
+ postcode: billPostalCode,
108
+ country: billCountry,
109
+ phone: billPhone1,
110
+ phone_2: billPhone2
111
+ } = order?.billing
123
112
 
124
- let orderItems = order.orderItems.map(item => {
125
113
  let {
126
- name: id,
127
- variationId: variationId,
128
- slaExpiresAt,
129
- total,
130
- totalTax,
131
- subtotal,
132
- subtotalTax,
133
- qty
134
- } = item
114
+ first_name: shipFirstName,
115
+ last_name: shipLastName,
116
+ address_1: shipAddress1,
117
+ address_2: shipAddress2,
118
+ address_3: shipAddress3,
119
+ address_4: shipAddress4,
120
+ address_5: shipAddress5,
121
+ city: shipCity,
122
+ postcode: shipPostalCode,
123
+ country: shipCountry,
124
+ phone: shipPhone1,
125
+ phone_2: shipPhone2
126
+ } = order.shipping
127
+
128
+ let orderPackage = order?.orderPackage || {}
129
+
130
+ let orderItems = order.orderItems.map(item => {
131
+ let {
132
+ name: id,
133
+ variationId: variationId,
134
+ slaExpiresAt,
135
+ total,
136
+ totalTax,
137
+ subtotal,
138
+ subtotalTax,
139
+ qty
140
+ } = item
141
+
142
+ return {
143
+ id,
144
+ variationId,
145
+ currency: order.orderShipping.collectionCurrency,
146
+ createdAt: order.orderCreatedAt,
147
+ updatedAt: order.orderUpdatedAt,
148
+ charges: [
149
+ {
150
+ name: 'CHARGES_MARKETING',
151
+ grossAmount: 0,
152
+ nettAmount: 0
153
+ },
154
+ {
155
+ name: 'CLAIMS_DAMAGE',
156
+ grossAmount: 0,
157
+ nettAmount: 0
158
+ },
159
+ {
160
+ name: 'CLAIMS_LOST',
161
+ grossAmount: 0,
162
+ nettAmount: 0
163
+ },
164
+ {
165
+ name: 'COMMISSION_PLATFORM',
166
+ grossAmount: 0,
167
+ nettAmount: 0
168
+ },
169
+ {
170
+ name: 'DEPOSIT_PRESALE',
171
+ grossAmount: 0,
172
+ nettAmount: 0
173
+ },
174
+ {
175
+ name: 'FEE_MISCELLANEOUS',
176
+ grossAmount: 0,
177
+ nettAmount: 0
178
+ },
179
+ {
180
+ name: 'FEE_TRANSACTION',
181
+ grossAmount: 0,
182
+ nettAmount: 0
183
+ },
184
+ {
185
+ name: 'PRICE_NORMAL_SELLING',
186
+ grossAmount: total,
187
+ nettAmount: subtotal
188
+ },
189
+ {
190
+ name: 'PRICE_RECOMMENDED_RETAIL',
191
+ grossAmount: 0,
192
+ nettAmount: 0
193
+ },
194
+ {
195
+ name: 'PROMOTIONS_CUSTOMER_RECEIVED',
196
+ grossAmount: 0,
197
+ nettAmount: 0
198
+ },
199
+ {
200
+ name: 'PROMOTIONS_REBATE_PLATFORM',
201
+ grossAmount: 0,
202
+ nettAmount: 0
203
+ },
204
+ {
205
+ name: 'PROMOTIONS_REBATE_SELLER',
206
+ grossAmount: 0,
207
+ nettAmount: 0
208
+ },
209
+ {
210
+ name: 'REVERSAL_CHARGES_MARKETING',
211
+ grossAmount: 0,
212
+ nettAmount: 0
213
+ },
214
+ {
215
+ name: 'REVERSAL_COMMISSION',
216
+ grossAmount: 0,
217
+ nettAmount: 0
218
+ },
219
+ {
220
+ name: 'REVERSAL_FEE_MISCELLANEOUS',
221
+ grossAmount: 0,
222
+ nettAmount: 0
223
+ },
224
+ {
225
+ name: 'REVERSAL_PROMOTIONS_CUSTOMER_RECEIVED',
226
+ grossAmount: 0,
227
+ nettAmount: 0
228
+ },
229
+ {
230
+ name: 'REVERSAL_SELLER_RETURN_REFUND_AMOUNT',
231
+ grossAmount: 0,
232
+ nettAmount: 0
233
+ },
234
+ {
235
+ name: 'REVERSAL_SHIPPING_CUSTOMER_PAID',
236
+ grossAmount: 0,
237
+ nettAmount: 0
238
+ },
239
+ {
240
+ name: 'REVERSAL_SHIPPING_REBATE_PLATFORM',
241
+ grossAmount: 0,
242
+ nettAmount: 0
243
+ },
244
+ {
245
+ name: 'REVERSAL_SHIPPING_SELLER_PAID',
246
+ grossAmount: 0,
247
+ nettAmount: 0
248
+ },
249
+ {
250
+ name: 'SHIPPING_COST_TOTAL',
251
+ grossAmount: 0,
252
+ nettAmount: 0
253
+ },
254
+ {
255
+ name: 'SHIPPING_CUSTOMER_PAID',
256
+ grossAmount: 0,
257
+ nettAmount: 0
258
+ },
259
+ {
260
+ name: 'SHIPPING_REBATE_PLATFORM',
261
+ grossAmount: 0,
262
+ nettAmount: 0
263
+ },
264
+ {
265
+ name: 'SHIPPING_SELLER_PAID',
266
+ grossAmount: 0,
267
+ nettAmount: 0
268
+ },
269
+ {
270
+ name: 'TAXES',
271
+ grossAmount: totalTax,
272
+ nettAmount: subtotalTax
273
+ },
274
+ {
275
+ name: 'VOUCHERS_CUSTOMER_RECEIVED',
276
+ grossAmount: 0,
277
+ nettAmount: 0
278
+ },
279
+ {
280
+ name: 'VOUCHERS_REBATE_PLATFORM',
281
+ grossAmount: 0,
282
+ nettAmount: 0
283
+ },
284
+ {
285
+ name: 'VOUCHERS_REBATE_SELLER',
286
+ grossAmount: 0,
287
+ nettAmount: 0
288
+ }
289
+ ],
290
+ slaExpiresAt,
291
+ qty
292
+ }
293
+ })
294
+
295
+ let mappedOrderItems = []
296
+ orderItems.map(oi => {
297
+ for (let i = 0; i < oi.qty; i++) {
298
+ mappedOrderItems.push({
299
+ ...oi,
300
+ id: `${oi.id}-${i + 1}`
301
+ })
302
+ }
303
+ })
135
304
 
136
305
  return {
306
+ custFirstName,
307
+ custLastName,
308
+ createdAt,
309
+ updatedAt,
137
310
  id,
138
- variationId,
139
- currency: order.orderShipping.collectionCurrency,
140
- createdAt: order.orderCreatedAt,
141
- updatedAt: order.orderUpdatedAt,
142
- charges: [
143
- {
144
- name: 'CHARGES_MARKETING',
145
- grossAmount: 0,
146
- nettAmount: 0
147
- },
148
- {
149
- name: 'CLAIMS_DAMAGE',
150
- grossAmount: 0,
151
- nettAmount: 0
152
- },
153
- {
154
- name: 'CLAIMS_LOST',
155
- grossAmount: 0,
156
- nettAmount: 0
157
- },
158
- {
159
- name: 'COMMISSION_PLATFORM',
160
- grossAmount: 0,
161
- nettAmount: 0
162
- },
163
- {
164
- name: 'DEPOSIT_PRESALE',
165
- grossAmount: 0,
166
- nettAmount: 0
167
- },
168
- {
169
- name: 'FEE_MISCELLANEOUS',
170
- grossAmount: 0,
171
- nettAmount: 0
172
- },
173
- {
174
- name: 'FEE_TRANSACTION',
175
- grossAmount: 0,
176
- nettAmount: 0
177
- },
178
- {
179
- name: 'PRICE_NORMAL_SELLING',
180
- grossAmount: total,
181
- nettAmount: subtotal
182
- },
183
- {
184
- name: 'PRICE_RECOMMENDED_RETAIL',
185
- grossAmount: 0,
186
- nettAmount: 0
187
- },
188
- {
189
- name: 'PROMOTIONS_CUSTOMER_RECEIVED',
190
- grossAmount: 0,
191
- nettAmount: 0
192
- },
193
- {
194
- name: 'PROMOTIONS_REBATE_PLATFORM',
195
- grossAmount: 0,
196
- nettAmount: 0
197
- },
198
- {
199
- name: 'PROMOTIONS_REBATE_SELLER',
200
- grossAmount: 0,
201
- nettAmount: 0
202
- },
203
- {
204
- name: 'REVERSAL_CHARGES_MARKETING',
205
- grossAmount: 0,
206
- nettAmount: 0
207
- },
208
- {
209
- name: 'REVERSAL_COMMISSION',
210
- grossAmount: 0,
211
- nettAmount: 0
212
- },
213
- {
214
- name: 'REVERSAL_FEE_MISCELLANEOUS',
215
- grossAmount: 0,
216
- nettAmount: 0
217
- },
218
- {
219
- name: 'REVERSAL_PROMOTIONS_CUSTOMER_RECEIVED',
220
- grossAmount: 0,
221
- nettAmount: 0
222
- },
223
- {
224
- name: 'REVERSAL_SELLER_RETURN_REFUND_AMOUNT',
225
- grossAmount: 0,
226
- nettAmount: 0
227
- },
228
- {
229
- name: 'REVERSAL_SHIPPING_CUSTOMER_PAID',
230
- grossAmount: 0,
231
- nettAmount: 0
232
- },
233
- {
234
- name: 'REVERSAL_SHIPPING_REBATE_PLATFORM',
235
- grossAmount: 0,
236
- nettAmount: 0
237
- },
238
- {
239
- name: 'REVERSAL_SHIPPING_SELLER_PAID',
240
- grossAmount: 0,
241
- nettAmount: 0
242
- },
243
- {
244
- name: 'SHIPPING_COST_TOTAL',
245
- grossAmount: 0,
246
- nettAmount: 0
247
- },
248
- {
249
- name: 'SHIPPING_CUSTOMER_PAID',
250
- grossAmount: 0,
251
- nettAmount: 0
252
- },
253
- {
254
- name: 'SHIPPING_REBATE_PLATFORM',
255
- grossAmount: 0,
256
- nettAmount: 0
257
- },
258
- {
259
- name: 'SHIPPING_SELLER_PAID',
260
- grossAmount: 0,
261
- nettAmount: 0
262
- },
263
- {
264
- name: 'TAXES',
265
- grossAmount: totalTax,
266
- nettAmount: subtotalTax
267
- },
268
- {
269
- name: 'VOUCHERS_CUSTOMER_RECEIVED',
270
- grossAmount: 0,
271
- nettAmount: 0
272
- },
273
- {
274
- name: 'VOUCHERS_REBATE_PLATFORM',
275
- grossAmount: 0,
276
- nettAmount: 0
277
- },
278
- {
279
- name: 'VOUCHERS_REBATE_SELLER',
280
- grossAmount: 0,
281
- nettAmount: 0
282
- }
283
- ],
284
- slaExpiresAt,
285
- qty
311
+ billFirstName,
312
+ billLastName,
313
+ billAddress1: billAddress1.toString() || shipAddress1.toString(),
314
+ billAddress2: billAddress2 || shipAddress2,
315
+ billAddress3: billAddress3 || shipAddress3,
316
+ billAddress4: billAddress4 || shipAddress4,
317
+ billAddress5: billAddress5 || shipAddress5,
318
+ billCity: billCity || shipCity,
319
+ billPostalCode: billPostalCode || shipPostalCode,
320
+ billCountry: billCountry || shipCountry,
321
+ billPhone1: billPhone1 || shipPhone1,
322
+ billPhone2: billPhone2 || shipPhone2,
323
+ shipFirstName,
324
+ shipLastName,
325
+ shipAddress1: shipAddress1.toString(),
326
+ shipAddress2,
327
+ shipAddress3,
328
+ shipAddress4,
329
+ shipAddress5,
330
+ shipCity,
331
+ shipPostalCode,
332
+ shipCountry,
333
+ shipPhone1,
334
+ shipPhone2,
335
+ mappedOrderItems,
336
+ channelShopId,
337
+ organisationId,
338
+ status,
339
+ isSOF: isSOF ? isSOF : false,
340
+ orderPackage,
341
+ charges: getOrderCharges(mappedOrderItems)
286
342
  }
287
343
  })
288
344
 
289
- let mappedOrderItems = []
290
- orderItems.map(oi => {
291
- for (let i = 0; i < oi.qty; i++) {
292
- mappedOrderItems.push({
293
- ...oi,
294
- id: `${oi.id}-${i + 1}`
295
- })
296
- }
297
- })
345
+ if (mappedOrderResult.length > 0) {
346
+ await SellercraftChannelIntegrationAPI.ingestChannelOrder(sellercraftStore, { orders: mappedOrderResult })
298
347
 
299
- return {
300
- custFirstName,
301
- custLastName,
302
- createdAt,
303
- updatedAt,
304
- id,
305
- billFirstName,
306
- billLastName,
307
- billAddress1: billAddress1.toString() || shipAddress1.toString(),
308
- billAddress2: billAddress2 || shipAddress2,
309
- billAddress3: billAddress3 || shipAddress3,
310
- billAddress4: billAddress4 || shipAddress4,
311
- billAddress5: billAddress5 || shipAddress5,
312
- billCity: billCity || shipCity,
313
- billPostalCode: billPostalCode || shipPostalCode,
314
- billCountry: billCountry || shipCountry,
315
- billPhone1: billPhone1 || shipPhone1,
316
- billPhone2: billPhone2 || shipPhone2,
317
- shipFirstName,
318
- shipLastName,
319
- shipAddress1: shipAddress1.toString(),
320
- shipAddress2,
321
- shipAddress3,
322
- shipAddress4,
323
- shipAddress5,
324
- shipCity,
325
- shipPostalCode,
326
- shipCountry,
327
- shipPhone1,
328
- shipPhone2,
329
- mappedOrderItems,
330
- channelShopId,
331
- organisationId,
332
- status,
333
- charges: getOrderCharges(mappedOrderItems)
334
- }
335
- })
348
+ await Promise.all(
349
+ mappedOrderResult.map(async result => {
350
+ if (!result?.isSOF && result?.orderPackage?.packageId) {
351
+ let orderPackage: any = result.orderPackage
352
+ let newOrderPackage: any = {
353
+ channelShopId,
354
+ nativeOrderId: result.id,
355
+ nativePackageId: orderPackage.packageId,
356
+ shippingTrackingCode: orderPackage.trackingNumber,
357
+ shippingTypeValue: orderPackage?.shippingType
358
+ ? orderPackage.shippingType
359
+ : SHIPPING_TYPE.DROP_SHIPPING,
360
+ warehouseCode: SHIPPING_TYPE.DROP_SHIPPING,
361
+ shipper: {
362
+ name: orderPackage.shippingProvider,
363
+ isCodSupported: orderPackage?.isCodSupport ? orderPackage.isCodSupport : false
364
+ },
365
+ documents: orderPackage?.orderDocument || [],
366
+ shipperLastMile: {
367
+ name: orderPackage.shippingProvider,
368
+ isCodSupported: orderPackage?.isCodSupport ? orderPackage.isCodSupport : false
369
+ },
370
+ orderItemIds: orderPackage?.orderListIdList
371
+ ? orderPackage.orderListIdList
372
+ : result.mappedOrderItems.map(orderItem => orderItem.id)
373
+ }
336
374
 
337
- if (mappedOrderResult.length > 0) {
338
- await SellercraftChannelIntegrationAPI.ingestChannelOrder(sellercraftStore, { orders: mappedOrderResult })
339
- }
375
+ await SellercraftChannelIntegrationAPI.ingestChannelOrderPackage(sellercraftStore, newOrderPackage)
376
+ }
377
+ })
378
+ )
379
+ }
380
+ } catch (e) {}
340
381
  }
341
382
 
342
383
  return true
343
- } catch (e) {
344
- debugger
345
- }
384
+ } catch (e) {}
346
385
  }
347
386
  }
348
387
  }