From 81a3549292b44fdd05e86bfc24d4f4c08a7a4662 Mon Sep 17 00:00:00 2001 From: pitang Date: Thu, 16 Jan 2025 04:13:10 +0900 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=EC=9E=A5=EB=B0=94?= =?UTF-8?q?=EA=B5=AC=EB=8B=88=20=EB=A1=9C=EC=A7=81=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `calculateCartTotal` 함수의 역할을 세분화하여 단일 책임 원칙 준수: - `calculateTotalBeforeDiscount` 함수 추가 - `calculateTotalAfterDiscount` 함수 추가 - `applyCoupon` 함수 추가 --- src/refactoring/models/cart.ts | 50 +++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/refactoring/models/cart.ts b/src/refactoring/models/cart.ts index 4a8ca9de..7ea37c00 100644 --- a/src/refactoring/models/cart.ts +++ b/src/refactoring/models/cart.ts @@ -7,35 +7,41 @@ export const calculateRemainingStock = (cart: CartItem[], product: Product) => { return product.stock - (cartItem?.quantity || 0); }; -// 장바구니 전체 금액 및 할인 계산 -export const calculateCartTotal = (cart: CartItem[], selectedCoupon: Coupon | null) => { - // 할인 적용 전 총 금액 계산 - const totalBeforeDiscount = cart.reduce((total, item) => { - const { product, quantity } = item; - return total + product.price * quantity; +// 할인 적용 전 총 금액 계산 +export const calculateTotalBeforeDiscount = (cart: CartItem[]) => { + return cart.reduce((total, item) => { + return total + item.product.price * item.quantity; }, 0); +}; - // 할인 적용 후 총 금액 계산 - let totalAfterDiscount = cart.reduce((total, item) => { +// 할인 적용 후 총 금액 계산 +export const calculateTotalAfterDiscount = (cart: CartItem[]) => { + return cart.reduce((total, item) => { return total + calculateItemTotal(item); }, 0); +}; - // 총 할인 금액 계산 - let totalDiscount = totalBeforeDiscount - totalAfterDiscount; +// 쿠폰 적용 +export const applyCoupon = (totalAfterDiscount: number, selectedCoupon: Coupon | null): number => { + if (!selectedCoupon) return totalAfterDiscount; - // 쿠폰 적용 - if (selectedCoupon != null) { - if (selectedCoupon.discountType === 'amount') { - totalAfterDiscount = Math.max(0, totalAfterDiscount - selectedCoupon.discountValue); - } else { - totalAfterDiscount *= 1 - selectedCoupon.discountValue / 100; - } - return { - totalBeforeDiscount: Math.round(totalBeforeDiscount), - totalAfterDiscount: Math.round(totalAfterDiscount), - totalDiscount: Math.round(totalBeforeDiscount - totalAfterDiscount), - }; + if (selectedCoupon.discountType === 'amount') { + return Math.max(0, totalAfterDiscount - selectedCoupon.discountValue); + } else { + return totalAfterDiscount * (1 - selectedCoupon.discountValue / 100); } +}; + +// 장바구니 전체 금액 및 할인 계산 +export const calculateCartTotal = (cart: CartItem[], selectedCoupon: Coupon | null) => { + const totalBeforeDiscount = calculateTotalBeforeDiscount(cart); + let totalAfterDiscount = calculateTotalAfterDiscount(cart); + + // 쿠폰 적용 + totalAfterDiscount = applyCoupon(totalAfterDiscount, selectedCoupon); + + // 총 할인 금액 계산 + let totalDiscount = totalBeforeDiscount - totalAfterDiscount; return { totalBeforeDiscount: Math.round(totalBeforeDiscount),