Skip to content

Instantly share code, notes, and snippets.

@radzserg
Last active December 16, 2020 15:49
Show Gist options
  • Select an option

  • Save radzserg/7b35f283e8a2d6e5304417b909b6907a to your computer and use it in GitHub Desktop.

Select an option

Save radzserg/7b35f283e8a2d6e5304417b909b6907a to your computer and use it in GitHub Desktop.
class OrderManager {
public async createOrder(orderData) {
if (!orderData.items.length) {
throw new ValidationError("Cannot create an order: cart is empty");
}
orderData.items.forEach((orderItem) => {
if (orderItem.productId === undefined || orderItem.quantity === undefined || orderItem.price === undefined) {
throw new ValidationError("Cannot create an order: product ID is missing");
}
})
let totalPrice = orderData.items.reduce( (totalPrice, orderItem) => totalPrice + (orderItem.price * orderItem.quantity));
totalPrice = totalPrice + totalPrice * 0.05; // add service commission
const paymentGateway = new MyPaymentGateway(process.env.STRIPE_KEY);
paymentGateway.pay(totalPrice, orderData.paymentInfo);
const order = new Order(orderData);
await orderRepository.save(orderData);
return order;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment