Skip to content

Commit

Permalink
Extract discount selection into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
bespoyasov committed Jan 21, 2022
1 parent 832b7d8 commit 822a367
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@ function totalPrice(products) {
return products.reduce((tally, { price, count }) => tally + price * count, 0);
}

export async function makePurchase({ user, cart, coupon }) {
if (!cart.products.length) throw new Error("The cart is empty.");
if (user.account < totalPrice(cart.products)) {
throw new Error("Not enough money.");
}

const _userId = user.name;
const products = cart.products;
const total = totalPrice(products);

function selectDiscount(total, coupon) {
let discount = 0;
switch (coupon) {
case "HAPPY_MONDAY":
Expand All @@ -25,6 +16,19 @@ export async function makePurchase({ user, cart, coupon }) {
default:
discount = 0;
}
return discount;
}

export async function makePurchase({ user, cart, coupon }) {
if (!cart.products.length) throw new Error("The cart is empty.");
if (user.account < totalPrice(cart.products)) {
throw new Error("Not enough money.");
}

const _userId = user.name;
const products = cart.products;
const total = totalPrice(products);
const discount = selectDiscount(total, coupon);

const order = {
user: _userId,
Expand Down

0 comments on commit 822a367

Please sign in to comment.