@things-factory/operato-hub 4.3.29 → 4.3.32

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