Skip to content

Instantly share code, notes, and snippets.

@Pyrolistical
Created October 22, 2021 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pyrolistical/f9649bae67c832fff6f89d2f0053ab02 to your computer and use it in GitHub Desktop.
Save Pyrolistical/f9649bae67c832fff6f89d2f0053ab02 to your computer and use it in GitHub Desktop.
ecommerce-escalation v1
import _ from 'lodash';
export class InsufficientStock extends Error {
constructor(message) {
super(message);
this.name = 'InsufficientStock';
}
}
export default (repository) => {
return {
async checkout(lineItems, shippingAddress, paymentDetails) {
for (const {productSku, quantity} of lineItems) {
const {stock: remainingStock} = await repository.findInventoryByProductSku(productSku);
if (remainingStock < quantity) {
throw new InsufficientStock({
productSku,
remainingStock,
requestedStock: quantity
});
}
await repository.updateInventoryStockByProductSku(productSku, remainingStock - quantity);
}
const orderID = await repository.createOrder({
lineItems,
shippingAddress,
paymentDetails
});
return orderID;
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment