feat(medusa): Allow empty fields/expand (#3220)
This commit is contained in:
committed by
GitHub
parent
61b0b2f3aa
commit
eee9283818
5
.changeset/chilly-grapes-play.md
Normal file
5
.changeset/chilly-grapes-play.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
feat(medusa): Allow empty fields/expand
|
||||
@@ -1545,6 +1545,30 @@ describe("/admin/orders", () => {
|
||||
)
|
||||
})
|
||||
|
||||
it("lists orders with specific fields and relations", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
"/admin/orders?fields=id,created_at&expand=billing_address",
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.orders).toHaveLength(6)
|
||||
expect(response.data.orders).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
id: "test-order",
|
||||
created_at: expect.any(String),
|
||||
billing_address: expect.objectContaining({
|
||||
id: "test-billing-address",
|
||||
first_name: "lebron",
|
||||
}),
|
||||
},
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("lists all orders with a fulfillment status = fulfilled and payment status = captured", async () => {
|
||||
const api = useApi()
|
||||
|
||||
|
||||
@@ -17,14 +17,14 @@ import {
|
||||
PaymentStatus,
|
||||
Return,
|
||||
Swap,
|
||||
TrackingLink
|
||||
TrackingLink,
|
||||
} from "../models"
|
||||
import { AddressRepository } from "../repositories/address"
|
||||
import { OrderRepository } from "../repositories/order"
|
||||
import { FindConfig, QuerySelector, Selector } from "../types/common"
|
||||
import {
|
||||
CreateFulfillmentOrder,
|
||||
FulFillmentItemType
|
||||
FulFillmentItemType,
|
||||
} from "../types/fulfillment"
|
||||
import { UpdateOrderInput } from "../types/orders"
|
||||
import { CreateShippingMethodDto } from "../types/shipping-options"
|
||||
@@ -48,7 +48,7 @@ import {
|
||||
ShippingOptionService,
|
||||
ShippingProfileService,
|
||||
TaxProviderService,
|
||||
TotalsService
|
||||
TotalsService,
|
||||
} from "."
|
||||
|
||||
export const ORDER_CART_ALREADY_EXISTS_ERROR = "Order from cart already exists"
|
||||
@@ -322,7 +322,7 @@ class OrderService extends TransactionBaseService {
|
||||
select = select.filter((v) => !totalFields.includes(v))
|
||||
}
|
||||
|
||||
const toSelect = select
|
||||
const toSelect = [...select]
|
||||
if (toSelect.length > 0 && toSelect.indexOf("tax_rate") === -1) {
|
||||
toSelect.push("tax_rate")
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ export function getRetrieveConfig<TModel extends BaseEntity>(
|
||||
|
||||
return {
|
||||
select: includeFields.length ? includeFields : defaultFields,
|
||||
relations: expandFields.length ? expandFields : defaultRelations,
|
||||
relations: isDefined(expand) ? expandFields : defaultRelations,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ export function getListConfig<TModel extends BaseEntity>(
|
||||
|
||||
return {
|
||||
select: includeFields.length ? includeFields : defaultFields,
|
||||
relations: expandFields.length ? expandFields : defaultRelations,
|
||||
relations: isDefined(expand) ? expandFields : defaultRelations,
|
||||
skip: offset,
|
||||
take: limit,
|
||||
order: orderBy,
|
||||
@@ -88,13 +88,13 @@ export function prepareListQuery<
|
||||
const { order, fields, expand, limit, offset } = validated
|
||||
|
||||
let expandRelations: string[] | undefined = undefined
|
||||
if (expand) {
|
||||
expandRelations = expand.split(",")
|
||||
if (isDefined(expand)) {
|
||||
expandRelations = expand.split(",").filter((v) => v)
|
||||
}
|
||||
|
||||
let expandFields: (keyof TEntity)[] | undefined = undefined
|
||||
if (fields) {
|
||||
expandFields = fields.split(",") as (keyof TEntity)[]
|
||||
if (isDefined(fields)) {
|
||||
expandFields = (fields.split(",") as (keyof TEntity)[]).filter((v) => v)
|
||||
}
|
||||
|
||||
if (expandFields?.length && queryConfig?.allowedFields?.length) {
|
||||
@@ -144,14 +144,14 @@ export function prepareRetrieveQuery<
|
||||
>(validated: T, queryConfig?: QueryConfig<TEntity>) {
|
||||
const { fields, expand } = validated
|
||||
|
||||
let expandRelations: string[] = []
|
||||
if (expand) {
|
||||
expandRelations = expand.split(",")
|
||||
let expandRelations: string[] | undefined = undefined
|
||||
if (isDefined(expand)) {
|
||||
expandRelations = expand.split(",").filter((v) => v)
|
||||
}
|
||||
|
||||
let expandFields: (keyof TEntity)[] | undefined = undefined
|
||||
if (fields) {
|
||||
expandFields = fields.split(",") as (keyof TEntity)[]
|
||||
if (isDefined(fields)) {
|
||||
expandFields = (fields.split(",") as (keyof TEntity)[]).filter((v) => v)
|
||||
}
|
||||
|
||||
if (expandFields?.length && queryConfig?.allowedFields?.length) {
|
||||
|
||||
Reference in New Issue
Block a user