Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(medusa): add middleware filters + scope products #7178

Merged
merged 4 commits into from Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-glasses-exercise.md
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

feat(medusa): add middleware filters + scope products
73 changes: 73 additions & 0 deletions integration-tests/modules/__tests__/product/store/index.spec.ts
Expand Up @@ -33,6 +33,27 @@ medusaIntegrationTestRunner({
return [response.data.product, response.data.product.variants || []]
}

const createCategory = async (data, productIds) => {
const response = await api.post(
"/admin/product-categories",
data,
adminHeaders
)

await api.post(
`/admin/product-categories/${response.data.product_category.id}/products`,
{ add: productIds },
adminHeaders
)

const response2 = await api.get(
`/admin/product-categories/${response.data.product_category.id}?fields=*products`,
adminHeaders
)

return response2.data.product_category
}

const createSalesChannel = async (data, productIds) => {
const response = await api.post(
"/admin/sales-channels",
Expand Down Expand Up @@ -156,6 +177,30 @@ medusaIntegrationTestRunner({
])
})

it("should list all products for a category", async () => {
const category = await createCategory(
{ name: "test", is_internal: false, is_active: true },
[product.id]
)

const category2 = await createCategory(
{ name: "test2", is_internal: true, is_active: true },
[product4.id]
)

const response = await api.get(
`/store/products?category_id[]=${category.id}&category_id[]=${category2.id}`
)

expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.products).toEqual([
expect.objectContaining({
id: product.id,
}),
])
})

describe("with publishable keys", () => {
let salesChannel1
let salesChannel2
Expand Down Expand Up @@ -349,6 +394,34 @@ medusaIntegrationTestRunner({
)
})

// TODO: There are 2 problems that need to be solved to enable this test
// 1. When adding product to another category, the product is being removed from earlier assigned categories
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sradevski do you know anything about this?

// 2. MikroORM seems to be doing a join strategy to load relationships, we need to send a separate query to fetch relationships
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info: for the second point you can pass the strategy as part of the options. But it would require to add it as part of the FindConfig type, the underlying service and repository will then consume it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, will fix this as a follow up.

// to scope the relationships
it.skip("should list only categories that are public and active", async () => {
const category = await createCategory(
{ name: "test 1", is_internal: true, is_active: true },
[product.id]
)

await createCategory(
{ name: "test 2", is_internal: false, is_active: true },
[product.id]
)

const response = await api.get(
`/store/products/${product.id}?fields=*categories`
)

expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
id: product.id,
categories: [expect.objectContaining({ id: category.id })],
})
)
})

it("should throw error when calculating prices without context", async () => {
let error = await api
.get(
Expand Down
20 changes: 11 additions & 9 deletions packages/medusa/src/api-v2/store/products/[id]/route.ts
Expand Up @@ -7,17 +7,19 @@ export const GET = async (
req: MedusaRequest<StoreGetProductsParamsType>,
res: MedusaResponse
) => {
const context = isPresent(req.pricingContext)
? {
"variants.calculated_price": { context: req.pricingContext },
}
: undefined
const filters: object = {
id: req.params.id,
...req.filterableFields,
}

if (isPresent(req.pricingContext)) {
filters["context"] = {
"variants.calculated_price": { context: req.pricingContext },
}
}

const product = await refetchProduct(
{
id: req.params.id,
context,
},
filters,
req.scope,
req.remoteQueryConfig.fields
)
Expand Down
24 changes: 0 additions & 24 deletions packages/medusa/src/api-v2/store/products/helpers.ts
@@ -1,29 +1,5 @@
import { MedusaContainer } from "@medusajs/types"
import { isPresent } from "@medusajs/utils"
import { refetchEntity } from "../../utils/refetch-entity"
import { StoreGetProductsParamsType } from "./validators"

// For category filters, we only allow showcasing public and active categories
// TODO: This should ideally be done in the middleware, write a generic filter to conditionally
// map these values or normalize the filters to the ones expected by remote query
export function wrapWithCategoryFilters(filters: StoreGetProductsParamsType) {
const categoriesFilter = isPresent(filters.category_id)
? {
categories: {
...filters.category_id,
is_internal: false,
is_active: true,
},
}
: {}

delete filters.category_id

return {
...filters,
...categoriesFilter,
}
}

export const refetchProduct = async (
idOrFilter: string | object,
Expand Down
23 changes: 20 additions & 3 deletions packages/medusa/src/api-v2/store/products/middlewares.ts
@@ -1,4 +1,4 @@
import { ProductStatus } from "@medusajs/utils"
import { isPresent, ProductStatus } from "@medusajs/utils"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
import { maybeApplyLinkFilter } from "../../utils/maybe-apply-link-filter"
Expand Down Expand Up @@ -38,8 +38,18 @@ export const storeProductRoutesMiddlewares: MiddlewareRoute[] = [
resourceId: "product_id",
filterableField: "sales_channel_id",
}),
applyDefaultFilters<StoreGetProductsParamsType>({
applyDefaultFilters({
status: ProductStatus.PUBLISHED,
categories: (filters: StoreGetProductsParamsType, fields: string[]) => {
const categoryIds = filters.category_id
delete filters.category_id

if (!isPresent(categoryIds)) {
return
}

return { id: categoryIds, is_internal: false, is_active: true }
},
}),
setPricingContext(),
],
Expand All @@ -58,8 +68,15 @@ export const storeProductRoutesMiddlewares: MiddlewareRoute[] = [
resourceId: "product_id",
filterableField: "sales_channel_id",
}),
applyDefaultFilters<StoreGetProductsParamsType>({
applyDefaultFilters({
status: ProductStatus.PUBLISHED,
categories: (_filters, fields: string[]) => {
if (!fields.some((field) => field.startsWith("categories"))) {
return
}

return { is_internal: false, is_active: true }
},
}),
setPricingContext(),
],
Expand Down
17 changes: 9 additions & 8 deletions packages/medusa/src/api-v2/store/products/route.ts
Expand Up @@ -4,26 +4,27 @@ import {
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
import { wrapWithCategoryFilters } from "./helpers"
import { StoreGetProductsParamsType } from "./validators"

export const GET = async (
req: MedusaRequest<StoreGetProductsParamsType>,
res: MedusaResponse
) => {
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const context = isPresent(req.pricingContext)
? {
"variants.calculated_price": { context: req.pricingContext },
}
: undefined
const context: object = {}

if (isPresent(req.pricingContext)) {
context["variants.calculated_price"] = {
context: req.pricingContext,
}
}

const queryObject = remoteQueryObjectFromString({
entryPoint: "product",
variables: {
filters: wrapWithCategoryFilters(req.filterableFields),
...context,
filters: req.filterableFields,
...req.remoteQueryConfig.pagination,
...context,
},
fields: req.remoteQueryConfig.fields,
})
Expand Down
@@ -1,27 +1,38 @@
import { isObject } from "@medusajs/utils"
import { isObject, isPresent } from "@medusajs/utils"
import { NextFunction } from "express"
import { MedusaRequest } from "../../../../types/routing"

export function applyDefaultFilters<TFilter extends object>(filters: TFilter) {
export function applyDefaultFilters<TFilter extends object>(
filtersToApply: TFilter
) {
return async (req: MedusaRequest, _, next: NextFunction) => {
const filterableFields = req.filterableFields || {}
for (const [filter, filterValue] of Object.entries(filtersToApply)) {
let valueToApply = filterValue

for (const [filter, filterValue] of Object.entries(filters)) {
let existingFilter = filterableFields[filter]
// If certain manipulations need to be done on a middleware level, we can provide a simple
// function that mutates the data based on any custom requirement
if (typeof filterValue === "function") {
// pass the actual filterable fields so that the function can mutate the original object.
// Currently we only need it to delete filter keys from the request filter object, but this could
// be used for other purposes. If we can't find other purposes, we can refactor to accept an array
// of strings to delete after filters have been applied.
valueToApply = filterValue(
req.filterableFields,
req.remoteQueryConfig.fields
)
}

if (existingFilter && isObject(existingFilter)) {
// If an existing filter is found, append to it
filterableFields[filter] = {
...existingFilter,
[filter]: filterValue,
// If the value to apply is an object, we add it to any existing filters thats already applied
if (isObject(valueToApply)) {
req.filterableFields[filter] = {
...(req.filterableFields[filter] || {}),
...valueToApply,
}
} else {
filterableFields[filter] = filterValue
} else if (isPresent(valueToApply)) {
req.filterableFields[filter] = valueToApply
}
}

req.filterableFields = filterableFields

return next()
}
}
Expand Up @@ -10,14 +10,30 @@ export function setPricingContext() {
if (
!req.remoteQueryConfig.fields.some((field) =>
field.startsWith("variants.calculated_price")
)
) &&
!req.filterableFields.region_id &&
!req.filterableFields.currency_code
) {
delete req.filterableFields.region_id
delete req.filterableFields.currency_code

return next()
}

// If pricing parameters are passed, but pricing fields are not passed, throw an error
if (
!req.remoteQueryConfig.fields.some((field) =>
field.startsWith("variants.calculated_price")
) &&
(req.filterableFields.region_id || req.filterableFields.currency_code)
) {
try {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Missing required pricing fields to calculate prices`
)
} catch (e) {
return next(e)
}
}

const query = req.filterableFields || {}
const pricingContext: MedusaPricingContext = {}
const customerId = req.user?.customer_id
Expand Down Expand Up @@ -70,8 +86,8 @@ export function setPricingContext() {
delete req.filterableFields.customer_id
}

// If a region or currency_code is not present in the context, we will not be able to calculate prices
if (!isPresent(pricingContext)) {
// If a currency_code is not present in the context, we will not be able to calculate prices
if (!isPresent(pricingContext.currency_code)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: based on the error message it seems that we should add a check on the region id, probably something along the line of !pricingContext.currency_code && !pricingContext.region_id then we throw

Copy link
Contributor Author

@riqwan riqwan Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the currency code will be set as a part of the region, so we only need to check currency_code here.

try {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
Expand Down