chore: admin product list transform filter (#11821)

This commit is contained in:
Carlos R. L. Rodrigues
2025-03-12 13:21:09 -03:00
committed by GitHub
parent e0109540fa
commit 5cf0bf4d93
3 changed files with 30 additions and 7 deletions

View File

@@ -0,0 +1,6 @@
---
"@medusajs/index": patch
"@medusajs/medusa": patch
---
chore: transform filter before query.index

View File

@@ -1,23 +1,25 @@
import { createProductsWorkflow } from "@medusajs/core-flows"
import { AdditionalData, HttpTypes } from "@medusajs/framework/types"
import { featureFlagRouter } from "@medusajs/framework"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
refetchEntities,
refetchEntity,
} from "@medusajs/framework/http"
import { remapKeysForProduct, remapProductResponse } from "./helpers"
import IndexEngineFeatureFlag from "../../../loaders/feature-flags/index-engine"
import { featureFlagRouter } from "@medusajs/framework"
import { AdditionalData, HttpTypes } from "@medusajs/framework/types"
import { ContainerRegistrationKeys, isPresent } from "@medusajs/framework/utils"
import IndexEngineFeatureFlag from "../../../loaders/feature-flags/index-engine"
import { remapKeysForProduct, remapProductResponse } from "./helpers"
export const GET = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminProductListParams>,
res: MedusaResponse<HttpTypes.AdminProductListResponse>
) => {
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 (
Object.keys(req.filterableFields).length === 0 ||
isPresent(req.filterableFields.tags) ||
isPresent(req.filterableFields.categories)
) {
@@ -58,10 +60,20 @@ async function getProductsWithIndexEngine(
) {
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({
entity: "product",
fields: req.queryConfig.fields ?? [],
filters: req.filterableFields,
filters: filters,
pagination: req.queryConfig.pagination,
})

View File

@@ -1,6 +1,7 @@
import { IndexTypes } from "@medusajs/framework/types"
import {
GraphQLUtils,
isDefined,
isObject,
isPresent,
isString,
@@ -276,13 +277,17 @@ export class QueryBuilder {
value = this.transformValueToType(attr, field, value)
if (Array.isArray(value)) {
if (value.length === 0) {
return
}
const castType = this.getPostgresCastType(attr, field).cast
const inPlaceholders = value.map(() => "?").join(",")
builder.whereRaw(
`(${aliasMapping[attr]}.data${nested}->>?)${castType} IN (${inPlaceholders})`,
[...field, ...value]
)
} else {
} else if (isDefined(value)) {
const operator = value === null ? "IS" : "="
if (operator === "=") {