diff --git a/integration-tests/api/__tests__/admin/order/order.js b/integration-tests/api/__tests__/admin/order/order.js index e6b59dd21cc2d..e736e339c7638 100644 --- a/integration-tests/api/__tests__/admin/order/order.js +++ b/integration-tests/api/__tests__/admin/order/order.js @@ -1431,6 +1431,22 @@ describe("/admin/orders", () => { ) }) + it("throws on invalid relation", async () => { + const api = useApi() + + try { + await api.get("/admin/orders?fields=id&expand=variants", { + headers: { + authorization: "Bearer test_token", + }, + }) + } catch (error) { + expect(error.response.data.message).toBe( + "Relations [variants] are not valid" + ) + } + }) + it("lists all orders with a fulfillment status = fulfilled and payment status = captured", async () => { const api = useApi() @@ -2236,4 +2252,52 @@ describe("/admin/orders", () => { await expectCancelToReturn({ code: 200 }) }) }) + + describe("GET /admin/orders/:id", () => { + beforeEach(async () => { + await adminSeeder(dbConnection) + await orderSeeder(dbConnection) + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("retrieves an order with fields and expand", async () => { + const api = useApi() + + const order = await api.get( + "/admin/orders/test-order?fields=id&expand=region", + { + headers: { + authorization: "Bearer test_token", + }, + } + ) + + expect(order.status).toEqual(200) + expect(order.data.order).toEqual( + expect.objectContaining({ + id: "test-order", + }) + ) + }) + + it("throws on invalid relation", async () => { + const api = useApi() + + try { + await api.get("/admin/orders/test-order?fields=id&expand=variants", { + headers: { + authorization: "Bearer test_token", + }, + }) + } catch (error) { + expect(error.response.data.message).toBe( + "Relations [variants] are not valid" + ) + } + }) + }) }) diff --git a/packages/medusa/src/api/routes/admin/orders/index.ts b/packages/medusa/src/api/routes/admin/orders/index.ts index 073f11c8987f8..675a6b927ecdd 100644 --- a/packages/medusa/src/api/routes/admin/orders/index.ts +++ b/packages/medusa/src/api/routes/admin/orders/index.ts @@ -1,15 +1,15 @@ import { Router } from "express" import "reflect-metadata" import { Order } from "../../../.." +import SalesChannelFeatureFlag from "../../../../loaders/feature-flags/sales-channels" import { DeleteResponse, FindParams, PaginatedResponse, } from "../../../../types/common" +import { FlagRouter } from "../../../../utils/flag-router" import middlewares, { transformQuery } from "../../../middlewares" import { AdminGetOrdersParams } from "./list-orders" -import { FlagRouter } from "../../../../utils/flag-router" -import SalesChannelFeatureFlag from "../../../../loaders/feature-flags/sales-channels" const route = Router() @@ -30,6 +30,7 @@ export default (app, featureFlagRouter: FlagRouter) => { defaultRelations: relations, defaultFields: defaultAdminOrdersFields, allowedFields: allowedAdminOrdersFields, + allowedRelations: allowedAdminOrdersRelations, isList: true, }), middlewares.wrap(require("./list-orders").default) @@ -44,6 +45,7 @@ export default (app, featureFlagRouter: FlagRouter) => { defaultRelations: relations, defaultFields: defaultAdminOrdersFields, allowedFields: allowedAdminOrdersFields, + allowedRelations: allowedAdminOrdersRelations, isList: false, }), middlewares.wrap(require("./get-order").default) @@ -342,6 +344,7 @@ export const allowedAdminOrdersFields = [ export const allowedAdminOrdersRelations = [ "customer", "region", + "sales_channel", "billing_address", "shipping_address", "discounts",