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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@medusajs/medusa": patch | ||
--- | ||
|
||
feat(medusa): add middleware filters + scope products |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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 | ||
|
@@ -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 | ||
// 2. MikroORM seems to be doing a join strategy to load relationships, we need to send a separate query to fetch relationships | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
There was a problem hiding this comment.
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?