@things-factory/integration-sellercraft 5.0.0-alpha.8 → 5.0.0-zeta.0

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 (63) hide show
  1. package/dist-server/constants/index.js +1 -0
  2. package/dist-server/constants/index.js.map +1 -1
  3. package/dist-server/constants/order-status-mapping.js +31 -0
  4. package/dist-server/constants/order-status-mapping.js.map +1 -0
  5. package/dist-server/constants/platform.js +4 -1
  6. package/dist-server/constants/platform.js.map +1 -1
  7. package/dist-server/controllers/index.js +2 -0
  8. package/dist-server/controllers/index.js.map +1 -1
  9. package/dist-server/controllers/sellercraft/sellercraft.js +2 -2
  10. package/dist-server/controllers/sellercraft/sellercraft.js.map +1 -1
  11. package/dist-server/controllers/sellercraft-api/decorators.js +2 -0
  12. package/dist-server/controllers/sellercraft-api/decorators.js.map +1 -1
  13. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-categories.js +19 -20
  14. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-categories.js.map +1 -1
  15. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-order-package.js +5 -2
  16. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-order-package.js.map +1 -1
  17. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-order.js +29 -19
  18. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-order.js.map +1 -1
  19. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-product.js +27 -41
  20. package/dist-server/controllers/sellercraft-channel-integration/apis/ingest-channel-product.js.map +1 -1
  21. package/dist-server/controllers/sellercraft-channel-integration/sellercraft-channel-integration.js +18 -7
  22. package/dist-server/controllers/sellercraft-channel-integration/sellercraft-channel-integration.js.map +1 -1
  23. package/dist-server/routers/sellercraft-router.js +221 -135
  24. package/dist-server/routers/sellercraft-router.js.map +1 -1
  25. package/dist-server/service/index.js +5 -1
  26. package/dist-server/service/index.js.map +1 -1
  27. package/dist-server/service/marketplace-channel/index.js +9 -0
  28. package/dist-server/service/marketplace-channel/index.js.map +1 -0
  29. package/dist-server/service/marketplace-channel/marketplace-channel-order-mutation.js +366 -0
  30. package/dist-server/service/marketplace-channel/marketplace-channel-order-mutation.js.map +1 -0
  31. package/dist-server/service/marketplace-channel/marketplace-channel-product-mutation.js +212 -0
  32. package/dist-server/service/marketplace-channel/marketplace-channel-product-mutation.js.map +1 -0
  33. package/dist-server/service/marketplace-channel/marketplace-channel.js +88 -0
  34. package/dist-server/service/marketplace-channel/marketplace-channel.js.map +1 -0
  35. package/dist-server/service/sellercraft/sellercraft-mutation.js +3 -6
  36. package/dist-server/service/sellercraft/sellercraft-mutation.js.map +1 -1
  37. package/dist-server/service/sellercraft/sellercraft-query.js +1 -1
  38. package/dist-server/service/sellercraft/sellercraft-query.js.map +1 -1
  39. package/dist-server/service/sellercraft/sellercraft.js +1 -1
  40. package/dist-server/utils/tokencraft-util.js +63 -0
  41. package/dist-server/utils/tokencraft-util.js.map +1 -0
  42. package/package.json +15 -15
  43. package/server/constants/index.ts +2 -1
  44. package/server/constants/order-status-mapping.ts +28 -0
  45. package/server/constants/platform.ts +4 -1
  46. package/server/controllers/index.ts +2 -0
  47. package/server/controllers/sellercraft/sellercraft.ts +2 -2
  48. package/server/controllers/sellercraft-api/decorators.ts +3 -0
  49. package/server/controllers/sellercraft-channel-integration/apis/ingest-channel-categories.ts +19 -20
  50. package/server/controllers/sellercraft-channel-integration/apis/ingest-channel-order-package.ts +6 -2
  51. package/server/controllers/sellercraft-channel-integration/apis/ingest-channel-order.ts +30 -19
  52. package/server/controllers/sellercraft-channel-integration/apis/ingest-channel-product.ts +34 -48
  53. package/server/controllers/sellercraft-channel-integration/sellercraft-channel-integration.ts +16 -7
  54. package/server/routers/sellercraft-router.ts +231 -147
  55. package/server/service/index.ts +6 -1
  56. package/server/service/marketplace-channel/index.ts +6 -0
  57. package/server/service/marketplace-channel/marketplace-channel-order-mutation.ts +418 -0
  58. package/server/service/marketplace-channel/marketplace-channel-product-mutation.ts +221 -0
  59. package/server/service/marketplace-channel/marketplace-channel.ts +68 -0
  60. package/server/service/sellercraft/sellercraft-mutation.ts +3 -3
  61. package/server/service/sellercraft/sellercraft-query.ts +1 -1
  62. package/server/service/sellercraft/sellercraft.ts +1 -1
  63. package/server/utils/tokencraft-util.ts +60 -0
@@ -11,35 +11,27 @@ export function ingestChannelProduct() {
11
11
  let productVariations: any[] = product.variations.map(variant => {
12
12
  return {
13
13
  seller_sku: variant.variationSku,
14
- native_variant_id: variant.variationId,
14
+ native_variant_id: variant.variationId.toString(),
15
15
  label: variant.name,
16
- is_enabled: variant.isEnabled,
17
- is_sellable: variant.isSellable,
16
+ is_enabled: variant.isEnabled || true,
17
+ is_sellable: variant.isSellable || true,
18
+ is_deleted: false, // default
18
19
  variant_attributes: variant?.attributes
19
- ? variant.attributes.map(attribute => {
20
+ ? variant?.attributes.map(attribute => {
20
21
  return {
21
- attribute_key: attribute.id,
22
- values: []
22
+ ...attribute,
23
+ native_attribute_id: attribute?.native_attribute_id?.toString()
23
24
  }
24
25
  })
25
26
  : [],
26
27
  stock_locked: variant?.stockLocked ? variant.stockLocked : 0,
27
28
  native_stock_reported: variant?.stockReported ? variant.stockReported : 0,
28
- price_full: variant.fullPrice,
29
- price_discounted: variant.priceDiscounted,
30
- price_discounts: variant?.product_discounts
31
- ? variant.product_discounts.map(discount => {
32
- return {
33
- price_discounted: discount.priceDiscounted,
34
- native_discount_id: discount.id,
35
- name: discount.name
36
- }
37
- })
38
- : [],
29
+ price_full: variant.fullPrice || 0,
30
+ price_discounted: variant.priceDiscounted || variant.fullPrice || 0,
39
31
  inventory_products: variant?.inventoryProducts
40
32
  ? variant.inventoryProducts.map(inventoryProduct => {
41
33
  return {
42
- quantity: inventoryProduct.qty,
34
+ quantity: inventoryProduct.qty || 0,
43
35
  name: inventoryProduct.name,
44
36
  inventory_sku: inventoryProduct.sku,
45
37
  product_versions: inventoryProduct?.productVersions
@@ -59,7 +51,8 @@ export function ingestChannelProduct() {
59
51
  : []
60
52
  }
61
53
  })
62
- : []
54
+ : [],
55
+ extra_metadata: variant?.extraMetadata
63
56
  }
64
57
  })
65
58
 
@@ -68,43 +61,36 @@ export function ingestChannelProduct() {
68
61
  channel_shop_id: product.channelShopId,
69
62
  channel_code: product.channelCode,
70
63
  channel_country: product.channelCountry,
71
- native_category_id: product.categoryId,
72
- native_product_id: product.productId,
64
+ native_category_id: product.categoryId.toString(),
65
+ native_product_id: product.productId.toString(),
73
66
  label: product.name,
74
- brand: product.brand,
75
- is_verified: product.isVerified,
76
- images: product.images.map(image => {
77
- return {
78
- file_url: image.url
79
- }
80
- }),
81
- product_attributes: product.attributes.map(attribute => {
82
- return {
83
- native_attribute_id: attribute.id,
84
- values: []
85
- }
86
- })
67
+ brand: product.brand || '',
68
+ is_verified: product.isVerified || true,
69
+ flexible_attributes: product.channelCode == 'WCM' || product.channelCode == 'MGT' || product.channelCode == 'SPF' ? true : false, // add channels that do not support category_attributes ingestion
70
+ images:
71
+ product?.images?.map(image => {
72
+ return {
73
+ file_url: image.url
74
+ }
75
+ }) || [],
76
+ product_attributes:
77
+ product?.attributes?.map(attribute => {
78
+ return {
79
+ ...attribute,
80
+ native_attribute_id: attribute.native_attribute_id.toString()
81
+ }
82
+ }) || [],
83
+ variants: productVariations,
84
+ has_all_variants: product.channelCode == 'MGT' ? false : true
87
85
  }
88
86
  })
89
87
 
90
88
  return {
91
- payload: {}
89
+ payload: [...newProducts]
92
90
  }
93
91
  },
94
92
  normalize(res) {
95
- const {
96
- handover_type: handOverType,
97
- pickup_time: pickupTime,
98
- pickup_address: pickupAddress,
99
- dropoff_address: dropoffAddress
100
- } = res.data
101
-
102
- return {
103
- handOverType,
104
- pickupTime,
105
- pickupAddress,
106
- dropoffAddress
107
- }
93
+ return res
108
94
  }
109
95
  }
110
96
  }
@@ -1,5 +1,6 @@
1
1
  import fetch from 'node-fetch'
2
- import uuid from 'uuid/v4'
2
+ import { v4 as uuidv4 } from 'uuid'
3
+ import { createPayloadLog } from '@things-factory/integration-base'
3
4
 
4
5
  const debug = require('debug')('things-factory:integration-sellercraft:sellercraft')
5
6
 
@@ -21,7 +22,7 @@ export class SellercraftChannelIntegration {
21
22
  }
22
23
 
23
24
  generateRequestId() {
24
- return uuid()
25
+ return uuidv4()
25
26
  }
26
27
 
27
28
  async post(path: string, data: any = {}) {
@@ -38,15 +39,21 @@ export class SellercraftChannelIntegration {
38
39
  headers: {
39
40
  'Content-Type': 'application/json',
40
41
  request_id: requestId,
41
- 'x-api-key': apiKey
42
+ 'X-Api-Key': apiKey
42
43
  },
43
44
  body: jsondata
44
45
  })
45
46
 
47
+ const result = await response.json()
48
+ try {
49
+ createPayloadLog(JSON.parse(jsondata)[0]?.channel_shop_id || requestId, fullPath, jsondata, result, {
50
+ state: { domain: null }
51
+ })
52
+ } catch (e) {}
46
53
  if (response.ok) {
47
- return await response.json()
54
+ return result
48
55
  } else {
49
- throw new Error(`response code - ${response.status}, ${response.statusText}`)
56
+ throw new Error(`(${response.status}) ${result.detail}`)
50
57
  }
51
58
  }
52
59
 
@@ -71,7 +78,8 @@ export class SellercraftChannelIntegration {
71
78
  if (response.ok) {
72
79
  return await response.json()
73
80
  } else {
74
- throw new Error(`response code - ${response.status}, ${response.statusText}`)
81
+ const result = await response.json()
82
+ throw new Error(`(${response.status}) ${result.message}`)
75
83
  }
76
84
  }
77
85
 
@@ -100,7 +108,8 @@ export class SellercraftChannelIntegration {
100
108
  if (response.ok) {
101
109
  return await response.json()
102
110
  } else {
103
- throw new Error(`response code - ${response.status}, ${response.statusText}`)
111
+ const result = await response.json()
112
+ throw new Error(`(${response.status}) ${result.message}`)
104
113
  }
105
114
  }
106
115
  }
@@ -1,202 +1,286 @@
1
1
  import Router from 'koa-router'
2
+ import { v4 as uuidv4 } from 'uuid'
2
3
 
3
4
  import { config } from '@things-factory/env'
4
5
  import { StoreAPI } from '@things-factory/integration-marketplace'
5
- import { PLATFORM } from '../constants'
6
+ import { getShop } from '../utils/tokencraft-util'
7
+
8
+ import { SHIPPING_TYPE } from '../constants'
6
9
 
7
10
  const debug = require('debug')('things-factory:integration-sellercraft:sellercraft-router')
8
11
 
9
12
  export const sellercraftRouter = new Router()
10
13
 
11
14
  sellercraftRouter.post('/sellercraft/store/update-product-price', async (context, next) => {
12
- const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
13
- const { apiKey, tokenCraftUrl } = sellercraftChannelIntegrationConfig
14
-
15
- const xApiKey = context.headers['x-api-key']
16
-
17
- if (apiKey !== xApiKey) context.throw(400, 'api key validation failed')
18
-
19
15
  const requestBody = context.request.body
20
16
 
21
17
  for (var i = 0; i < requestBody.length; i++) {
22
- var store: any = {} // get from tokencraft
18
+ let mappedStore: any = await getShop(context, requestBody[i].channel_id, requestBody[i].shop_id)
23
19
  var result
24
20
 
25
- // https://staging-tokencraft.sellercraft.co/v1/get-shop?channel_id=4bfb3362-d57c-47f8-8781-007316d179bf&shop_id=dd9cf3b7-114f-4d74-a7e2-7b524ae086f2
26
- var fullPath = tokenCraftUrl + '?channel_id=4bfb3362-d57c-47f8-8781-007316d179bf&shop_id=' + requestBody[i].shop_id
27
- const response: any = await fetch(fullPath, {
28
- method: 'get',
29
- headers: {
30
- 'Content-Type': 'application/json',
31
- 'x-api-key': apiKey
21
+ try {
22
+ if (requestBody[i].variant.native_variant_id != requestBody[i].native_product_id) {
23
+ const req = {
24
+ costPrice: requestBody[i].variant.full_price,
25
+ sellPrice: requestBody[i].variant.sale_price,
26
+ productId: requestBody[i].native_product_id,
27
+ variationId: requestBody[i].variant.native_variant_id,
28
+ variationSku: requestBody[i].variant.sku,
29
+ context: { state: { domain: null } }
30
+ }
31
+ result = await StoreAPI.updateStoreProductVariationPrice(mappedStore, req)
32
+ } else {
33
+ const req = {
34
+ costPrice: requestBody[i].variant.full_price,
35
+ sellPrice: requestBody[i].variant.sale_price,
36
+ productId: requestBody[i].native_product_id,
37
+ variationSku: requestBody[i].variant.sku,
38
+ context: { state: { domain: null } }
39
+ }
40
+ result = await StoreAPI.updateStoreProductPrice(mappedStore, req)
32
41
  }
33
- })
34
- if (response.ok) {
35
- store = await response.json()
36
- }
37
42
 
38
- let mappedStore: any = {
39
- accessKey: store.shop.credential.consumer_key,
40
- accessSecret: store.shop.credential.consumer_secret,
41
- storeURL: store.shop.credential.store_url,
42
- platform: PLATFORM[`${store.shop.org_prefix}`]
43
- }
43
+ debug(result)
44
44
 
45
- if (requestBody[i].native_variant_id) {
46
- const req = {
47
- costPrice: requestBody[i].full_price,
48
- sellPrice: requestBody[i].sale_price,
49
- productId: requestBody[i].native_product_id,
50
- variationId: requestBody[i].native_variant_id
51
- }
52
- result = await StoreAPI.updateStoreProductVariationPrice(mappedStore, req)
53
- } else {
54
- const req = {
55
- costPrice: requestBody[i].full_price,
56
- sellPrice: requestBody[i].sale_price,
57
- productId: requestBody[i].native_product_id
58
- }
59
- result = await StoreAPI.updateStoreProductPrice(mappedStore, req)
45
+ context.type = 'application/json'
46
+ context.status = 200
47
+ context.body = 'Succeeded'
48
+ } catch (e) {
49
+ context.type = 'application/json'
50
+ context.status = 500
51
+ context.body = e.message
60
52
  }
61
-
62
- debug(result)
63
53
  }
64
-
65
- context.status = 200
66
54
  })
67
55
 
68
56
  sellercraftRouter.post('/sellercraft/store/update-product-stock', async (context, next) => {
69
- const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
70
- const { apiKey, tokenCraftUrl } = sellercraftChannelIntegrationConfig
71
-
72
- const xApiKey = context.headers['x-api-key']
73
-
74
- if (apiKey !== xApiKey) context.throw(400, 'api key validation failed')
75
-
76
57
  const requestBody = context.request.body
77
58
  for (var i = 0; i < requestBody.length; i++) {
78
- var store: any = {} // get from tokencraft
59
+ let mappedStore: any = await getShop(context, requestBody[i].channel_id, requestBody[i].shop_id)
79
60
  var result
80
61
 
81
- // https://staging-tokencraft.sellercraft.co/v1/get-shop?channel_id=4bfb3362-d57c-47f8-8781-007316d179bf&shop_id=dd9cf3b7-114f-4d74-a7e2-7b524ae086f2
82
- var fullPath = tokenCraftUrl + '?channel_id=4bfb3362-d57c-47f8-8781-007316d179bf&shop_id=' + requestBody[i].shop_id
83
- const response: any = await fetch(fullPath, {
84
- method: 'get',
85
- headers: {
86
- 'Content-Type': 'application/json',
87
- 'x-api-key': apiKey
62
+ try {
63
+ if (requestBody[i].variant.native_variant_id != requestBody[i].native_product_id) {
64
+ const req = {
65
+ qty: requestBody[i].variant.stock,
66
+ itemId: requestBody[i].native_product_id,
67
+ variationId: requestBody[i].variant.native_variant_id,
68
+ distributors: requestBody[i].distributors || [], // for Magento
69
+ variationSku: requestBody[i].variant.sku,
70
+ locationId: requestBody[i]?.variant?.extra_metadata?.locationId, // for Shopify
71
+ inventoryItemId: requestBody[i]?.variant?.extra_metadata?.inventoryItemId, // for Shopify
72
+ context: { state: { domain: null } }
73
+ }
74
+ if (req.locationId) {
75
+ let location = JSON.parse(req.locationId)
76
+ req.qty -= await StoreAPI.getStoreProductVariationStock(mappedStore, { variantId: req.variationId })
77
+ req.locationId = location[0].location_id
78
+ }
79
+ result = await StoreAPI.updateStoreProductVariationStock(mappedStore, req)
80
+ } else {
81
+ const req = {
82
+ qty: requestBody[i].variant.stock,
83
+ itemId: requestBody[i].native_product_id,
84
+ distributors: requestBody[i].distributors || [], // for Magento
85
+ variationSku: requestBody[i].variant.sku,
86
+ context: { state: { domain: null } }
87
+ }
88
+ result = await StoreAPI.updateStoreProductStock(mappedStore, req)
88
89
  }
89
- })
90
- if (response.ok) {
91
- store = await response.json()
92
- }
93
90
 
94
- let mappedStore: any = {
95
- accessKey: store.shop.credential.consumer_key,
96
- accessSecret: store.shop.credential.consumer_secret,
97
- storeURL: store.shop.credential.store_url,
98
- platform: PLATFORM[`${store.shop.org_prefix}`]
99
- }
91
+ debug(result)
100
92
 
101
- if (requestBody[i].native_variant_id) {
102
- const req = {
103
- qty: requestBody[i].stock,
104
- itemId: requestBody[i].native_product_id,
105
- variationId: requestBody[i].native_variant_id
106
- }
107
- result = await StoreAPI.updateStoreProductVariationStock(mappedStore, req)
108
- } else {
109
- const req = {
110
- qty: requestBody[i].stock,
111
- itemId: requestBody[i].native_product_id
112
- }
113
- result = await StoreAPI.updateStoreProductStock(mappedStore, req)
93
+ context.type = 'application/json'
94
+ context.status = 200
95
+ context.body = 'Succeeded'
96
+ } catch (e) {
97
+ context.type = 'application/json'
98
+ context.status = 500
99
+ context.body = e.message
114
100
  }
115
-
116
- debug(result)
117
101
  }
118
- context.status = 200
119
102
  })
120
103
 
121
104
  sellercraftRouter.post('/sellercraft/store/update-order-status', async (context, next) => {
122
- const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
123
- const { apiKey, tokenCraftUrl } = sellercraftChannelIntegrationConfig
124
-
125
- const xApiKey = context.headers['x-api-key']
105
+ try {
106
+ const requestBody = context.request.body
107
+ var result: any = {}
108
+ let mappedStore: any = await getShop(context, requestBody.channel_id, requestBody.shop_id)
109
+
110
+ const reqBody = {
111
+ orderId: requestBody.native_order_id,
112
+ status: requestBody.order_status,
113
+ carrier: requestBody?.shipper_last_mile,
114
+ trackingNo: requestBody?.tracking_number,
115
+ orderItems: unmapOrderItems(
116
+ requestBody?.order_items.map(oi => {
117
+ return oi.native_order_item_id
118
+ }) || []
119
+ ),
120
+ isSOF: requestBody?.seller_logistics,
121
+ locations:
122
+ mappedStore.platform == 'shopify'
123
+ ? requestBody?.order_items.map(oi => {
124
+ let jsonLocation = oi.extra_metadata.locationId
125
+ return JSON.parse(jsonLocation)[0].location_id
126
+ })
127
+ : null,
128
+ context: { state: { domain: null } }
129
+ }
126
130
 
127
- if (apiKey !== xApiKey) context.throw(400, 'api key validation failed')
131
+ let responseBody: any = { operation_id: uuidv4() }
132
+ try {
133
+ try {
134
+ result = (await StoreAPI.updateOrderStatus(mappedStore, reqBody)) || {}
135
+ debug(result)
136
+ } catch (e) {
137
+ if (e?.requiredDocument) result.requiredDocument = e.requiredDocument
138
+ }
128
139
 
129
- const requestBody = context.request.body
130
- var result
131
- var store: any = {}
132
-
133
- // https://staging-tokencraft.sellercraft.co/v1/get-shop?channel_id=4bfb3362-d57c-47f8-8781-007316d179bf&shop_id=dd9cf3b7-114f-4d74-a7e2-7b524ae086f2
134
- var fullPath = tokenCraftUrl + '?channel_id=4bfb3362-d57c-47f8-8781-007316d179bf&shop_id=' + requestBody.shop_id
135
- const response: any = await fetch(fullPath, {
136
- method: 'get',
137
- headers: {
138
- 'Content-Type': 'application/json',
139
- 'x-api-key': apiKey
140
- }
141
- })
142
- if (response.ok) {
143
- store = await response.json()
144
- }
140
+ if (result?.requiredDocument) {
141
+ // call document api and ingest channel order package
142
+ const order: any = await StoreAPI.getStoreOrder(mappedStore, { orderId: reqBody.orderId })
143
+ let orderPackage: any = order ? (order[0]?.orderPackage ? order[0].orderPackage : {}) : {}
144
+ if (orderPackage) {
145
+ const packageId: string = orderPackage.packageId
146
+
147
+ let orderDocument: any = orderPackage.orderDocument
148
+ if (orderDocument?.length == 0) {
149
+ let hasDocument: boolean = false
150
+ while (!hasDocument) {
151
+ orderDocument = await StoreAPI.getStoreOrderDocument(mappedStore, { packageId, documentType: 1 })
152
+ if (orderDocument?.length > 0) {
153
+ hasDocument = true
154
+ break
155
+ }
156
+ }
157
+ }
158
+
159
+ // Order Package Payload
160
+ let newOrderPackage: any =
161
+ {
162
+ channel_shop_id: mappedStore.channelShopId,
163
+ native_order_id: reqBody.orderId,
164
+ native_package_id: orderPackage.packageId.toString(),
165
+ shipping_tracking_code: orderPackage.trackingNumber,
166
+ shipping_type_value: orderPackage?.shippingType ? orderPackage.shippingType : SHIPPING_TYPE.DROP_SHIPPING,
167
+ warehouse_code: SHIPPING_TYPE.DROP_SHIPPING,
168
+ shipper: {
169
+ name: orderPackage.shippingProvider,
170
+ is_cod_supported: orderPackage?.isCodSupport ? orderPackage.isCodSupport : false
171
+ },
172
+ documents:
173
+ orderDocument?.map(doc => {
174
+ return {
175
+ file: doc.file,
176
+ file_type_value: doc.fileTypeValue,
177
+ mime_type: doc.mimeType
178
+ }
179
+ }) || [],
180
+ shipper_last_mile: {
181
+ name: orderPackage.shippingProvider,
182
+ is_cod_supported: orderPackage?.isCodSupport ? orderPackage.isCodSupport : false
183
+ },
184
+ order_item_ids: orderPackage?.orderListIdList
185
+ ? orderPackage.orderListIdList
186
+ : reqBody.orderItems.map(oi => {
187
+ return oi.order_item_id
188
+ })
189
+ } || {}
190
+
191
+ responseBody.package = newOrderPackage
192
+ }
193
+ responseBody.response_code = 'E0'
194
+ responseBody.message = 'Success'
195
+ }
145
196
 
146
- let mappedStore: any = {
147
- accessKey: store.shop.credential.consumer_key,
148
- accessSecret: store.shop.credential.consumer_secret,
149
- storeURL: store.shop.credential.store_url,
150
- platform: PLATFORM[`${store.shop.org_prefix}`]
151
- }
197
+ responseBody.time = Math.floor(new Date(new Date().toUTCString()).getTime() / 1000)
152
198
 
153
- const reqBody = { orderId: requestBody.native_order_id, status: requestBody.order_status }
199
+ context.type = 'application/json'
200
+ context.status = 200
201
+ context.body = responseBody
154
202
 
155
- result = await StoreAPI.updateOrderStatus(mappedStore, reqBody)
156
- debug(result)
203
+ await next()
204
+ } catch (e) {
205
+ responseBody.response_code = 'E1'
206
+ responseBody.message = e.message
157
207
 
158
- context.status = 200
208
+ context.type = 'application/json'
209
+ context.status = 500
210
+ context.body = responseBody
211
+ }
212
+ } catch (e) {}
159
213
  })
160
214
 
161
215
  sellercraftRouter.post('/sellercraft/store/update-product-attribute', async (context, next) => {
162
- const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
163
- const { apiKey, tokenCraftUrl } = sellercraftChannelIntegrationConfig
216
+ const requestBody = context.request.body
217
+ let mappedStore: any = await getShop(context, requestBody.channel_id, requestBody.shop_id)
164
218
 
165
- const xApiKey = context.headers['x-api-key']
219
+ let { product_sku: productSku, native_product_id: itemId, variants } = requestBody
166
220
 
167
- if (apiKey !== xApiKey) context.throw(400, 'api key validation failed')
221
+ variants.map(variant => {
222
+ let { variant_sku: variantSku, native_variant_id: variantId, attributes } = variant
168
223
 
169
- const requestBody = context.request.body
170
- var store: any = {}
171
- const attributes: any[] = requestBody.attributes
172
-
173
- // https://staging-tokencraft.sellercraft.co/v1/get-shop?channel_id=4bfb3362-d57c-47f8-8781-007316d179bf&shop_id=dd9cf3b7-114f-4d74-a7e2-7b524ae086f2
174
- var fullPath = tokenCraftUrl + '?channel_id=4bfb3362-d57c-47f8-8781-007316d179bf&shop_id=' + requestBody.shop_id
175
- const response: any = await fetch(fullPath, {
176
- method: 'get',
177
- headers: {
178
- 'Content-Type': 'application/json',
179
- 'x-api-key': apiKey
224
+ attributes.map(attribute => {
225
+ let { native_attribute_id: id, attribute_key: name, attribute_value: value } = attribute
226
+
227
+ return {
228
+ id,
229
+ name,
230
+ value
231
+ }
232
+ })
233
+
234
+ return {
235
+ variantSku,
236
+ variantId,
237
+ attributes
180
238
  }
181
239
  })
182
- if (response.ok) {
183
- store = await response.json()
184
- }
185
240
 
186
- let mappedStore: any = {
187
- accessKey: store.shop.credential.consumer_key,
188
- accessSecret: store.shop.credential.consumer_secret,
189
- storeURL: store.shop.credential.store_url,
190
- platform: PLATFORM[`${store.shop.org_prefix}`]
241
+ for (var i = 0; i < variants.length; i++) {
242
+ try {
243
+ let variant = variants[i]
244
+ await StoreAPI.updateProductAttribute(mappedStore, {
245
+ productSku,
246
+ itemId,
247
+ variant,
248
+ context: { state: { domain: null } }
249
+ })
250
+ context.type = 'application/json'
251
+ context.status = 200
252
+ context.body = 'Succeeded'
253
+ } catch (e) {
254
+ context.type = 'application/json'
255
+ context.status = 500
256
+ context.body = e.message
257
+ }
191
258
  }
259
+ })
260
+
261
+ function unmapOrderItems(orderItems) {
262
+ let itemsList = []
263
+ let res = orderItems.map(e => {
264
+ return e.split('-')[0]
265
+ })
266
+
267
+ for (let item of res) {
268
+ let count = 0
192
269
 
193
- await Promise.all(
194
- attributes.map(async attribute => {
195
- const attributeReqBody = { attributeId: attribute.native_attribute_id, name: attribute.attribute_key }
270
+ res.forEach(element => {
271
+ if (element === item) {
272
+ count += 1
273
+ }
274
+ })
196
275
 
197
- await StoreAPI.updateProductAttribute(mappedStore, attributeReqBody)
276
+ itemsList.push({
277
+ order_item_id: item,
278
+ qty: count
198
279
  })
199
- )
280
+ }
200
281
 
201
- context.status = 200
202
- })
282
+ itemsList = itemsList.filter(
283
+ (value, index, self) => index === self.findIndex(t => t.order_item_id === value.order_item_id)
284
+ )
285
+ return itemsList
286
+ }
@@ -3,10 +3,12 @@ export * from './sellercraft/sellercraft'
3
3
 
4
4
  /* IMPORT ENTITIES AND RESOLVERS */
5
5
  import { entities as SellercraftEntities, resolvers as SellercraftResolvers } from './sellercraft'
6
+ import { entities as MarketplaceChannelEntities, resolvers as MarketplaceSellercraftResolvers } from './marketplace-channel'
6
7
 
7
8
  export const entities = [
8
9
  /* ENTITIES */
9
10
  ...SellercraftEntities,
11
+ ...MarketplaceChannelEntities
10
12
  ]
11
13
 
12
14
 
@@ -14,5 +16,8 @@ export const schema = {
14
16
  resolverClasses: [
15
17
  /* RESOLVER CLASSES */
16
18
  ...SellercraftResolvers,
17
- ]
19
+ ...MarketplaceSellercraftResolvers
20
+ ]
18
21
  }
22
+
23
+ export { MarketplaceSellercraftResolvers }
@@ -0,0 +1,6 @@
1
+ import { MarketplaceChannel } from './marketplace-channel'
2
+ import { MarketplaceChannelOrderMutation } from './marketplace-channel-order-mutation'
3
+ import { MarketplaceChannelProductMutation } from './marketplace-channel-product-mutation'
4
+
5
+ export const entities = [MarketplaceChannel]
6
+ export const resolvers = [MarketplaceChannelOrderMutation, MarketplaceChannelProductMutation]