fix(medusa,product): fix ordering product categories (#13487)
CLOSES CORE-1191 cc @SteelRazor47
This commit is contained in:
6
.changeset/pretty-items-shout.md
Normal file
6
.changeset/pretty-items-shout.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@medusajs/product": patch
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
fix(medusa,product): fix ordering product categories
|
||||
@@ -717,6 +717,28 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("gets categories sorted by name", async () => {
|
||||
const response = await api.get(
|
||||
`/admin/product-categories?order=name`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const names = response.data.product_categories.map(pc => pc.name)
|
||||
const sortedNames = [...names].sort((a: string, b: string) => a.localeCompare(b))
|
||||
expect(names).toEqual(sortedNames)
|
||||
})
|
||||
|
||||
it("gets categories sorted by name descending", async () => {
|
||||
const response = await api.get(
|
||||
`/admin/product-categories?order=-name`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const names = response.data.product_categories.map(pc => pc.name)
|
||||
const sortedNames = [...names].sort((a: string, b: string) => b.localeCompare(a))
|
||||
expect(names).toEqual(sortedNames)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/product-categories", () => {
|
||||
|
||||
@@ -29,15 +29,8 @@ import {
|
||||
IRegionModuleService,
|
||||
ISalesChannelModuleService,
|
||||
IStockLocationService,
|
||||
PricingContext,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
Modules,
|
||||
PriceListStatus,
|
||||
PriceListType,
|
||||
RuleOperator,
|
||||
} from "@medusajs/utils"
|
||||
import { ContainerRegistrationKeys, Modules, PriceListStatus, PriceListType, RuleOperator, } from "@medusajs/utils"
|
||||
import {
|
||||
adminHeaders,
|
||||
createAdminUser,
|
||||
|
||||
@@ -2,10 +2,7 @@ import {
|
||||
StoreProductCategoryListParams,
|
||||
StoreProductCategoryListResponse,
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -15,23 +12,19 @@ export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<StoreProductCategoryListParams>,
|
||||
res: MedusaResponse<StoreProductCategoryListResponse>
|
||||
) => {
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "product_category",
|
||||
variables: {
|
||||
filters: req.filterableFields,
|
||||
...req.queryConfig.pagination,
|
||||
},
|
||||
const { data: product_categories, metadata } = await query.graph({
|
||||
entity: "product_category",
|
||||
fields: req.queryConfig.fields,
|
||||
filters: req.filterableFields,
|
||||
pagination: req.queryConfig.pagination,
|
||||
})
|
||||
|
||||
const { rows: product_categories, metadata } = await remoteQuery(queryObject)
|
||||
|
||||
res.json({
|
||||
product_categories,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
count: metadata!.count,
|
||||
offset: metadata!.skip,
|
||||
limit: metadata!.take,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
ProductTypes,
|
||||
} from "@medusajs/framework/types"
|
||||
import { DALUtils, isDefined, MedusaError } from "@medusajs/framework/utils"
|
||||
import { LoadStrategy, FindOptions as MikroOptions } from "@mikro-orm/core"
|
||||
import { FindOptions as MikroOptions, LoadStrategy } from "@mikro-orm/core"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { ProductCategory } from "@models"
|
||||
import { UpdateCategoryInput } from "@types"
|
||||
@@ -21,10 +21,9 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
) {
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
findOptions_.options.orderBy = {
|
||||
findOptions_.options.orderBy ??= {
|
||||
id: "ASC",
|
||||
rank: "ASC",
|
||||
...findOptions_.options.orderBy,
|
||||
}
|
||||
|
||||
const fields = (findOptions_.options.fields ??= [])
|
||||
|
||||
Reference in New Issue
Block a user