hotfix: throw on empty carts

This commit is contained in:
Sebastian Rindom
2020-11-05 10:19:28 +01:00
parent c5fdeab27b
commit 53124349ef
4 changed files with 15 additions and 2 deletions
+1 -1
View File
@@ -258,7 +258,7 @@ export const carts = {
completeCart: { completeCart: {
_id: IdMap.getId("complete-cart"), _id: IdMap.getId("complete-cart"),
region_id: IdMap.getId("region-france"), region_id: IdMap.getId("region-france"),
items: [], items: [{ data: "items" }],
email: "test", email: "test",
payment_sessions: [ payment_sessions: [
{ {
@@ -821,6 +821,7 @@ describe("CartService", () => {
}, },
{ {
$set: { $set: {
items: [],
region_id: IdMap.getId("region-us"), region_id: IdMap.getId("region-us"),
shipping_methods: [], shipping_methods: [],
payment_sessions: [], payment_sessions: [],
@@ -56,6 +56,11 @@ describe("OrderService", () => {
jest.clearAllMocks() jest.clearAllMocks()
}) })
it("fails when no items", async () => {
const res = orderService.createFromCart(carts.emptyCart)
expect(res).rejects.toThrow("Cannot create order from empty cart")
})
it("calls order model functions", async () => { it("calls order model functions", async () => {
await orderService.createFromCart({ await orderService.createFromCart({
...carts.completeCart, ...carts.completeCart,
+8 -1
View File
@@ -287,6 +287,13 @@ class OrderService extends BaseService {
// Create DB session for transaction // Create DB session for transaction
const dbSession = await this.orderModel_.startSession() const dbSession = await this.orderModel_.startSession()
if (cart.items.length === 0) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Cannot create order from empty cart"
)
}
// Initialize DB transaction // Initialize DB transaction
return dbSession return dbSession
.withTransaction(async () => { .withTransaction(async () => {
@@ -308,7 +315,7 @@ class OrderService extends BaseService {
// Would be the case if a discount code is applied that covers the item // Would be the case if a discount code is applied that covers the item
// total // total
if (total !== 0 && cart.items.length > 0) { if (total !== 0) {
// Throw if payment method does not exist // Throw if payment method does not exist
if (!payment_method) { if (!payment_method) {
throw new MedusaError( throw new MedusaError(