Last active
December 16, 2020 15:49
-
-
Save radzserg/7b35f283e8a2d6e5304417b909b6907a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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