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

View File

@@ -258,7 +258,7 @@ export const carts = {
completeCart: {
_id: IdMap.getId("complete-cart"),
region_id: IdMap.getId("region-france"),
items: [],
items: [{ data: "items" }],
email: "test",
payment_sessions: [
{

View File

@@ -821,6 +821,7 @@ describe("CartService", () => {
},
{
$set: {
items: [],
region_id: IdMap.getId("region-us"),
shipping_methods: [],
payment_sessions: [],

View File

@@ -56,6 +56,11 @@ describe("OrderService", () => {
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 () => {
await orderService.createFromCart({
...carts.completeCart,

View File

@@ -287,6 +287,13 @@ class OrderService extends BaseService {
// Create DB session for transaction
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
return dbSession
.withTransaction(async () => {
@@ -308,7 +315,7 @@ class OrderService extends BaseService {
// Would be the case if a discount code is applied that covers the item
// total
if (total !== 0 && cart.items.length > 0) {
if (total !== 0) {
// Throw if payment method does not exist
if (!payment_method) {
throw new MedusaError(