Skip to content

Commit

Permalink
fix: ensuring return basePrice, discountAmount, and finalPrice on error
Browse files Browse the repository at this point in the history
  • Loading branch information
joywin2003 committed Nov 19, 2024
1 parent a0409f4 commit 500a831
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/app/actions/get-price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 500a831

Please sign in to comment.