@things-factory/integration-sellercraft 4.3.27 → 4.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/integration-sellercraft",
3
- "version": "4.3.27",
3
+ "version": "4.3.28",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "client/index.js",
6
6
  "things-factory": true,
@@ -31,7 +31,7 @@
31
31
  "@things-factory/context-ui": "^4.3.27",
32
32
  "@things-factory/grist-ui": "^4.3.27",
33
33
  "@things-factory/i18n-base": "^4.3.27",
34
- "@things-factory/integration-marketplace": "^4.3.27",
34
+ "@things-factory/integration-marketplace": "^4.3.28",
35
35
  "@things-factory/integration-ui": "^4.3.27",
36
36
  "@things-factory/more-ui": "^4.3.27",
37
37
  "@things-factory/resource-ui": "^4.3.27",
@@ -51,5 +51,5 @@
51
51
  "nock": "^13.0.2",
52
52
  "should": "^13.2.3"
53
53
  },
54
- "gitHead": "52398bed65c4cf97263cd983c2c518e882ddbcde"
54
+ "gitHead": "f1af695e2848b49871ca1d4f1661a5620ed428da"
55
55
  }
@@ -1,5 +1,6 @@
1
1
  import fetch from 'node-fetch'
2
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
 
@@ -43,11 +44,15 @@ export class SellercraftChannelIntegration {
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
- const result = await response.json()
48
54
  return result
49
55
  } else {
50
- const result = await response.json()
51
56
  throw new Error(`(${response.status}) ${result.detail}`)
52
57
  }
53
58
  }
@@ -3,50 +3,21 @@ import { v4 as uuidv4 } from 'uuid'
3
3
 
4
4
  import { config } from '@things-factory/env'
5
5
  import { StoreAPI } from '@things-factory/integration-marketplace'
6
+ import { getShop } from '../utils/tokencraft-util'
6
7
 
7
- import { PLATFORM, SHIPPING_TYPE } from '../constants'
8
+ import { SHIPPING_TYPE } from '../constants'
8
9
 
9
10
  const debug = require('debug')('things-factory:integration-sellercraft:sellercraft-router')
10
11
 
11
12
  export const sellercraftRouter = new Router()
12
13
 
13
14
  sellercraftRouter.post('/sellercraft/store/update-product-price', async (context, next) => {
14
- const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
15
- const { tokenCraftApiKey: apiKey, tokenCraftUrl } = sellercraftChannelIntegrationConfig
16
-
17
- const xApiKey = context.headers['x-api-key']
18
-
19
- if (apiKey !== xApiKey) context.throw(400, 'api key validation failed')
20
-
21
15
  const requestBody = context.request.body
22
16
 
23
17
  for (var i = 0; i < requestBody.length; i++) {
24
- var store: any = {} // get from tokencraft
18
+ let mappedStore: any = await getShop(context, requestBody[i].channel_id, requestBody[i].shop_id)
25
19
  var result
26
20
 
27
- // https://staging-tokencraft.sellercraft.co/v1/get-shop?channel_id=4bfb3362-d57c-47f8-8781-007316d179bf&shop_id=dd9cf3b7-114f-4d74-a7e2-7b524ae086f2
28
- var fullPath = `${tokenCraftUrl}?channel_id=${requestBody[i].channel_id}&shop_id=${requestBody[i].shop_id}`
29
- const response: any = await fetch(fullPath, {
30
- method: 'get',
31
- headers: {
32
- 'Content-Type': 'application/json',
33
- 'x-api-key': apiKey
34
- }
35
- })
36
- if (response.ok) {
37
- store = await response.json()
38
- }
39
-
40
- let mappedStore: any = {
41
- accessKey: store.shop?.credential?.consumer_key || '',
42
- accessSecret: store.shop?.credential?.consumer_secret || '',
43
- storeURL: store.shop?.credential?.store_url,
44
- platform: PLATFORM[`${store.shop.org_prefix}`],
45
- accessToken: store.shop?.credential?.access_token,
46
- channelShopId: store.shop?.channel_shop_id,
47
- storeId: store.shop?.credential?.store_url
48
- }
49
-
50
21
  try {
51
22
  if (requestBody[i].variant.native_variant_id != requestBody[i].native_product_id) {
52
23
  const req = {
@@ -83,41 +54,11 @@ sellercraftRouter.post('/sellercraft/store/update-product-price', async (context
83
54
  })
84
55
 
85
56
  sellercraftRouter.post('/sellercraft/store/update-product-stock', async (context, next) => {
86
- const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
87
- const { tokenCraftApiKey: apiKey, tokenCraftUrl } = sellercraftChannelIntegrationConfig
88
-
89
- const xApiKey = context.headers['x-api-key']
90
-
91
- if (apiKey !== xApiKey) context.throw(400, 'api key validation failed')
92
-
93
57
  const requestBody = context.request.body
94
58
  for (var i = 0; i < requestBody.length; i++) {
95
- var store: any = {} // get from tokencraft
59
+ let mappedStore: any = await getShop(context, requestBody[i].channel_id, requestBody[i].shop_id)
96
60
  var result
97
61
 
98
- // https://staging-tokencraft.sellercraft.co/v1/get-shop?channel_id=4bfb3362-d57c-47f8-8781-007316d179bf&shop_id=dd9cf3b7-114f-4d74-a7e2-7b524ae086f2
99
- var fullPath = `${tokenCraftUrl}?channel_id=${requestBody[i].channel_id}&shop_id=${requestBody[i].shop_id}`
100
- const response: any = await fetch(fullPath, {
101
- method: 'get',
102
- headers: {
103
- 'Content-Type': 'application/json',
104
- 'x-api-key': apiKey
105
- }
106
- })
107
- if (response.ok) {
108
- store = await response.json()
109
- }
110
-
111
- let mappedStore: any = {
112
- accessKey: store.shop?.credential?.consumer_key || '',
113
- accessSecret: store.shop?.credential?.consumer_secret || '',
114
- storeURL: store.shop?.credential?.store_url,
115
- platform: PLATFORM[`${store.shop.org_prefix}`],
116
- accessToken: store.shop?.credential?.access_token,
117
- channelShopId: store.shop?.channel_shop_id,
118
- storeId: store.shop?.credential?.store_url
119
- }
120
-
121
62
  try {
122
63
  if (requestBody[i].variant.native_variant_id != requestBody[i].native_product_id) {
123
64
  const req = {
@@ -162,39 +103,9 @@ sellercraftRouter.post('/sellercraft/store/update-product-stock', async (context
162
103
 
163
104
  sellercraftRouter.post('/sellercraft/store/update-order-status', async (context, next) => {
164
105
  try {
165
- const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
166
- const { tokenCraftApiKey: apiKey, tokenCraftUrl } = sellercraftChannelIntegrationConfig
167
-
168
- const xApiKey = context.headers['x-api-key']
169
-
170
- if (apiKey !== xApiKey) context.throw(400, 'api key validation failed')
171
-
172
106
  const requestBody = context.request.body
173
107
  var result: any = {}
174
- var store: any = {}
175
-
176
- // https://staging-tokencraft.sellercraft.co/v1/get-shop?channel_id=4bfb3362-d57c-47f8-8781-007316d179bf&shop_id=dd9cf3b7-114f-4d74-a7e2-7b524ae086f2
177
- var fullPath = `${tokenCraftUrl}?channel_id=${requestBody.channel_id}&shop_id=${requestBody.shop_id}`
178
- const response: any = await fetch(fullPath, {
179
- method: 'get',
180
- headers: {
181
- 'Content-Type': 'application/json',
182
- 'x-api-key': apiKey
183
- }
184
- })
185
- if (response.ok) {
186
- store = await response.json()
187
- }
188
-
189
- let mappedStore: any = {
190
- accessKey: store.shop?.credential?.consumer_key || '',
191
- accessSecret: store.shop?.credential?.consumer_secret || '',
192
- storeURL: store.shop?.credential?.store_url,
193
- platform: PLATFORM[`${store.shop.org_prefix}`],
194
- accessToken: store.shop?.credential?.access_token,
195
- channelShopId: store.shop?.channel_shop_id,
196
- storeId: store.shop?.credential?.store_url
197
- }
108
+ let mappedStore: any = await getShop(context, requestBody.channel_id, requestBody.shop_id)
198
109
 
199
110
  const reqBody = {
200
111
  orderId: requestBody.native_order_id,
@@ -302,37 +213,8 @@ sellercraftRouter.post('/sellercraft/store/update-order-status', async (context,
302
213
  })
303
214
 
304
215
  sellercraftRouter.post('/sellercraft/store/update-product-attribute', async (context, next) => {
305
- const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
306
- const { tokenCraftApiKey: apiKey, tokenCraftUrl } = sellercraftChannelIntegrationConfig
307
-
308
- const xApiKey = context.headers['x-api-key']
309
-
310
- if (apiKey !== xApiKey) context.throw(400, 'api key validation failed')
311
-
312
216
  const requestBody = context.request.body
313
- var store: any = {}
314
-
315
- // https://staging-tokencraft.sellercraft.co/v1/get-shop?channel_id=4bfb3362-d57c-47f8-8781-007316d179bf&shop_id=dd9cf3b7-114f-4d74-a7e2-7b524ae086f2
316
- var fullPath = `${tokenCraftUrl}?channel_id=${requestBody.channel_id}&shop_id=${requestBody.shop_id}`
317
- const response: any = await fetch(fullPath, {
318
- method: 'get',
319
- headers: {
320
- 'Content-Type': 'application/json',
321
- 'x-api-key': apiKey
322
- }
323
- })
324
- if (response.ok) {
325
- store = await response.json()
326
- }
327
-
328
- let mappedStore: any = {
329
- accessKey: store.shop?.credential?.consumer_key || '',
330
- accessSecret: store.shop?.credential?.consumer_secret || '',
331
- storeURL: store.shop?.credential?.store_url,
332
- platform: PLATFORM[`${store.shop.org_prefix}`],
333
- accessToken: store.shop?.credential?.access_token,
334
- channelShopId: store.shop?.channel_shop_id
335
- }
217
+ let mappedStore: any = await getShop(context, requestBody.channel_id, requestBody.shop_id)
336
218
 
337
219
  let { product_sku: productSku, native_product_id: itemId, variants } = requestBody
338
220
 
@@ -7,6 +7,7 @@ import { StoreAPI } from '@things-factory/integration-marketplace'
7
7
  import { SHIPPING_TYPE } from '../../constants'
8
8
  import { SellercraftChannelIntegrationAPI } from '../../controllers/sellercraft-channel-integration-api'
9
9
  import { MarketplaceChannel } from './marketplace-channel'
10
+ import { getShops } from '../../utils/tokencraft-util'
10
11
 
11
12
  @Resolver()
12
13
  export class MarketplaceChannelOrderMutation {
@@ -19,27 +20,13 @@ export class MarketplaceChannelOrderMutation {
19
20
  @Arg('toUpdatedDate', { nullable: true }) toUpdatedDate?: string
20
21
  ): Promise<boolean> {
21
22
  const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
22
- const { tokenCraftApiKey: apiKey, getShopsTokenCraftUrl } = sellercraftChannelIntegrationConfig
23
23
 
24
24
  const channels: MarketplaceChannel[] = await getRepository(MarketplaceChannel).find({ where: { isActive: true } })
25
25
 
26
26
  for (var i = 0; i < channels.length; i++) {
27
27
  try {
28
28
  const channel: MarketplaceChannel = channels[i]
29
- var channelsFullPath = getShopsTokenCraftUrl + '?channel_id=' + channel.channelId
30
- const channelResponse: any = await fetch(channelsFullPath, {
31
- method: 'get',
32
- headers: {
33
- 'Content-Type': 'application/json',
34
- 'x-api-key': apiKey
35
- }
36
- })
37
-
38
- if (!channelResponse.ok) {
39
- throw new Error(channelResponse)
40
- }
41
- var shopsResponse = await channelResponse.json()
42
- var shops = shopsResponse.shops
29
+ let shops: any = await getShops(channel.channelId)
43
30
 
44
31
  for (var j = 0; j < shops.length; j++) {
45
32
  try {
@@ -6,6 +6,7 @@ import { StoreAPI } from '@things-factory/integration-marketplace'
6
6
 
7
7
  import { SellercraftChannelIntegrationAPI } from '../../controllers/sellercraft-channel-integration-api'
8
8
  import { MarketplaceChannel } from './marketplace-channel'
9
+ import { getShops } from '../../utils/tokencraft-util'
9
10
 
10
11
  @Resolver()
11
12
  export class MarketplaceChannelProductMutation {
@@ -16,26 +17,12 @@ export class MarketplaceChannelProductMutation {
16
17
  @Arg('toUpdatedDate', { nullable: true }) toUpdatedDate?: string
17
18
  ): Promise<boolean> {
18
19
  const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
19
- const { tokenCraftApiKey: apiKey, getShopsTokenCraftUrl } = sellercraftChannelIntegrationConfig
20
20
 
21
21
  const channels: MarketplaceChannel[] = await getRepository(MarketplaceChannel).find({ where: { isActive: true } })
22
22
 
23
23
  for (var i = 0; i < channels.length; i++) {
24
24
  try {
25
- var channelsFullPath = getShopsTokenCraftUrl + '?channel_id=' + channels[i].channelId
26
- const channelResponse: any = await fetch(channelsFullPath, {
27
- method: 'get',
28
- headers: {
29
- 'Content-Type': 'application/json',
30
- 'x-api-key': apiKey
31
- }
32
- })
33
-
34
- if (!channelResponse.ok) {
35
- throw new Error(channelResponse)
36
- }
37
- var shopsResponse = await channelResponse.json()
38
- var shops = shopsResponse.shops
25
+ let shops: any = await getShops(channels[i].channelId)
39
26
 
40
27
  for (var j = 0; j < shops.length; j++) {
41
28
  try {
@@ -0,0 +1,60 @@
1
+ import { config } from '@things-factory/env'
2
+ import { PLATFORM } from '../constants'
3
+ import { createPayloadLog } from '@things-factory/integration-base'
4
+
5
+ export async function getShop(context: any, channelId: string, shopId: string) {
6
+ const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
7
+ const { tokenCraftApiKey: apiKey, tokenCraftUrl } = sellercraftChannelIntegrationConfig
8
+ const xApiKey = context.headers['x-api-key']
9
+
10
+ if (apiKey !== xApiKey) context.throw(400, 'api key validation failed')
11
+
12
+ var fullPath = `${tokenCraftUrl}?channel_id=${channelId}&shop_id=${shopId}`
13
+ const response: any = await fetch(fullPath, {
14
+ method: 'get',
15
+ headers: {
16
+ 'Content-Type': 'application/json',
17
+ 'x-api-key': apiKey
18
+ }
19
+ })
20
+ if (response.ok) {
21
+ let store = await response.json()
22
+ let mappedStore: any = {
23
+ accessKey: store.shop?.credential?.consumer_key || '',
24
+ accessSecret: store.shop?.credential?.consumer_secret || '',
25
+ storeURL: store.shop?.credential?.store_url,
26
+ platform: PLATFORM[`${store.shop.org_prefix}`],
27
+ accessToken: store.shop?.credential?.access_token,
28
+ channelShopId: store.shop?.channel_shop_id,
29
+ storeId: store.shop?.credential?.store_url
30
+ }
31
+ return mappedStore
32
+ } else {
33
+ createPayloadLog(shopId, tokenCraftUrl, `channel_id=${channelId}&shop_id=${shopId}`, response?.statusText || 500, {
34
+ state: { domain: null }
35
+ })
36
+ }
37
+ }
38
+
39
+ export async function getShops(channelId: string) {
40
+ const sellercraftChannelIntegrationConfig = config.get('sellercraftChannelIntegrationConfig', {})
41
+ const { tokenCraftApiKey: apiKey, getShopsTokenCraftUrl } = sellercraftChannelIntegrationConfig
42
+
43
+ var channelsFullPath = getShopsTokenCraftUrl + '?channel_id=' + channelId
44
+ const channelResponse: any = await fetch(channelsFullPath, {
45
+ method: 'get',
46
+ headers: {
47
+ 'Content-Type': 'application/json',
48
+ 'x-api-key': apiKey
49
+ }
50
+ })
51
+
52
+ if (!channelResponse.ok) {
53
+ createPayloadLog(channelId, getShopsTokenCraftUrl, `channel_id=${channelId}`, channelResponse?.statusText || 500, {
54
+ state: { domain: null }
55
+ })
56
+ throw new Error(channelResponse)
57
+ }
58
+ var shopsResponse = await channelResponse.json()
59
+ return shopsResponse.shops
60
+ }