@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
@@ -14,332 +14,408 @@ 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
+ .andWhere('"loc"."type" NOT IN (:...locationTypes)')
211
+ .setParameters({
212
+ domainId: domain.id,
213
+ bizplaceId: customerBizplace.id,
214
+ packingType: packingType,
215
+ productId: product.id,
216
+ status: warehouse_base_1.INVENTORY_STATUS.STORED,
217
+ locationTypes: [warehouse_base_1.LOCATION_TYPE.QUARANTINE, warehouse_base_1.LOCATION_TYPE.RESERVE]
218
+ });
219
+ if ((locationSortingRules === null || locationSortingRules === void 0 ? void 0 : locationSortingRules.length) > 0) {
220
+ locationSortingRules.forEach((rule, idx) => {
221
+ idx === 0
222
+ ? qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC')
223
+ : qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC');
224
+ });
225
+ }
226
+ else {
227
+ qb.addOrderBy('"iv"."created_at"', 'ASC');
228
+ }
229
+ let inventories = await qb.getMany();
230
+ if (!(inventories === null || inventories === void 0 ? void 0 : inventories.length))
231
+ context.throw(404, t(`error.no inventories found for ${product.sku}`));
232
+ inventories = inventories.map(inventory => {
233
+ return Object.assign(Object.assign({}, inventory), { remainQty: inventory.qty - inventory.lockedQty, remainUomValue: inventory.uomValue - inventory.lockedUomValue });
234
+ });
235
+ const targetInventories = _composeTargetInventories(context, product, orderInventory, inventories);
236
+ targetInventories.map(item => {
237
+ finalOrderInventories.push(item.targetInventory);
238
+ });
136
239
  }
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
240
+ else {
241
+ let filters = [
242
+ {
243
+ name: 'productName',
244
+ operator: 'eq',
245
+ value: orderInventory.product.sku
246
+ },
247
+ {
248
+ name: 'bizplace_id',
249
+ operator: 'eq',
250
+ value: customerBizplaceId
251
+ }
252
+ ];
253
+ // 2. get all inventory data according to SKU and batchId
254
+ const response = await client.query({
255
+ query: (0, graphql_tag_1.default) `
256
+ query inventoryProductGroup($filters: [Filter]) {
257
+ inventoryProductGroup(filters: $filters) {
258
+ items {
259
+ productId
260
+ productSKU
261
+ productName
262
+ batchId
263
+ packingType
264
+ remainQty
265
+ remainWeight
266
+ remainUomValue
148
267
  }
149
- batchId
150
- packingType
151
- remainQty
152
- remainWeight
153
- remainUomValue
268
+ total
154
269
  }
155
- total
156
270
  }
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}`));
271
+ `,
272
+ variables: { filters },
273
+ context: Object.assign(Object.assign({}, context), { state: Object.assign(Object.assign({}, context.state), { bizplace: customerBizplace, domain }) })
274
+ });
275
+ if (!response.errors) {
276
+ const items = response.data.inventoryProductGroup.items || [];
277
+ if (!(items === null || items === void 0 ? void 0 : items.length))
278
+ 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}`));
279
+ items.forEach(item => {
280
+ var _a;
281
+ if (orderInventory.releaseQty > item.remainQty || orderInventory.releaseQty <= 0) {
282
+ context.throw(404, t(`error.invalid release qty for ${(_a = orderInventory.product) === null || _a === void 0 ? void 0 : _a.sku}`));
283
+ }
284
+ // 3. add more information to orderInventory
285
+ orderInventory.product = { id: item.productId };
286
+ orderInventory.packingType = item.packingType;
287
+ orderInventory.batchId = item.batchId;
288
+ orderInventory.type = sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS;
289
+ orderInventory.releaseUomValue = orderInventory.releaseQty * orderInventory.uomValue;
290
+ orderInventory.uom = item.uom;
291
+ });
292
+ delete orderInventory.uomValue;
293
+ finalOrderInventories.push(orderInventory);
170
294
  }
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');
295
+ else
296
+ context.throw(404, t('error.failed to fetch inventory'));
297
+ }
215
298
  }
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
299
  }
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 {
300
+ context.body = {
301
+ result: (await client.mutate({
302
+ mutation: (0, graphql_tag_1.default) `
303
+ mutation addReleaseGood($releaseGood: NewReleaseGood, $shippingOrder: ShippingOrderPatch, $file: Upload) {
304
+ addReleaseGood(releaseGood: $releaseGood, shippingOrder: $shippingOrder, file: $file) {
305
+ id
265
306
  name
266
- }
267
- orderInventories {
268
- product {
269
- sku
307
+ refNo
308
+ status
309
+ truckNo
310
+ ownTransport
311
+ crossDocking
312
+ marketplaceOrderStatus
313
+ billingAddress
314
+ deliveryAddress1
315
+ deliveryAddress2
316
+ deliveryAddress3
317
+ deliveryAddress4
318
+ deliveryAddress5
319
+ attentionTo
320
+ attentionCompany
321
+ city
322
+ state
323
+ postalCode
324
+ country
325
+ phone1
326
+ phone2
327
+ email
328
+ transporter
329
+ trackingNo
330
+ airwayBill
331
+ invoice
332
+ type
333
+ arrivalNotice {
270
334
  name
271
- description
272
335
  }
273
- batchId
274
- packingType
275
- packingSize
276
- releaseQty
277
- releaseUomValue
278
- }
279
- orderVass {
280
- vas {
336
+ exportOption
337
+ releaseDate
338
+ collectionOrderNo
339
+ bizplace {
281
340
  name
282
- operationGuide
283
- operationGuideType
284
341
  }
285
- set
286
- targetType
287
- targetBatchId
288
- targetProduct {
289
- name
342
+ orderInventories {
343
+ product {
344
+ sku
345
+ name
346
+ description
347
+ }
348
+ batchId
349
+ packingType
350
+ packingSize
351
+ releaseQty
352
+ releaseUomValue
353
+ }
354
+ orderVass {
355
+ vas {
356
+ name
357
+ operationGuide
358
+ operationGuideType
359
+ }
360
+ set
361
+ targetType
362
+ targetBatchId
363
+ targetProduct {
364
+ name
365
+ description
366
+ }
367
+ packingType
368
+ qty
369
+ uomValue
370
+ otherTarget
290
371
  description
372
+ remark
373
+ status
374
+ operationGuide
291
375
  }
292
- packingType
293
- qty
294
- uomValue
295
- otherTarget
296
- description
297
- remark
298
- status
299
- operationGuide
300
376
  }
301
377
  }
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
- };
378
+ `,
379
+ variables: {
380
+ releaseGood: {
381
+ customerBizplaceId: customerBizplace.id,
382
+ courierOption: bodyReq.courierOption,
383
+ collectionOrderNo: bodyReq.collectionOrderNo,
384
+ exportOption: bodyReq.exportOption,
385
+ orderInventories: finalOrderInventories,
386
+ ownTransport: bodyReq.ownTransport,
387
+ packingOption: bodyReq.packingOption,
388
+ refNo: bodyReq.refNo,
389
+ refNo2: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.refNo2,
390
+ releaseDate: bodyReq.releaseDate,
391
+ type: bodyReq.type,
392
+ marketplaceOrderStatus: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.marketplaceOrderStatus,
393
+ billingAddress: ((_c = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.billTo) === null || _c === void 0 ? void 0 : _c.billingAddress) || null,
394
+ deliveryAddress1: ((_d = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _d === void 0 ? void 0 : _d.deliveryAddress1) || null,
395
+ deliveryAddress2: ((_e = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _e === void 0 ? void 0 : _e.deliveryAddress2) || null,
396
+ deliveryAddress3: ((_f = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _f === void 0 ? void 0 : _f.deliveryAddress3) || null,
397
+ deliveryAddress4: ((_g = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _g === void 0 ? void 0 : _g.deliveryAddress4) || null,
398
+ deliveryAddress5: ((_h = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _h === void 0 ? void 0 : _h.deliveryAddress5) || null,
399
+ attentionTo: ((_j = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _j === void 0 ? void 0 : _j.attentionTo) || null,
400
+ attentionCompany: ((_k = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _k === void 0 ? void 0 : _k.attentionCompany) || null,
401
+ city: ((_l = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _l === void 0 ? void 0 : _l.city) || null,
402
+ state: ((_m = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _m === void 0 ? void 0 : _m.state) || null,
403
+ postalCode: ((_o = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _o === void 0 ? void 0 : _o.postalCode) || null,
404
+ country: ((_p = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _p === void 0 ? void 0 : _p.country) || null,
405
+ phone1: ((_q = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _q === void 0 ? void 0 : _q.phone1) || null,
406
+ phone2: ((_r = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _r === void 0 ? void 0 : _r.phone2) || null,
407
+ email: ((_s = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _s === void 0 ? void 0 : _s.email) || null,
408
+ transporter: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.transporter,
409
+ trackingNo: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.trackingNo,
410
+ airwayBill: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.airwayBill,
411
+ invoice: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.invoice
412
+ }
413
+ },
414
+ context: Object.assign(Object.assign({}, context), { hasUpload: true, state: Object.assign(Object.assign({}, context.state), { customerBizplace,
415
+ domain, type: 'api' }) })
416
+ })).data
417
+ };
418
+ });
343
419
  });
344
420
  async function checkMandatoryKey(context, params) {
345
421
  let noparams = ['releaseDate', 'exportOption'].filter(param => !(param in params));
@@ -361,9 +437,18 @@ async function checkMandatoryKey(context, params) {
361
437
  }
362
438
  }
363
439
  if ('orderInventories' in params) {
364
- if (params.orderInventories.filter(record => !record.releaseQty || !record.product).length) {
365
- noparams.push('orderInventories');
366
- }
440
+ params.orderInventories.forEach((param, key) => {
441
+ let noSubParams = [];
442
+ if (!(param === null || param === void 0 ? void 0 : param.releaseQty))
443
+ noSubParams.push('releaseQty');
444
+ if (!(param === null || param === void 0 ? void 0 : param.product))
445
+ noSubParams.push('product');
446
+ if (noSubParams === null || noSubParams === void 0 ? void 0 : noSubParams.length)
447
+ context.throw(400, context.t('error.required param x not found', { x: `${noSubParams.join(', ')}` }));
448
+ });
449
+ }
450
+ else {
451
+ noparams.push('orderInventories');
367
452
  }
368
453
  if (noparams === null || noparams === void 0 ? void 0 : noparams.length) {
369
454
  context.throw(400, context.t('error.required param x not found', { x: `${noparams.join(', ')}` }));
@@ -379,7 +464,7 @@ function _composeTargetInventories(context, product, record, inventories) {
379
464
  let compReleaseUomValue = 0;
380
465
  let totalInventoryQty = 0;
381
466
  inventories.map(inventory => {
382
- totalInventoryQty += inventory.qty;
467
+ totalInventoryQty += inventory.remainQty;
383
468
  });
384
469
  if (totalInventoryQty < record.releaseQty) {
385
470
  context.throw(404, t(`error.invalid release qty for ${product === null || product === void 0 ? void 0 : product.sku}`));
@@ -388,7 +473,7 @@ function _composeTargetInventories(context, product, record, inventories) {
388
473
  let idx = 0;
389
474
  while (compReleaseQty < record.releaseQty) {
390
475
  const inv = inventories[idx];
391
- if (inv.qty > leftReleaseQty) {
476
+ if (inv.remainQty > leftReleaseQty) {
392
477
  compReleaseQty += leftReleaseQty;
393
478
  compReleaseUomValue += leftReleaseUomValue;
394
479
  targetInventories.push({
@@ -398,23 +483,25 @@ function _composeTargetInventories(context, product, record, inventories) {
398
483
  batchId: inv.batchId,
399
484
  releaseQty: leftReleaseQty,
400
485
  releaseUomValue: leftReleaseQty * record.uomValue,
486
+ uom: inv.uom,
401
487
  product: { id: product.id },
402
488
  type: sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS
403
489
  }
404
490
  });
405
491
  }
406
492
  else {
407
- compReleaseQty += inv.qty;
408
- compReleaseUomValue += inv.uomValue;
409
- leftReleaseQty -= inv.qty;
410
- leftReleaseUomValue -= inv.uomValue;
493
+ compReleaseQty += inv.remainQty;
494
+ compReleaseUomValue += inv.remainUomValue;
495
+ leftReleaseQty -= inv.remainQty;
496
+ leftReleaseUomValue -= inv.remainUomValue;
411
497
  targetInventories.push({
412
498
  targetInventory: {
413
499
  inventory: { id: inv.id },
414
500
  packingType: inv.packingType,
415
501
  batchId: inv.batchId,
416
- releaseQty: inv.qty,
417
- releaseUomValue: inv.uomValue,
502
+ releaseQty: inv.remainQty,
503
+ releaseUomValue: inv.remainUomValue,
504
+ uom: inv.uom,
418
505
  product: { id: product.id },
419
506
  type: sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS
420
507
  }