@things-factory/operato-hub 4.0.9 → 4.0.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.
Files changed (64) hide show
  1. package/config.development.js +49 -0
  2. package/dist-server/controllers/bizplace-initializer/dataset/codes.js +28 -4
  3. package/dist-server/controllers/bizplace-initializer/dataset/codes.js.map +1 -1
  4. package/dist-server/controllers/bizplace-initializer/dataset/menus.js +22 -0
  5. package/dist-server/controllers/bizplace-initializer/dataset/menus.js.map +1 -1
  6. package/dist-server/controllers/bizplace-initializer/dataset/roles-privileges.js +49 -0
  7. package/dist-server/controllers/bizplace-initializer/dataset/roles-privileges.js.map +1 -1
  8. package/dist-server/controllers/bizplace-initializer/dataset/settings.js +5 -0
  9. package/dist-server/controllers/bizplace-initializer/dataset/settings.js.map +1 -1
  10. package/dist-server/graphql/resolvers/synchronization/fetch-sync-invoices.js +1 -1
  11. package/dist-server/graphql/resolvers/synchronization/fetch-sync-invoices.js.map +1 -1
  12. package/dist-server/graphql/resolvers/synchronization/fetch-sync-purchase-orders.js +23 -10
  13. package/dist-server/graphql/resolvers/synchronization/fetch-sync-purchase-orders.js.map +1 -1
  14. package/dist-server/graphql/types/synchronization/index.js +1 -1
  15. package/dist-server/routers/api/restful-apis/unstable/add-inbound-order.js +9 -9
  16. package/dist-server/routers/api/restful-apis/unstable/add-inbound-order.js.map +1 -1
  17. package/dist-server/routers/api/restful-apis/unstable/add-release-order.js +396 -306
  18. package/dist-server/routers/api/restful-apis/unstable/add-release-order.js.map +1 -1
  19. package/dist-server/routers/api/restful-apis/unstable/get-marketplace-order-shipping-list.js +11 -19
  20. package/dist-server/routers/api/restful-apis/unstable/get-marketplace-order-shipping-list.js.map +1 -1
  21. package/dist-server/routers/api/restful-apis/unstable/index.js +1 -0
  22. package/dist-server/routers/api/restful-apis/unstable/index.js.map +1 -1
  23. package/dist-server/routers/api/restful-apis/unstable/update-release-order-details.js +82 -0
  24. package/dist-server/routers/api/restful-apis/unstable/update-release-order-details.js.map +1 -0
  25. package/dist-server/routers/api/restful-apis/v1/add-release-order.js +394 -307
  26. package/dist-server/routers/api/restful-apis/v1/add-release-order.js.map +1 -1
  27. package/dist-server/routers/api/restful-apis/v1/get-marketplace-order-shipping-list.js +13 -22
  28. package/dist-server/routers/api/restful-apis/v1/get-marketplace-order-shipping-list.js.map +1 -1
  29. package/dist-server/routers/api/restful-apis/v1/index.js +1 -0
  30. package/dist-server/routers/api/restful-apis/v1/index.js.map +1 -1
  31. package/dist-server/routers/api/restful-apis/v1/update-release-order-details.js +82 -0
  32. package/dist-server/routers/api/restful-apis/v1/update-release-order-details.js.map +1 -0
  33. package/dist-server/routers/api/restful-apis/v2/add-release-order.js +394 -307
  34. package/dist-server/routers/api/restful-apis/v2/add-release-order.js.map +1 -1
  35. package/dist-server/routers/api/restful-apis/v2/get-marketplace-order-shipping-list.js +13 -22
  36. package/dist-server/routers/api/restful-apis/v2/get-marketplace-order-shipping-list.js.map +1 -1
  37. package/dist-server/routers/api/restful-apis/v2/index.js +1 -0
  38. package/dist-server/routers/api/restful-apis/v2/index.js.map +1 -1
  39. package/dist-server/routers/api/restful-apis/v2/update-release-order-details.js +82 -0
  40. package/dist-server/routers/api/restful-apis/v2/update-release-order-details.js.map +1 -0
  41. package/openapi/unstable/outbound.yaml +85 -0
  42. package/openapi/v1/outbound.yaml +85 -0
  43. package/openapi/v2/outbound.yaml +85 -0
  44. package/package.json +67 -67
  45. package/server/controllers/bizplace-initializer/dataset/codes.ts +28 -4
  46. package/server/controllers/bizplace-initializer/dataset/menus.ts +22 -0
  47. package/server/controllers/bizplace-initializer/dataset/roles-privileges.ts +49 -0
  48. package/server/controllers/bizplace-initializer/dataset/settings.ts +5 -0
  49. package/server/graphql/resolvers/synchronization/fetch-sync-invoices.ts +1 -1
  50. package/server/graphql/resolvers/synchronization/fetch-sync-purchase-orders.ts +33 -9
  51. package/server/graphql/types/synchronization/index.ts +1 -1
  52. package/server/routers/api/restful-apis/unstable/add-inbound-order.ts +7 -7
  53. package/server/routers/api/restful-apis/unstable/add-release-order.ts +447 -336
  54. package/server/routers/api/restful-apis/unstable/get-marketplace-order-shipping-list.ts +12 -12
  55. package/server/routers/api/restful-apis/unstable/index.ts +1 -0
  56. package/server/routers/api/restful-apis/unstable/update-release-order-details.ts +94 -0
  57. package/server/routers/api/restful-apis/v1/add-release-order.ts +444 -339
  58. package/server/routers/api/restful-apis/v1/get-marketplace-order-shipping-list.ts +14 -15
  59. package/server/routers/api/restful-apis/v1/index.ts +1 -0
  60. package/server/routers/api/restful-apis/v1/update-release-order-details.ts +94 -0
  61. package/server/routers/api/restful-apis/v2/add-release-order.ts +444 -339
  62. package/server/routers/api/restful-apis/v2/get-marketplace-order-shipping-list.ts +14 -15
  63. package/server/routers/api/restful-apis/v2/index.ts +1 -0
  64. package/server/routers/api/restful-apis/v2/update-release-order-details.ts +94 -0
@@ -16,330 +16,409 @@ 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;
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.productBundleSettings', '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 && 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
+ }
125
+ }
96
126
  }
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);
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;
133
+ let filters = [
134
+ {
135
+ name: 'batchId',
136
+ operator: 'eq',
137
+ value: orderInventory.batchId
138
+ },
139
+ {
140
+ name: 'productName',
141
+ operator: 'eq',
142
+ value: orderInventory.product.sku
143
+ },
144
+ {
145
+ name: 'bizplace',
146
+ operator: 'in',
147
+ value: [customerBizplaceId]
148
+ }
149
+ ];
150
+ // 2. get all inventory data according to SKU and batchId
151
+ const response = await client.query({
152
+ query: (0, graphql_tag_1.default) `
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
168
+ }
169
+ }
170
+ `,
171
+ variables: { filters, locationSortingRules },
172
+ context: Object.assign(Object.assign({}, context), { state: Object.assign(Object.assign({}, context.state), { bizplace: customerBizplace, domain }) })
109
173
  });
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
- }
174
+ if (!response.errors) {
175
+ const items = response.data.inventoriesByPallet.items || [];
176
+ if (!(items === null || items === void 0 ? void 0 : items.length))
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}`));
178
+ items.forEach(item => {
179
+ var _a;
180
+ if (orderInventory.releaseQty > item.remainQty || orderInventory.releaseQty <= 0) {
181
+ context.throw(404, t(`error.invalid release qty for ${(_a = orderInventory.product) === null || _a === void 0 ? void 0 : _a.sku}`));
182
+ }
183
+ // 3. add more information to orderInventory
184
+ orderInventory.product.id = item.product.id;
185
+ orderInventory.packingType = item.packingType;
186
+ orderInventory.batchId = item.batchId;
187
+ orderInventory.type = sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS;
188
+ orderInventory.releaseUomValue = orderInventory.releaseQty * orderInventory.uomValue;
189
+ orderInventory.uom = item.uom;
190
+ });
191
+ return orderInventory;
192
+ }
193
+ else
194
+ context.throw(404, t('error.failed to fetch inventory'));
195
+ }));
114
196
  }
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]
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
+ .andWhere('"loc"."type" NOT IN (:...locationTypes)')
214
+ .setParameters({
215
+ domainId: domain.id,
216
+ bizplaceId: customerBizplace.id,
217
+ packingType: packingType,
218
+ productId: product.id,
219
+ status: warehouse_base_1.INVENTORY_STATUS.STORED,
220
+ locationTypes: [warehouse_base_1.LOCATION_TYPE.QUARANTINE, warehouse_base_1.LOCATION_TYPE.RESERVE]
221
+ });
222
+ if ((locationSortingRules === null || locationSortingRules === void 0 ? void 0 : locationSortingRules.length) > 0) {
223
+ locationSortingRules.forEach((rule, idx) => {
224
+ idx === 0
225
+ ? qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC')
226
+ : qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC');
227
+ });
228
+ }
229
+ else {
230
+ qb.addOrderBy('"iv"."created_at"', 'ASC');
231
+ }
232
+ let inventories = await qb.getMany();
233
+ if (!(inventories === null || inventories === void 0 ? void 0 : inventories.length))
234
+ context.throw(404, t(`error.no inventories found for ${product.sku}`));
235
+ inventories = inventories.map(inventory => {
236
+ return Object.assign(Object.assign({}, inventory), { remainQty: inventory.qty - inventory.lockedQty, remainUomValue: inventory.uomValue - inventory.lockedUomValue });
237
+ });
238
+ const targetInventories = _composeTargetInventories(context, product, orderInventory, inventories);
239
+ targetInventories.map(item => {
240
+ finalOrderInventories.push(item.targetInventory);
241
+ });
136
242
  }
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
243
+ else {
244
+ let filters = [
245
+ {
246
+ name: 'productName',
247
+ operator: 'eq',
248
+ value: orderInventory.product.sku
249
+ },
250
+ {
251
+ name: 'bizplace_id',
252
+ operator: 'eq',
253
+ value: customerBizplaceId
254
+ }
255
+ ];
256
+ // 2. get all inventory data according to SKU and batchId
257
+ const response = await client.query({
258
+ query: (0, graphql_tag_1.default) `
259
+ query inventoryProductGroup($filters: [Filter]) {
260
+ inventoryProductGroup(filters: $filters) {
261
+ items {
262
+ productId
263
+ productSKU
264
+ productName
265
+ batchId
266
+ packingType
267
+ remainQty
268
+ remainWeight
269
+ remainUomValue
148
270
  }
149
- batchId
150
- packingType
151
- remainQty
152
- remainWeight
153
- remainUomValue
271
+ total
154
272
  }
155
- total
156
273
  }
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}`));
274
+ `,
275
+ variables: { filters },
276
+ context: Object.assign(Object.assign({}, context), { state: Object.assign(Object.assign({}, context.state), { bizplace: customerBizplace, domain }) })
277
+ });
278
+ if (!response.errors) {
279
+ const items = response.data.inventoryProductGroup.items || [];
280
+ if (!(items === null || items === void 0 ? void 0 : items.length))
281
+ 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}`));
282
+ items.forEach(item => {
283
+ var _a;
284
+ if (orderInventory.releaseQty > item.remainQty || orderInventory.releaseQty <= 0) {
285
+ context.throw(404, t(`error.invalid release qty for ${(_a = orderInventory.product) === null || _a === void 0 ? void 0 : _a.sku}`));
286
+ }
287
+ // 3. add more information to orderInventory
288
+ orderInventory.product = { id: item.productId };
289
+ orderInventory.packingType = item.packingType;
290
+ orderInventory.batchId = item.batchId;
291
+ orderInventory.type = sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS;
292
+ orderInventory.releaseUomValue = orderInventory.releaseQty * orderInventory.uomValue;
293
+ orderInventory.uom = item.uom;
294
+ });
295
+ delete orderInventory.uomValue;
296
+ finalOrderInventories.push(orderInventory);
170
297
  }
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
- });
178
- return orderInventory;
179
- }
180
- else
181
- context.throw(404, t('error.failed to fetch inventory'));
182
- }));
183
- }
184
- else {
185
- // Case 2. Some of order inventories doesn't have specified batch id
186
- // MMS order doesn't have batch ID field.
187
- // To cover this case first result of found inventory will be a target of RO
188
- for (let i = 0; i < newOrderInventories.length; i++) {
189
- const orderInventory = newOrderInventories[i];
190
- const product = orderInventory.product;
191
- const packingType = orderInventory.packingType;
192
- let qb = (0, typeorm_1.getRepository)(warehouse_base_1.Inventory).createQueryBuilder('iv');
193
- qb.leftJoinAndSelect('iv.location', 'loc')
194
- .andWhere('"iv"."domain_id" = :domainId')
195
- .andWhere('"iv"."bizplace_id" = :bizplaceId')
196
- .andWhere('"iv"."packing_type" = :packingType')
197
- .andWhere('"iv"."product_id" = :productId')
198
- .andWhere('"iv"."status" = :status')
199
- .setParameters({
200
- domainId: domain.id,
201
- bizplaceId: customerBizplace.id,
202
- packingType: packingType,
203
- productId: product.id,
204
- status: warehouse_base_1.INVENTORY_STATUS.STORED
205
- });
206
- if ((locationSortingRules === null || locationSortingRules === void 0 ? void 0 : locationSortingRules.length) > 0) {
207
- locationSortingRules.forEach((rule, idx) => {
208
- idx === 0
209
- ? qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC')
210
- : qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC');
211
- });
212
- }
213
- else {
214
- qb.addOrderBy('"iv"."created_at"', 'ASC');
298
+ else
299
+ context.throw(404, t('error.failed to fetch inventory'));
300
+ }
215
301
  }
216
- const inventories = await qb.getMany();
217
- if (!(inventories === null || inventories === void 0 ? void 0 : inventories.length))
218
- context.throw(404, t(`error.no inventories found for ${product.sku}`));
219
- const targetInventories = _composeTargetInventories(context, product, orderInventory, inventories);
220
- targetInventories.map(item => {
221
- finalOrderInventories.push(item.targetInventory);
222
- });
223
302
  }
224
- }
225
- context.body = {
226
- result: (await client.mutate({
227
- mutation: (0, graphql_tag_1.default) `
228
- mutation addReleaseGood($releaseGood: NewReleaseGood, $shippingOrder: ShippingOrderPatch, $file: Upload) {
229
- addReleaseGood(releaseGood: $releaseGood, shippingOrder: $shippingOrder, file: $file) {
230
- id
231
- name
232
- refNo
233
- status
234
- truckNo
235
- ownTransport
236
- crossDocking
237
- marketplaceOrderStatus
238
- billingAddress
239
- deliveryAddress1
240
- deliveryAddress2
241
- deliveryAddress3
242
- deliveryAddress4
243
- deliveryAddress5
244
- attentionTo
245
- attentionCompany
246
- city
247
- state
248
- postalCode
249
- country
250
- phone1
251
- phone2
252
- email
253
- transporter
254
- trackingNo
255
- airwayBill
256
- invoice
257
- type
258
- arrivalNotice {
259
- name
260
- }
261
- exportOption
262
- releaseDate
263
- collectionOrderNo
264
- bizplace {
303
+ context.body = {
304
+ result: (await client.mutate({
305
+ mutation: (0, graphql_tag_1.default) `
306
+ mutation addReleaseGood($releaseGood: NewReleaseGood, $shippingOrder: ShippingOrderPatch, $file: Upload) {
307
+ addReleaseGood(releaseGood: $releaseGood, shippingOrder: $shippingOrder, file: $file) {
308
+ id
265
309
  name
266
- }
267
- orderInventories {
268
- product {
269
- sku
310
+ refNo
311
+ status
312
+ truckNo
313
+ ownTransport
314
+ crossDocking
315
+ marketplaceOrderStatus
316
+ billingAddress
317
+ deliveryAddress1
318
+ deliveryAddress2
319
+ deliveryAddress3
320
+ deliveryAddress4
321
+ deliveryAddress5
322
+ attentionTo
323
+ attentionCompany
324
+ city
325
+ state
326
+ postalCode
327
+ country
328
+ phone1
329
+ phone2
330
+ email
331
+ transporter
332
+ trackingNo
333
+ airwayBill
334
+ invoice
335
+ type
336
+ arrivalNotice {
270
337
  name
271
- description
272
338
  }
273
- batchId
274
- packingType
275
- packingSize
276
- releaseQty
277
- releaseUomValue
278
- }
279
- orderVass {
280
- vas {
339
+ exportOption
340
+ releaseDate
341
+ collectionOrderNo
342
+ bizplace {
281
343
  name
282
- operationGuide
283
- operationGuideType
284
344
  }
285
- set
286
- targetType
287
- targetBatchId
288
- targetProduct {
289
- name
345
+ orderInventories {
346
+ product {
347
+ sku
348
+ name
349
+ description
350
+ }
351
+ batchId
352
+ packingType
353
+ packingSize
354
+ releaseQty
355
+ releaseUomValue
356
+ }
357
+ orderVass {
358
+ vas {
359
+ name
360
+ operationGuide
361
+ operationGuideType
362
+ }
363
+ set
364
+ targetType
365
+ targetBatchId
366
+ targetProduct {
367
+ name
368
+ description
369
+ }
370
+ packingType
371
+ qty
372
+ uomValue
373
+ otherTarget
290
374
  description
375
+ remark
376
+ status
377
+ operationGuide
291
378
  }
292
- packingType
293
- qty
294
- uomValue
295
- otherTarget
296
- description
297
- remark
298
- status
299
- operationGuide
300
379
  }
301
380
  }
302
- }
303
- `,
304
- variables: {
305
- releaseGood: {
306
- customerBizplaceId: customerBizplace.id,
307
- courierOption: bodyReq.courierOption,
308
- collectionOrderNo: bodyReq.collectionOrderNo,
309
- exportOption: bodyReq.exportOption,
310
- orderInventories: finalOrderInventories,
311
- ownTransport: bodyReq.ownTransport,
312
- packingOption: bodyReq.packingOption,
313
- refNo: bodyReq.refNo,
314
- refNo2: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.refNo2,
315
- releaseDate: bodyReq.releaseDate,
316
- type: bodyReq.type,
317
- marketplaceOrderStatus: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.marketplaceOrderStatus,
318
- billingAddress: ((_b = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.billTo) === null || _b === void 0 ? void 0 : _b.billingAddress) || null,
319
- deliveryAddress1: ((_c = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _c === void 0 ? void 0 : _c.deliveryAddress1) || null,
320
- deliveryAddress2: ((_d = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _d === void 0 ? void 0 : _d.deliveryAddress2) || null,
321
- deliveryAddress3: ((_e = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _e === void 0 ? void 0 : _e.deliveryAddress3) || null,
322
- deliveryAddress4: ((_f = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _f === void 0 ? void 0 : _f.deliveryAddress4) || null,
323
- deliveryAddress5: ((_g = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _g === void 0 ? void 0 : _g.deliveryAddress5) || null,
324
- attentionTo: ((_h = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _h === void 0 ? void 0 : _h.attentionTo) || null,
325
- attentionCompany: ((_j = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _j === void 0 ? void 0 : _j.attentionCompany) || null,
326
- city: ((_k = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _k === void 0 ? void 0 : _k.city) || null,
327
- state: ((_l = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _l === void 0 ? void 0 : _l.state) || null,
328
- postalCode: ((_m = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _m === void 0 ? void 0 : _m.postalCode) || null,
329
- country: ((_o = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _o === void 0 ? void 0 : _o.country) || null,
330
- phone1: ((_p = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _p === void 0 ? void 0 : _p.phone1) || null,
331
- phone2: ((_q = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _q === void 0 ? void 0 : _q.phone2) || null,
332
- email: ((_r = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _r === void 0 ? void 0 : _r.email) || null,
333
- transporter: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.transporter,
334
- trackingNo: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.trackingNo,
335
- airwayBill: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.airwayBill,
336
- invoice: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.invoice
337
- }
338
- },
339
- context: Object.assign(Object.assign({}, context), { hasUpload: true, state: Object.assign(Object.assign({}, context.state), { customerBizplace,
340
- domain, type: 'api' }) })
341
- })).data
342
- };
381
+ `,
382
+ variables: {
383
+ releaseGood: {
384
+ customerBizplaceId: customerBizplace.id,
385
+ courierOption: bodyReq.courierOption,
386
+ collectionOrderNo: bodyReq.collectionOrderNo,
387
+ exportOption: bodyReq.exportOption,
388
+ orderInventories: finalOrderInventories,
389
+ ownTransport: bodyReq.ownTransport,
390
+ packingOption: bodyReq.packingOption,
391
+ refNo: bodyReq.refNo,
392
+ refNo2: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.refNo2,
393
+ releaseDate: bodyReq.releaseDate,
394
+ type: bodyReq.type,
395
+ marketplaceOrderStatus: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.marketplaceOrderStatus,
396
+ billingAddress: ((_c = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.billTo) === null || _c === void 0 ? void 0 : _c.billingAddress) || null,
397
+ deliveryAddress1: ((_d = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _d === void 0 ? void 0 : _d.deliveryAddress1) || null,
398
+ deliveryAddress2: ((_e = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _e === void 0 ? void 0 : _e.deliveryAddress2) || null,
399
+ deliveryAddress3: ((_f = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _f === void 0 ? void 0 : _f.deliveryAddress3) || null,
400
+ deliveryAddress4: ((_g = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _g === void 0 ? void 0 : _g.deliveryAddress4) || null,
401
+ deliveryAddress5: ((_h = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _h === void 0 ? void 0 : _h.deliveryAddress5) || null,
402
+ attentionTo: ((_j = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _j === void 0 ? void 0 : _j.attentionTo) || null,
403
+ attentionCompany: ((_k = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _k === void 0 ? void 0 : _k.attentionCompany) || null,
404
+ city: ((_l = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _l === void 0 ? void 0 : _l.city) || null,
405
+ state: ((_m = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _m === void 0 ? void 0 : _m.state) || null,
406
+ postalCode: ((_o = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _o === void 0 ? void 0 : _o.postalCode) || null,
407
+ country: ((_p = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _p === void 0 ? void 0 : _p.country) || null,
408
+ phone1: ((_q = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _q === void 0 ? void 0 : _q.phone1) || null,
409
+ phone2: ((_r = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _r === void 0 ? void 0 : _r.phone2) || null,
410
+ email: ((_s = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _s === void 0 ? void 0 : _s.email) || null,
411
+ transporter: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.transporter,
412
+ trackingNo: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.trackingNo,
413
+ airwayBill: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.airwayBill,
414
+ invoice: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.invoice
415
+ }
416
+ },
417
+ context: Object.assign(Object.assign({}, context), { hasUpload: true, state: Object.assign(Object.assign({}, context.state), { customerBizplace,
418
+ domain, type: 'api' }) })
419
+ })).data
420
+ };
421
+ });
343
422
  });
344
423
  async function checkMandatoryKey(context, params) {
345
424
  let noparams = ['releaseDate', 'exportOption'].filter(param => !(param in params));
@@ -361,9 +440,18 @@ async function checkMandatoryKey(context, params) {
361
440
  }
362
441
  }
363
442
  if ('orderInventories' in params) {
364
- if (params.orderInventories.filter(record => !record.releaseQty || !record.product).length) {
365
- noparams.push('orderInventories');
366
- }
443
+ params.orderInventories.forEach((param, key) => {
444
+ let noSubParams = [];
445
+ if (!(param === null || param === void 0 ? void 0 : param.releaseQty))
446
+ noSubParams.push('releaseQty');
447
+ if (!(param === null || param === void 0 ? void 0 : param.product))
448
+ noSubParams.push('product');
449
+ if (noSubParams === null || noSubParams === void 0 ? void 0 : noSubParams.length)
450
+ context.throw(400, context.t('error.required param x not found', { x: `${noSubParams.join(', ')}` }));
451
+ });
452
+ }
453
+ else {
454
+ noparams.push('orderInventories');
367
455
  }
368
456
  if (noparams === null || noparams === void 0 ? void 0 : noparams.length) {
369
457
  context.throw(400, context.t('error.required param x not found', { x: `${noparams.join(', ')}` }));
@@ -379,7 +467,7 @@ function _composeTargetInventories(context, product, record, inventories) {
379
467
  let compReleaseUomValue = 0;
380
468
  let totalInventoryQty = 0;
381
469
  inventories.map(inventory => {
382
- totalInventoryQty += inventory.qty;
470
+ totalInventoryQty += inventory.remainQty;
383
471
  });
384
472
  if (totalInventoryQty < record.releaseQty) {
385
473
  context.throw(404, t(`error.invalid release qty for ${product === null || product === void 0 ? void 0 : product.sku}`));
@@ -388,7 +476,7 @@ function _composeTargetInventories(context, product, record, inventories) {
388
476
  let idx = 0;
389
477
  while (compReleaseQty < record.releaseQty) {
390
478
  const inv = inventories[idx];
391
- if (inv.qty > leftReleaseQty) {
479
+ if (inv.remainQty > leftReleaseQty) {
392
480
  compReleaseQty += leftReleaseQty;
393
481
  compReleaseUomValue += leftReleaseUomValue;
394
482
  targetInventories.push({
@@ -398,23 +486,25 @@ function _composeTargetInventories(context, product, record, inventories) {
398
486
  batchId: inv.batchId,
399
487
  releaseQty: leftReleaseQty,
400
488
  releaseUomValue: leftReleaseQty * record.uomValue,
489
+ uom: inv.uom,
401
490
  product: { id: product.id },
402
491
  type: sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS
403
492
  }
404
493
  });
405
494
  }
406
495
  else {
407
- compReleaseQty += inv.qty;
408
- compReleaseUomValue += inv.uomValue;
409
- leftReleaseQty -= inv.qty;
410
- leftReleaseUomValue -= inv.uomValue;
496
+ compReleaseQty += inv.remainQty;
497
+ compReleaseUomValue += inv.remainUomValue;
498
+ leftReleaseQty -= inv.remainQty;
499
+ leftReleaseUomValue -= inv.remainUomValue;
411
500
  targetInventories.push({
412
501
  targetInventory: {
413
502
  inventory: { id: inv.id },
414
503
  packingType: inv.packingType,
415
504
  batchId: inv.batchId,
416
- releaseQty: inv.qty,
417
- releaseUomValue: inv.uomValue,
505
+ releaseQty: inv.remainQty,
506
+ releaseUomValue: inv.remainUomValue,
507
+ uom: inv.uom,
418
508
  product: { id: product.id },
419
509
  type: sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS
420
510
  }