From 4e4f99f7c90d4e583cd47fb107e44225ebbca8b5 Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Tue, 5 Mar 2024 10:05:55 -0500 Subject: [PATCH] Moving line item pricing to price calculator --- src/helpers/price-calculator.js | 10 ++ src/helpers/price-calculator.spec.js | 56 +++++++++++ .../coverage-statement/coverage-statement.vue | 92 +++++++------------ 3 files changed, 99 insertions(+), 59 deletions(-) create mode 100644 src/helpers/price-calculator.js create mode 100644 src/helpers/price-calculator.spec.js diff --git a/src/helpers/price-calculator.js b/src/helpers/price-calculator.js new file mode 100644 index 00000000..9e69259b --- /dev/null +++ b/src/helpers/price-calculator.js @@ -0,0 +1,10 @@ +function getPriceOfLineItem(lineItem) { + return lineItem.kitPrice + lineItem.laborAmount + lineItem.sellingPrice; +} + +export default function getPriceOfLineItems(lineItems) { + return lineItems.reduce( + (accumulator, lineItem) => accumulator + getPriceOfLineItem(lineItem), + 0 + ); +} diff --git a/src/helpers/price-calculator.spec.js b/src/helpers/price-calculator.spec.js new file mode 100644 index 00000000..4d09128b --- /dev/null +++ b/src/helpers/price-calculator.spec.js @@ -0,0 +1,56 @@ +import getPriceOfLineItems from "@/helpers/price-calculator.js"; + +describe('getPriceOfLineItems', () => { + test('Returns zero when no line items', () => { + // Arrange + const lineItems = []; + + // Act + const result = getPriceOfLineItems(lineItems); + + // Assert + expect(result).toBe(0); + }); + test('Returns expected when one line item', () => { + // Arrange + const lineItems = [ + { + kitPrice: 1, + laborAmount: 2, + sellingPrice: 3 + } + ]; + const expected = 6; + + // Act + const result = getPriceOfLineItems(lineItems); + + // Assert + expect(result).toBe(expected); + }); + test('Returns expected when multiple line items', () => { + // Arrange + const lineItems = [ + { + kitPrice: 1, + laborAmount: 2, + sellingPrice: 3 + }, + { + kitPrice: 1 + }, + { + kitPrice: 10, + laborAmount: 100, + sellingPrice: 1000 + } + ]; + const expected = 1117; + + // Act + const result = getPriceOfLineItems(lineItems); + + // Assert + expect(result).toBe(expected); + }); +}); diff --git a/src/layouts/coverage-statement/coverage-statement.vue b/src/layouts/coverage-statement/coverage-statement.vue index b6ed7771..5ac78138 100644 --- a/src/layouts/coverage-statement/coverage-statement.vue +++ b/src/layouts/coverage-statement/coverage-statement.vue @@ -40,7 +40,7 @@
- {{ formattedServicePrice }} + {{ servicePriceForDisplay }}
this.totalServicePrice; }, - // TODO maybe tweak coveredAndServicePriceAboveOrEqualDeductible() { return !this.verifiedNoComp && this.totalServicePrice >= this.deductibleValue; }, @@ -316,21 +321,17 @@ export default { const parts = useMainStore().order.lineItems.glassParts; return parts !== null && !!parts.find((part) => part.requiresRecalibration); }, - // TODO replace with better method totalServicePrice() { - let total = 0; - this.availableLineItems.forEach((lineItem) => { - total += this.getTotalLineItemPrice(lineItem); - }); - return total; + return getPriceOfLineItems(this.baseServiceLineItems); }, - formattedServicePrice() { - return this.getServicePriceString(this.totalServicePrice); + servicePriceForDisplay() { + return this.getFormattedAmount(this.totalServicePrice); }, - costSavings() { - const savings = this.getITACCostSavings(this.deductibleValue, this.totalServicePrice); - const formattedSavings = parseFloat(savings).toFixed(2); - return `$${formattedSavings}`; + itacCostSavings() { + return this.deductibleValue - this.totalServicePrice; + }, + itacCostSavingsForDisplay() { + return this.getFormattedAmount(this.itacCostSavings); }, serviceProviderQuestionText() { return this.getCmsContent( @@ -367,10 +368,13 @@ export default { arePagePrerequisitesValid() { return !!useMainStore().vehicle.carId; }, + getFormattedAmount(amount) { + return this.currencyFormatter.format(amount); + }, async initializeComponent() { useMainStore().updatePolicyITACFlag(this.verifiedITAC); if (this.policyLookupSuccessful - && useMainStore().order.vehicle.policyVehicleId >= 0 + && useMainStore().vehicle.policyVehicleId >= 0 && useMainStore().isClaimRegistrationRequired && !useMainStore().isClaimAlreadyRegistered && (this.coveredAndServicePriceAboveOrEqualDeductible || this.verifiedITAC)) { @@ -380,11 +384,7 @@ export default { }, async navigateForward() { if (this.unverified || this.verifiedDeductible) { -<<<<<<< HEAD - useMainStore().saveSupportingItems(this.supportingItems); -======= useMainStore().updateSupportingItems(this.supportingItems); ->>>>>>> 24103335eec282fad8ded09087cf8502b0150ad6 this.$router.navigate( navigationScenarios.CLICKED_FORWARD, this.$route, @@ -392,37 +392,28 @@ export default { { [routerParams.SAVE_SESSION_SYNCHRONOUS]: true } ); } else if (this.verifiedITAC || this.verifiedNoComp) { -<<<<<<< HEAD useMainStore().updateIsSafeliteProvider(this.selectedProvider === SAFELITE_PROVIDER); if (this.selectedProvider === SAFELITE_PROVIDER) { - useMainStore().saveSupportingItems(this.supportingItems); -======= - useMainStore().updateIsSafeliteProvider(this.selectedProvider === 'Safelite'); - if (this.selectedProvider === 'Safelite') { useMainStore().updateSupportingItems(this.supportingItems); ->>>>>>> 24103335eec282fad8ded09087cf8502b0150ad6 this.$router.navigate( navigationScenarios.CLICKED_FORWARD_WITH_SAFELITE, - this.$route, - {}, - { [routerParams.SAVE_SESSION_SYNCHRONOUS]: true } + this.$route ); } else { useMainStore().setBailout(this.$router.currentRoute, bailoutMessage.RequestCallback()); this.$router.navigate( navigationScenarios.CLICKED_FORWARD_WITH_NON_SAFELITE_SHOP, - this.$route, - {}, - { [routerParams.SAVE_SESSION_SYNCHRONOUS]: true } + this.$route ); } } else { - useMainStore().setBailout(this.$router.currentRoute, bailoutMessage.coverageStatementInvalidState()); + useMainStore().setBailout( + this.$router.currentRoute, + bailoutMessage.coverageStatementInvalidState() + ); this.$router.navigate( navigationScenarios.CLICKED_FORWARD_WITH_INVALID_STATE, - this.$route, - {}, - { [routerParams.SAVE_SESSION_SYNCHRONOUS]: true } + this.$route ); } }, @@ -454,28 +445,11 @@ export default { return null; } }, - // TODO move to common location - getTotalLineItemPrice(lineItem) { - return lineItem.kitPrice + lineItem.laborAmount + lineItem.sellingPrice; - }, - // TODO use price formatter - getDeductibleString(deductible) { - const formattedDeductibleFloat = parseFloat(deductible).toFixed(2); - return `$${formattedDeductibleFloat}`; - }, - // TODO use price formatter - getServicePriceString(price) { - const formattedPriceFloat = parseFloat(price).toFixed(2); - return `$${formattedPriceFloat}`; - }, - getITACCostSavings(vehicleDeductible, totalServicePrice) { - return vehicleDeductible - totalServicePrice; - }, setSupportingItems(newSupportingItems) { this.supportingItems = newSupportingItems; }, - setAvailableLineItems(newAvailableLineItems) { - this.availableLineItems = newAvailableLineItems; + setBaseServiceLineItems(lineItems) { + this.baseServiceLineItems = lineItems; } } }; @@ -485,7 +459,7 @@ export default { .cost { color: $green; font-size: 2rem; - font-weight: 300; + font-weight: $font-weight-light; line-height: 2.75rem; }