diff --git a/src/helpers/cart-helper.js b/src/helpers/cart-helper.js index 1920df3c..22b0dcdb 100644 --- a/src/helpers/cart-helper.js +++ b/src/helpers/cart-helper.js @@ -165,8 +165,9 @@ export function getCartTotal(order) { export function getRecalibrationTotal(order) { const itemsWithRecalibration = getServiceLineItems(order) .filter((item) => item.requiresRecalibration); - const recalibrationLineItems = itemsWithRecalibration.map((item) => { - return item.childParts?.find((child) => child.partType === partTypeStrings.RECALIBRATION || child.partType === partTypeStrings.ADAS_RECALIBRATION) ?? {}; + + const recalibrationLineItems = itemsWithRecalibration.flatMap((item) => { + return item.childParts?.filter((child) => child.partType === partTypeStrings.RECALIBRATION || child.partType === partTypeStrings.ADAS_RECALIBRATION) ?? []; }); return getPriceOfLineItems(recalibrationLineItems); diff --git a/src/helpers/cart-helper.spec.js b/src/helpers/cart-helper.spec.js index 5a70feab..c57eaa86 100644 --- a/src/helpers/cart-helper.spec.js +++ b/src/helpers/cart-helper.spec.js @@ -4,6 +4,7 @@ import { getDeductible, getLineItems, getMobileFeeLineItem, getNonServiceLineItems, + getRecalibrationTotal, getRecycleFeeLineItem, getSalesTax, getServiceLineItems, getSubtotal, isOrderITAC, isOrderNoComp, isOrderUnverified @@ -25,7 +26,7 @@ describe('cart-helper', () => { }; } - function createDummyFeeItem(partNumber, partType, price, salesTax) { + function createDummyItemWithPartType(partNumber, partType, price, salesTax) { return { partNumber, partType, @@ -56,8 +57,8 @@ describe('cart-helper', () => { const glassLineItem = createDummyItem('glass', 40, 2); const otherLineItem = createDummyItem('other', 50, 3); const vapsLineItem = createDummyItem('vaps', 60, 4); - const mobileFeeLineItem = createDummyFeeItem(null, partTypeStrings.MOBILE_FEE, 70, 5); - const recycleFeeLineItem = createDummyFeeItem(partNumberStrings.RECYCLE_FEE, null, 80, 6); + const mobileFeeLineItem = createDummyItemWithPartType(null, partTypeStrings.MOBILE_FEE, 70, 5); + const recycleFeeLineItem = createDummyItemWithPartType(partNumberStrings.RECYCLE_FEE, null, 80, 6); const defaultLineItems = { supportingItems: [supportingLineItem], @@ -592,4 +593,99 @@ describe('cart-helper', () => { }); }); }); + + describe('getRecalibrationTotal', () => { + test('Returns total price of recalibration items when there is only 1', () => { + // Arrange + const recalibrationItem = createDummyItemWithPartType('recal', partTypeStrings.RECALIBRATION, 25, 2); + const lineItemWithRecal = { + ...glassLineItem, + requiresRecalibration: true, + childParts: [recalibrationItem] + }; + const order = { + lineItems: { + ...defaultLineItems, + glassParts: [lineItemWithRecal] + } + }; + + // Act + const result = getRecalibrationTotal(order); + + // Assert + expect(result).toBe(25); + }); + test('Returns total price of recalibration items when there are multiple', () => { + // Arrange + const recalibrationItem = createDummyItemWithPartType('recal', partTypeStrings.RECALIBRATION, 25, 2); + const thirdRecalibrationItem = createDummyItemWithPartType('third-recal', partTypeStrings.ADAS_RECALIBRATION, 35, 3); + const lineItemWithRecal = { + ...glassLineItem, + requiresRecalibration: true, + childParts: [recalibrationItem, thirdRecalibrationItem] + }; + const order = { + lineItems: { + ...defaultLineItems, + glassParts: [lineItemWithRecal] + } + }; + + // Act + const result = getRecalibrationTotal(order); + + // Assert + expect(result).toBe(60); + }); + + test('Returns 0 when no items require recalibration', () => { + // Arrange + const order = { + lineItems: defaultLineItems + }; + + // Act + const result = getRecalibrationTotal(order); + + // Assert + expect(result).toBe(0); + }); + + test('Returns 0 when line items are null', () => { + // Arrange + const order = { + lineItems: nullLineItems + }; + + // Act + const result = getRecalibrationTotal(order); + + // Assert + expect(result).toBe(0); + }); + + test('Filters out non-recalibration child parts', () => { + // Arrange + const recalibrationItem = createDummyItemWithPartType('recal', partTypeStrings.RECALIBRATION, 25, 2); + const otherChildItem = createDummyItem('other-child', 15, 1); + const lineItemWithRecal = { + ...glassLineItem, + requiresRecalibration: true, + childParts: [recalibrationItem, otherChildItem] + }; + const order = { + lineItems: { + ...defaultLineItems, + glassParts: [lineItemWithRecal] + } + }; + + // Act + const result = getRecalibrationTotal(order); + + // Assert + expect(result).toBe(25); + }); + }); }); diff --git a/src/store/index.js b/src/store/index.js index e0a6a097..0fb6adc7 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1120,7 +1120,7 @@ export const useMainStore = defineStore({ .then((response) => { const recalResponse = response.data; if (recalResponse && recalResponse.recalibrationParts && recalResponse.recalibrationParts.length > 0) { - this.addGlassPartRecalibrationInfo(partNumberChecked, recalResponse.recalibrationParts[0]); + this.addGlassPartRecalibrationInfo(partNumberChecked, recalResponse.recalibrationParts); } })); } @@ -1128,15 +1128,17 @@ export const useMainStore = defineStore({ await Promise.allSettled(recalPromises); } }, - addGlassPartRecalibrationInfo(partNumber, recalPartInformation) { + addGlassPartRecalibrationInfo(partNumber, recalPartArray) { const glassPart = this.lineItems.glassParts.find((gp) => gp.partNumber === partNumber); if (glassPart) { - const recalChildPart = glassPart.childParts?.find((cp) => cp.partNumber === recalPartInformation.partNumber); - if (!recalChildPart) { - if (!Array.isArray(glassPart.childParts) || !glassPart.childParts.length) { - glassPart.childParts = []; + for (const recalPartInformation of recalPartArray) { + const recalChildPart = glassPart.childParts?.find((cp) => cp.partNumber === recalPartInformation.partNumber); + if (!recalChildPart) { + if (!Array.isArray(glassPart.childParts) || !glassPart.childParts.length) { + glassPart.childParts = []; + } + glassPart.childParts.push(recalPartInformation); } - glassPart.childParts.push(recalPartInformation); } } },