@things-factory/operato-hub 3.7.9 → 3.7.13

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