@things-factory/integration-sellercraft 5.0.0-alpha.38 → 5.0.0-alpha.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist-server/constants/order-status-mapping.js +21 -3
  2. package/dist-server/constants/order-status-mapping.js.map +1 -1
  3. package/dist-server/constants/platform.js +3 -1
  4. package/dist-server/constants/platform.js.map +1 -1
  5. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-categories.js +3 -2
  6. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-categories.js.map +1 -1
  7. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-order-package.js +5 -2
  8. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-order-package.js.map +1 -1
  9. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-order.js +21 -14
  10. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-order.js.map +1 -1
  11. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-product.js +15 -11
  12. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-product.js.map +1 -1
  13. package/dist-server/controllers/sellercraft-channel-integration/sellercraft-channel-integration.js +8 -4
  14. package/dist-server/controllers/sellercraft-channel-integration/sellercraft-channel-integration.js.map +1 -1
  15. package/dist-server/routers/sellercraft-router.js +195 -75
  16. package/dist-server/routers/sellercraft-router.js.map +1 -1
  17. package/dist-server/service/marketplace-channel/marketplace-channel-order-mutation.js +317 -103
  18. package/dist-server/service/marketplace-channel/marketplace-channel-order-mutation.js.map +1 -1
  19. package/dist-server/service/marketplace-channel/marketplace-channel-product-mutation.js +83 -25
  20. package/dist-server/service/marketplace-channel/marketplace-channel-product-mutation.js.map +1 -1
  21. package/dist-server/service/marketplace-channel/marketplace-channel.js +5 -0
  22. package/dist-server/service/marketplace-channel/marketplace-channel.js.map +1 -1
  23. package/package.json +15 -15
  24. package/server/constants/order-status-mapping.ts +21 -2
  25. package/server/constants/platform.ts +3 -1
  26. package/server/controllers/sellercraft-channel-integration/apis/ingest-channel-categories.ts +4 -3
  27. package/server/controllers/sellercraft-channel-integration/apis/ingest-channel-order-package.ts +6 -2
  28. package/server/controllers/sellercraft-channel-integration/apis/ingest-channel-order.ts +22 -15
  29. package/server/controllers/sellercraft-channel-integration/apis/ingest-channel-product.ts +26 -21
  30. package/server/controllers/sellercraft-channel-integration/sellercraft-channel-integration.ts +8 -4
  31. package/server/routers/sellercraft-router.ts +199 -79
  32. package/server/service/marketplace-channel/marketplace-channel-order-mutation.ts +379 -149
  33. package/server/service/marketplace-channel/marketplace-channel-product-mutation.ts +83 -26
  34. package/server/service/marketplace-channel/marketplace-channel.ts +4 -0
@@ -1,11 +1,12 @@
1
1
  import { Arg, Ctx, Mutation, Resolver } from 'type-graphql'
2
2
  import { getRepository } from 'typeorm'
3
3
 
4
- import { MarketplaceChannel } from './marketplace-channel'
5
-
6
4
  import { config } from '@things-factory/env'
7
5
  import { StoreAPI } from '@things-factory/integration-marketplace'
6
+
7
+ import { SHIPPING_TYPE } from '../../constants'
8
8
  import { SellercraftChannelIntegrationAPI } from '../../controllers/sellercraft-channel-integration-api'
9
+ import { MarketplaceChannel } from './marketplace-channel'
9
10
 
10
11
  @Resolver()
11
12
  export class MarketplaceChannelOrderMutation {
@@ -18,165 +19,394 @@ export class MarketplaceChannelOrderMutation {
18
19
  const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
19
20
  const { tokenCraftApiKey: apiKey, getShopsTokenCraftUrl } = sellercraftChannelIntegrationConfig
20
21
 
21
- const channels: MarketplaceChannel[] = await getRepository(MarketplaceChannel).find()
22
+ const channels: MarketplaceChannel[] = await getRepository(MarketplaceChannel).find({ where: { isActive: true } })
22
23
 
23
24
  for (var i = 0; i < channels.length; i++) {
24
- var channelsFullPath = getShopsTokenCraftUrl + '?channel_id=' + channels[i].channelId
25
- const channelResponse: any = await fetch(channelsFullPath, {
26
- method: 'get',
27
- headers: {
28
- 'Content-Type': 'application/json',
29
- 'x-api-key': apiKey
30
- }
31
- })
25
+ try {
26
+ const channel: MarketplaceChannel = channels[i]
27
+ var channelsFullPath = getShopsTokenCraftUrl + '?channel_id=' + channel.channelId
28
+ const channelResponse: any = await fetch(channelsFullPath, {
29
+ method: 'get',
30
+ headers: {
31
+ 'Content-Type': 'application/json',
32
+ 'x-api-key': apiKey
33
+ }
34
+ })
32
35
 
33
- if (!channelResponse.ok) {
34
- throw new Error(channelResponse)
35
- }
36
- var shopsResponse = await channelResponse.json()
37
- var shops = shopsResponse.shops
38
-
39
- for (var j = 0; j < shops.length; j++) {
40
- var store = {
41
- accessKey: shops[j].credential.consumer_key,
42
- accessSecret: shops[j].credential.consumer_secret,
43
- storeURL: shops[j].credential.store_url,
44
- platform: channels[i].name,
45
- accessToken: shops[j].credential.access_token // Magento+
36
+ if (!channelResponse.ok) {
37
+ throw new Error(channelResponse)
46
38
  }
39
+ var shopsResponse = await channelResponse.json()
40
+ var shops = shopsResponse.shops
47
41
 
48
- // let countryCode = shops[j].country_code
49
- // let channelCode = shops[j].org_prefix
50
- let organisationId = shops[j].account_id
51
- let channelShopId = shops[j].channel_shop_id
42
+ for (var j = 0; j < shops.length; j++) {
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
+ }
52
52
 
53
- const orderReq = {
54
- fromDate: fromDate,
55
- toDate: toDate
56
- }
57
- const orderResult = await StoreAPI.getStoreOrders(store, orderReq)
58
-
59
- var sellercraftStore = { ...store, platform: 'sellercraftChannelIntegration' }
60
-
61
- let mappedOrderResult = orderResult.results.map(order => {
62
- let {
63
- firstName: custFirstName,
64
- lastName: custLastName,
65
- orderCreatedAt: createdAt,
66
- orderUpdatedAt: updatedAt,
67
- orderNo: id,
68
- status
69
- } = order
70
-
71
- let {
72
- first_name: billFirstName,
73
- last_name: billLastName,
74
- address_1: billAddress1,
75
- address_2: billAddress2,
76
- address_3: billAddress3,
77
- address_4: billAddress4,
78
- address_5: billAddress5,
79
- city: billCity,
80
- postcode: billPostalCode,
81
- country: billCountry,
82
- phone: billPhone1,
83
- phone_2: billPhone2
84
- } = order?.billing
85
-
86
- let {
87
- first_name: shipFirstName,
88
- last_name: shipLastName,
89
- address_1: shipAddress1,
90
- address_2: shipAddress2,
91
- address_3: shipAddress3,
92
- address_4: shipAddress4,
93
- address_5: shipAddress5,
94
- city: shipCity,
95
- postcode: shipPostalCode,
96
- country: shipCountry,
97
- phone: shipPhone1,
98
- phone_2: shipPhone2
99
- } = order.shipping
100
-
101
- let orderItems = order.orderItems.map(item => {
102
- let { name: id, variationId: variationId, slaExpiresAt, total, totalTax, subtotal, subtotalTax, qty } = item
103
-
104
- return {
105
- id,
106
- variationId,
107
- currency: order.orderShipping.collectionCurrency,
108
- createdAt: order.orderCreatedAt,
109
- updatedAt: order.orderUpdatedAt,
110
- charges: [
111
- {
112
- name: 'PRICE_NORMAL_SELLING',
113
- grossAmount: total,
114
- nettAmount: subtotal
115
- },
116
- {
117
- name: 'TAXES',
118
- grossAmount: totalTax,
119
- nettAmount: subtotalTax
120
- }
121
- ],
122
- slaExpiresAt,
123
- qty
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
57
+
58
+ let orderResult = []
59
+ let page: number = store.platform == 'magento' ? 1 : 0
60
+ let hasMorePage: boolean = true
61
+ let lastOrderId: string
62
+ let cursor: string
63
+ var limit: number = 50
64
+
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
+ })
77
+
78
+ orderResult.push(...marketplaceOrders)
79
+
80
+ if (more) page++
81
+ hasMorePage = more
82
+ cursor = nextCursor
124
83
  }
125
- })
126
-
127
- let mappedOrderItems = []
128
- orderItems.map(oi => {
129
- for (let i = 0; i < oi.qty; i++) {
130
- mappedOrderItems.push({
131
- ...oi,
132
- id: `${oi.id}-${i+1}`
84
+
85
+ var sellercraftStore = { ...store, platform: 'sellercraftChannelIntegration' }
86
+
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
97
+
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
112
+
113
+ let {
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
+ })
304
+
305
+ return {
306
+ custFirstName,
307
+ custLastName,
308
+ createdAt,
309
+ updatedAt,
310
+ id,
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)
342
+ }
343
+ })
344
+
345
+ if (mappedOrderResult.length > 0) {
346
+ await SellercraftChannelIntegrationAPI.ingestChannelOrder(sellercraftStore, {
347
+ orders: mappedOrderResult
133
348
  })
349
+
350
+ await Promise.all(
351
+ mappedOrderResult.map(async result => {
352
+ if (!result?.isSOF && result?.orderPackage?.packageId) {
353
+ let orderPackage: any = result.orderPackage
354
+ let newOrderPackage: any = {
355
+ channelShopId,
356
+ nativeOrderId: result.id,
357
+ nativePackageId: orderPackage.packageId,
358
+ shippingTrackingCode: orderPackage.trackingNumber,
359
+ shippingTypeValue: orderPackage?.shippingType
360
+ ? orderPackage.shippingType
361
+ : SHIPPING_TYPE.DROP_SHIPPING,
362
+ warehouseCode: SHIPPING_TYPE.DROP_SHIPPING,
363
+ shipper: {
364
+ name: orderPackage.shippingProvider,
365
+ isCodSupported: orderPackage?.isCodSupport ? orderPackage.isCodSupport : false
366
+ },
367
+ documents: orderPackage?.orderDocument || [],
368
+ shipperLastMile: {
369
+ name: orderPackage.shippingProvider,
370
+ isCodSupported: orderPackage?.isCodSupport ? orderPackage.isCodSupport : false
371
+ },
372
+ orderItemIds: orderPackage?.orderListIdList
373
+ ? orderPackage.orderListIdList
374
+ : result.mappedOrderItems.map(orderItem => orderItem.id)
375
+ }
376
+
377
+ await SellercraftChannelIntegrationAPI.ingestChannelOrderPackage(sellercraftStore, newOrderPackage)
378
+ }
379
+ })
380
+ )
134
381
  }
135
- })
136
-
137
- return {
138
- custFirstName,
139
- custLastName,
140
- createdAt,
141
- updatedAt,
142
- id,
143
- billFirstName,
144
- billLastName,
145
- billAddress1: billAddress1.toString() || shipAddress1.toString(),
146
- billAddress2: billAddress2 || shipAddress2,
147
- billAddress3: billAddress3 || shipAddress3,
148
- billAddress4: billAddress4 || shipAddress4,
149
- billAddress5: billAddress5 || shipAddress5,
150
- billCity: billCity || shipCity,
151
- billPostalCode: billPostalCode || shipPostalCode,
152
- billCountry: billCountry || shipCountry,
153
- billPhone1: billPhone1 || shipPhone1,
154
- billPhone2: billPhone2 || shipPhone2,
155
- shipFirstName,
156
- shipLastName,
157
- shipAddress1: shipAddress1.toString(),
158
- shipAddress2,
159
- shipAddress3,
160
- shipAddress4,
161
- shipAddress5,
162
- shipCity,
163
- shipPostalCode,
164
- shipCountry,
165
- shipPhone1,
166
- shipPhone2,
167
- mappedOrderItems,
168
- channelShopId,
169
- organisationId,
170
- status
171
- }
172
- })
382
+ } catch (e) {}
383
+ }
384
+ } catch (e) {}
385
+ }
386
+ return true
387
+ }
388
+ }
173
389
 
174
- if (mappedOrderResult.length > 0) {
175
- await SellercraftChannelIntegrationAPI.ingestChannelOrder(sellercraftStore, { orders: mappedOrderResult })
390
+ function getOrderCharges(mappedOrderItems) {
391
+ let chargesList = []
392
+
393
+ for (let i = 0; i < mappedOrderItems.length; i++) {
394
+ for (let j = 0; j < mappedOrderItems[i].charges.length; j++) {
395
+ let charge = mappedOrderItems[i].charges[j]
396
+ let foundCharge = chargesList.find(cl => cl.name == charge.name)
397
+ if (foundCharge) {
398
+ foundCharge.grossAmount = parseFloat(foundCharge.grossAmount) + parseFloat(charge.grossAmount)
399
+ foundCharge.nettAmount = parseFloat(foundCharge.nettAmount) + parseFloat(charge.nettAmount)
400
+ chargesList = chargesList.filter(cl => cl.name != charge.name)
401
+ } else {
402
+ foundCharge = {
403
+ name: charge.name,
404
+ grossAmount: charge.grossAmount,
405
+ nettAmount: charge.nettAmount
176
406
  }
177
407
  }
178
-
179
- return true
408
+ chargesList.push(foundCharge)
180
409
  }
181
410
  }
411
+ return chargesList
182
412
  }