fix: Ensure passed Id filters get applied on top of maybe link filter (#7182)
This commit is contained in:
@@ -247,6 +247,18 @@ medusaIntegrationTestRunner({
|
|||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should list products by id", async () => {
|
||||||
|
let response = await api.get(`/store/products?id[]=${product.id}`)
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
expect(response.data.count).toEqual(1)
|
||||||
|
expect(response.data.products).toEqual([
|
||||||
|
expect.objectContaining({
|
||||||
|
id: product.id,
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
it("should throw error when publishable key is invalid", async () => {
|
it("should throw error when publishable key is invalid", async () => {
|
||||||
let error = await api
|
let error = await api
|
||||||
.get(`/store/products?sales_channel_id[]=does-not-exist`, {
|
.get(`/store/products?sales_channel_id[]=does-not-exist`, {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
remoteQueryObjectFromString,
|
remoteQueryObjectFromString,
|
||||||
|
arrayIntersection,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { NextFunction } from "express"
|
import { NextFunction } from "express"
|
||||||
import { MedusaRequest } from "../../types/routing"
|
import { MedusaRequest } from "../../types/routing"
|
||||||
@@ -37,7 +38,19 @@ export function maybeApplyLinkFilter({
|
|||||||
|
|
||||||
const resources = await remoteQuery(queryObject)
|
const resources = await remoteQuery(queryObject)
|
||||||
|
|
||||||
filterableFields.id = resources.map((p) => p[resourceId])
|
let existingIdFilters = filterableFields.id as string[] | string | undefined
|
||||||
|
if (existingIdFilters) {
|
||||||
|
if (typeof existingIdFilters === "string") {
|
||||||
|
existingIdFilters = [existingIdFilters]
|
||||||
|
}
|
||||||
|
|
||||||
|
filterableFields.id = arrayIntersection(
|
||||||
|
existingIdFilters,
|
||||||
|
resources.map((p) => p[resourceId])
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
filterableFields.id = resources.map((p) => p[resourceId])
|
||||||
|
}
|
||||||
|
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { arrayIntersection } from "../array-intersection"
|
||||||
|
|
||||||
|
describe("arrayIntersection", function () {
|
||||||
|
it("should return the intersection between two arrays", function () {
|
||||||
|
const output = arrayIntersection(["foo", "bar"], ["bar", "baz"])
|
||||||
|
expect(output).toEqual(["bar"])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return an empty array if there is no intersection", function () {
|
||||||
|
const output = arrayIntersection(["bar", "baz"], ["foo", "boo"])
|
||||||
|
expect(output).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return an all items when the arrays are equivalent", function () {
|
||||||
|
const output = arrayIntersection(["bar", "baz"], ["baz", "bar"])
|
||||||
|
expect(output).toEqual(["baz", "bar"])
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
type ArrayIntersectionElement = string | number
|
||||||
|
|
||||||
|
export function arrayIntersection<TElement = ArrayIntersectionElement>(
|
||||||
|
firstArray: TElement[],
|
||||||
|
secondArray: TElement[]
|
||||||
|
): TElement[] {
|
||||||
|
const firstArraySet = new Set(firstArray)
|
||||||
|
const res = new Set<TElement>()
|
||||||
|
|
||||||
|
secondArray.forEach((element) => {
|
||||||
|
if (firstArraySet.has(element)) {
|
||||||
|
res.add(element)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return Array.from(res)
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
export * from "./alter-columns-helper"
|
export * from "./alter-columns-helper"
|
||||||
export * from "./array-difference"
|
export * from "./array-difference"
|
||||||
|
export * from "./array-intersection"
|
||||||
export * from "./build-query"
|
export * from "./build-query"
|
||||||
export * from "./camel-to-snake-case"
|
export * from "./camel-to-snake-case"
|
||||||
export * from "./container"
|
export * from "./container"
|
||||||
|
|||||||
Reference in New Issue
Block a user