Created
October 22, 2021 07:42
-
-
Save Pyrolistical/f9649bae67c832fff6f89d2f0053ab02 to your computer and use it in GitHub Desktop.
ecommerce-escalation v1
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
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