@things-factory/integration-sellercraft 5.0.0-alpha.4 → 5.0.0-alpha.40
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-server/constants/index.js +1 -0
- package/dist-server/constants/index.js.map +1 -1
- package/dist-server/constants/order-status-mapping.js +28 -0
- package/dist-server/constants/order-status-mapping.js.map +1 -0
- package/dist-server/constants/platform.js +3 -1
- package/dist-server/constants/platform.js.map +1 -1
- package/dist-server/controllers/index.js +2 -0
- package/dist-server/controllers/index.js.map +1 -1
- package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-categories.js +19 -20
- package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-categories.js.map +1 -1
- package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-order-package.js +5 -2
- package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-order-package.js.map +1 -1
- package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-order.js +29 -19
- package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-order.js.map +1 -1
- package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-product.js +24 -40
- package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-product.js.map +1 -1
- package/dist-server/controllers/sellercraft-channel-integration/sellercraft-channel-integration.js +9 -5
- package/dist-server/controllers/sellercraft-channel-integration/sellercraft-channel-integration.js.map +1 -1
- package/dist-server/routers/sellercraft-router.js +220 -84
- package/dist-server/routers/sellercraft-router.js.map +1 -1
- package/dist-server/service/index.js +5 -1
- package/dist-server/service/index.js.map +1 -1
- package/dist-server/service/marketplace-channel/index.js +9 -0
- package/dist-server/service/marketplace-channel/index.js.map +1 -0
- package/dist-server/service/marketplace-channel/marketplace-channel-order-mutation.js +364 -0
- package/dist-server/service/marketplace-channel/marketplace-channel-order-mutation.js.map +1 -0
- package/dist-server/service/marketplace-channel/marketplace-channel-product-mutation.js +207 -0
- package/dist-server/service/marketplace-channel/marketplace-channel-product-mutation.js.map +1 -0
- package/dist-server/service/marketplace-channel/marketplace-channel.js +88 -0
- package/dist-server/service/marketplace-channel/marketplace-channel.js.map +1 -0
- package/dist-server/service/sellercraft/sellercraft.js +1 -1
- package/package.json +15 -15
- package/server/constants/index.ts +2 -1
- package/server/constants/order-status-mapping.ts +25 -0
- package/server/constants/platform.ts +3 -1
- package/server/controllers/index.ts +2 -0
- package/server/controllers/sellercraft-channel-integration/apis/ingest-channel-categories.ts +19 -20
- package/server/controllers/sellercraft-channel-integration/apis/ingest-channel-order-package.ts +6 -2
- package/server/controllers/sellercraft-channel-integration/apis/ingest-channel-order.ts +30 -19
- package/server/controllers/sellercraft-channel-integration/apis/ingest-channel-product.ts +32 -47
- package/server/controllers/sellercraft-channel-integration/sellercraft-channel-integration.ts +9 -5
- package/server/routers/sellercraft-router.ts +225 -87
- package/server/service/index.ts +6 -1
- package/server/service/marketplace-channel/index.ts +6 -0
- package/server/service/marketplace-channel/marketplace-channel-order-mutation.ts +412 -0
- package/server/service/marketplace-channel/marketplace-channel-product-mutation.ts +218 -0
- package/server/service/marketplace-channel/marketplace-channel.ts +68 -0
- package/server/service/sellercraft/sellercraft.ts +1 -1
@@ -0,0 +1,412 @@
|
|
1
|
+
import { Arg, Ctx, Mutation, Resolver } from 'type-graphql'
|
2
|
+
import { getRepository } from 'typeorm'
|
3
|
+
|
4
|
+
import { config } from '@things-factory/env'
|
5
|
+
import { StoreAPI } from '@things-factory/integration-marketplace'
|
6
|
+
|
7
|
+
import { SHIPPING_TYPE } from '../../constants'
|
8
|
+
import { SellercraftChannelIntegrationAPI } from '../../controllers/sellercraft-channel-integration-api'
|
9
|
+
import { MarketplaceChannel } from './marketplace-channel'
|
10
|
+
|
11
|
+
@Resolver()
|
12
|
+
export class MarketplaceChannelOrderMutation {
|
13
|
+
@Mutation(returns => Boolean)
|
14
|
+
async syncAllMarketplaceChannelOrders(
|
15
|
+
@Arg('fromDate') fromDate: string,
|
16
|
+
@Arg('toDate') toDate: string,
|
17
|
+
@Ctx() context: any
|
18
|
+
): Promise<boolean> {
|
19
|
+
const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
|
20
|
+
const { tokenCraftApiKey: apiKey, getShopsTokenCraftUrl } = sellercraftChannelIntegrationConfig
|
21
|
+
|
22
|
+
const channels: MarketplaceChannel[] = await getRepository(MarketplaceChannel).find({ where: { isActive: true } })
|
23
|
+
|
24
|
+
for (var i = 0; i < channels.length; i++) {
|
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
|
+
})
|
35
|
+
|
36
|
+
if (!channelResponse.ok) {
|
37
|
+
throw new Error(channelResponse)
|
38
|
+
}
|
39
|
+
var shopsResponse = await channelResponse.json()
|
40
|
+
var shops = shopsResponse.shops
|
41
|
+
|
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
|
+
|
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
|
83
|
+
}
|
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
|
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
|
+
)
|
381
|
+
}
|
382
|
+
} catch (e) {}
|
383
|
+
}
|
384
|
+
} catch (e) {}
|
385
|
+
}
|
386
|
+
return true
|
387
|
+
}
|
388
|
+
}
|
389
|
+
|
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
|
406
|
+
}
|
407
|
+
}
|
408
|
+
chargesList.push(foundCharge)
|
409
|
+
}
|
410
|
+
}
|
411
|
+
return chargesList
|
412
|
+
}
|
@@ -0,0 +1,218 @@
|
|
1
|
+
import { Ctx, Mutation, Resolver } from 'type-graphql'
|
2
|
+
import { getRepository } from 'typeorm'
|
3
|
+
|
4
|
+
import { config } from '@things-factory/env'
|
5
|
+
import { StoreAPI } from '@things-factory/integration-marketplace'
|
6
|
+
|
7
|
+
import { SellercraftChannelIntegrationAPI } from '../../controllers/sellercraft-channel-integration-api'
|
8
|
+
import { MarketplaceChannel } from './marketplace-channel'
|
9
|
+
|
10
|
+
@Resolver()
|
11
|
+
export class MarketplaceChannelProductMutation {
|
12
|
+
@Mutation(returns => Boolean)
|
13
|
+
async syncAllMarketplaceChannelProducts(@Ctx() context: any): Promise<boolean> {
|
14
|
+
const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
|
15
|
+
const { tokenCraftApiKey: apiKey, getShopsTokenCraftUrl } = sellercraftChannelIntegrationConfig
|
16
|
+
|
17
|
+
const channels: MarketplaceChannel[] = await getRepository(MarketplaceChannel).find({ where: { isActive: true } })
|
18
|
+
|
19
|
+
for (var i = 0; i < channels.length; i++) {
|
20
|
+
var channelsFullPath = getShopsTokenCraftUrl + '?channel_id=' + channels[i].channelId
|
21
|
+
const channelResponse: any = await fetch(channelsFullPath, {
|
22
|
+
method: 'get',
|
23
|
+
headers: {
|
24
|
+
'Content-Type': 'application/json',
|
25
|
+
'x-api-key': apiKey
|
26
|
+
}
|
27
|
+
})
|
28
|
+
|
29
|
+
if (!channelResponse.ok) {
|
30
|
+
throw new Error(channelResponse)
|
31
|
+
}
|
32
|
+
var shopsResponse = await channelResponse.json()
|
33
|
+
var shops = shopsResponse.shops
|
34
|
+
|
35
|
+
for (var j = 0; j < shops.length; j++) {
|
36
|
+
var store = {
|
37
|
+
accessKey: shops[j]?.credential?.consumer_key || '',
|
38
|
+
accessSecret: shops[j]?.credential?.consumer_secret || '',
|
39
|
+
storeURL: shops[j]?.credential?.store_url || '',
|
40
|
+
platform: channels[i].name,
|
41
|
+
accessToken: shops[j]?.credential?.access_token, // Magento+, Tiktok
|
42
|
+
channelShopId: shops[j]?.channel_shop_id
|
43
|
+
}
|
44
|
+
|
45
|
+
let countryCode = shops[j].country_code
|
46
|
+
let channelCode = shops[j].org_prefix
|
47
|
+
let organisationId = shops[j].account_id
|
48
|
+
let channelShopId = shops[j].channel_shop_id
|
49
|
+
|
50
|
+
var sellercraftStore = { ...store, platform: 'sellercraftChannelIntegration' }
|
51
|
+
|
52
|
+
const productResult = []
|
53
|
+
let totalPages: number = 1
|
54
|
+
let limit: number = 50
|
55
|
+
let parentLinks = []
|
56
|
+
|
57
|
+
for (let page = 0; page < totalPages; page++) {
|
58
|
+
const { results, total, parentLinkList } = await StoreAPI.getStoreProducts(store, {
|
59
|
+
pagination: { page, limit }
|
60
|
+
})
|
61
|
+
totalPages = Math.ceil(total / limit)
|
62
|
+
productResult.push(...results)
|
63
|
+
if (store.platform == 'magento') parentLinks.push(...parentLinkList)
|
64
|
+
}
|
65
|
+
|
66
|
+
const categoryResult = []
|
67
|
+
let totalPagesCategory: number = 1
|
68
|
+
let limitCategory: number = 100
|
69
|
+
|
70
|
+
for (let page = 0; page < totalPagesCategory; page++) {
|
71
|
+
const { results, total } = await StoreAPI.getStoreProductCategories(store, {
|
72
|
+
pagination: { page, limitCategory }
|
73
|
+
})
|
74
|
+
totalPagesCategory = Math.ceil(total / limitCategory)
|
75
|
+
categoryResult.push(...results)
|
76
|
+
}
|
77
|
+
|
78
|
+
let mappedProducts = productResult.map(item => {
|
79
|
+
let { categoryId, itemId: productId, name, brand, isVerified, images, attributes, variations } = item
|
80
|
+
|
81
|
+
variations = variations.map(variation => {
|
82
|
+
let {
|
83
|
+
variationSku,
|
84
|
+
variationId,
|
85
|
+
name,
|
86
|
+
isEnabled: isEnabled,
|
87
|
+
isSellable: isSellable,
|
88
|
+
attributes,
|
89
|
+
stockLocked,
|
90
|
+
qty: stockReported,
|
91
|
+
costPrice: fullPrice,
|
92
|
+
sellPrice: priceDiscounted,
|
93
|
+
length,
|
94
|
+
width,
|
95
|
+
height,
|
96
|
+
weight
|
97
|
+
} = variation
|
98
|
+
|
99
|
+
return {
|
100
|
+
variationSku,
|
101
|
+
variationId,
|
102
|
+
name,
|
103
|
+
isEnabled,
|
104
|
+
isSellable,
|
105
|
+
attributes,
|
106
|
+
stockLocked,
|
107
|
+
stockReported,
|
108
|
+
fullPrice,
|
109
|
+
priceDiscounted,
|
110
|
+
inventoryProducts: [
|
111
|
+
{
|
112
|
+
qty: 1,
|
113
|
+
name: `${name} - ${variationSku}`,
|
114
|
+
sku: variationSku,
|
115
|
+
productVersions: [
|
116
|
+
{
|
117
|
+
label: 'Default',
|
118
|
+
packageLengthMM: length,
|
119
|
+
packageWidthMM: width,
|
120
|
+
packageHeightMM: height,
|
121
|
+
packageWeightGram: weight,
|
122
|
+
qty: stockReported
|
123
|
+
}
|
124
|
+
]
|
125
|
+
}
|
126
|
+
]
|
127
|
+
}
|
128
|
+
})
|
129
|
+
|
130
|
+
images = images?.map(image => {
|
131
|
+
return {
|
132
|
+
url: image
|
133
|
+
}
|
134
|
+
})
|
135
|
+
|
136
|
+
return {
|
137
|
+
organisationId,
|
138
|
+
channelShopId: channelShopId,
|
139
|
+
channelCode: channels[i].channelCode,
|
140
|
+
channelCountry: shops[j].country_code,
|
141
|
+
categoryId,
|
142
|
+
productId: parentLinks.find(e => e.children.includes(productId))?.id || productId,
|
143
|
+
name,
|
144
|
+
brand,
|
145
|
+
isVerified,
|
146
|
+
images,
|
147
|
+
attributes,
|
148
|
+
variations
|
149
|
+
}
|
150
|
+
})
|
151
|
+
|
152
|
+
let mappedCategories = categoryResult.map(category => {
|
153
|
+
let { id: categoryId, name: categoryName, parent, isActive } = category
|
154
|
+
|
155
|
+
return {
|
156
|
+
categoryId,
|
157
|
+
categoryName,
|
158
|
+
parent,
|
159
|
+
isActive: isActive || true,
|
160
|
+
channelCode,
|
161
|
+
countryCode,
|
162
|
+
childrenCategories: []
|
163
|
+
}
|
164
|
+
})
|
165
|
+
|
166
|
+
if (store.platform == 'magento') {
|
167
|
+
let newList = []
|
168
|
+
for (let np of mappedProducts) {
|
169
|
+
if (np.productId == np.variations[0].variationId) {
|
170
|
+
let vars = mappedProducts
|
171
|
+
.filter(e => e.productId == np.productId)
|
172
|
+
.map(e => {
|
173
|
+
return e.variations[0]
|
174
|
+
})
|
175
|
+
np.variations = vars
|
176
|
+
if (np.variations.length > 1) {
|
177
|
+
np.variations = np.variations.filter(v => v.variationId != np.productId)
|
178
|
+
}
|
179
|
+
newList.push(np)
|
180
|
+
}
|
181
|
+
}
|
182
|
+
mappedProducts = newList
|
183
|
+
}
|
184
|
+
|
185
|
+
try {
|
186
|
+
let filterList = []
|
187
|
+
mappedCategories = mappedCategories.map(category => {
|
188
|
+
if (mappedCategories.filter(e => e.parent == category.categoryId).length > 0) {
|
189
|
+
category.childrenCategories = mappedCategories.filter(e => e.parent == category.categoryId)
|
190
|
+
filterList.push(...mappedCategories.filter(e => e.parent == category.categoryId))
|
191
|
+
}
|
192
|
+
return category
|
193
|
+
})
|
194
|
+
|
195
|
+
mappedCategories = mappedCategories.map(mc => {
|
196
|
+
if (filterList.indexOf(mc) == -1) {
|
197
|
+
return mc
|
198
|
+
}
|
199
|
+
}).filter(e => e)
|
200
|
+
|
201
|
+
await SellercraftChannelIntegrationAPI.ingestChannelCategories(sellercraftStore, {
|
202
|
+
categories: mappedCategories
|
203
|
+
})
|
204
|
+
} catch (e) {}
|
205
|
+
|
206
|
+
try {
|
207
|
+
for (let k = 0, l = mappedProducts.length; k < l; k++) {
|
208
|
+
await SellercraftChannelIntegrationAPI.ingestChannelProduct(sellercraftStore, {
|
209
|
+
products: [mappedProducts[k]]
|
210
|
+
})
|
211
|
+
}
|
212
|
+
} catch (e) {}
|
213
|
+
}
|
214
|
+
|
215
|
+
}
|
216
|
+
return true
|
217
|
+
}
|
218
|
+
}
|
@@ -0,0 +1,68 @@
|
|
1
|
+
import { Field, ID, ObjectType } from 'type-graphql'
|
2
|
+
import {
|
3
|
+
Column,
|
4
|
+
CreateDateColumn,
|
5
|
+
Entity,
|
6
|
+
Index,
|
7
|
+
ManyToOne,
|
8
|
+
PrimaryGeneratedColumn,
|
9
|
+
RelationId,
|
10
|
+
UpdateDateColumn
|
11
|
+
} from 'typeorm'
|
12
|
+
|
13
|
+
import { User } from '@things-factory/auth-base'
|
14
|
+
import { Domain } from '@things-factory/shell'
|
15
|
+
|
16
|
+
@Entity()
|
17
|
+
@Index('ix_marketplace_channel_0 ', (marketplaceChannel: MarketplaceChannel) => [marketplaceChannel.domain, marketplaceChannel.name], { unique: true })
|
18
|
+
@ObjectType({ description: 'Entity for Marketplace Channel' })
|
19
|
+
export class MarketplaceChannel {
|
20
|
+
@PrimaryGeneratedColumn('uuid')
|
21
|
+
@Field(type => ID)
|
22
|
+
readonly id: string
|
23
|
+
|
24
|
+
@ManyToOne(type => Domain)
|
25
|
+
@Field({ nullable: true })
|
26
|
+
domain?: Domain
|
27
|
+
|
28
|
+
@RelationId((marketplaceChannel: MarketplaceChannel) => marketplaceChannel.domain)
|
29
|
+
domainId?: string
|
30
|
+
|
31
|
+
@Column()
|
32
|
+
@Field()
|
33
|
+
name: string
|
34
|
+
|
35
|
+
@Column()
|
36
|
+
@Field()
|
37
|
+
channelCode: string
|
38
|
+
|
39
|
+
@Column()
|
40
|
+
@Field()
|
41
|
+
channelId: string
|
42
|
+
|
43
|
+
@Column({ default: true })
|
44
|
+
@Field()
|
45
|
+
isActive: boolean
|
46
|
+
|
47
|
+
@CreateDateColumn()
|
48
|
+
@Field({ nullable: true })
|
49
|
+
createdAt?: Date
|
50
|
+
|
51
|
+
@UpdateDateColumn()
|
52
|
+
@Field({ nullable: true })
|
53
|
+
updatedAt?: Date
|
54
|
+
|
55
|
+
@ManyToOne(type => User, { nullable: true })
|
56
|
+
@Field({ nullable: true })
|
57
|
+
creator?: User
|
58
|
+
|
59
|
+
@RelationId((marketplaceChannel: MarketplaceChannel) => marketplaceChannel.creator)
|
60
|
+
creatorId?: string
|
61
|
+
|
62
|
+
@ManyToOne(type => User, { nullable: true })
|
63
|
+
@Field({ nullable: true })
|
64
|
+
updater?: User
|
65
|
+
|
66
|
+
@RelationId((marketplaceChannel: MarketplaceChannel) => marketplaceChannel.updater)
|
67
|
+
updaterId?: string
|
68
|
+
}
|