fix(medusa): Fix q param on /admin/price-lists/:id/products (#2813)

This commit is contained in:
Kasper Fabricius Kristensen
2022-12-18 20:11:38 +01:00
committed by GitHub
parent 5e4decbc1c
commit 9e05fef4b9
3 changed files with 56 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
Fixes a bug where using the q param with the endpoint /admin/price-lists/:id/products would also return products not associated with the price list.

View File

@@ -1108,6 +1108,7 @@ describe("/admin/price-lists", () => {
dbConnection,
{
id: "test-prod-2",
title: "MedusaShoes",
tags: ["test-tag"],
variants: [{ id: "test-variant-3" }, { id: "test-variant-4" }],
},
@@ -1120,11 +1121,23 @@ describe("/admin/price-lists", () => {
dbConnection,
{
id: "test-prod-3",
title: "MedusaShirt",
variants: [{ id: "test-variant-5" }],
},
3
)
// Used to validate that products that are not associated with the price list are not returned
await simpleProductFactory(
dbConnection,
{
id: "test-prod-4",
title: "OtherHeadphones",
variants: [{ id: "test-variant-6" }],
},
4
)
await simplePriceListFactory(dbConnection, {
id: "test-list",
customer_groups: ["test-group"],
@@ -1236,7 +1249,7 @@ describe("/admin/price-lists", () => {
const api = useApi()
const response = await api
.get(`/admin/price-lists/test-list/products?q=MedusaHeadphones`, {
.get(`/admin/price-lists/test-list/products?q=Headphones`, {
headers: {
Authorization: "Bearer test_token",
},

View File

@@ -353,6 +353,18 @@ export class ProductRepository extends Repository<Product> {
options: FindWithoutRelationsOptions = { where: {} },
relations: string[] = []
): Promise<[Product[], number]> {
const tags = options.where.tags
delete options.where.tags
const price_lists = options.where.price_list_id
delete options.where.price_list_id
const sales_channels = options.where.sales_channel_id
delete options.where.sales_channel_id
const discount_condition_id = options.where.discount_condition_id
delete options.where.discount_condition_id
const cleanedOptions = this._cleanOptions(options)
let qb = this.createQueryBuilder("product")
@@ -372,13 +384,35 @@ export class ProductRepository extends Repository<Product> {
.skip(cleanedOptions.skip)
.take(cleanedOptions.take)
const discountConditionId = options.where.discount_condition_id
if (discountConditionId) {
if (discount_condition_id) {
qb.innerJoin(
"discount_condition_product",
"dc_product",
`dc_product.product_id = product.id AND dc_product.condition_id = :dcId`,
{ dcId: discountConditionId }
{ dcId: discount_condition_id }
)
}
if (tags) {
qb.leftJoin("product.tags", "tags").andWhere(`tags.id IN (:...tag_ids)`, {
tag_ids: tags.value,
})
}
if (price_lists) {
qb.leftJoin("product.variants", "variants")
.leftJoin("variants.prices", "ma")
.andWhere("ma.price_list_id IN (:...price_list_ids)", {
price_list_ids: price_lists.value,
})
}
if (sales_channels) {
qb.innerJoin(
"product.sales_channels",
"sales_channels",
"sales_channels.id IN (:...sales_channels_ids)",
{ sales_channels_ids: sales_channels.value }
)
}