feat(medusa): Allow creating DraftOrders without items (#2719)
This commit is contained in:
5
.changeset/good-needles-shave.md
Normal file
5
.changeset/good-needles-shave.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
feat(medusa): Create draft orders without items
|
||||
@@ -381,6 +381,30 @@ describe("/admin/draft-orders", () => {
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("creates a draft order without a single item", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const payload = {
|
||||
email: "oli@test.dk",
|
||||
shipping_address: "oli-shipping",
|
||||
region_id: "test-region",
|
||||
customer_id: "oli-test",
|
||||
shipping_methods: [
|
||||
{
|
||||
option_id: "test-option",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const response = await api.post(
|
||||
"/admin/draft-orders",
|
||||
payload,
|
||||
adminReqConfig
|
||||
)
|
||||
expect(response.data.draft_order.cart.items).toEqual([])
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("creates a draft order with product variant with custom price and custom item price set to 0", async () => {
|
||||
const api = useApi()
|
||||
|
||||
|
||||
@@ -39,7 +39,6 @@ import { IsType } from "../../../../utils/validators/is-type"
|
||||
* type: object
|
||||
* required:
|
||||
* - email
|
||||
* - items
|
||||
* - region_id
|
||||
* - shipping_methods
|
||||
* properties:
|
||||
@@ -270,7 +269,8 @@ export class AdminPostDraftOrdersReq {
|
||||
@Type(() => Item)
|
||||
@IsNotEmpty()
|
||||
@ValidateNested({ each: true })
|
||||
items: Item[]
|
||||
@IsOptional()
|
||||
items?: Item[]
|
||||
|
||||
@IsString()
|
||||
region_id: string
|
||||
|
||||
@@ -209,17 +209,12 @@ describe("DraftOrderService", () => {
|
||||
}
|
||||
})
|
||||
|
||||
it("fails on missing items", async () => {
|
||||
try {
|
||||
await draftOrderService.create({
|
||||
region_id: "test-region",
|
||||
items: [],
|
||||
})
|
||||
} catch (error) {
|
||||
expect(error.message).toEqual(
|
||||
`Items are required to create a draft order`
|
||||
)
|
||||
}
|
||||
it("creating a draft order without items is allowed", async () => {
|
||||
await draftOrderService.create({
|
||||
region_id: "test-region",
|
||||
items: [],
|
||||
shipping_methods: []
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -259,13 +259,6 @@ class DraftOrderService extends TransactionBaseService {
|
||||
)
|
||||
}
|
||||
|
||||
if (!data.items || !data.items.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Items are required to create a draft order`
|
||||
)
|
||||
}
|
||||
|
||||
const {
|
||||
shipping_methods,
|
||||
no_notification_order,
|
||||
@@ -310,7 +303,7 @@ class DraftOrderService extends TransactionBaseService {
|
||||
const lineItemServiceTx =
|
||||
this.lineItemService_.withTransaction(transactionManager)
|
||||
|
||||
for (const item of items) {
|
||||
for (const item of (items || [])) {
|
||||
if (item.variant_id) {
|
||||
const line = await lineItemServiceTx.generate(
|
||||
item.variant_id,
|
||||
|
||||
@@ -9,7 +9,7 @@ export type DraftOrderCreateProps = {
|
||||
billing_address?: Partial<AddressPayload>
|
||||
shipping_address_id?: string
|
||||
shipping_address?: Partial<AddressPayload>
|
||||
items: Item[]
|
||||
items?: Item[]
|
||||
region_id: string
|
||||
discounts?: Discount[]
|
||||
customer_id?: string
|
||||
|
||||
Reference in New Issue
Block a user