From 500a831de4126958c5134ce52bfb9ec9b43a4da0 Mon Sep 17 00:00:00 2001 From: Joywin Bennis <107112207+joywin2003@users.noreply.github.com> Date: Tue, 19 Nov 2024 21:47:59 +0530 Subject: [PATCH] fix: ensuring return basePrice, discountAmount, and finalPrice on error --- src/app/actions/get-price.ts | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/app/actions/get-price.ts b/src/app/actions/get-price.ts index 769a610..522a9c0 100644 --- a/src/app/actions/get-price.ts +++ b/src/app/actions/get-price.ts @@ -4,13 +4,13 @@ import prisma from "@/server/db"; import { basePrice, initialdiscount } from "@/constants"; export const getPrice = async ( - couponCode?: string, + couponCode?: string ): Promise<{ success: boolean; message?: string; - basePrice?: number; - discountAmount?: number; - finalPrice?: number; + basePrice: number; + discountAmount: number; + finalPrice: number; }> => { let discountAmount = initialdiscount; let finalPrice = basePrice; @@ -19,16 +19,34 @@ export const getPrice = async ( where: { code: couponCode }, }); if (!coupon) { - return { success: false, message: "Coupon code not found" }; + return { + basePrice, + discountAmount, + finalPrice, + success: false, + message: "Coupon code not found", + }; } if (coupon.isUsed) { - return { success: false, message: "Coupon code is already used" }; + return { + basePrice, + discountAmount, + finalPrice, + success: false, + message: "Coupon code is already used", + }; } const discountPercentage = parseFloat(coupon.discountPercentage ?? "0"); if (isNaN(discountPercentage)) { - return { success: false, message: "Invalid discount percentage format" }; + return { + basePrice, + discountAmount, + finalPrice, + success: false, + message: "Invalid discount percentage format", + }; } discountAmount = basePrice * (discountPercentage / 100); finalPrice = Math.floor(basePrice - discountAmount);