diff --git a/src/services/UtilService.ts b/src/services/UtilService.ts index 5486e1ab..be01b6df 100644 --- a/src/services/UtilService.ts +++ b/src/services/UtilService.ts @@ -126,6 +126,53 @@ const findShipmentPackages = async(shipmentIds: Array): Promise => return shipmentPackages; } +const findShipmentPackageContents = async (shipmentIds: Array): Promise => { + let viewIndex = 0; + let shipmentPackageContents: any[] = []; + let shipmentPackageContentInfo: { [key: string]: any[] } = {}; + let resp; + + try { + do { + resp = await api({ + url: "performFind", + method: "get", + params: { + "entityName": "ShipmentPackageAndContent", + "inputFields": { + "shipmentId": shipmentIds, + "shipmentId_op": "in" + }, + "fieldList": ["shipmentId", "shipmentItemSeqId", "shipmentPackageSeqId", "packageName", "quantity"], + viewIndex, + "viewSize": 250, + "distinct": "Y" + } + }) as any; + + if (!hasError(resp) && resp.data.count) { + shipmentPackageContents = shipmentPackageContents.concat(resp.data.docs); + viewIndex++; + } else { + throw resp; + } + } while (resp.data.docs.length >= 250); + } catch (error) { + logger.error(error); + } + + shipmentPackageContentInfo = shipmentPackageContents.reduce((contents: any, shipmentPackageContent: any) => { + if (contents[shipmentPackageContent.shipmentId]) { + contents[shipmentPackageContent.shipmentId].push(shipmentPackageContent); + } else { + contents[shipmentPackageContent.shipmentId] = [shipmentPackageContent]; + } + return contents; + }, {}); + + return shipmentPackageContentInfo; +}; + const findCarrierPartyIdsForShipment = async(shipmentIds: Array): Promise => { let carrierPartyIdsByShipment = {}; @@ -578,6 +625,7 @@ export const UtilService = { findShipmentIdsForOrders, findShipmentItemInformation, findShipmentPackages, + findShipmentPackageContents, fetchTransferOrderFacets, getAvailablePickers, getProductStoreSetting, diff --git a/src/store/modules/order/actions.ts b/src/store/modules/order/actions.ts index 41848f93..b0c47caf 100644 --- a/src/store/modules/order/actions.ts +++ b/src/store/modules/order/actions.ts @@ -38,7 +38,7 @@ const actions: ActionTree = { // TODO: handle case when shipmentIds is empty // https://stackoverflow.com/questions/28066429/promise-all-order-of-resolved-values - const [shipmentPackagesByOrderInformationAndPicklistBin, itemInformationByOrderInformation, carrierPartyIdsByShipmentInformation] = await Promise.all([UtilService.findShipmentPackages(orderShipmentIds), UtilService.findShipmentItemInformation(orderShipmentIds), UtilService.findCarrierPartyIdsForShipment(orderShipmentIds)]) + const [shipmentPackagesByOrderInformationAndPicklistBin, shipmentPackageContents, itemInformationByOrderInformation, carrierPartyIdsByShipmentInformation] = await Promise.all([UtilService.findShipmentPackages(orderShipmentIds), UtilService.findShipmentPackageContents(orderShipmentIds), UtilService.findShipmentItemInformation(orderShipmentIds), UtilService.findCarrierPartyIdsForShipment(orderShipmentIds)]) // TODO: try fetching the carrierPartyIds when fetching packages information, as ShipmentPackageRouteSegDetail entity contain carrierPartyIds as well const carrierPartyIds = [...new Set(Object.values(carrierPartyIdsByShipmentInformation).map((carrierPartyIds: any) => carrierPartyIds.map((carrier: any) => carrier.carrierPartyId)).flat())] @@ -88,8 +88,8 @@ const actions: ActionTree = { item.shipmentId = shipment.shipmentId item.shipmentItemSeqId = shipment.shipmentItemSeqId } - - item.selectedBox = shipmentPackagesByOrderAndPicklistBin[`${item.orderId}_${item.picklistBinId}`]?.find((shipmentPackage: any) => shipmentPackage.shipmentId === item.shipmentId)?.packageName + + item.selectedBox = shipmentPackageContents[`${item.shipmentId}`].find((shipmentPackageContent: any) => shipmentPackageContent.shipmentItemSeqId === item.shipmentItemSeqId)?.packageName }) const orderItem = order.items[0]; @@ -1083,7 +1083,7 @@ const actions: ActionTree = { // TODO: handle case when shipmentIds is empty // https://stackoverflow.com/questions/28066429/promise-all-order-of-resolved-values - const [shipmentPackagesByOrderInformationAndPicklistBin, itemInformationByOrderInformation, carrierPartyIdsByShipmentInformation] = await Promise.all([UtilService.findShipmentPackages(orderShipmentIds), UtilService.findShipmentItemInformation(orderShipmentIds), UtilService.findCarrierPartyIdsForShipment(orderShipmentIds)]) + const [shipmentPackagesByOrderInformationAndPicklistBin, shipmentPackageContents, itemInformationByOrderInformation, carrierPartyIdsByShipmentInformation] = await Promise.all([UtilService.findShipmentPackages(orderShipmentIds), UtilService.findShipmentPackageContents(orderShipmentIds), UtilService.findShipmentItemInformation(orderShipmentIds), UtilService.findCarrierPartyIdsForShipment(orderShipmentIds)]) // TODO: try fetching the carrierPartyIds when fetching packages information, as ShipmentPackageRouteSegDetail entity contain carrierPartyIds as well const carrierPartyIds = [...new Set(Object.values(carrierPartyIdsByShipmentInformation).map((carrierPartyIds: any) => carrierPartyIds.map((carrier: any) => carrier.carrierPartyId)).flat())] @@ -1133,6 +1133,7 @@ const actions: ActionTree = { item.shipmentItemSeqId = shipment.shipmentItemSeqId } + item.selectedBox = shipmentPackageContents[`${item.shipmentId}`].find((shipmentPackageContent: any) => shipmentPackageContent.shipmentItemSeqId === item.shipmentItemSeqId)?.packageName item.selectedBox = shipmentPackagesByOrderAndPicklistBin[`${item.orderId}_${item.picklistBinId}`]?.find((shipmentPackage: any) => shipmentPackage.shipmentId === item.shipmentId)?.packageName })