Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(medusa): Add allowed relations to order retrieval #2370

Merged
merged 5 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions integration-tests/api/__tests__/admin/order/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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"
)
}
})
})
})
6 changes: 4 additions & 2 deletions packages/medusa/src/api/routes/admin/orders/index.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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)
Expand All @@ -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)
Expand Down