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", () => {
|
describe("POST /admin/product-categories", () => {
|
||||||
|
|||||||
@@ -29,15 +29,8 @@ import {
|
|||||||
IRegionModuleService,
|
IRegionModuleService,
|
||||||
ISalesChannelModuleService,
|
ISalesChannelModuleService,
|
||||||
IStockLocationService,
|
IStockLocationService,
|
||||||
PricingContext,
|
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import {
|
import { ContainerRegistrationKeys, Modules, PriceListStatus, PriceListType, RuleOperator, } from "@medusajs/utils"
|
||||||
ContainerRegistrationKeys,
|
|
||||||
Modules,
|
|
||||||
PriceListStatus,
|
|
||||||
PriceListType,
|
|
||||||
RuleOperator,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import {
|
import {
|
||||||
adminHeaders,
|
adminHeaders,
|
||||||
createAdminUser,
|
createAdminUser,
|
||||||
|
|||||||
@@ -2,10 +2,7 @@ import {
|
|||||||
StoreProductCategoryListParams,
|
StoreProductCategoryListParams,
|
||||||
StoreProductCategoryListResponse,
|
StoreProductCategoryListResponse,
|
||||||
} from "@medusajs/framework/types"
|
} from "@medusajs/framework/types"
|
||||||
import {
|
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/framework/utils"
|
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
@@ -15,23 +12,19 @@ export const GET = async (
|
|||||||
req: AuthenticatedMedusaRequest<StoreProductCategoryListParams>,
|
req: AuthenticatedMedusaRequest<StoreProductCategoryListParams>,
|
||||||
res: MedusaResponse<StoreProductCategoryListResponse>
|
res: MedusaResponse<StoreProductCategoryListResponse>
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
const { data: product_categories, metadata } = await query.graph({
|
||||||
entryPoint: "product_category",
|
entity: "product_category",
|
||||||
variables: {
|
|
||||||
filters: req.filterableFields,
|
|
||||||
...req.queryConfig.pagination,
|
|
||||||
},
|
|
||||||
fields: req.queryConfig.fields,
|
fields: req.queryConfig.fields,
|
||||||
|
filters: req.filterableFields,
|
||||||
|
pagination: req.queryConfig.pagination,
|
||||||
})
|
})
|
||||||
|
|
||||||
const { rows: product_categories, metadata } = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
product_categories,
|
product_categories,
|
||||||
count: metadata.count,
|
count: metadata!.count,
|
||||||
offset: metadata.skip,
|
offset: metadata!.skip,
|
||||||
limit: metadata.take,
|
limit: metadata!.take,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
ProductTypes,
|
ProductTypes,
|
||||||
} from "@medusajs/framework/types"
|
} from "@medusajs/framework/types"
|
||||||
import { DALUtils, isDefined, MedusaError } from "@medusajs/framework/utils"
|
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 { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||||
import { ProductCategory } from "@models"
|
import { ProductCategory } from "@models"
|
||||||
import { UpdateCategoryInput } from "@types"
|
import { UpdateCategoryInput } from "@types"
|
||||||
@@ -21,10 +21,9 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
|||||||
) {
|
) {
|
||||||
const findOptions_ = { ...findOptions }
|
const findOptions_ = { ...findOptions }
|
||||||
findOptions_.options ??= {}
|
findOptions_.options ??= {}
|
||||||
findOptions_.options.orderBy = {
|
findOptions_.options.orderBy ??= {
|
||||||
id: "ASC",
|
id: "ASC",
|
||||||
rank: "ASC",
|
rank: "ASC",
|
||||||
...findOptions_.options.orderBy,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const fields = (findOptions_.options.fields ??= [])
|
const fields = (findOptions_.options.fields ??= [])
|
||||||
|
|||||||
Reference in New Issue
Block a user