Skip to content

Commit

Permalink
feat(medusa): skip draft orders on order list (#10195)
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan authored Nov 21, 2024
1 parent d91da7b commit 2a8811e
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 7 deletions.
9 changes: 8 additions & 1 deletion integration-tests/http/__tests__/fixtures/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,27 @@ import {
export async function createOrderSeeder({
api,
container,
storeHeaderOverride,
productOverride,
additionalProducts,
stockChannelOverride,
inventoryItemOverride,
}: {
api: any
container: MedusaContainer
storeHeaderOverride?: any
productOverride?: AdminProduct
stockChannelOverride?: AdminStockLocation
additionalProducts?: { variant_id: string; quantity: number }[]
inventoryItemOverride?: AdminInventoryItem
}) {
const publishableKey = await generatePublishableKey(container)
const storeHeaders = generateStoreHeaders({ publishableKey })

const storeHeaders =
storeHeaderOverride ??
generateStoreHeaders({
publishableKey,
})

const region = (
await api.post(
Expand Down
138 changes: 138 additions & 0 deletions integration-tests/http/__tests__/order/store/order.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { ModuleRegistrationName } from "@medusajs/utils"
import {
adminHeaders,
createAdminUser,
generatePublishableKey,
generateStoreHeaders,
} from "../../../../helpers/create-admin-user"
import { setupTaxStructure } from "../../../../modules/__tests__/fixtures"
import { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer"
import { createOrderSeeder } from "../../fixtures/order"

jest.setTimeout(300000)

medusaIntegrationTestRunner({
testSuite: ({ dbConnection, getContainer, api }) => {
let order,
draftOrder,
seeder,
storeHeaders,
customer,
storeHeadersWithCustomer

beforeEach(async () => {
const container = getContainer()

await setupTaxStructure(container.resolve(ModuleRegistrationName.TAX))
await createAdminUser(dbConnection, adminHeaders, container)
const publishableKey = await generatePublishableKey(container)
storeHeaders = generateStoreHeaders({ publishableKey })

const result = await createAuthenticatedCustomer(api, storeHeaders, {
first_name: "tony",
last_name: "stark",
email: "[email protected]",
})

customer = result.customer
storeHeadersWithCustomer = {
headers: {
...storeHeaders.headers,
authorization: `Bearer ${result.jwt}`,
},
}

seeder = await createOrderSeeder({
api,
container: getContainer(),
storeHeaderOverride: storeHeadersWithCustomer,
})
order = seeder.order
order = (await api.get(`/admin/orders/${order.id}`, adminHeaders)).data
.order

const payload = {
email: "[email protected]",
region_id: seeder.region.id,
status: "completed",
shipping_methods: [
{
shipping_option_id: seeder.shippingOption.id,
amount: 10,
name: "test",
},
],
}

const draftOrderResposne = await api.post(
"/admin/draft-orders?fields=+is_draft_order",
payload,
adminHeaders
)

await api.post(
`/admin/orders/${draftOrderResposne.data.draft_order.id}/complete`,
{},
adminHeaders
)

draftOrder = draftOrderResposne.data.draft_order
})

describe("GET /store/orders", () => {
it("should successfully list non draft orders", async () => {
const response = await api.get(
`/store/orders`,
storeHeadersWithCustomer
)

expect(response.data.orders).toHaveLength(1)
expect(response.data.orders).toEqual([
expect.objectContaining({
id: order.id,
}),
])
})

it("should throw an error when customer isn't authenticated", async () => {
const { response } = await api
.get(`/store/orders`, storeHeaders)
.catch((e) => e)

expect(response.status).toEqual(401)
expect(response.data.message).toEqual("Unauthorized")
})
})

describe("GET /store/orders/:id", () => {
it("should successfully fetch non draft order", async () => {
const response = await api.get(
`/store/orders/${order.id}`,
storeHeadersWithCustomer
)

expect(response.data.order).toEqual(
expect.objectContaining({
id: order.id,
})
)
})

// TODO: This should have thrown an error, but doesn't seem to.
it.skip("should throw an error when fetching draft order", async () => {
const response = await api
.get(
`/store/orders/${draftOrder.id}?fields=+is_draft_order`,
storeHeaders
)
.catch((e) => e)

expect(response.status).toEqual(404)
expect(response.data.message).toEqual(
`Order with id: ${draftOrder.id} was not found`
)
})
})
},
})
10 changes: 6 additions & 4 deletions packages/core/core-flows/src/order/workflows/get-order-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const getOrderDetailWorkflow = createWorkflow(
getOrderDetailWorkflowId,
(
input: WorkflowData<{
filters?: { is_draft_order?: boolean; customer_id?: string }
fields: string[]
order_id: string
version?: number
Expand All @@ -36,13 +37,14 @@ export const getOrderDetailWorkflow = createWorkflow(
])
})

const variables = transform({ input }, ({ input }) => {
return { ...input.filters, id: input.order_id, version: input.version }
})

const order: OrderDTO = useRemoteQueryStep({
entry_point: "orders",
fields,
variables: {
id: input.order_id,
version: input.version,
},
variables,
list: false,
throw_if_key_not_found: true,
})
Expand Down
3 changes: 3 additions & 0 deletions packages/medusa/src/api/store/orders/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export const GET = async (
input: {
fields: req.remoteQueryConfig.fields,
order_id: req.params.id,
filters: {
is_draft_order: false,
},
},
})

Expand Down
4 changes: 2 additions & 2 deletions packages/medusa/src/api/store/orders/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { validateAndTransformQuery } from "@medusajs/framework"
import {
MiddlewareRoute,
validateAndTransformBody,
} from "@medusajs/framework/http"
import { authenticate } from "../../../utils/middlewares/authenticate-middleware"
import { validateAndTransformQuery } from "@medusajs/framework"
import * as QueryConfig from "./query-config"
import {
StoreAcceptOrderTransfer,
StoreGetOrderParams,
StoreGetOrdersParams,
StoreAcceptOrderTransfer,
StoreRequestOrderTransfer,
} from "./validators"

Expand Down
1 change: 1 addition & 0 deletions packages/medusa/src/api/store/orders/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const GET = async (
const variables = {
filters: {
...req.filterableFields,
is_draft_order: false,
customer_id: req.auth_context.actor_id,
},
...req.remoteQueryConfig.pagination,
Expand Down

0 comments on commit 2a8811e

Please sign in to comment.