Skip to content

Commit d7834f6

Browse files
authored
Merge pull request #344 from R-Sourabh/#337-purchase-order-page-fix
Improved: Updated the purchase order page code and optimized the fetchProducts functionality (#337)
2 parents cb04d1d + 4c8bcc5 commit d7834f6

File tree

5 files changed

+76
-51
lines changed

5 files changed

+76
-51
lines changed

src/components/MissingSkuModal.vue

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@
2929
<!-- If two different POs contain same missing SKU then in MissingSkuModal, both the products will be selected. -->
3030
<ion-radio-group @ionChange="updatedSku = $event.detail.value; hasSkuUpdated = false; isSkuInvalid = false;" v-model="unidentifiedProductSku">
3131
<ion-list v-if="segmentSelected === 'pending'">
32-
<ion-item v-for="item in getPendingItems()" :key="item.shopifyProductSKU">
33-
<ion-radio :value="item.shopifyProductSKU">
32+
<ion-item v-for="item in getPendingItems()" :key="item.identification">
33+
<ion-radio :value="item.identification">
3434
<ion-label>
35-
{{ item.shopifyProductSKU }}
35+
{{ item.identification }}
3636
<p>{{ item.orderId }}</p>
3737
</ion-label>
3838
</ion-radio>
3939
</ion-item>
4040
</ion-list>
4141

4242
<ion-list v-if="segmentSelected === 'completed'">
43-
<ion-item v-for="item in getCompletedItems()" :key="item.shopifyProductSKU">
43+
<ion-item v-for="item in getCompletedItems()" :key="item.identification">
4444
<ion-thumbnail slot="start">
4545
<DxpShopifyImg :src="item.imageUrl" size="small" />
4646
</ion-thumbnail>
@@ -163,16 +163,27 @@ export default defineComponent({
163163
const payload = {
164164
viewSize: 1,
165165
viewIndex: 0,
166-
productIds: [this.updatedSku]
166+
productIdentificationIds: [this.updatedSku],
167+
identificationTypeId: this.unidentifiedItems[0]?.identificationTypeId
167168
}
168169
const products = await this.store.dispatch("product/fetchProducts", payload);
169-
const product = products[this.updatedSku];
170+
171+
// Create a mapping from identification value to pseudoId
172+
let pseudoId = '';
173+
Object.values(products).forEach((product: any) => {
174+
const matchingIdentifier = product.identifications?.find((id: any) => id.productIdTypeEnumId === this.unidentifiedItems[0]?.identificationTypeId);
175+
if(matchingIdentifier && matchingIdentifier.idValue === this.updatedSku) {
176+
pseudoId = product.pseudoId;
177+
}
178+
});
179+
180+
const product = products[pseudoId];
170181
if (!product) {
171182
this.isSkuInvalid = true;
172183
return;
173184
}
174185
175-
const unidentifiedItem = this.unidentifiedItems.find((unidentifiedItem: any) => unidentifiedItem.shopifyProductSKU === this.unidentifiedProductSku || unidentifiedItem.updatedSku === this.unidentifiedProductSku );
186+
const unidentifiedItem = this.unidentifiedItems.find((unidentifiedItem: any) => unidentifiedItem.identification === this.unidentifiedProductSku || unidentifiedItem.updatedSku === this.unidentifiedProductSku );
176187
177188
unidentifiedItem.updatedSku = this.updatedSku;
178189
unidentifiedItem.parentProductId = product.parent.id;

src/components/ProductPopover.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,8 @@ export default defineComponent({
111111
this.purchaseOrders.parsed[this.poId] = this.purchaseOrders.parsed[this.poId].map(element => {
112112
if(element.parentProductId === this.id) {
113113
const item = original[this.poId].find(item => {
114-
// shopifyProductSKU check prevents reverting all the items of parent product to the first one as all the products have same parent product Id.
115-
// shopifyProductSKU check prevents reverting all the items of parent product to the first one as all the products have same parent product Id.
116-
// shopifyProductSKU check prevents reverting all the items of parent product to the first one as all the products have same parent product Id.
117-
return item.parentProductId === this.id && item.shopifyProductSKU === element.shopifyProductSKU;
114+
// identification check prevents reverting all the items of parent product to the first one as all the products have same parent product Id.
115+
return item.parentProductId === this.id && item.identification === element.identification;
118116
})
119117
element = item;
120118
}
@@ -127,7 +125,7 @@ export default defineComponent({
127125
this.stockItems.parsed = this.stockItems.parsed.map(element => {
128126
if(element.parentProductId === this.id) {
129127
const item = original.find(item => {
130-
return item.parentProductId === this.id && item.shopifyProductSKU === element.shopifyProductSKU;
128+
return item.parentProductId === this.id && item.identification === element.identification;
131129
})
132130
element = item;
133131
}

src/store/modules/order/actions.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,30 @@ import * as types from './mutation-types'
77

88
const actions: ActionTree<OrderState, RootState> = {
99
async fetchOrderDetails ({commit, rootGetters}, items) {
10-
const productIds = items.filter((item: any) => item.identification).map((item: any) => {
11-
return item.identification
12-
})
13-
const viewSize = productIds.length;
14-
const viewIndex = 0;
10+
const productIdentificationIds = Array.from(new Set(items.filter((item: any) => item.identification).map((item: any) => item.identification)));
11+
const viewSize = productIdentificationIds.length, viewIndex = 0;
1512
const payload = {
1613
viewSize,
1714
viewIndex,
18-
productIds,
15+
productIdentificationIds,
1916
identificationTypeId: items[0]?.identificationTypeId
2017
}
21-
await store.dispatch("product/fetchProducts", payload);
22-
const unidentifiedItems = [] as any;
2318

19+
const products = await store.dispatch("product/fetchProducts", payload);
20+
21+
// Create a mapping from identification value to pseudoId
22+
const identificationToPseudoId = {} as any;
23+
Object.values(products).forEach((product: any) => {
24+
const matchingIdentifier = product.identifications?.find((id: any) => id.productIdTypeEnumId.toLowerCase() === items[0]?.identificationTypeId.toLowerCase());
25+
if(matchingIdentifier && productIdentificationIds.includes(matchingIdentifier.idValue)) {
26+
identificationToPseudoId[matchingIdentifier.idValue] = product.pseudoId;
27+
}
28+
});
29+
30+
const unidentifiedItems = [] as any;
2431
items = items.filter((item: any) => item.identification).map((item: any) => {
25-
const product = rootGetters['product/getProduct'](item.identification)
32+
const pseudoId = identificationToPseudoId[item.identification];
33+
const product = rootGetters['product/getProduct'](pseudoId)
2634

2735
if (Object.keys(product).length > 0) {
2836
item.parentProductId = product?.parent?.id;
@@ -57,8 +65,7 @@ const actions: ActionTree<OrderState, RootState> = {
5765
let original = state.purchaseOrders.original as any;
5866
const unidentifiedItems = payload.unidentifiedItems.map((item: any) => {
5967
if(item.updatedSku) {
60-
item.initialSKU = item.shopifyProductSKU;
61-
item.shopifyProductSKU = item.updatedSku;
68+
item.identification = item.updatedSku;
6269
parsed[item.orderId] ? parsed[item.orderId].push(item) : parsed[item.orderId] = [item];
6370
original[item.orderId] ? original[item.orderId].push(item) : original[item.orderId] = [item];
6471
} else {

src/store/modules/product/actions.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,46 @@ import { UtilService } from '@/services/UtilService'
1010

1111
const actions: ActionTree<ProductState, RootState> = {
1212

13-
async fetchProducts ( { commit, state }, { productIds, identificationTypeId }) {
13+
async fetchProducts ( { commit, state }, { productIdentificationIds, identificationTypeId }) {
1414

15-
// TODO Add try-catch block
15+
const productIdentificationFilter = productIdentificationIds.reduce((filter: Array<any>, productIdentificationId: any) => {
16+
if(!productIdentificationId) return filter;
17+
18+
// Check if this productIdentificationId exists in any cached product's identifications
19+
const productExistsInCache = Object.values(state.cached).some((cachedProduct: any) => {
20+
return cachedProduct.identifications?.some((identification: any) =>
21+
identification.productIdTypeEnumId.toLowerCase() === identificationTypeId.toLowerCase() && identification.idValue === productIdentificationId);
22+
});
1623

17-
const cachedProductIds = Object.keys(state.cached);
18-
const productIdFilter= productIds.reduce((filter: Array<any>, productId: any) => {
1924
// If product does not exist in cached products then add the id
20-
if (!cachedProductIds.includes(productId) && productId) {
21-
filter.push(productId);
22-
}
25+
if(!productExistsInCache) filter.push(productIdentificationId);
2326
return filter;
2427
}, []);
2528

26-
// If there are no product ids to search skip the API call
27-
if (productIdFilter.length == 0) return state.cached;
29+
// If there are no product to search skip the API call
30+
if (productIdentificationFilter.length == 0) return state.cached;
2831

29-
const modifiedProductIdFilters = productIdFilter.map((productId: string) => identificationTypeId + '/' + productId);
30-
const resp = await fetchProducts({
31-
filters: { 'goodIdentifications': { 'value': modifiedProductIdFilters }},
32-
viewSize: productIdFilter.length,
33-
viewIndex: 0
34-
})
35-
36-
if (!isError(resp)) {
37-
const products = resp.products;
38-
// Handled empty response in case of failed query
39-
if (resp.total) commit(types.PRODUCT_ADD_TO_CACHED_MULTIPLE, { products });
40-
} else {
41-
logger.error(resp.serverResponse)
32+
const modifiedProductIdentificationFilters = productIdentificationFilter.map((productIdentificationId: string) => identificationTypeId + '/' + productIdentificationId);
33+
let products = [] as any, viewIndex = 0, resp;
34+
try {
35+
do {
36+
resp = await fetchProducts({
37+
filters: { 'goodIdentifications': { 'value': modifiedProductIdentificationFilters }},
38+
viewSize: productIdentificationFilter.length,
39+
viewIndex
40+
})
41+
42+
if (!isError(resp) && resp.total > 0) {
43+
products = products.concat(resp.products);
44+
commit(types.PRODUCT_ADD_TO_CACHED_MULTIPLE, { products });
45+
viewIndex += resp.products.length;
46+
} else {
47+
logger.error(resp.serverResponse)
48+
}
49+
} while(resp.total > products.length);
50+
} catch (error) {
51+
logger.error(error);
4252
}
43-
// TODO Handle specific error
4453
return state.cached;
4554
},
4655
async findProduct({ commit, state }, payload) {

src/store/modules/stock/actions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const actions: ActionTree<StockState, RootState> = {
1515
this.dispatch('util/updateFileProcessingStatus', true);
1616

1717
//Fetching only top
18-
const productIds = items.slice(0, process.env['VUE_APP_VIEW_SIZE']).map((item: any) => item.identification);
18+
const productIdentificationIds = items.slice(0, process.env['VUE_APP_VIEW_SIZE']).map((item: any) => item.identification);
1919

2020
// We are getting external facilityId from CSV, extract facilityId and pass for getting locations
2121
const externalFacilityIds = [...new Set(items.map((item: any) => item.externalFacilityId))]
@@ -29,12 +29,12 @@ const actions: ActionTree<StockState, RootState> = {
2929
}).filter((facilityId: any) => facilityId)
3030
store.dispatch('util/fetchFacilityLocations', facilityIds);
3131

32-
const viewSize = productIds.length;
32+
const viewSize = productIdentificationIds.length;
3333
const viewIndex = 0;
3434
const payload = {
3535
viewSize,
3636
viewIndex,
37-
productIds,
37+
productIdentificationIds,
3838
identificationTypeId: items[0]?.identificationTypeId //fetching identificationTypeId from first item, as all the items will have one identification type
3939
}
4040
const cachedProducts = await store.dispatch("product/fetchProducts", payload);
@@ -78,10 +78,10 @@ const actions: ActionTree<StockState, RootState> = {
7878
},
7979
async processUpdateRestockItems({ commit }, items) {
8080

81-
const productIds = items.map((item: any) => item.identification);
81+
const productIdentificationIds = items.map((item: any) => item.identification);
8282

8383
const payload = {
84-
productIds,
84+
productIdentificationIds,
8585
identificationTypeId: items[0]?.identificationTypeId
8686
};
8787

0 commit comments

Comments
 (0)