@things-factory/operato-hub 3.7.10 → 3.7.14

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.
@@ -16,395 +16,407 @@ const business_middleware_1 = require("../../../../middlewares/business-middlewa
16
16
  const warehouse_middleware_1 = require("../../../../middlewares/warehouse-middleware");
17
17
  const debug = require('debug')('things-factory:operato-hub:restful-api:unstable:add-release-order');
18
18
  api_1.restfulApiRouter.post('/unstable/warehouse/:warehouseId/add-release-order', business_middleware_1.businessMiddleware, warehouse_middleware_1.warehouseMiddleware, async (context, next) => {
19
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
20
- const { client, domain } = context.state;
21
- const { warehouseId: customerBizplaceId } = context.params;
22
- const { t } = context;
23
- if (!customerBizplaceId)
24
- context.throw(404, t('error.warehouse id not found'));
25
- let bodyReq = context.request.body;
26
- if (typeof bodyReq === undefined || !((_a = Object.keys(bodyReq)) === null || _a === void 0 ? void 0 : _a.length)) {
27
- context.throw(400, t('error.invalid body request format'));
28
- }
29
- debug('post:/add-release-order request.body', bodyReq);
30
- await checkMandatoryKey(context, bodyReq);
31
- const isAutoAssign = bodyReq.hasOwnProperty('isAutoAssign') ? bodyReq.isAutoAssign : true;
32
- // get customer company's bizplace
33
- const custCompanyBizplace = await (0, biz_base_1.getCompanyBizplace)(null, null, customerBizplaceId);
34
- const customerBizplace = await (0, typeorm_1.getRepository)(biz_base_1.Bizplace).findOne({
35
- where: { id: customerBizplaceId }
36
- });
37
- const pickingProductSetting = await (0, typeorm_1.getRepository)(setting_base_1.Setting).findOne({
38
- where: { domain, name: 'rule-for-picking-product' }
39
- });
40
- let locationSortingRules = [];
41
- if (pickingProductSetting) {
42
- let locationSetting = JSON.parse(pickingProductSetting.value);
43
- for (const key in locationSetting) {
44
- locationSortingRules.push({ name: key, desc: locationSetting[key] == 'ASC' ? false : true });
19
+ return await (0, typeorm_1.getConnection)().transaction(async (tx) => {
20
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
21
+ const { client, domain } = context.state;
22
+ const { warehouseId: customerBizplaceId } = context.params;
23
+ const { t } = context;
24
+ if (!customerBizplaceId)
25
+ context.throw(404, t('error.warehouse id not found'));
26
+ let bodyReq = context.request.body;
27
+ if (typeof bodyReq === undefined || !((_a = Object.keys(bodyReq)) === null || _a === void 0 ? void 0 : _a.length)) {
28
+ context.throw(400, t('error.invalid body request format'));
45
29
  }
46
- }
47
- const orderInventories = bodyReq.orderInventories;
48
- let newOrderInventories = [];
49
- await Promise.all(orderInventories.map(async (orderInventory) => {
50
- var _a, _b, _c;
51
- const sku = (_a = orderInventory.product) === null || _a === void 0 ? void 0 : _a.sku;
52
- const refCode = (_b = orderInventory.product) === null || _b === void 0 ? void 0 : _b.refCode;
53
- const packingType = orderInventory.packingType;
54
- const qb = (0, typeorm_1.getRepository)(product_base_1.ProductDetail)
55
- .createQueryBuilder('PD')
56
- .innerJoinAndSelect('PD.product', 'PROD')
57
- .where('PROD.bizplace_id = :bizplaceId', { bizplaceId: custCompanyBizplace.id });
58
- if (sku) {
59
- qb.andWhere('PROD.sku = :sku', { sku });
60
- if (packingType) {
61
- qb.andWhere('PD.packing_type = :packingType', { packingType });
30
+ debug('post:/add-release-order request.body', bodyReq);
31
+ await checkMandatoryKey(context, bodyReq);
32
+ const isAutoAssign = bodyReq.hasOwnProperty('isAutoAssign') ? bodyReq.isAutoAssign : true;
33
+ // get customer company's bizplace
34
+ const custCompanyBizplace = await (0, biz_base_1.getCompanyBizplace)(null, null, customerBizplaceId, tx);
35
+ const customerBizplace = await tx.getRepository(biz_base_1.Bizplace).findOne({
36
+ where: { id: customerBizplaceId }
37
+ });
38
+ const pickingProductSetting = await tx.getRepository(setting_base_1.Setting).findOne({
39
+ where: { domain, name: 'rule-for-picking-product' }
40
+ });
41
+ let locationSortingRules = [];
42
+ if (pickingProductSetting) {
43
+ let locationSetting = JSON.parse(pickingProductSetting.value);
44
+ for (const key in locationSetting) {
45
+ locationSortingRules.push({ name: key, desc: locationSetting[key] == 'ASC' ? false : true });
62
46
  }
63
47
  }
64
- if (refCode) {
65
- qb.andWhere('PD.ref_code = :refCode', { refCode });
66
- if (packingType) {
67
- qb.andWhere('PD.packing_type = :packingType', { packingType });
68
- }
69
- }
70
- const productDetail = await qb.getOne();
71
- if (productDetail) {
72
- let newOrderInventory = Object.assign({}, orderInventory);
73
- newOrderInventory.product.sku = productDetail.product.sku;
74
- newOrderInventory.product = productDetail.product;
75
- newOrderInventory.packingType = productDetail.packingType;
76
- newOrderInventory.uomValue = productDetail.uomValue;
77
- newOrderInventories.push(newOrderInventory);
78
- }
79
- if (!productDetail) {
80
- const qb = (0, typeorm_1.getRepository)(product_base_1.ProductBundle)
81
- .createQueryBuilder('PB')
82
- .innerJoinAndSelect('PB.productBundleSettings', 'PBS')
83
- .innerJoinAndSelect('PBS.productDetail', 'PBD')
84
- .innerJoinAndSelect('PBD.product', 'PROD')
85
- .where('PB.bizplace_id = :bizplaceId', { bizplaceId: custCompanyBizplace.id });
86
- if (sku) {
87
- qb.andWhere('PB.sku = :sku', { sku });
88
- if (packingType) {
89
- qb.andWhere('PB.packing_type = :packingType', { packingType });
90
- }
48
+ const orderInventories = bodyReq.orderInventories;
49
+ let newOrderInventories = [];
50
+ await Promise.all(orderInventories.map(async (orderInventory) => {
51
+ var _a, _b;
52
+ const sku = (_a = orderInventory.product) === null || _a === void 0 ? void 0 : _a.sku;
53
+ const refCode = (_b = orderInventory.product) === null || _b === void 0 ? void 0 : _b.refCode;
54
+ const packingType = orderInventory.packingType;
55
+ const qb = tx
56
+ .getRepository(product_base_1.ProductDetail)
57
+ .createQueryBuilder('PD')
58
+ .innerJoinAndSelect('PD.product', 'PROD')
59
+ .where('PROD.bizplace_id = :bizplaceId', { bizplaceId: custCompanyBizplace.id });
60
+ if (!sku && !refCode) {
61
+ context.throw(404, t('error.sku or refCode not found'));
91
62
  }
92
63
  if (refCode) {
93
- qb.andWhere('PB.ref_code = :refCode', { refCode });
94
- if (packingType) {
95
- qb.andWhere('PB.packing_type = :packingType', { packingType });
96
- }
64
+ qb.andWhere('PD.ref_code = :refCode', { refCode });
97
65
  }
98
- const foundProductBundle = await qb.getOne();
99
- if (foundProductBundle) {
100
- const bundleProductSettings = foundProductBundle.productBundleSettings;
101
- bundleProductSettings.map(bundleProductSetting => {
102
- const productDetailBundle = bundleProductSetting.productDetail;
103
- let newOrderInventory = Object.assign({}, orderInventory);
104
- newOrderInventory.product.sku = productDetailBundle.product.sku;
105
- newOrderInventory.packingType = productDetailBundle.packingType;
106
- newOrderInventory.uomValue = productDetailBundle.uomValue;
107
- newOrderInventory.releaseQty = orderInventory.releaseQty * bundleProductSetting.bundleQty;
108
- newOrderInventory.product = productDetailBundle.product;
109
- newOrderInventories.push(newOrderInventory);
110
- });
66
+ if (sku) {
67
+ qb.andWhere('PROD.sku = :sku', { sku });
111
68
  }
112
- else {
113
- context.throw(404, t(`error.${(_c = orderInventory.product) === null || _c === void 0 ? void 0 : _c.sku} not exist in product / bundle master`));
69
+ if (packingType) {
70
+ qb.andWhere('PD.packing_type = :packingType', { packingType });
114
71
  }
115
- }
116
- }));
117
- let finalOrderInventories = [];
118
- // Case 1. Every order inventories has specified batch id
119
- if (newOrderInventories.every((orderInventory) => orderInventory.batchId)) {
120
- finalOrderInventories = await Promise.all(newOrderInventories.map(async (orderInventory) => {
121
- var _a;
122
- let filters = [
123
- {
124
- name: 'batchId',
125
- operator: 'eq',
126
- value: orderInventory.batchId
127
- },
128
- {
129
- name: 'productName',
130
- operator: 'eq',
131
- value: orderInventory.product.sku
132
- },
133
- {
134
- name: 'bizplace',
135
- operator: 'in',
136
- value: [customerBizplaceId]
72
+ const productDetail = await qb.getOne();
73
+ if (productDetail) {
74
+ let newOrderInventory = Object.assign({}, orderInventory);
75
+ newOrderInventory.product.sku = productDetail.product.sku;
76
+ newOrderInventory.product = productDetail.product;
77
+ newOrderInventory.packingType = productDetail.packingType;
78
+ newOrderInventory.uomValue = productDetail.uomValue;
79
+ newOrderInventories.push(newOrderInventory);
80
+ }
81
+ if (!productDetail) {
82
+ const qb = tx
83
+ .getRepository(product_base_1.ProductBundle)
84
+ .createQueryBuilder('PB')
85
+ .innerJoinAndSelect('PB.productBundleSettings', 'PBS')
86
+ .innerJoinAndSelect('PBS.productDetail', 'PBD')
87
+ .innerJoinAndSelect('PBD.product', 'PROD')
88
+ .where('PB.bizplace_id = :bizplaceId', { bizplaceId: custCompanyBizplace.id });
89
+ if (!sku && !refCode) {
90
+ context.throw(404, t('error.sku or refCode not found'));
137
91
  }
138
- ];
139
- // 2. get all inventory data according to SKU and batchId
140
- const response = await client.query({
141
- query: (0, graphql_tag_1.default) `
142
- query inventoriesByPallet($filters: [Filter]) {
143
- inventoriesByPallet(filters: $filters) {
144
- items {
145
- product {
146
- id
147
- name
148
- description
149
- }
150
- batchId
151
- packingType
152
- remainQty
153
- remainWeight
154
- remainUomValue
155
- }
156
- total
92
+ if (refCode) {
93
+ qb.andWhere('PB.ref_code = :refCode', { refCode });
157
94
  }
158
- }
159
- `,
160
- variables: { filters, locationSortingRules },
161
- context: Object.assign(Object.assign({}, context), { state: Object.assign(Object.assign({}, context.state), { bizplace: customerBizplace, domain }) })
162
- });
163
- if (!response.errors) {
164
- const items = response.data.inventoriesByPallet.items || [];
165
- if (!(items === null || items === void 0 ? void 0 : items.length))
166
- context.throw(404, t(`error.no inventories found for ${(_a = orderInventory.product) === null || _a === void 0 ? void 0 : _a.sku} with batch ID ${orderInventory === null || orderInventory === void 0 ? void 0 : orderInventory.batchId}`));
167
- items.forEach(item => {
168
- var _a;
169
- if (orderInventory.releaseQty > item.remainQty || orderInventory.releaseQty <= 0) {
170
- context.throw(404, t(`error.invalid release qty for ${(_a = orderInventory.product) === null || _a === void 0 ? void 0 : _a.sku}`));
171
- }
172
- // 3. add more information to orderInventory
173
- orderInventory.product.id = item.product.id;
174
- orderInventory.packingType = item.packingType;
175
- orderInventory.batchId = item.batchId;
176
- orderInventory.type = sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS;
177
- orderInventory.releaseUomValue = orderInventory.releaseQty * orderInventory.uomValue;
178
- orderInventory.uom = item.uom;
179
- });
180
- return orderInventory;
181
- }
182
- else
183
- context.throw(404, t('error.failed to fetch inventory'));
184
- }));
185
- }
186
- else {
187
- // Case 2. Some of order inventories doesn't have specified batch id
188
- // MMS order doesn't have batch ID field.
189
- // To cover this case first result of found inventory will be a target of RO
190
- for (let i = 0; i < newOrderInventories.length; i++) {
191
- const orderInventory = newOrderInventories[i];
192
- const product = orderInventory.product;
193
- const packingType = orderInventory.packingType;
194
- if (isAutoAssign) {
195
- let qb = (0, typeorm_1.getRepository)(warehouse_base_1.Inventory).createQueryBuilder('iv');
196
- qb.leftJoinAndSelect('iv.location', 'loc')
197
- .andWhere('"iv"."domain_id" = :domainId')
198
- .andWhere('"iv"."bizplace_id" = :bizplaceId')
199
- .andWhere('"iv"."packing_type" = :packingType')
200
- .andWhere('"iv"."product_id" = :productId')
201
- .andWhere('"iv"."status" = :status')
202
- .setParameters({
203
- domainId: domain.id,
204
- bizplaceId: customerBizplace.id,
205
- packingType: packingType,
206
- productId: product.id,
207
- status: warehouse_base_1.INVENTORY_STATUS.STORED
208
- });
209
- if ((locationSortingRules === null || locationSortingRules === void 0 ? void 0 : locationSortingRules.length) > 0) {
210
- locationSortingRules.forEach((rule, idx) => {
211
- idx === 0
212
- ? qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC')
213
- : qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC');
95
+ if (sku) {
96
+ qb.andWhere('PB.sku = :sku', { sku });
97
+ }
98
+ if (packingType) {
99
+ qb.andWhere('PB.packing_type = :packingType', { packingType });
100
+ }
101
+ const foundProductBundle = await qb.getOne();
102
+ if (foundProductBundle) {
103
+ const bundleProductSettings = foundProductBundle.productBundleSettings;
104
+ bundleProductSettings.map(bundleProductSetting => {
105
+ const productDetailBundle = bundleProductSetting.productDetail;
106
+ let newOrderInventory = Object.assign({}, orderInventory);
107
+ newOrderInventory.product.sku = productDetailBundle.product.sku;
108
+ newOrderInventory.packingType = productDetailBundle.packingType;
109
+ newOrderInventory.uomValue = productDetailBundle.uomValue;
110
+ newOrderInventory.releaseQty = orderInventory.releaseQty * bundleProductSetting.bundleQty;
111
+ newOrderInventory.product = productDetailBundle.product;
112
+ newOrderInventories.push(newOrderInventory);
214
113
  });
215
114
  }
216
115
  else {
217
- qb.addOrderBy('"iv"."created_at"', 'ASC');
116
+ if (sku && refCode) {
117
+ context.throw(404, t(`error.sku ${sku} and refCode ${refCode} not exist in product / bundle master`));
118
+ }
119
+ if (sku) {
120
+ context.throw(404, t(`error.sku ${sku} not exist in product / bundle master`));
121
+ }
122
+ if (refCode) {
123
+ context.throw(404, t(`error.refCode ${refCode} not exist in product / bundle master`));
124
+ }
218
125
  }
219
- let inventories = await qb.getMany();
220
- if (!(inventories === null || inventories === void 0 ? void 0 : inventories.length))
221
- context.throw(404, t(`error.no inventories found for ${product.sku}`));
222
- inventories = inventories.map(inventory => {
223
- return Object.assign(Object.assign({}, inventory), { remainQty: inventory.qty - inventory.lockedQty, remainUomValue: inventory.uomValue - inventory.lockedUomValue });
224
- });
225
- const targetInventories = _composeTargetInventories(context, product, orderInventory, inventories);
226
- targetInventories.map(item => {
227
- finalOrderInventories.push(item.targetInventory);
228
- });
229
126
  }
230
- else {
127
+ }));
128
+ let finalOrderInventories = [];
129
+ // Case 1. Every order inventories has specified batch id
130
+ if (newOrderInventories.every((orderInventory) => orderInventory.batchId)) {
131
+ finalOrderInventories = await Promise.all(newOrderInventories.map(async (orderInventory) => {
132
+ var _a;
231
133
  let filters = [
134
+ {
135
+ name: 'batchId',
136
+ operator: 'eq',
137
+ value: orderInventory.batchId
138
+ },
232
139
  {
233
140
  name: 'productName',
234
141
  operator: 'eq',
235
142
  value: orderInventory.product.sku
236
143
  },
237
144
  {
238
- name: 'bizplace_id',
239
- operator: 'eq',
240
- value: customerBizplaceId
145
+ name: 'bizplace',
146
+ operator: 'in',
147
+ value: [customerBizplaceId]
241
148
  }
242
149
  ];
243
150
  // 2. get all inventory data according to SKU and batchId
244
151
  const response = await client.query({
245
152
  query: (0, graphql_tag_1.default) `
246
- query inventoryProductGroup($filters: [Filter]) {
247
- inventoryProductGroup(filters: $filters) {
248
- items {
249
- productId
250
- productSKU
251
- productName
252
- batchId
253
- packingType
254
- remainQty
255
- remainWeight
256
- remainUomValue
153
+ query inventoriesByPallet($filters: [Filter]) {
154
+ inventoriesByPallet(filters: $filters) {
155
+ items {
156
+ product {
157
+ id
158
+ name
159
+ description
160
+ }
161
+ batchId
162
+ packingType
163
+ remainQty
164
+ remainWeight
165
+ remainUomValue
166
+ }
167
+ total
257
168
  }
258
- total
259
169
  }
260
- }
261
- `,
262
- variables: { filters },
170
+ `,
171
+ variables: { filters, locationSortingRules },
263
172
  context: Object.assign(Object.assign({}, context), { state: Object.assign(Object.assign({}, context.state), { bizplace: customerBizplace, domain }) })
264
173
  });
265
174
  if (!response.errors) {
266
- const items = response.data.inventoryProductGroup.items || [];
175
+ const items = response.data.inventoriesByPallet.items || [];
267
176
  if (!(items === null || items === void 0 ? void 0 : items.length))
268
- context.throw(404, t(`error.no inventories found for ${(_b = orderInventory.product) === null || _b === void 0 ? void 0 : _b.sku} with batch ID ${orderInventory === null || orderInventory === void 0 ? void 0 : orderInventory.batchId}`));
177
+ context.throw(404, t(`error.no inventories found for ${(_a = orderInventory.product) === null || _a === void 0 ? void 0 : _a.sku} with batch ID ${orderInventory === null || orderInventory === void 0 ? void 0 : orderInventory.batchId}`));
269
178
  items.forEach(item => {
270
179
  var _a;
271
180
  if (orderInventory.releaseQty > item.remainQty || orderInventory.releaseQty <= 0) {
272
181
  context.throw(404, t(`error.invalid release qty for ${(_a = orderInventory.product) === null || _a === void 0 ? void 0 : _a.sku}`));
273
182
  }
274
183
  // 3. add more information to orderInventory
275
- orderInventory.product = { id: item.productId };
184
+ orderInventory.product.id = item.product.id;
276
185
  orderInventory.packingType = item.packingType;
277
186
  orderInventory.batchId = item.batchId;
278
187
  orderInventory.type = sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS;
279
188
  orderInventory.releaseUomValue = orderInventory.releaseQty * orderInventory.uomValue;
280
189
  orderInventory.uom = item.uom;
281
190
  });
282
- delete orderInventory.uomValue;
283
- finalOrderInventories.push(orderInventory);
191
+ return orderInventory;
284
192
  }
285
193
  else
286
194
  context.throw(404, t('error.failed to fetch inventory'));
287
- }
195
+ }));
288
196
  }
289
- }
290
- context.body = {
291
- result: (await client.mutate({
292
- mutation: (0, graphql_tag_1.default) `
293
- mutation addReleaseGood($releaseGood: NewReleaseGood, $shippingOrder: ShippingOrderPatch, $file: Upload) {
294
- addReleaseGood(releaseGood: $releaseGood, shippingOrder: $shippingOrder, file: $file) {
295
- id
296
- name
297
- refNo
298
- status
299
- truckNo
300
- ownTransport
301
- crossDocking
302
- marketplaceOrderStatus
303
- billingAddress
304
- deliveryAddress1
305
- deliveryAddress2
306
- deliveryAddress3
307
- deliveryAddress4
308
- deliveryAddress5
309
- attentionTo
310
- attentionCompany
311
- city
312
- state
313
- postalCode
314
- country
315
- phone1
316
- phone2
317
- email
318
- transporter
319
- trackingNo
320
- airwayBill
321
- invoice
322
- type
323
- arrivalNotice {
324
- name
325
- }
326
- exportOption
327
- releaseDate
328
- collectionOrderNo
329
- bizplace {
330
- name
197
+ else {
198
+ // Case 2. Some of order inventories doesn't have specified batch id
199
+ // MMS order doesn't have batch ID field.
200
+ // To cover this case first result of found inventory will be a target of RO
201
+ for (let i = 0; i < newOrderInventories.length; i++) {
202
+ const orderInventory = newOrderInventories[i];
203
+ const product = orderInventory.product;
204
+ const packingType = orderInventory.packingType;
205
+ if (isAutoAssign) {
206
+ let qb = tx.getRepository(warehouse_base_1.Inventory).createQueryBuilder('iv');
207
+ qb.leftJoinAndSelect('iv.location', 'loc')
208
+ .andWhere('"iv"."domain_id" = :domainId')
209
+ .andWhere('"iv"."bizplace_id" = :bizplaceId')
210
+ .andWhere('"iv"."packing_type" = :packingType')
211
+ .andWhere('"iv"."product_id" = :productId')
212
+ .andWhere('"iv"."status" = :status')
213
+ .setParameters({
214
+ domainId: domain.id,
215
+ bizplaceId: customerBizplace.id,
216
+ packingType: packingType,
217
+ productId: product.id,
218
+ status: warehouse_base_1.INVENTORY_STATUS.STORED
219
+ });
220
+ if ((locationSortingRules === null || locationSortingRules === void 0 ? void 0 : locationSortingRules.length) > 0) {
221
+ locationSortingRules.forEach((rule, idx) => {
222
+ idx === 0
223
+ ? qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC')
224
+ : qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC');
225
+ });
226
+ }
227
+ else {
228
+ qb.addOrderBy('"iv"."created_at"', 'ASC');
229
+ }
230
+ let inventories = await qb.getMany();
231
+ if (!(inventories === null || inventories === void 0 ? void 0 : inventories.length))
232
+ context.throw(404, t(`error.no inventories found for ${product.sku}`));
233
+ inventories = inventories.map(inventory => {
234
+ return Object.assign(Object.assign({}, inventory), { remainQty: inventory.qty - inventory.lockedQty, remainUomValue: inventory.uomValue - inventory.lockedUomValue });
235
+ });
236
+ const targetInventories = _composeTargetInventories(context, product, orderInventory, inventories);
237
+ targetInventories.map(item => {
238
+ finalOrderInventories.push(item.targetInventory);
239
+ });
331
240
  }
332
- orderInventories {
333
- product {
334
- sku
335
- name
336
- description
241
+ else {
242
+ let filters = [
243
+ {
244
+ name: 'productName',
245
+ operator: 'eq',
246
+ value: orderInventory.product.sku
247
+ },
248
+ {
249
+ name: 'bizplace_id',
250
+ operator: 'eq',
251
+ value: customerBizplaceId
252
+ }
253
+ ];
254
+ // 2. get all inventory data according to SKU and batchId
255
+ const response = await client.query({
256
+ query: (0, graphql_tag_1.default) `
257
+ query inventoryProductGroup($filters: [Filter]) {
258
+ inventoryProductGroup(filters: $filters) {
259
+ items {
260
+ productId
261
+ productSKU
262
+ productName
263
+ batchId
264
+ packingType
265
+ remainQty
266
+ remainWeight
267
+ remainUomValue
268
+ }
269
+ total
337
270
  }
338
- batchId
339
- packingType
340
- packingSize
341
- releaseQty
342
- releaseUomValue
343
271
  }
344
- orderVass {
345
- vas {
272
+ `,
273
+ variables: { filters },
274
+ context: Object.assign(Object.assign({}, context), { state: Object.assign(Object.assign({}, context.state), { bizplace: customerBizplace, domain }) })
275
+ });
276
+ if (!response.errors) {
277
+ const items = response.data.inventoryProductGroup.items || [];
278
+ if (!(items === null || items === void 0 ? void 0 : items.length))
279
+ context.throw(404, t(`error.no inventories found for ${(_b = orderInventory.product) === null || _b === void 0 ? void 0 : _b.sku} with batch ID ${orderInventory === null || orderInventory === void 0 ? void 0 : orderInventory.batchId}`));
280
+ items.forEach(item => {
281
+ var _a;
282
+ if (orderInventory.releaseQty > item.remainQty || orderInventory.releaseQty <= 0) {
283
+ context.throw(404, t(`error.invalid release qty for ${(_a = orderInventory.product) === null || _a === void 0 ? void 0 : _a.sku}`));
284
+ }
285
+ // 3. add more information to orderInventory
286
+ orderInventory.product = { id: item.productId };
287
+ orderInventory.packingType = item.packingType;
288
+ orderInventory.batchId = item.batchId;
289
+ orderInventory.type = sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS;
290
+ orderInventory.releaseUomValue = orderInventory.releaseQty * orderInventory.uomValue;
291
+ orderInventory.uom = item.uom;
292
+ });
293
+ delete orderInventory.uomValue;
294
+ finalOrderInventories.push(orderInventory);
295
+ }
296
+ else
297
+ context.throw(404, t('error.failed to fetch inventory'));
298
+ }
299
+ }
300
+ }
301
+ context.body = {
302
+ result: (await client.mutate({
303
+ mutation: (0, graphql_tag_1.default) `
304
+ mutation addReleaseGood($releaseGood: NewReleaseGood, $shippingOrder: ShippingOrderPatch, $file: Upload) {
305
+ addReleaseGood(releaseGood: $releaseGood, shippingOrder: $shippingOrder, file: $file) {
306
+ id
307
+ name
308
+ refNo
309
+ status
310
+ truckNo
311
+ ownTransport
312
+ crossDocking
313
+ marketplaceOrderStatus
314
+ billingAddress
315
+ deliveryAddress1
316
+ deliveryAddress2
317
+ deliveryAddress3
318
+ deliveryAddress4
319
+ deliveryAddress5
320
+ attentionTo
321
+ attentionCompany
322
+ city
323
+ state
324
+ postalCode
325
+ country
326
+ phone1
327
+ phone2
328
+ email
329
+ transporter
330
+ trackingNo
331
+ airwayBill
332
+ invoice
333
+ type
334
+ arrivalNotice {
346
335
  name
347
- operationGuide
348
- operationGuideType
349
336
  }
350
- set
351
- targetType
352
- targetBatchId
353
- targetProduct {
337
+ exportOption
338
+ releaseDate
339
+ collectionOrderNo
340
+ bizplace {
354
341
  name
342
+ }
343
+ orderInventories {
344
+ product {
345
+ sku
346
+ name
347
+ description
348
+ }
349
+ batchId
350
+ packingType
351
+ packingSize
352
+ releaseQty
353
+ releaseUomValue
354
+ }
355
+ orderVass {
356
+ vas {
357
+ name
358
+ operationGuide
359
+ operationGuideType
360
+ }
361
+ set
362
+ targetType
363
+ targetBatchId
364
+ targetProduct {
365
+ name
366
+ description
367
+ }
368
+ packingType
369
+ qty
370
+ uomValue
371
+ otherTarget
355
372
  description
373
+ remark
374
+ status
375
+ operationGuide
356
376
  }
357
- packingType
358
- qty
359
- uomValue
360
- otherTarget
361
- description
362
- remark
363
- status
364
- operationGuide
365
377
  }
366
378
  }
367
- }
368
- `,
369
- variables: {
370
- releaseGood: {
371
- customerBizplaceId: customerBizplace.id,
372
- courierOption: bodyReq.courierOption,
373
- collectionOrderNo: bodyReq.collectionOrderNo,
374
- exportOption: bodyReq.exportOption,
375
- orderInventories: finalOrderInventories,
376
- ownTransport: bodyReq.ownTransport,
377
- packingOption: bodyReq.packingOption,
378
- refNo: bodyReq.refNo,
379
- refNo2: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.refNo2,
380
- releaseDate: bodyReq.releaseDate,
381
- type: bodyReq.type,
382
- marketplaceOrderStatus: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.marketplaceOrderStatus,
383
- billingAddress: ((_c = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.billTo) === null || _c === void 0 ? void 0 : _c.billingAddress) || null,
384
- deliveryAddress1: ((_d = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _d === void 0 ? void 0 : _d.deliveryAddress1) || null,
385
- deliveryAddress2: ((_e = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _e === void 0 ? void 0 : _e.deliveryAddress2) || null,
386
- deliveryAddress3: ((_f = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _f === void 0 ? void 0 : _f.deliveryAddress3) || null,
387
- deliveryAddress4: ((_g = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _g === void 0 ? void 0 : _g.deliveryAddress4) || null,
388
- deliveryAddress5: ((_h = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _h === void 0 ? void 0 : _h.deliveryAddress5) || null,
389
- attentionTo: ((_j = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _j === void 0 ? void 0 : _j.attentionTo) || null,
390
- attentionCompany: ((_k = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _k === void 0 ? void 0 : _k.attentionCompany) || null,
391
- city: ((_l = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _l === void 0 ? void 0 : _l.city) || null,
392
- state: ((_m = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _m === void 0 ? void 0 : _m.state) || null,
393
- postalCode: ((_o = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _o === void 0 ? void 0 : _o.postalCode) || null,
394
- country: ((_p = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _p === void 0 ? void 0 : _p.country) || null,
395
- phone1: ((_q = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _q === void 0 ? void 0 : _q.phone1) || null,
396
- phone2: ((_r = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _r === void 0 ? void 0 : _r.phone2) || null,
397
- email: ((_s = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _s === void 0 ? void 0 : _s.email) || null,
398
- transporter: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.transporter,
399
- trackingNo: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.trackingNo,
400
- airwayBill: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.airwayBill,
401
- invoice: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.invoice
402
- }
403
- },
404
- context: Object.assign(Object.assign({}, context), { hasUpload: true, state: Object.assign(Object.assign({}, context.state), { customerBizplace,
405
- domain, type: 'api' }) })
406
- })).data
407
- };
379
+ `,
380
+ variables: {
381
+ releaseGood: {
382
+ customerBizplaceId: customerBizplace.id,
383
+ courierOption: bodyReq.courierOption,
384
+ collectionOrderNo: bodyReq.collectionOrderNo,
385
+ exportOption: bodyReq.exportOption,
386
+ orderInventories: finalOrderInventories,
387
+ ownTransport: bodyReq.ownTransport,
388
+ packingOption: bodyReq.packingOption,
389
+ refNo: bodyReq.refNo,
390
+ refNo2: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.refNo2,
391
+ releaseDate: bodyReq.releaseDate,
392
+ type: bodyReq.type,
393
+ marketplaceOrderStatus: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.marketplaceOrderStatus,
394
+ billingAddress: ((_c = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.billTo) === null || _c === void 0 ? void 0 : _c.billingAddress) || null,
395
+ deliveryAddress1: ((_d = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _d === void 0 ? void 0 : _d.deliveryAddress1) || null,
396
+ deliveryAddress2: ((_e = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _e === void 0 ? void 0 : _e.deliveryAddress2) || null,
397
+ deliveryAddress3: ((_f = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _f === void 0 ? void 0 : _f.deliveryAddress3) || null,
398
+ deliveryAddress4: ((_g = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _g === void 0 ? void 0 : _g.deliveryAddress4) || null,
399
+ deliveryAddress5: ((_h = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _h === void 0 ? void 0 : _h.deliveryAddress5) || null,
400
+ attentionTo: ((_j = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _j === void 0 ? void 0 : _j.attentionTo) || null,
401
+ attentionCompany: ((_k = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _k === void 0 ? void 0 : _k.attentionCompany) || null,
402
+ city: ((_l = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _l === void 0 ? void 0 : _l.city) || null,
403
+ state: ((_m = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _m === void 0 ? void 0 : _m.state) || null,
404
+ postalCode: ((_o = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _o === void 0 ? void 0 : _o.postalCode) || null,
405
+ country: ((_p = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _p === void 0 ? void 0 : _p.country) || null,
406
+ phone1: ((_q = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _q === void 0 ? void 0 : _q.phone1) || null,
407
+ phone2: ((_r = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _r === void 0 ? void 0 : _r.phone2) || null,
408
+ email: ((_s = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _s === void 0 ? void 0 : _s.email) || null,
409
+ transporter: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.transporter,
410
+ trackingNo: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.trackingNo,
411
+ airwayBill: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.airwayBill,
412
+ invoice: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.invoice
413
+ }
414
+ },
415
+ context: Object.assign(Object.assign({}, context), { hasUpload: true, state: Object.assign(Object.assign({}, context.state), { customerBizplace,
416
+ domain, type: 'api' }) })
417
+ })).data
418
+ };
419
+ });
408
420
  });
409
421
  async function checkMandatoryKey(context, params) {
410
422
  let noparams = ['releaseDate', 'exportOption'].filter(param => !(param in params));
@@ -426,9 +438,18 @@ async function checkMandatoryKey(context, params) {
426
438
  }
427
439
  }
428
440
  if ('orderInventories' in params) {
429
- if (params.orderInventories.filter(record => !record.releaseQty || !record.product).length) {
430
- noparams.push('orderInventories');
431
- }
441
+ params.orderInventories.forEach((param, key) => {
442
+ let noSubParams = [];
443
+ if (!(param === null || param === void 0 ? void 0 : param.releaseQty))
444
+ noSubParams.push('releaseQty');
445
+ if (!(param === null || param === void 0 ? void 0 : param.product))
446
+ noSubParams.push('product');
447
+ if (noSubParams === null || noSubParams === void 0 ? void 0 : noSubParams.length)
448
+ context.throw(400, context.t('error.required param x not found', { x: `${noSubParams.join(', ')}` }));
449
+ });
450
+ }
451
+ else {
452
+ noparams.push('orderInventories');
432
453
  }
433
454
  if (noparams === null || noparams === void 0 ? void 0 : noparams.length) {
434
455
  context.throw(400, context.t('error.required param x not found', { x: `${noparams.join(', ')}` }));