chore: admin product list transform filter (#11821)
This commit is contained in:
1 parent
e0109540fa
commit
5cf0bf4d93
3 files changed
+30
-7
No files matched your search
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/index": patch
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
chore: transform filter before query.index
|
||||||
@@ -1,23 +1,25 @@
|
|||||||
import { createProductsWorkflow } from "@medusajs/core-flows"
|
import { createProductsWorkflow } from "@medusajs/core-flows"
|
||||||
import { AdditionalData, HttpTypes } from "@medusajs/framework/types"
|
import { featureFlagRouter } from "@medusajs/framework"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
refetchEntities,
|
refetchEntities,
|
||||||
refetchEntity,
|
refetchEntity,
|
||||||
} from "@medusajs/framework/http"
|
} from "@medusajs/framework/http"
|
||||||
import { remapKeysForProduct, remapProductResponse } from "./helpers"
|
import { AdditionalData, HttpTypes } from "@medusajs/framework/types"
|
||||||
import IndexEngineFeatureFlag from "../../../loaders/feature-flags/index-engine"
|
|
||||||
import { featureFlagRouter } from "@medusajs/framework"
|
|
||||||
import { ContainerRegistrationKeys, isPresent } from "@medusajs/framework/utils"
|
import { ContainerRegistrationKeys, isPresent } from "@medusajs/framework/utils"
|
||||||
|
import IndexEngineFeatureFlag from "../../../loaders/feature-flags/index-engine"
|
||||||
|
import { remapKeysForProduct, remapProductResponse } from "./helpers"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest<HttpTypes.AdminProductListParams>,
|
req: AuthenticatedMedusaRequest<HttpTypes.AdminProductListParams>,
|
||||||
res: MedusaResponse<HttpTypes.AdminProductListResponse>
|
res: MedusaResponse<HttpTypes.AdminProductListResponse>
|
||||||
) => {
|
) => {
|
||||||
if (featureFlagRouter.isFeatureEnabled(IndexEngineFeatureFlag.key)) {
|
if (featureFlagRouter.isFeatureEnabled(IndexEngineFeatureFlag.key)) {
|
||||||
// TODO: These filters are not supported by the index engine yet
|
// Use regular list when no filters are provided
|
||||||
|
// TODO: Tags and categories are not supported by the index engine yet
|
||||||
if (
|
if (
|
||||||
|
Object.keys(req.filterableFields).length === 0 ||
|
||||||
isPresent(req.filterableFields.tags) ||
|
isPresent(req.filterableFields.tags) ||
|
||||||
isPresent(req.filterableFields.categories)
|
isPresent(req.filterableFields.categories)
|
||||||
) {
|
) {
|
||||||
@@ -58,10 +60,20 @@ async function getProductsWithIndexEngine(
|
|||||||
) {
|
) {
|
||||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||||
|
|
||||||
|
const filters: Record<string, any> = req.filterableFields
|
||||||
|
if (isPresent(filters.sales_channel_id)) {
|
||||||
|
const salesChannelIds = filters.sales_channel_id
|
||||||
|
|
||||||
|
filters["sales_channels"] ??= {}
|
||||||
|
filters["sales_channels"]["id"] = salesChannelIds
|
||||||
|
|
||||||
|
delete filters.sales_channel_id
|
||||||
|
}
|
||||||
|
|
||||||
const { data: products, metadata } = await query.index({
|
const { data: products, metadata } = await query.index({
|
||||||
entity: "product",
|
entity: "product",
|
||||||
fields: req.queryConfig.fields ?? [],
|
fields: req.queryConfig.fields ?? [],
|
||||||
filters: req.filterableFields,
|
filters: filters,
|
||||||
pagination: req.queryConfig.pagination,
|
pagination: req.queryConfig.pagination,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { IndexTypes } from "@medusajs/framework/types"
|
import { IndexTypes } from "@medusajs/framework/types"
|
||||||
import {
|
import {
|
||||||
GraphQLUtils,
|
GraphQLUtils,
|
||||||
|
isDefined,
|
||||||
isObject,
|
isObject,
|
||||||
isPresent,
|
isPresent,
|
||||||
isString,
|
isString,
|
||||||
@@ -276,13 +277,17 @@ export class QueryBuilder {
|
|||||||
|
|
||||||
value = this.transformValueToType(attr, field, value)
|
value = this.transformValueToType(attr, field, value)
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
|
if (value.length === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const castType = this.getPostgresCastType(attr, field).cast
|
const castType = this.getPostgresCastType(attr, field).cast
|
||||||
const inPlaceholders = value.map(() => "?").join(",")
|
const inPlaceholders = value.map(() => "?").join(",")
|
||||||
builder.whereRaw(
|
builder.whereRaw(
|
||||||
`(${aliasMapping[attr]}.data${nested}->>?)${castType} IN (${inPlaceholders})`,
|
`(${aliasMapping[attr]}.data${nested}->>?)${castType} IN (${inPlaceholders})`,
|
||||||
[...field, ...value]
|
[...field, ...value]
|
||||||
)
|
)
|
||||||
} else {
|
} else if (isDefined(value)) {
|
||||||
const operator = value === null ? "IS" : "="
|
const operator = value === null ? "IS" : "="
|
||||||
|
|
||||||
if (operator === "=") {
|
if (operator === "=") {
|
||||||
|
|||||||
Reference in new issue
Block a user