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): order retrieval missing relations + allow for expand #2267

Merged
merged 3 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
58 changes: 55 additions & 3 deletions integration-tests/api/__tests__/admin/order/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ describe("/admin/orders", () => {
const manager = dbConnection.manager

await manager.query(
`UPDATE "product_variant" SET manage_inventory=false WHERE id = 'test-variant'`
`UPDATE "product_variant"
SET manage_inventory= false
WHERE id = 'test-variant'`
)

const initialInventoryRes = await api.get("/store/variants/test-variant")
Expand Down Expand Up @@ -1346,7 +1348,9 @@ describe("/admin/orders", () => {
const manager = dbConnection.manager

await manager.query(
`UPDATE "product_variant" SET manage_inventory=false WHERE id = 'test-variant'`
`UPDATE "product_variant"
SET manage_inventory= false
WHERE id = 'test-variant'`
)

const returned = await api.post(
Expand Down Expand Up @@ -1955,7 +1959,7 @@ describe("/admin/orders", () => {
}
)

// find item to test returned quantiy for
// find item to test returned quantity for
const toTest = returnedOrderSecond.data.order.items.find(
(i) => i.id === "test-item-many"
)
Expand Down Expand Up @@ -2057,6 +2061,54 @@ describe("/admin/orders", () => {
expect(received.status).toEqual(200)
})

it("creates a swap with return and return shipping", async () => {
const api = useApi()

const response = await api.post(
"/admin/orders/test-order/swaps",
{
return_items: [
{
item_id: "test-item",
quantity: 1,
},
],
return_shipping: { option_id: "test-return-option", price: 0 },
},
{
headers: {
authorization: "Bearer test_token",
},
}
)

expect(response.status).toEqual(200)

const swap = response.data.order.swaps[0]
expect(swap.return_order.items).toHaveLength(1)
expect(swap.return_order.items[0]).toEqual(
expect.objectContaining({
item_id: "test-item",
quantity: 1,
})
)

expect(swap.return_order.shipping_method).toEqual(
expect.objectContaining({
price: 0,
shipping_option_id: "test-return-option",
})
)

expect(swap.return_order.shipping_method.tax_lines).toHaveLength(1)
expect(swap.return_order.shipping_method.tax_lines[0]).toEqual(
expect.objectContaining({
rate: 0,
name: "default",
})
)
})

it("creates a return on a swap", async () => {
const api = useApi()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,77 +1,7 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { OrderServiceMock } from "../../../../../services/__mocks__/order"

const defaultRelations = [
"customer",
"billing_address",
"shipping_address",
"discounts",
"discounts.rule",
"shipping_methods",
"payments",
"fulfillments",
"fulfillments.tracking_links",
"fulfillments.items",
"returns",
"returns.shipping_method",
"returns.shipping_method.tax_lines",
"returns.items",
"returns.items.reason",
"gift_cards",
"gift_card_transactions",
"claims",
"claims.return_order",
"claims.return_order.shipping_method",
"claims.shipping_methods",
"claims.shipping_address",
"claims.additional_items",
"claims.fulfillments",
"claims.fulfillments.tracking_links",
"claims.claim_items",
"claims.claim_items.item",
"claims.claim_items.images",
"swaps",
"swaps.return_order",
"swaps.payment",
"swaps.shipping_methods",
"swaps.shipping_address",
"swaps.additional_items",
"swaps.fulfillments",
"swaps.fulfillments.tracking_links",
]

const defaultFields = [
"id",
"status",
"fulfillment_status",
"payment_status",
"display_id",
"cart_id",
"draft_order_id",
"customer_id",
"email",
"region_id",
"currency_code",
"tax_rate",
"canceled_at",
"created_at",
"updated_at",
"metadata",
"items.refundable",
"swaps.additional_items.refundable",
"claims.additional_items.refundable",
"shipping_total",
"discount_total",
"tax_total",
"refunded_total",
"gift_card_total",
"subtotal",
"total",
"paid_total",
"refundable_amount",
"no_notification",
]
import { defaultAdminOrdersFields, defaultAdminOrdersRelations } from "../index"

describe("GET /admin/orders", () => {
describe("successfully gets an order", () => {
Expand Down Expand Up @@ -100,8 +30,8 @@ describe("GET /admin/orders", () => {
expect(OrderServiceMock.retrieve).toHaveBeenCalledWith(
IdMap.getId("test-order"),
{
select: defaultFields,
relations: defaultRelations,
select: defaultAdminOrdersFields,
relations: defaultAdminOrdersRelations,
}
)
})
Expand Down
8 changes: 6 additions & 2 deletions packages/medusa/src/api/routes/admin/orders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "reflect-metadata"
import { Order } from "../../../.."
import {
DeleteResponse,
EmptyQueryParams,
FindParams,
PaginatedResponse,
} from "../../../../types/common"
import middlewares, { transformQuery } from "../../../middlewares"
Expand Down Expand Up @@ -45,7 +45,7 @@ export default (app, featureFlagRouter: FlagRouter) => {
*/
route.get(
"/:id",
transformQuery(EmptyQueryParams, {
transformQuery(FindParams, {
defaultRelations: relations,
defaultFields: defaultAdminOrdersFields,
allowedFields: allowedAdminOrdersFields,
Expand Down Expand Up @@ -260,6 +260,7 @@ export const defaultAdminOrdersRelations = [
"claims",
"claims.return_order",
"claims.return_order.shipping_method",
"claims.return_order.shipping_method.tax_lines",
"claims.shipping_methods",
"claims.shipping_address",
"claims.additional_items",
Expand All @@ -271,8 +272,11 @@ export const defaultAdminOrdersRelations = [
// "claims.claim_items.tags",
"swaps",
"swaps.return_order",
"swaps.return_order.shipping_method",
"swaps.return_order.shipping_method.tax_lines",
"swaps.payment",
"swaps.shipping_methods",
"swaps.shipping_methods.tax_lines",
"swaps.shipping_address",
"swaps.additional_items",
"swaps.fulfillments",
Expand Down
4 changes: 2 additions & 2 deletions packages/medusa/src/api/routes/admin/products/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router } from "express"
import "reflect-metadata"
import { Product, ProductTag, ProductType } from "../../../.."
import { EmptyQueryParams, PaginatedResponse } from "../../../../types/common"
import { FindParams, PaginatedResponse } from "../../../../types/common"
import { PricedProduct } from "../../../../types/pricing"
import { FlagRouter } from "../../../../utils/flag-router"
import middlewares, { transformQuery } from "../../../middlewares"
Expand Down Expand Up @@ -70,7 +70,7 @@ export default (app, featureFlagRouter: FlagRouter) => {
)
route.get(
"/:id",
transformQuery(EmptyQueryParams, {
transformQuery(FindParams, {
defaultRelations: defaultAdminProductRelations,
defaultFields: defaultAdminProductFields,
allowedFields: allowedAdminProductFields,
Expand Down
5 changes: 3 additions & 2 deletions packages/medusa/src/api/routes/store/carts/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Router } from "express"
import "reflect-metadata"
import { Cart, Order, Swap } from "../../../../"
import { DeleteResponse, EmptyQueryParams } from "../../../../types/common"
import { DeleteResponse, FindParams } from "../../../../types/common"
import middlewares, {
transformBody,
transformQuery,
} from "../../../middlewares"
import { StorePostCartsCartReq } from "./update-cart"
import { StorePostCartReq } from "./create-cart"

const route = Router()

export default (app, container) => {
Expand All @@ -28,7 +29,7 @@ export default (app, container) => {

route.get(
"/:id",
transformQuery(EmptyQueryParams, {
transformQuery(FindParams, {
defaultRelations: defaultStoreCartRelations,
defaultFields: defaultStoreCartFields,
isList: false,
Expand Down