@things-factory/operato-hub 4.3.28 → 4.3.31

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.
@@ -1,18 +1,19 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports._composeTargetInventories = exports.checkMandatoryKey = void 0;
7
- const graphql_tag_1 = __importDefault(require("graphql-tag"));
8
3
  const typeorm_1 = require("typeorm");
4
+ const uuid_1 = require("uuid");
9
5
  const api_1 = require("@things-factory/api");
10
6
  const biz_base_1 = require("@things-factory/biz-base");
7
+ const env_1 = require("@things-factory/env");
8
+ const id_rule_base_1 = require("@things-factory/id-rule-base");
11
9
  const integration_base_1 = require("@things-factory/integration-base");
10
+ const integration_sellercraft_1 = require("@things-factory/integration-sellercraft");
11
+ const marketplace_base_1 = require("@things-factory/marketplace-base");
12
12
  const product_base_1 = require("@things-factory/product-base");
13
13
  const sales_base_1 = require("@things-factory/sales-base");
14
14
  const setting_base_1 = require("@things-factory/setting-base");
15
15
  const warehouse_base_1 = require("@things-factory/warehouse-base");
16
+ const index_1 = require("../../../../errors/index");
16
17
  const business_middleware_1 = require("../../../../middlewares/business-middleware");
17
18
  const warehouse_middleware_1 = require("../../../../middlewares/warehouse-middleware");
18
19
  const debug = require('debug')('things-factory:operato-hub:restful-api:unstable:add-release-order');
@@ -20,10 +21,14 @@ const apiVersion = 'unstable';
20
21
  api_1.restfulApiRouter.post(`/${apiVersion}/warehouse/:warehouseId/add-release-order`, business_middleware_1.businessMiddleware, warehouse_middleware_1.warehouseMiddleware, async (context, next) => {
21
22
  return await (0, typeorm_1.getConnection)().transaction(async (tx) => {
22
23
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
23
- const { client, domain } = context.state;
24
+ const { client, domain, user } = context.state;
24
25
  const { warehouseId: customerBizplaceId } = context.params;
25
26
  const { t } = context;
26
27
  const bodyReq = context.request.body;
28
+ const requiredDraft = bodyReq.hasOwnProperty('requiredDraft') ? bodyReq.requiredDraft : true;
29
+ let releaseGood;
30
+ let massagedData = { orderProducts: [], combinedItems: [] };
31
+ // const isAutoAssign: Boolean = bodyReq.hasOwnProperty('isAutoAssign') ? bodyReq.isAutoAssign : true
27
32
  try {
28
33
  if (!customerBizplaceId)
29
34
  context.throw(404, t('error.warehouse id not found'));
@@ -32,550 +37,162 @@ api_1.restfulApiRouter.post(`/${apiVersion}/warehouse/:warehouseId/add-release-o
32
37
  }
33
38
  debug('post:/add-release-order request.body', bodyReq);
34
39
  await checkMandatoryKey(context, bodyReq);
35
- const isAutoAssign = bodyReq.hasOwnProperty('isAutoAssign') ? bodyReq.isAutoAssign : true;
36
- const requiredDraft = bodyReq.hasOwnProperty('requiredDraft') ? bodyReq.requiredDraft : true;
37
40
  // get customer company's bizplace
38
41
  const custCompanyBizplace = await (0, biz_base_1.getCompanyBizplace)(null, null, customerBizplaceId, tx);
39
42
  const customerBizplace = await tx.getRepository(biz_base_1.Bizplace).findOne({
40
43
  where: { id: customerBizplaceId }
41
44
  });
42
- let locationSortingRules = [];
43
- const inventoryAssignmentSetting = await tx.getRepository(setting_base_1.Setting).findOne({
44
- where: { domain, name: 'rule-for-inventory-assignment' }
45
- });
46
- if (inventoryAssignmentSetting) {
47
- let locationSetting = JSON.parse(inventoryAssignmentSetting.value);
48
- for (const key in locationSetting) {
49
- locationSortingRules.push({ name: key, desc: locationSetting[key] == 'ASC' ? false : true });
50
- }
51
- }
52
- else {
53
- const pickingProductSetting = await tx.getRepository(setting_base_1.Setting).findOne({
54
- where: { domain, name: 'rule-for-picking-product' }
55
- });
56
- if (pickingProductSetting) {
57
- let locationSetting = JSON.parse(pickingProductSetting.value);
58
- for (const key in locationSetting) {
59
- locationSortingRules.push({ name: key, desc: locationSetting[key] == 'ASC' ? false : true });
60
- }
61
- }
62
- }
63
- const orderInventories = bodyReq.orderInventories;
64
- let newOrderInventories = [];
65
- let bundleInfo = [];
66
- await Promise.all(orderInventories.map(async (orderInventory) => {
67
- var _a, _b;
68
- const sku = (_a = orderInventory.product) === null || _a === void 0 ? void 0 : _a.sku;
69
- const refCode = (_b = orderInventory.product) === null || _b === void 0 ? void 0 : _b.refCode;
70
- const packingType = orderInventory.packingType;
71
- const qb = tx
72
- .getRepository(product_base_1.ProductDetail)
73
- .createQueryBuilder('PD')
74
- .innerJoinAndSelect('PD.product', 'PROD')
75
- .where('PROD.bizplace_id = :bizplaceId', { bizplaceId: custCompanyBizplace.id });
76
- if (!sku && !refCode) {
77
- context.throw(404, t('error.sku or refCode not found'));
78
- }
79
- if (refCode) {
80
- qb.andWhere('PD.ref_code = :refCode', { refCode });
81
- }
82
- if (sku) {
83
- qb.andWhere('PROD.sku = :sku', { sku });
84
- }
85
- if (packingType) {
86
- qb.andWhere('PD.packing_type = :packingType', { packingType });
87
- }
88
- if (!packingType && !refCode) {
89
- qb.andWhere('PD.is_default = :isDefault', { isDefault: true });
90
- }
91
- const productDetail = await qb.getOne();
92
- if (productDetail) {
93
- let newOrderInventory = Object.assign({}, orderInventory);
94
- newOrderInventory.product.sku = productDetail.product.sku;
95
- newOrderInventory.product = productDetail.product;
96
- newOrderInventory.packingType = productDetail.packingType;
97
- newOrderInventory.uomValue = productDetail.uomValue;
98
- newOrderInventories.push(newOrderInventory);
99
- }
100
- if (!productDetail) {
101
- const qb = tx
102
- .getRepository(product_base_1.ProductBundle)
103
- .createQueryBuilder('PB')
104
- .innerJoinAndSelect('PB.productBundleSettings', 'PBS')
105
- .innerJoinAndSelect('PBS.productDetail', 'PBD')
106
- .innerJoinAndSelect('PBD.product', 'PROD')
107
- .where('PB.bizplace_id = :bizplaceId', { bizplaceId: custCompanyBizplace.id });
108
- if (!sku && !refCode) {
109
- context.throw(404, t('error.sku or refCode not found'));
110
- }
111
- if (refCode) {
112
- qb.andWhere('PB.ref_code = :refCode', { refCode });
113
- }
114
- if (sku) {
115
- qb.andWhere('PB.sku = :sku', { sku });
116
- }
117
- if (packingType) {
118
- qb.andWhere('PB.packing_type = :packingType', { packingType });
119
- }
120
- const foundProductBundle = await qb.getOne();
121
- if (foundProductBundle) {
122
- bundleInfo.push({
123
- productBundleId: foundProductBundle.id,
124
- sku: foundProductBundle.sku,
125
- releaseQty: orderInventory.releaseQty
126
- });
127
- const bundleProductSettings = foundProductBundle.productBundleSettings;
128
- bundleProductSettings.map(bundleProductSetting => {
129
- const productDetailBundle = bundleProductSetting.productDetail;
130
- let newOrderInventory = Object.assign({}, orderInventory);
131
- newOrderInventory.product.sku = productDetailBundle.product.sku;
132
- newOrderInventory.packingType = productDetailBundle.packingType;
133
- newOrderInventory.uomValue = productDetailBundle.uomValue;
134
- newOrderInventory.releaseQty = orderInventory.releaseQty * bundleProductSetting.bundleQty;
135
- newOrderInventory.product = productDetailBundle.product;
136
- newOrderInventories.push(newOrderInventory);
137
- });
138
- }
139
- else {
140
- if (sku && refCode) {
141
- context.throw(404, t(`error.sku ${sku} and refCode ${refCode} not exist in product / bundle master`));
142
- }
143
- if (sku) {
144
- context.throw(404, t(`error.sku ${sku} not exist in product / bundle master`));
145
- }
146
- if (refCode) {
147
- context.throw(404, t(`error.refCode ${refCode} not exist in product / bundle master`));
148
- }
149
- }
45
+ releaseGood = {
46
+ domain,
47
+ bizplace: customerBizplace,
48
+ companyBizplace: custCompanyBizplace,
49
+ courierOption: bodyReq.courierOption,
50
+ collectionOrderNo: bodyReq.collectionOrderNo,
51
+ exportOption: bodyReq.exportOption,
52
+ ownTransport: bodyReq.ownTransport,
53
+ packingOption: bodyReq.packingOption,
54
+ refNo: bodyReq.refNo,
55
+ refNo2: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.refNo2,
56
+ refNo3: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.refNo3,
57
+ releaseDate: bodyReq.releaseDate,
58
+ type: bodyReq.type,
59
+ marketplaceOrderStatus: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.marketplaceOrderStatus,
60
+ remark: (bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.remark) || null,
61
+ billingAddress: ((_b = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.billTo) === null || _b === void 0 ? void 0 : _b.billingAddress) || null,
62
+ deliveryAddress1: ((_c = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _c === void 0 ? void 0 : _c.deliveryAddress1) || null,
63
+ deliveryAddress2: ((_d = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _d === void 0 ? void 0 : _d.deliveryAddress2) || null,
64
+ deliveryAddress3: ((_e = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _e === void 0 ? void 0 : _e.deliveryAddress3) || null,
65
+ deliveryAddress4: ((_f = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _f === void 0 ? void 0 : _f.deliveryAddress4) || null,
66
+ deliveryAddress5: ((_g = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _g === void 0 ? void 0 : _g.deliveryAddress5) || null,
67
+ attentionTo: ((_h = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _h === void 0 ? void 0 : _h.attentionTo) || null,
68
+ attentionCompany: ((_j = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _j === void 0 ? void 0 : _j.attentionCompany) || null,
69
+ city: ((_k = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _k === void 0 ? void 0 : _k.city) || null,
70
+ state: ((_l = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _l === void 0 ? void 0 : _l.state) || null,
71
+ postalCode: ((_m = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _m === void 0 ? void 0 : _m.postalCode) || null,
72
+ country: ((_o = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _o === void 0 ? void 0 : _o.country) || null,
73
+ phone1: ((_p = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _p === void 0 ? void 0 : _p.phone1) || null,
74
+ phone2: ((_q = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _q === void 0 ? void 0 : _q.phone2) || null,
75
+ email: ((_r = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _r === void 0 ? void 0 : _r.email) || null,
76
+ transporter: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.transporter,
77
+ trackingNo: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.trackingNo,
78
+ airwayBill: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.airwayBill,
79
+ invoice: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.invoice,
80
+ storeName: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.storeName,
81
+ storeId: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.storeId,
82
+ routeId: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.routeId,
83
+ stopId: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.stopId
84
+ };
85
+ // check existing order
86
+ await checkExistingOrder(releaseGood, customerBizplace, context, tx);
87
+ //massage data
88
+ massagedData = await massageOrderItems(releaseGood, bodyReq.orderInventories, context);
89
+ // validation
90
+ let errorList = [];
91
+ await Promise.all(massagedData.combinedItems.map(async (item) => {
92
+ let itemList = await tx.query(`
93
+ select wboi.* from warehouse_bizplace_onhand_inventories wboi
94
+ where wboi."domainId"= $1 and
95
+ wboi."bizplaceId" = $2 and
96
+ wboi."groupType" = 'SINGLE' and
97
+ wboi."productId" = $3 and
98
+ wboi."packingType" = $4 and
99
+ wboi."packingSize" = $5 and
100
+ wboi."uom" = $6 and
101
+ wboi."remainQty" >= $7 and
102
+ wboi."remainUomValue" >= $8
103
+ `, [
104
+ domain.id,
105
+ customerBizplace.id,
106
+ item.product.id,
107
+ item.packingType,
108
+ item.packingSize,
109
+ item.uom,
110
+ item.releaseQty,
111
+ item.releaseUomValue
112
+ ]);
113
+ if (itemList.length <= 0) {
114
+ errorList.push(JSON.stringify({
115
+ code: 'insufficient stock',
116
+ sku: item.product.sku,
117
+ packingType: item.packingType,
118
+ packingSize: item.packingSize,
119
+ uom: item.uom,
120
+ qty: item.releaseQty,
121
+ uom_value: item.releaseUomValue
122
+ }));
150
123
  }
124
+ console.log();
151
125
  }));
152
- let finalOrderInventories = [];
153
- // Case 1. Every order inventories has specified batch id
154
- if (newOrderInventories.every((orderInventory) => orderInventory.batchId)) {
155
- finalOrderInventories = await Promise.all(newOrderInventories.map(async (orderInventory) => {
156
- let filters = [
157
- {
158
- name: 'batchId',
159
- operator: 'eq',
160
- value: orderInventory.batchId
161
- },
162
- {
163
- name: 'productName',
164
- operator: 'eq',
165
- value: orderInventory.product.sku
166
- },
167
- {
168
- name: 'bizplace',
169
- operator: 'in',
170
- value: [customerBizplaceId]
171
- }
172
- ];
173
- let sortings = [];
174
- switch (orderInventory.product.pickingStrategy) {
175
- case 'FIFO':
176
- sortings.push({ name: 'created_at', desc: false });
177
- break;
178
- case 'LIFO':
179
- sortings.push({ name: 'created_at', desc: true });
180
- break;
181
- case 'FEFO':
182
- sortings.push({ name: 'expiration_date', desc: false });
183
- break;
184
- case 'FMFO':
185
- sortings.push({ name: 'manufacture_date', desc: false });
186
- break;
187
- }
188
- // 2. get all inventory data according to SKU and batchId
189
- const response = await client.query({
190
- query: (0, graphql_tag_1.default) `
191
- query inventoriesByPallet(
192
- $filters: [Filter]
193
- $sortings: [Sorting]
194
- $locationSortingRules: [Sorting]
195
- ) {
196
- inventoriesByPallet(
197
- filters: $filters
198
- sortings: $sortings
199
- locationSortingRules: $locationSortingRules
200
- ) {
201
- items {
202
- product {
203
- id
204
- name
205
- description
206
- }
207
- batchId
208
- packingType
209
- remainQty
210
- remainUomValue
211
- }
212
- total
213
- }
214
- }
215
- `,
216
- variables: { filters, sortings, locationSortingRules },
217
- context: Object.assign(Object.assign({}, context), { state: Object.assign(Object.assign({}, context.state), { bizplace: customerBizplace, domain }) })
218
- });
219
- if (!response.errors) {
220
- const items = response.data.inventoriesByPallet.items || [];
221
- if (requiredDraft) {
222
- if (!(items === null || items === void 0 ? void 0 : items.length)) {
223
- context.body = {
224
- result: await createDraftOrder(client, customerBizplace, bodyReq, bundleInfo, domain, context, orderInventories, tx)
225
- };
226
- return;
227
- }
228
- }
229
- items.forEach(async (item) => {
230
- if (requiredDraft) {
231
- if (orderInventory.releaseQty > item.remainQty || orderInventory.releaseQty <= 0) {
232
- context.body = {
233
- result: await createDraftOrder(client, customerBizplace, bodyReq, bundleInfo, domain, context, orderInventories, tx)
234
- };
235
- return;
236
- }
237
- }
238
- // 3. add more information to orderInventory
239
- orderInventory.product.id = item.product.id;
240
- orderInventory.packingType = item.packingType;
241
- orderInventory.batchId = item.batchId;
242
- orderInventory.type = sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS;
243
- orderInventory.releaseUomValue = orderInventory.releaseQty * orderInventory.uomValue;
244
- orderInventory.uom = item.uom;
245
- });
246
- return orderInventory;
247
- }
248
- else {
249
- if (requiredDraft) {
250
- context.body = {
251
- result: await createDraftOrder(client, customerBizplace, bodyReq, bundleInfo, domain, context, orderInventories, tx)
252
- };
253
- return;
254
- }
255
- }
256
- }));
257
- }
258
- else {
259
- // Case 2. Some of order inventories doesn't have specified batch id
260
- // MMS order doesn't have batch ID field.
261
- // To cover this case first result of found inventory will be a target of RO
262
- // To combine items from different bundles
263
- newOrderInventories = combineItems(newOrderInventories);
264
- for (let i = 0; i < newOrderInventories.length; i++) {
265
- const orderInventory = newOrderInventories[i];
266
- const { product, packingType } = orderInventory;
267
- const pickingStrategy = product.pickingStrategy;
268
- if (isAutoAssign) {
269
- let qb = tx.getRepository(warehouse_base_1.Inventory).createQueryBuilder('iv');
270
- qb.leftJoinAndSelect('iv.location', 'loc')
271
- .andWhere('"iv"."domain_id" = :domainId')
272
- .andWhere('"iv"."bizplace_id" = :bizplaceId')
273
- .andWhere('"iv"."packing_type" = :packingType')
274
- .andWhere('"iv"."product_id" = :productId')
275
- .andWhere('"iv"."status" = :status')
276
- .andWhere('"loc"."type" NOT IN (:...locationTypes)')
277
- .setParameters({
278
- domainId: domain.id,
279
- bizplaceId: customerBizplace.id,
280
- packingType: packingType,
281
- productId: product.id,
282
- status: warehouse_base_1.INVENTORY_STATUS.STORED,
283
- locationTypes: [warehouse_base_1.LOCATION_TYPE.QUARANTINE, warehouse_base_1.LOCATION_TYPE.RESERVE]
284
- });
285
- switch (pickingStrategy) {
286
- case 'FIFO':
287
- qb.orderBy('"iv"."created_at"', 'ASC');
288
- if ((locationSortingRules === null || locationSortingRules === void 0 ? void 0 : locationSortingRules.length) > 0) {
289
- locationSortingRules.forEach((rule, idx) => {
290
- qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC');
291
- });
292
- }
293
- break;
294
- case 'LIFO':
295
- qb.orderBy('"iv"."created_at"', 'DESC');
296
- if ((locationSortingRules === null || locationSortingRules === void 0 ? void 0 : locationSortingRules.length) > 0) {
297
- locationSortingRules.forEach((rule, idx) => {
298
- qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC');
299
- });
300
- }
301
- break;
302
- case 'FEFO':
303
- qb.orderBy('"iv"."expiration_date"', 'ASC');
304
- if ((locationSortingRules === null || locationSortingRules === void 0 ? void 0 : locationSortingRules.length) > 0) {
305
- locationSortingRules.forEach((rule, idx) => {
306
- qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC');
307
- });
308
- }
309
- break;
310
- case 'FMFO':
311
- qb.orderBy('"iv"."manufacture_date"', 'ASC');
312
- if ((locationSortingRules === null || locationSortingRules === void 0 ? void 0 : locationSortingRules.length) > 0) {
313
- locationSortingRules.forEach((rule, idx) => {
314
- qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC');
315
- });
316
- }
317
- break;
318
- case 'LOCATION':
319
- if ((locationSortingRules === null || locationSortingRules === void 0 ? void 0 : locationSortingRules.length) > 0) {
320
- locationSortingRules.forEach((rule, idx) => {
321
- idx === 0
322
- ? qb.orderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC')
323
- : qb.addOrderBy(`loc.${rule.name}`, rule.desc ? 'DESC' : 'ASC');
324
- });
325
- }
326
- else {
327
- qb.orderBy('"loc"."name"', 'DESC');
328
- }
329
- break;
330
- }
331
- let inventories = await qb.getMany();
332
- if (requiredDraft) {
333
- if (!(inventories === null || inventories === void 0 ? void 0 : inventories.length)) {
334
- context.body = {
335
- result: await createDraftOrder(client, customerBizplace, bodyReq, bundleInfo, domain, context, orderInventories, tx)
336
- };
337
- return;
338
- }
339
- }
340
- inventories = inventories
341
- .map(inventory => {
342
- return Object.assign(Object.assign({}, inventory), { remainQty: inventory.qty - inventory.lockedQty, remainUomValue: inventory.uomValue - inventory.lockedUomValue });
343
- })
344
- .filter(inventory => inventory.remainQty > 0);
345
- let targetInventories;
346
- try {
347
- targetInventories = _composeTargetInventories(context, product, orderInventory, inventories);
348
- }
349
- catch (e) {
350
- if (requiredDraft) {
351
- context.body = {
352
- result: await createDraftOrder(client, customerBizplace, bodyReq, bundleInfo, domain, context, orderInventories, tx)
353
- };
354
- return;
355
- }
356
- else {
357
- targetInventories = _composeTargetInventories(context, product, orderInventory, inventories);
358
- }
359
- }
360
- if (!(targetInventories === null || targetInventories === void 0 ? void 0 : targetInventories.length)) {
361
- context.throw(404, t(`error.no inventories found`));
362
- }
363
- targetInventories.map(item => {
364
- finalOrderInventories.push(item.targetInventory);
365
- });
366
- }
367
- else {
368
- let filters = [
369
- {
370
- name: 'productName',
371
- operator: 'eq',
372
- value: orderInventory.product.sku
373
- },
374
- {
375
- name: 'bizplace_id',
376
- operator: 'eq',
377
- value: customerBizplaceId
378
- }
379
- ];
380
- // 2. get all inventory data according to SKU and batchId
381
- const response = await client.query({
382
- query: (0, graphql_tag_1.default) `
383
- query inventoryProductGroup($filters: [Filter]) {
384
- inventoryProductGroup(filters: $filters) {
385
- items {
386
- productId
387
- productSKU
388
- productName
389
- batchId
390
- packingType
391
- remainQty
392
- remainUomValue
393
- }
394
- total
395
- }
396
- }
397
- `,
398
- variables: { filters },
399
- context: Object.assign(Object.assign({}, context), { state: Object.assign(Object.assign({}, context.state), { bizplace: customerBizplace, domain }) })
400
- });
401
- if (!response.errors) {
402
- const items = response.data.inventoryProductGroup.items || [];
403
- if (requiredDraft) {
404
- if (!(items === null || items === void 0 ? void 0 : items.length)) {
405
- context.body = {
406
- result: await createDraftOrder(client, customerBizplace, bodyReq, bundleInfo, domain, context, orderInventories, tx)
407
- };
408
- return;
409
- }
410
- }
411
- items.forEach(async (item) => {
412
- if (requiredDraft) {
413
- if (orderInventory.releaseQty > item.remainQty || orderInventory.releaseQty <= 0) {
414
- context.body = {
415
- result: await createDraftOrder(client, customerBizplace, bodyReq, bundleInfo, domain, context, orderInventories, tx)
416
- };
417
- return;
418
- }
419
- }
420
- // 3. add more information to orderInventory
421
- orderInventory.product = { id: item.productId };
422
- orderInventory.packingType = item.packingType;
423
- orderInventory.batchId = item.batchId;
424
- orderInventory.type = sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS;
425
- orderInventory.releaseUomValue = orderInventory.releaseQty * orderInventory.uomValue;
426
- orderInventory.uom = item.uom;
427
- });
428
- delete orderInventory.uomValue;
429
- finalOrderInventories.push(orderInventory);
430
- }
431
- else {
432
- if (requiredDraft) {
433
- context.body = {
434
- result: await createDraftOrder(client, customerBizplace, bodyReq, bundleInfo, domain, context, orderInventories, tx)
435
- };
436
- return;
437
- }
438
- }
439
- }
440
- }
126
+ if (errorList.length > 0) {
127
+ throw new index_1.ValidationError({
128
+ errorCode: 'INSUFFICIENT_STOCK',
129
+ message: 'insufficient stock',
130
+ detail: errorList.join(', ')
131
+ });
441
132
  }
442
- context.body = {
443
- result: (await client.mutate({
444
- mutation: (0, graphql_tag_1.default) `
445
- mutation addReleaseGood(
446
- $releaseGood: NewReleaseGood
447
- $shippingOrder: ShippingOrderPatch
448
- $file: Upload
449
- ) {
450
- addReleaseGood(releaseGood: $releaseGood, shippingOrder: $shippingOrder, file: $file) {
451
- id
452
- name
453
- refNo
454
- status
455
- truckNo
456
- ownTransport
457
- crossDocking
458
- marketplaceOrderStatus
459
- billingAddress
460
- deliveryAddress1
461
- deliveryAddress2
462
- deliveryAddress3
463
- deliveryAddress4
464
- deliveryAddress5
465
- attentionTo
466
- attentionCompany
467
- city
468
- state
469
- postalCode
470
- country
471
- phone1
472
- phone2
473
- email
474
- transporter
475
- trackingNo
476
- airwayBill
477
- invoice
478
- type
479
- storeName
480
- storeId
481
- routeId
482
- stopId
483
- arrivalNotice {
484
- name
485
- }
486
- exportOption
487
- releaseDate
488
- collectionOrderNo
489
- bizplace {
490
- name
491
- }
492
- orderInventories {
493
- product {
494
- sku
495
- name
496
- description
497
- }
498
- batchId
499
- packingType
500
- packingSize
501
- releaseQty
502
- releaseUomValue
503
- }
504
- orderVass {
505
- vas {
506
- name
507
- operationGuide
508
- operationGuideType
509
- }
510
- set
511
- targetType
512
- targetBatchId
513
- targetProduct {
514
- name
515
- description
516
- }
517
- packingType
518
- qty
519
- uomValue
520
- otherTarget
521
- description
522
- remark
523
- status
524
- operationGuide
525
- }
526
- }
527
- }
528
- `,
529
- variables: {
530
- releaseGood: {
531
- customerBizplaceId: customerBizplace.id,
532
- courierOption: bodyReq.courierOption,
533
- collectionOrderNo: bodyReq.collectionOrderNo,
534
- exportOption: bodyReq.exportOption,
535
- orderInventories: finalOrderInventories,
536
- ownTransport: bodyReq.ownTransport,
537
- packingOption: bodyReq.packingOption,
538
- refNo: bodyReq.refNo,
539
- refNo2: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.refNo2,
540
- releaseDate: bodyReq.releaseDate,
541
- type: bodyReq.type,
542
- marketplaceOrderStatus: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.marketplaceOrderStatus,
543
- remark: (bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.remark) || null,
544
- billingAddress: ((_b = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.billTo) === null || _b === void 0 ? void 0 : _b.billingAddress) || null,
545
- deliveryAddress1: ((_c = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _c === void 0 ? void 0 : _c.deliveryAddress1) || null,
546
- deliveryAddress2: ((_d = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _d === void 0 ? void 0 : _d.deliveryAddress2) || null,
547
- deliveryAddress3: ((_e = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _e === void 0 ? void 0 : _e.deliveryAddress3) || null,
548
- deliveryAddress4: ((_f = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _f === void 0 ? void 0 : _f.deliveryAddress4) || null,
549
- deliveryAddress5: ((_g = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _g === void 0 ? void 0 : _g.deliveryAddress5) || null,
550
- attentionTo: ((_h = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _h === void 0 ? void 0 : _h.attentionTo) || null,
551
- attentionCompany: ((_j = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _j === void 0 ? void 0 : _j.attentionCompany) || null,
552
- city: ((_k = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _k === void 0 ? void 0 : _k.city) || null,
553
- state: ((_l = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _l === void 0 ? void 0 : _l.state) || null,
554
- postalCode: ((_m = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _m === void 0 ? void 0 : _m.postalCode) || null,
555
- country: ((_o = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _o === void 0 ? void 0 : _o.country) || null,
556
- phone1: ((_p = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _p === void 0 ? void 0 : _p.phone1) || null,
557
- phone2: ((_q = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _q === void 0 ? void 0 : _q.phone2) || null,
558
- email: ((_r = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _r === void 0 ? void 0 : _r.email) || null,
559
- transporter: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.transporter,
560
- trackingNo: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.trackingNo,
561
- airwayBill: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.airwayBill,
562
- invoice: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.invoice,
563
- bundleInfo: JSON.stringify(bundleInfo),
564
- storeName: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.storeName,
565
- storeId: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.storeId,
566
- routeId: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.routeId,
567
- stopId: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.stopId
568
- }
569
- },
570
- context: Object.assign(Object.assign({}, context), { hasUpload: true, state: Object.assign(Object.assign({}, context.state), { customerBizplace,
571
- domain, type: 'api' }) })
572
- })).data
133
+ // assignment
134
+ console.time('assign');
135
+ let assignedOrderProducts = await assignToInventory(massagedData.orderProducts, customerBizplace, context, tx);
136
+ console.timeEnd('assign');
137
+ // create order
138
+ console.time('save');
139
+ let result = await createReleaseGood(releaseGood, assignedOrderProducts, customerBizplace, context, tx);
140
+ console.timeEnd('save');
141
+ let addReleaseGood = {
142
+ id: result.id,
143
+ name: result.name,
144
+ refNo: result.refNo,
145
+ refNo2: result.refNo2,
146
+ refNo3: result.refNo3,
147
+ status: result.status,
148
+ truckNo: result.truckNo,
149
+ ownTransport: result.ownTransport,
150
+ crossDocking: result.crossDocking,
151
+ marketplaceOrderStatus: result.marketplaceOrderStatus,
152
+ billingAddress: result.billingAddress,
153
+ deliveryAddress1: result.deliveryAddress1,
154
+ deliveryAddress2: result.deliveryAddress2,
155
+ deliveryAddress3: result.deliveryAddress3,
156
+ deliveryAddress4: result.deliveryAddress4,
157
+ deliveryAddress5: result.deliveryAddress5,
158
+ attentionTo: result.attentionTo,
159
+ attentionCompany: result.attentionCompany,
160
+ city: result.city,
161
+ state: result.state,
162
+ postalCode: result.postalCode,
163
+ country: result.country,
164
+ phone1: result.phone1,
165
+ phone2: result.phone2,
166
+ email: result.email,
167
+ transporter: result.transporter,
168
+ trackingNo: result.trackingNo,
169
+ airwayBill: result.airwayBill,
170
+ invoice: result.invoice,
171
+ type: result.type,
172
+ storeName: result.storeName,
173
+ storeId: result.storeId,
174
+ routeId: result.routeId,
175
+ stopId: result.stopId,
176
+ exportOption: result.exportOption,
177
+ releaseDate: result.releaseDate,
178
+ collectionOrderNo: result.collectionOrderNo,
179
+ bizplace: { name: result.bizplace.name },
180
+ domain: { name: result.domain.name },
181
+ orderInventories: result.orderInventories
573
182
  };
183
+ // return body
184
+ context.body = { result: addReleaseGood };
574
185
  (0, integration_base_1.createPayloadLog)(customerBizplaceId, `/${apiVersion}/warehouse/:warehouseId/add-release-order`, bodyReq, context.body, context, integration_base_1.PayloadType.INGESTION);
575
186
  }
576
187
  catch (error) {
577
188
  (0, integration_base_1.createPayloadLog)(customerBizplaceId, `/${apiVersion}/warehouse/:warehouseId/add-release-order`, bodyReq, error, context, integration_base_1.PayloadType.INGESTION);
578
- throw error;
189
+ if (error.errorCode) {
190
+ if (error.errorCode == 'INSUFFICIENT_STOCK' && requiredDraft) {
191
+ await createDraftOrder(releaseGood, massagedData.orderProducts, context);
192
+ }
193
+ }
194
+ env_1.logger.error(`add-release-order: ${JSON.stringify(error)}`);
195
+ context.throw(400, context.t(error));
579
196
  }
580
197
  });
581
198
  });
@@ -617,156 +234,467 @@ async function checkMandatoryKey(context, params) {
617
234
  }
618
235
  return params;
619
236
  }
620
- exports.checkMandatoryKey = checkMandatoryKey;
621
- function _composeTargetInventories(context, product, record, inventories) {
622
- const { t } = context;
623
- let leftReleaseQty = record.releaseQty;
624
- let leftReleaseUomValue = record.releaseUomValue;
625
- let compReleaseQty = 0;
626
- let compReleaseUomValue = 0;
627
- let totalInventoryQty = 0;
628
- inventories.map(inventory => {
629
- totalInventoryQty += inventory.remainQty;
630
- });
631
- if (totalInventoryQty < record.releaseQty) {
632
- context.throw(404, t(`error.invalid release qty for ${product === null || product === void 0 ? void 0 : product.sku}`)); //
633
- }
634
- let targetInventories = [];
635
- let idx = 0;
636
- while (compReleaseQty < record.releaseQty) {
637
- const inv = inventories[idx];
638
- if (inv.remainQty > leftReleaseQty) {
639
- compReleaseQty += leftReleaseQty;
640
- compReleaseUomValue += leftReleaseUomValue;
641
- targetInventories.push({
642
- targetInventory: {
643
- inventory: { id: inv.id },
644
- packingType: inv.packingType,
645
- batchId: inv.batchId,
646
- releaseQty: leftReleaseQty,
647
- releaseUomValue: leftReleaseQty * record.uomValue,
648
- uom: inv.uom,
649
- product: { id: product.id },
650
- type: sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS
651
- }
652
- });
237
+ async function checkExistingOrder(releaseGood, bizplace, context, tx) {
238
+ const { domain, user } = context.state;
239
+ if (releaseGood.type == 'b2c') {
240
+ const sellercraft = await tx.getRepository(integration_sellercraft_1.Sellercraft).findOne({ domain: bizplace.domain });
241
+ if (sellercraft) {
242
+ const sellercraftCtrl = new sales_base_1.SellercraftController(tx, bizplace.domain, user);
243
+ await sellercraftCtrl.checkExistingReleaseGood(sellercraft, releaseGood);
653
244
  }
654
245
  else {
655
- compReleaseQty += inv.remainQty;
656
- compReleaseUomValue += inv.remainUomValue;
657
- leftReleaseQty -= inv.remainQty;
658
- leftReleaseUomValue -= inv.remainUomValue;
659
- targetInventories.push({
660
- targetInventory: {
661
- inventory: { id: inv.id },
662
- packingType: inv.packingType,
663
- batchId: inv.batchId,
664
- releaseQty: inv.remainQty,
665
- releaseUomValue: inv.remainUomValue,
666
- uom: inv.uom,
667
- product: { id: product.id },
668
- type: sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS
669
- }
246
+ const refNo = releaseGood.refNo;
247
+ const foundReleaseGood = await tx.getRepository(sales_base_1.ReleaseGood).findOne({
248
+ where: {
249
+ domain: domain,
250
+ refNo,
251
+ status: (0, typeorm_1.Not)((0, typeorm_1.In)([sales_base_1.ORDER_STATUS.CANCELLED, sales_base_1.ORDER_STATUS.PENDING_CANCEL]))
252
+ },
253
+ relations: ['bizplace', 'bizplace.domain', 'bizplace.company', 'bizplace.company.domain']
670
254
  });
255
+ if (foundReleaseGood) {
256
+ const customerCompanyDomain = foundReleaseGood.bizplace.company.domain;
257
+ const marketplaceOrder = await tx.getRepository(marketplace_base_1.MarketplaceOrder).findOne({
258
+ where: { orderNo: refNo, domain: customerCompanyDomain }
259
+ });
260
+ // Need to restructure the validation
261
+ if (marketplaceOrder === null || marketplaceOrder === void 0 ? void 0 : marketplaceOrder.isSplitted) {
262
+ const refNo2 = releaseGood.refNo2;
263
+ const foundSplittedReleaseGood = await tx.getRepository(sales_base_1.ReleaseGood).findOne({
264
+ where: {
265
+ domain,
266
+ refNo2,
267
+ status: (0, typeorm_1.Not)((0, typeorm_1.In)([sales_base_1.ORDER_STATUS.CANCELLED, sales_base_1.ORDER_STATUS.PENDING_CANCEL]))
268
+ },
269
+ relations: ['bizplace', 'bizplace.domain', 'bizplace.company', 'bizplace.company.domain']
270
+ });
271
+ if (foundSplittedReleaseGood)
272
+ throw new index_1.ValidationError({
273
+ errorCode: 'EXISTING_ORDER',
274
+ message: 'existing release order found',
275
+ detail: foundSplittedReleaseGood.name
276
+ });
277
+ }
278
+ else {
279
+ throw new index_1.ValidationError({
280
+ errorCode: 'EXISTING_ORDER',
281
+ message: 'existing release order found',
282
+ detail: foundReleaseGood.name
283
+ });
284
+ }
285
+ }
671
286
  }
672
- idx++;
673
287
  }
674
- return targetInventories;
675
288
  }
676
- exports._composeTargetInventories = _composeTargetInventories;
677
- function combineItems(inventoryList) {
678
- let mappedList = [];
679
- inventoryList.map(e => {
680
- let item = mappedList.find(i => i.product.id === e.product.id);
681
- if (item) {
682
- mappedList = mappedList.filter(m => m != item);
683
- item.releaseQty += e.releaseQty;
684
- mappedList.push(item);
289
+ async function createDraftOrder(releaseGood, orderProducts, context) {
290
+ return await (0, typeorm_1.getConnection)().transaction(async (tx) => {
291
+ return await (0, sales_base_1.createDraftReleaseGoodFunction)(null, releaseGood, orderProducts, null, [], context, tx);
292
+ });
293
+ }
294
+ async function createReleaseGood(releaseGood, orderProducts, bizplace, context, tx) {
295
+ try {
296
+ const { domain, user } = context.state;
297
+ const settingRepo = (tx === null || tx === void 0 ? void 0 : tx.getRepository(setting_base_1.Setting)) || (0, typeorm_1.getRepository)(setting_base_1.Setting);
298
+ if (orderProducts.length <= 0) {
299
+ throw new index_1.ValidationError({
300
+ errorCode: 'ITEM_NOT_FOUND',
301
+ message: 'no order items found',
302
+ detail: JSON.stringify(releaseGood)
303
+ });
685
304
  }
686
- else {
687
- mappedList.push(e);
305
+ let newReleaseGood = new sales_base_1.ReleaseGood();
306
+ // find RO number rule setting
307
+ const roNoSetting = await settingRepo.findOne({
308
+ where: {
309
+ domain,
310
+ name: sales_base_1.ORDER_NUMBER_SETTING_KEY.RO_NUMBER_RULE
311
+ }
312
+ });
313
+ newReleaseGood = Object.assign(Object.assign({}, newReleaseGood), { name: roNoSetting
314
+ ? await (0, id_rule_base_1.generateId)({ domain, type: sales_base_1.ORDER_NUMBER_RULE_TYPE.RO_NUMBER, seed: {} })
315
+ : sales_base_1.OrderNoGenerator.releaseGood(), domain: domain, bizplace: bizplace, collectionOrderNo: releaseGood.collectionOrderNo, courierOption: releaseGood.courierOption, exportOption: releaseGood.exportOption, ownTransport: releaseGood.ownTransport, packingOption: releaseGood.packingOption, marketplaceOrderStatus: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.marketplaceOrderStatus) || null, billingAddress: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.billingAddress) || null, deliveryAddress1: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.deliveryAddress1) || null, deliveryAddress2: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.deliveryAddress2) || null, deliveryAddress3: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.deliveryAddress3) || null, deliveryAddress4: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.deliveryAddress4) || null, deliveryAddress5: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.deliveryAddress5) || null, attentionTo: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.attentionTo) || null, attentionCompany: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.attentionCompany) || null, city: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.city) || null, state: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.state) || null, postalCode: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.postalCode) || null, country: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.country) || null, phone1: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.phone1) || null, phone2: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.phone2) || null, email: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.email) || null, transporter: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.transporter, trackingNo: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.trackingNo, airwayBill: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.airwayBill, invoice: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.invoice, refNo: releaseGood.refNo, refNo2: releaseGood.refNo2 || null, refNo3: releaseGood.refNo3 || null, remark: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.remark) || null, releaseDate: releaseGood.releaseDate, truckNo: releaseGood.truckNo, type: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.type) ? releaseGood.type : 'b2b', status: sales_base_1.ORDER_STATUS.PENDING_WORKSHEET, storeName: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.storeName, storeId: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.storeId, routeId: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.routeId, stopId: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.stopId, noOfItems: orderProducts.length, creator: user, updater: user, acceptedBy: user, acceptedAt: new Date() });
316
+ let resultReleaseGood = await tx.getRepository(sales_base_1.ReleaseGood).save(newReleaseGood);
317
+ let combinedOrderInventories = [];
318
+ await Promise.all(orderProducts.map(async (orderProduct) => {
319
+ let orderInventories = orderProduct.orderInventories;
320
+ orderProduct = Object.assign(new sales_base_1.OrderProduct(), Object.assign(Object.assign({}, orderProduct), { name: (0, uuid_1.v4)(), domain: domain, bizplace: bizplace, releaseGood: resultReleaseGood, type: sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS, status: sales_base_1.ORDER_PRODUCT_STATUS.ASSIGNED }));
321
+ await tx.getRepository(sales_base_1.OrderProduct).save(orderProduct);
322
+ for (let oiIdx = 0; oiIdx < orderInventories.length; oiIdx++) {
323
+ let orderInventory = Object.assign(new sales_base_1.OrderInventory(), Object.assign(Object.assign({}, orderInventories[oiIdx]), { name: (0, uuid_1.v4)(), domain: domain, bizplace: bizplace, releaseGood: resultReleaseGood, type: sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS, status: sales_base_1.ORDER_PRODUCT_STATUS.ASSIGNED }));
324
+ await tx.getRepository(sales_base_1.OrderInventory).save(orderInventory);
325
+ combinedOrderInventories.push({
326
+ product: { name: orderInventory.product.name, sku: orderInventory.product.sku },
327
+ batchId: orderInventory.batchId,
328
+ packingType: orderInventory.packingType,
329
+ packingSize: orderInventory.packingSize,
330
+ releaseQty: orderInventory.releaseQty,
331
+ releaseUomValue: orderInventory.releaseUomValue
332
+ });
333
+ }
334
+ }));
335
+ newReleaseGood.orderInventories = combinedOrderInventories;
336
+ return newReleaseGood;
337
+ }
338
+ catch (error) {
339
+ throw error;
340
+ }
341
+ }
342
+ async function massageOrderItems(releaseGood, inputOrderProducts, context) {
343
+ const { t } = context;
344
+ try {
345
+ let orderProducts = [];
346
+ let errorList = [];
347
+ //combine any repeated skus
348
+ inputOrderProducts = inputOrderProducts.reduce((acc, itm, idx) => {
349
+ let { sku, refCode } = itm.product;
350
+ let { packingType, packingSize, batchId, releaseQty } = itm;
351
+ if (!sku && !refCode) {
352
+ errorList.push(JSON.stringify({
353
+ code: 'INVALID_DATA',
354
+ message: 'sku or refCode not found',
355
+ sku: sku,
356
+ refCode: refCode,
357
+ packingType: packingType === null || packingType === void 0 ? void 0 : packingType.trim(),
358
+ packingSize: packingSize,
359
+ qty: releaseQty,
360
+ }));
361
+ }
362
+ if (releaseQty <= 0) {
363
+ errorList.push(JSON.stringify({
364
+ code: 'INVALID_DATA',
365
+ message: 'negative stock request',
366
+ sku: sku,
367
+ refCode: refCode,
368
+ packingType: packingType === null || packingType === void 0 ? void 0 : packingType.trim(),
369
+ packingSize: packingSize,
370
+ qty: releaseQty,
371
+ }));
372
+ }
373
+ let existingIndex = acc.findIndex(itm => (itm === null || itm === void 0 ? void 0 : itm.sku) == (sku === null || sku === void 0 ? void 0 : sku.trim()) &&
374
+ (itm === null || itm === void 0 ? void 0 : itm.refCode) == (refCode === null || refCode === void 0 ? void 0 : refCode.trim()) &&
375
+ (itm === null || itm === void 0 ? void 0 : itm.packingType) == (packingType === null || packingType === void 0 ? void 0 : packingType.trim()) &&
376
+ (itm === null || itm === void 0 ? void 0 : itm.packingSize) == packingSize);
377
+ if (existingIndex >= 0 && releaseQty > 0) {
378
+ acc[existingIndex].releaseQty = acc[existingIndex].releaseQty + releaseQty;
379
+ }
380
+ else {
381
+ acc.push({
382
+ sku: sku === null || sku === void 0 ? void 0 : sku.trim(),
383
+ refCode: refCode === null || refCode === void 0 ? void 0 : refCode.trim(),
384
+ packingType: packingType === null || packingType === void 0 ? void 0 : packingType.trim(),
385
+ packingSize: packingSize,
386
+ releaseQty: releaseQty
387
+ });
388
+ }
389
+ return acc;
390
+ }, []);
391
+ if (errorList.length > 0) {
392
+ throw new index_1.ValidationError({
393
+ errorCode: 'INVALID_DATA',
394
+ message: 'invalid data',
395
+ detail: errorList.join(', ')
396
+ });
688
397
  }
689
- });
690
- return mappedList;
398
+ //find customer sku in system
399
+ await Promise.all(inputOrderProducts.map(async (inputOrderItem) => {
400
+ const sku = inputOrderItem.sku;
401
+ const refCode = inputOrderItem.refCode;
402
+ const packingType = inputOrderItem.packingType;
403
+ const packingSize = inputOrderItem.packingSize;
404
+ const qb = (0, typeorm_1.getRepository)(product_base_1.ProductDetail)
405
+ .createQueryBuilder('PD')
406
+ .innerJoinAndSelect('PD.product', 'PROD')
407
+ .where('PROD.bizplace_id = :bizplaceId', { bizplaceId: releaseGood.companyBizplace.id });
408
+ if (!sku && !refCode) {
409
+ context.throw(404, t('error.sku or refCode not found'));
410
+ }
411
+ if (refCode) {
412
+ qb.andWhere('PD.ref_code = :refCode', { refCode });
413
+ }
414
+ if (sku) {
415
+ qb.andWhere('PROD.sku = :sku', { sku });
416
+ }
417
+ if (packingType) {
418
+ qb.andWhere('PD.packing_type = :packingType', { packingType });
419
+ }
420
+ if ((!packingType && !refCode) || !packingSize) {
421
+ qb.andWhere('PD.is_default = :isDefault', { isDefault: true });
422
+ }
423
+ const productDetail = await qb.getOne();
424
+ // order Product based on product detail
425
+ if (productDetail) {
426
+ let newOrderProduct = Object.assign({}, inputOrderItem);
427
+ newOrderProduct = Object.assign(Object.assign({}, newOrderProduct), { product: productDetail.product, productDetail: productDetail, packingType: productDetail.packingType, packingSize: packingSize || productDetail.packingSize, uom: productDetail.uom, uomValue: productDetail.uomValue, refCode: inputOrderItem.refCode, releaseQty: inputOrderItem.releaseQty, releaseUomValue: inputOrderItem.releaseQty * (productDetail.uomValue || 1), packQty: 0, actualPackQty: 0, palletQty: 0, actualPalletQty: 0, type: 'RELEASE_OF_GOODS', status: sales_base_1.ORDER_PRODUCT_STATUS.ASSIGNED, batchId: '-' });
428
+ newOrderProduct.orderInventories = [
429
+ Object.assign(Object.assign({}, newOrderProduct), { packQty: 0, actualPackQty: 0, palletQty: 0, actualPalletQty: 0, type: 'RELEASE_OF_GOODS', status: sales_base_1.ORDER_INVENTORY_STATUS.PENDING_WORKSHEET })
430
+ ];
431
+ orderProducts.push(newOrderProduct);
432
+ }
433
+ if (!productDetail) {
434
+ const qb = (0, typeorm_1.getRepository)(product_base_1.ProductBundle)
435
+ .createQueryBuilder('PB')
436
+ .innerJoinAndSelect('PB.productBundleSettings', 'PBS')
437
+ .innerJoinAndSelect('PBS.productDetail', 'PBD')
438
+ .innerJoinAndSelect('PBD.product', 'PROD')
439
+ .where('PB.bizplace_id = :bizplaceId', { bizplaceId: releaseGood.companyBizplace.id });
440
+ if (!sku && !refCode) {
441
+ context.throw(404, t('error.sku or refCode not found'));
442
+ }
443
+ if (refCode) {
444
+ qb.andWhere('PB.ref_code = :refCode', { refCode });
445
+ }
446
+ if (sku) {
447
+ qb.andWhere('PB.sku = :sku', { sku });
448
+ }
449
+ if (packingType) {
450
+ qb.andWhere('PB.packing_type = :packingType', { packingType });
451
+ }
452
+ const foundProductBundle = await qb.getOne();
453
+ if (foundProductBundle) {
454
+ let orderProduct = {
455
+ productBundle: foundProductBundle,
456
+ refCode,
457
+ packingType: foundProductBundle.packingType,
458
+ packingSize: packingSize || 1,
459
+ uom: inputOrderItem === null || inputOrderItem === void 0 ? void 0 : inputOrderItem.uom,
460
+ releaseQty: inputOrderItem.releaseQty,
461
+ releaseUomValue: inputOrderItem.releaseQty,
462
+ packQty: 0,
463
+ actualPackQty: 0,
464
+ palletQty: 0,
465
+ actualPalletQty: 0,
466
+ orderInventories: [],
467
+ type: 'RELEASE_OF_GOODS',
468
+ status: sales_base_1.ORDER_PRODUCT_STATUS.ASSIGNED,
469
+ batchId: '-'
470
+ };
471
+ const bundleProductSettings = foundProductBundle.productBundleSettings;
472
+ bundleProductSettings.map(bundleProductSetting => {
473
+ const productDetailBundle = bundleProductSetting.productDetail;
474
+ let newOrderInventory = {
475
+ product: productDetailBundle.product,
476
+ productDetail: productDetailBundle,
477
+ sku: productDetailBundle.product.sku,
478
+ packingType: productDetailBundle.packingType,
479
+ packingSize: productDetailBundle.packingSize,
480
+ uom: productDetailBundle.uom,
481
+ packQty: 0,
482
+ actualPackQty: 0,
483
+ palletQty: 0,
484
+ actualPalletQty: 0,
485
+ releaseQty: inputOrderItem.releaseQty * bundleProductSetting.bundleQty,
486
+ releaseUomValue: inputOrderItem.releaseQty *
487
+ bundleProductSetting.bundleQty *
488
+ (productDetailBundle.uomValue <= 0 ? 1 : productDetailBundle.uomValue),
489
+ type: 'RELEASE_OF_GOODS',
490
+ status: sales_base_1.ORDER_INVENTORY_STATUS.PENDING_WORKSHEET
491
+ };
492
+ orderProduct.orderInventories.push(newOrderInventory);
493
+ });
494
+ orderProducts.push(orderProduct);
495
+ }
496
+ else {
497
+ if (sku && refCode) {
498
+ context.throw(404, t(`error.sku ${sku} and refCode ${refCode} not exist in product / bundle master`));
499
+ }
500
+ if (sku) {
501
+ context.throw(404, t(`error.sku ${sku} not exist in product / bundle master`));
502
+ }
503
+ if (refCode) {
504
+ context.throw(404, t(`error.refCode ${refCode} not exist in product / bundle master`));
505
+ }
506
+ }
507
+ }
508
+ }));
509
+ //combine any repeated skus
510
+ let combinedItems = orderProducts.reduce((acc, op, idx) => {
511
+ let oi = (op === null || op === void 0 ? void 0 : op.orderInventories) || [];
512
+ oi.forEach(itm => {
513
+ let { sku, refCode } = itm.product;
514
+ let { packingType, packingSize, batchId, releaseQty, product, productDetail, uom } = itm;
515
+ if (!sku && !refCode) {
516
+ context.throw(404, t('error.sku or refCode not found'));
517
+ }
518
+ let existingIndex = acc.findIndex(itm => (itm === null || itm === void 0 ? void 0 : itm.sku) == (sku === null || sku === void 0 ? void 0 : sku.trim()) &&
519
+ (itm === null || itm === void 0 ? void 0 : itm.refCode) == (refCode === null || refCode === void 0 ? void 0 : refCode.trim()) &&
520
+ (itm === null || itm === void 0 ? void 0 : itm.packingType) == (packingType === null || packingType === void 0 ? void 0 : packingType.trim()) &&
521
+ (itm === null || itm === void 0 ? void 0 : itm.packingSize) == packingSize &&
522
+ (itm === null || itm === void 0 ? void 0 : itm.batchId) == (batchId === null || batchId === void 0 ? void 0 : batchId.trim()) &&
523
+ (itm === null || itm === void 0 ? void 0 : itm.uom) == (uom === null || uom === void 0 ? void 0 : uom.trim()));
524
+ if (existingIndex >= 0 && releaseQty > 0) {
525
+ acc[existingIndex].releaseQty = acc[existingIndex].releaseQty + releaseQty;
526
+ acc[existingIndex].releaseUomValue =
527
+ acc[existingIndex].releaseUomValue + releaseQty * (productDetail.uomValue <= 0 ? 1 : productDetail.uomValue);
528
+ }
529
+ else {
530
+ let updateObj = {
531
+ sku: sku === null || sku === void 0 ? void 0 : sku.trim(),
532
+ packingType: packingType === null || packingType === void 0 ? void 0 : packingType.trim(),
533
+ packingSize: packingSize,
534
+ uom: uom === null || uom === void 0 ? void 0 : uom.trim(),
535
+ releaseQty: releaseQty,
536
+ releaseUomValue: releaseQty * (productDetail.uomValue <= 0 ? 1 : productDetail.uomValue),
537
+ product,
538
+ productDetail
539
+ };
540
+ if (refCode)
541
+ updateObj['refCode'] = refCode.trim();
542
+ updateObj['batchId'] = batchId ? batchId === null || batchId === void 0 ? void 0 : batchId.trim() : '-';
543
+ acc.push(updateObj);
544
+ }
545
+ });
546
+ return acc;
547
+ }, []);
548
+ return { orderProducts, combinedItems };
549
+ }
550
+ catch (error) {
551
+ throw error;
552
+ }
691
553
  }
692
- async function createDraftOrder(client, customerBizplace, bodyReq, bundleInfo, domain, context, orderInventories, tx) {
693
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
694
- return (await client.mutate({
695
- mutation: (0, graphql_tag_1.default) `
696
- mutation upsertDraftReleaseGood(
697
- $draftReleaseGood: DraftReleaseGoodPatch!
698
- $orderProducts: [OrderProductPatch!]!
699
- ) {
700
- upsertDraftReleaseGood(draftReleaseGood: $draftReleaseGood, orderProducts: $orderProducts) {
701
- id
702
- name
703
- refNo
704
- refNo2
705
- status
706
- truckNo
707
- ownTransport
708
- marketplaceOrderStatus
709
- billingAddress
710
- deliveryAddress1
711
- deliveryAddress2
712
- deliveryAddress3
713
- deliveryAddress4
714
- deliveryAddress5
715
- attentionTo
716
- attentionCompany
717
- city
718
- state
719
- postalCode
720
- country
721
- phone1
722
- phone2
723
- email
724
- type
725
- exportOption
726
- releaseDate
727
- collectionOrderNo
728
- bizplace {
729
- name
554
+ async function assignToInventory(orderProducts, customerBizplace, context, tx) {
555
+ const { client, domain, user } = context.state;
556
+ for (let opIdx = 0; opIdx < orderProducts.length; opIdx++) {
557
+ let orderProduct = orderProducts[opIdx];
558
+ let assignedOrderInventories = [];
559
+ for (let oiIdx = 0; oiIdx < orderProducts[opIdx].orderInventories.length; oiIdx++) {
560
+ const orderInventory = orderProduct.orderInventories[oiIdx];
561
+ let sortings = [];
562
+ switch (orderInventory.product.pickingStrategy) {
563
+ case 'LIFO':
564
+ sortings.push({ name: 'iv.created_at', desc: true });
565
+ break;
566
+ case 'FEFO':
567
+ sortings.push({ name: 'iv.expiration_date', desc: false });
568
+ sortings.push({ name: 'iv.created_at', desc: false });
569
+ break;
570
+ case 'FMFO':
571
+ sortings.push({ name: 'iv.manufacture_date', desc: false });
572
+ sortings.push({ name: 'iv.created_at', desc: false });
573
+ break;
574
+ case 'LOCATION':
575
+ const pickingProductSetting = await tx.getRepository(setting_base_1.Setting).findOne({
576
+ where: { domain, name: 'rule-for-picking-product' }
577
+ });
578
+ if (pickingProductSetting === null || pickingProductSetting === void 0 ? void 0 : pickingProductSetting.value) {
579
+ for (const [key, value] of Object.entries(JSON.parse(pickingProductSetting.value))) {
580
+ sortings.push({ name: `loc.${key}`, desc: value == 'DESC' ? true : false });
581
+ }
582
+ }
583
+ else {
584
+ sortings.push({ name: 'loc.name', desc: true });
585
+ }
586
+ break;
587
+ //Every other case includes 'FIFO' will be applicable for this case
588
+ default:
589
+ sortings.push({ name: 'iv.created_at', desc: false });
590
+ break;
730
591
  }
731
- }
592
+ let queryFilters = [];
593
+ let queryStrings = queryFilters.reduce((acc, itm, idx, arr) => {
594
+ acc.values.push(itm.filters);
595
+ switch (itm === null || itm === void 0 ? void 0 : itm.operator) {
596
+ case 'notin':
597
+ case 'in':
598
+ acc.query.push(`${itm.query} ${itm.operator == 'notin' ? 'NOT IN' : 'IN'} (${itm.filtes
599
+ .map((itm, idx) => {
600
+ return `$${acc.values.length + 1}`;
601
+ })
602
+ .join(',')})`);
603
+ break;
604
+ default:
605
+ acc.query.push(`${itm.query} ${(itm === null || itm === void 0 ? void 0 : itm.operator) ? itm.operator : '='} $${acc.values.length + 1}`);
606
+ break;
607
+ }
608
+ acc.query.push(`${itm.query} ${(itm === null || itm === void 0 ? void 0 : itm.operator) ? itm.operator : '='} $${acc.values.length + 1}`);
609
+ }, {
610
+ query: [],
611
+ values: []
612
+ });
613
+ let sortQuery = sortings
614
+ .map(itm => {
615
+ return `${itm.name} ${itm.desc ? 'DESC' : 'ASC'}`;
616
+ })
617
+ .join(`,`);
618
+ let updatedInventories = await tx.getRepository(warehouse_base_1.Inventory).query(`
619
+ update inventories tgt set locked_qty = coalesce(locked_qty,0) + src.reserve_qty,
620
+ locked_uom_value = coalesce(locked_uom_value,0) + src.reserve_uom_value,
621
+ updated_at = NOW(),
622
+ updater_id = $1
623
+ from (
624
+ select "id", "pallet_id","carton_id", "batch_id", "batch_id_ref", "unit", "uom", "packing_type", "packing_size", "manufacture_year",
625
+ "reserve_qty", (("uom_value"/"qty") * "reserve_qty") as "reserve_uom_value" from (
626
+ select "sort_seq", "id", "pallet_id", "batch_id", "batch_id_ref", "unit", "uom", "packing_type", "packing_size", "manufacture_year", "carton_id", "uom_value", "locked_uom_value", "qty", "locked_qty", "created_at",
627
+ "release_qty", "release_uom_value", lock_amount,
628
+ case when "lock_amount" < 0 then "available_qty" else "available_qty" - "lock_amount" end as "reserve_qty"
629
+ from (
630
+ select *,
631
+ qty - locked_qty as available_qty,
632
+ sum(qty - locked_qty - release_qty) over (order by sort_seq asc rows between unbounded preceding and current row) as lock_amount
633
+ from (
634
+ SELECT 0 as sort_seq, null as id, null as pallet_id, null as batch_id, null as batch_id_ref,
635
+ null as unit, '${orderInventory.uom}' as uom, '${orderInventory.packingType}' as packing_type, '${orderInventory.packingSize}' as packing_size,
636
+ null as manufacture_year, null as carton_id, 0 as uom_value, 0 as locked_uom_value, 0 as qty, 0 as locked_qty, null as created_at,
637
+ '${orderInventory.releaseQty}' as release_qty, '${orderInventory.releaseUomValue}' as release_uom_value
638
+ UNION
639
+ (
640
+ SELECT ROW_NUMBER() OVER(PARTITION BY iv.domain_id ORDER BY ${sortQuery}) as sort_seq,
641
+ iv.id, iv.pallet_id, iv.batch_id, iv.batch_id_ref,
642
+ iv.unit, iv.uom, iv.packing_type, iv.packing_size,
643
+ iv.manufacture_year, iv.carton_id, iv.uom_value,
644
+ coalesce(iv.locked_uom_value,0) as locked_uom_value, iv.qty, coalesce(iv.locked_qty,0) as locked_qty, iv.created_at,
645
+ 0 as release_qty, 0 as release_uom_value
646
+ FROM "inventories" "iv"
647
+ INNER JOIN "locations" "loc" ON "loc"."id"="iv"."location_id"
648
+ WHERE case when coalesce("iv"."locked_qty",0) < 0 then 0 else ("iv"."qty" - coalesce("iv"."locked_qty",0)) end > 0 AND
649
+ "iv"."domain_id" = $2 AND "iv"."bizplace_id" = $3 AND "iv"."packing_type" = $4 AND "iv"."packing_size" = $5 AND "iv"."product_id" = $6 AND "iv"."status" = $7 AND "loc"."type" NOT IN ($8, $9)
650
+ ${queryStrings.query.length > 0 ? `AND ${queryStrings.join(' AND ')}` : ''}
651
+ ORDER BY ${sortQuery}
652
+ )
653
+ ) dt1
654
+ ) dt2 where case when "lock_amount" < 0 then "available_qty" else "available_qty" - "lock_amount" end > 0
655
+ ) dt3 where sort_seq > 0
656
+ ) src where src.id = tgt.id
657
+ returning
658
+ tgt."id", tgt."qty", tgt."pallet_id", tgt."carton_id", tgt."batch_id", tgt."batch_id_ref", tgt."unit",
659
+ tgt."uom", tgt."packing_type", tgt."packing_size", tgt."manufacture_year",
660
+ tgt."locked_qty", tgt."uom_value", tgt."locked_uom_value", src."reserve_qty", src."reserve_uom_value";`, [
661
+ user.id,
662
+ domain.id,
663
+ customerBizplace.id,
664
+ orderInventory.packingType,
665
+ orderInventory.packingSize,
666
+ orderInventory.product.id,
667
+ warehouse_base_1.INVENTORY_STATUS.STORED,
668
+ warehouse_base_1.LOCATION_TYPE.QUARANTINE,
669
+ warehouse_base_1.LOCATION_TYPE.RESERVE,
670
+ ...queryStrings.values
671
+ ]);
672
+ // let data = await qb.getMany()
673
+ let totalAssigned = updatedInventories[0].reduce((acc, inv) => {
674
+ return acc + inv.reserve_qty;
675
+ }, 0);
676
+ if (orderInventory.releaseQty != totalAssigned) {
677
+ throw new index_1.ValidationError({
678
+ errorCode: 'INSUFFICIENT_STOCK',
679
+ message: 'insufficient stock',
680
+ detail: JSON.stringify({
681
+ code: 'insufficient stock',
682
+ sku: orderInventory.product.sku,
683
+ packingType: orderInventory.packingType,
684
+ packingSize: orderInventory.packingSize,
685
+ uom: orderInventory.uom,
686
+ qty: orderInventory.releaseQty,
687
+ uom_value: orderInventory.releaseUomValue
688
+ })
689
+ });
690
+ }
691
+ updatedInventories[0].forEach(inv => {
692
+ let oi = Object.assign(Object.assign({}, orderInventory), { inventory: { id: inv.id }, uom: inv.uom, packingType: inv.packing_type, batchId: inv.batch_id, releaseQty: inv.reserve_qty, releaseUomValue: inv.reserve_uom_value, pickedQty: 0, packedQty: 0, type: sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS, status: sales_base_1.ORDER_INVENTORY_STATUS.PENDING_WORKSHEET, domain: { id: domain.id }, bizplace: { id: customerBizplace.id } });
693
+ assignedOrderInventories.push(oi);
694
+ });
732
695
  }
733
- `,
734
- variables: {
735
- draftReleaseGood: {
736
- bizplace: {
737
- id: customerBizplace.id
738
- },
739
- courierOption: bodyReq.courierOption,
740
- collectionOrderNo: bodyReq.collectionOrderNo,
741
- exportOption: bodyReq.exportOption,
742
- ownTransport: bodyReq.ownTransport,
743
- packingOption: bodyReq.packingOption,
744
- refNo: bodyReq.refNo,
745
- refNo2: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.refNo2,
746
- releaseDate: bodyReq.releaseDate,
747
- type: bodyReq.type,
748
- marketplaceOrderStatus: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.marketplaceOrderStatus,
749
- remark: (bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.remark) || null,
750
- billingAddress: ((_a = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.billTo) === null || _a === void 0 ? void 0 : _a.billingAddress) || null,
751
- deliveryAddress1: ((_b = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _b === void 0 ? void 0 : _b.deliveryAddress1) || null,
752
- deliveryAddress2: ((_c = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _c === void 0 ? void 0 : _c.deliveryAddress2) || null,
753
- deliveryAddress3: ((_d = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _d === void 0 ? void 0 : _d.deliveryAddress3) || null,
754
- deliveryAddress4: ((_e = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _e === void 0 ? void 0 : _e.deliveryAddress4) || null,
755
- deliveryAddress5: ((_f = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _f === void 0 ? void 0 : _f.deliveryAddress5) || null,
756
- attentionTo: ((_g = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _g === void 0 ? void 0 : _g.attentionTo) || null,
757
- attentionCompany: ((_h = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _h === void 0 ? void 0 : _h.attentionCompany) || null,
758
- city: ((_j = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _j === void 0 ? void 0 : _j.city) || null,
759
- state: ((_k = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _k === void 0 ? void 0 : _k.state) || null,
760
- postalCode: ((_l = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _l === void 0 ? void 0 : _l.postalCode) || null,
761
- country: ((_m = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _m === void 0 ? void 0 : _m.country) || null,
762
- phone1: ((_o = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _o === void 0 ? void 0 : _o.phone1) || null,
763
- phone2: ((_p = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _p === void 0 ? void 0 : _p.phone2) || null,
764
- email: ((_q = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _q === void 0 ? void 0 : _q.email) || null
765
- },
766
- orderProducts: orderInventories
767
- },
768
- context: Object.assign(Object.assign({}, context), { hasUpload: true, state: Object.assign(Object.assign({}, context.state), { customerBizplace,
769
- domain, type: 'api' }) })
770
- })).data;
696
+ orderProducts[opIdx].orderInventories = assignedOrderInventories;
697
+ }
698
+ return orderProducts;
771
699
  }
772
700
  //# sourceMappingURL=add-release-order.js.map