hotfix: throw on empty carts
This commit is contained in:
@@ -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: [
|
||||
{
|
||||
|
||||
@@ -821,6 +821,7 @@ describe("CartService", () => {
|
||||
},
|
||||
{
|
||||
$set: {
|
||||
items: [],
|
||||
region_id: IdMap.getId("region-us"),
|
||||
shipping_methods: [],
|
||||
payment_sessions: [],
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user