feat(medusa): Allow empty fields/expand (#3220)

This commit is contained in:
Adrien de Peretti
2023-02-09 18:05:56 +01:00
committed by GitHub
parent 61b0b2f3aa
commit eee9283818
4 changed files with 44 additions and 15 deletions
+5
View 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 () => { it("lists all orders with a fulfillment status = fulfilled and payment status = captured", async () => {
const api = useApi() const api = useApi()
+4 -4
View File
@@ -17,14 +17,14 @@ import {
PaymentStatus, PaymentStatus,
Return, Return,
Swap, Swap,
TrackingLink TrackingLink,
} from "../models" } from "../models"
import { AddressRepository } from "../repositories/address" import { AddressRepository } from "../repositories/address"
import { OrderRepository } from "../repositories/order" import { OrderRepository } from "../repositories/order"
import { FindConfig, QuerySelector, Selector } from "../types/common" import { FindConfig, QuerySelector, Selector } from "../types/common"
import { import {
CreateFulfillmentOrder, CreateFulfillmentOrder,
FulFillmentItemType FulFillmentItemType,
} from "../types/fulfillment" } from "../types/fulfillment"
import { UpdateOrderInput } from "../types/orders" import { UpdateOrderInput } from "../types/orders"
import { CreateShippingMethodDto } from "../types/shipping-options" import { CreateShippingMethodDto } from "../types/shipping-options"
@@ -48,7 +48,7 @@ import {
ShippingOptionService, ShippingOptionService,
ShippingProfileService, ShippingProfileService,
TaxProviderService, TaxProviderService,
TotalsService TotalsService,
} from "." } from "."
export const ORDER_CART_ALREADY_EXISTS_ERROR = "Order from cart already exists" 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)) select = select.filter((v) => !totalFields.includes(v))
} }
const toSelect = select const toSelect = [...select]
if (toSelect.length > 0 && toSelect.indexOf("tax_rate") === -1) { if (toSelect.length > 0 && toSelect.indexOf("tax_rate") === -1) {
toSelect.push("tax_rate") toSelect.push("tax_rate")
} }
+11 -11
View File
@@ -39,7 +39,7 @@ export function getRetrieveConfig<TModel extends BaseEntity>(
return { return {
select: includeFields.length ? includeFields : defaultFields, 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 { return {
select: includeFields.length ? includeFields : defaultFields, select: includeFields.length ? includeFields : defaultFields,
relations: expandFields.length ? expandFields : defaultRelations, relations: isDefined(expand) ? expandFields : defaultRelations,
skip: offset, skip: offset,
take: limit, take: limit,
order: orderBy, order: orderBy,
@@ -88,13 +88,13 @@ export function prepareListQuery<
const { order, fields, expand, limit, offset } = validated const { order, fields, expand, limit, offset } = validated
let expandRelations: string[] | undefined = undefined let expandRelations: string[] | undefined = undefined
if (expand) { if (isDefined(expand)) {
expandRelations = expand.split(",") expandRelations = expand.split(",").filter((v) => v)
} }
let expandFields: (keyof TEntity)[] | undefined = undefined let expandFields: (keyof TEntity)[] | undefined = undefined
if (fields) { if (isDefined(fields)) {
expandFields = fields.split(",") as (keyof TEntity)[] expandFields = (fields.split(",") as (keyof TEntity)[]).filter((v) => v)
} }
if (expandFields?.length && queryConfig?.allowedFields?.length) { if (expandFields?.length && queryConfig?.allowedFields?.length) {
@@ -144,14 +144,14 @@ export function prepareRetrieveQuery<
>(validated: T, queryConfig?: QueryConfig<TEntity>) { >(validated: T, queryConfig?: QueryConfig<TEntity>) {
const { fields, expand } = validated const { fields, expand } = validated
let expandRelations: string[] = [] let expandRelations: string[] | undefined = undefined
if (expand) { if (isDefined(expand)) {
expandRelations = expand.split(",") expandRelations = expand.split(",").filter((v) => v)
} }
let expandFields: (keyof TEntity)[] | undefined = undefined let expandFields: (keyof TEntity)[] | undefined = undefined
if (fields) { if (isDefined(fields)) {
expandFields = fields.split(",") as (keyof TEntity)[] expandFields = (fields.split(",") as (keyof TEntity)[]).filter((v) => v)
} }
if (expandFields?.length && queryConfig?.allowedFields?.length) { if (expandFields?.length && queryConfig?.allowedFields?.length) {