Skip to content

Commit

Permalink
fix: add sc to filter to list
Browse files Browse the repository at this point in the history
  • Loading branch information
fPolic committed Dec 15, 2023
1 parent fb8389b commit 87fa07b
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions packages/medusa/src/utils/queries/products/list-products.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MedusaContainer } from "@medusajs/types"
import { MedusaV2Flag, promiseAll } from "@medusajs/utils"

import { PriceListService, SalesChannelService } from "../../../services"
import { PriceListService } from "../../../services"
import { getVariantsFromPriceList } from "./get-variants-from-price-list"

export async function listProducts(
Expand All @@ -23,35 +23,6 @@ export async function listProducts(
const salesChannelIdFilter = filterableFields.sales_channel_id
delete filterableFields.sales_channel_id

if (salesChannelIdFilter) {
const salesChannelService = container.resolve(
"salesChannelService"
) as SalesChannelService

promises.push(
salesChannelService
.listProductIdsBySalesChannelIds(salesChannelIdFilter)
.then((productIdsInSalesChannel) => {
let filteredProductIds =
productIdsInSalesChannel[salesChannelIdFilter]

if (filterableFields.id) {
filterableFields.id = Array.isArray(filterableFields.id)
? filterableFields.id
: [filterableFields.id]

const salesChannelProductIdsSet = new Set(filteredProductIds)

filteredProductIds = filterableFields.id.filter((productId) =>
salesChannelProductIdsSet.has(productId)
)
}

filteredProductIds.map((id) => productIdsFilter.add(id))
})
)
}

const priceListId = filterableFields.price_list_id
delete filterableFields.price_list_id

Expand Down Expand Up @@ -112,6 +83,10 @@ export async function listProducts(
},
}

if (salesChannelIdFilter) {
query.product["sales_channels"]["__args"] = { id: salesChannelIdFilter }
}

const {
rows: products,
metadata: { count },
Expand Down Expand Up @@ -245,4 +220,16 @@ export const defaultAdminProductRemoteQueryObject = {
profile: {
fields: ["id", "created_at", "updated_at", "deleted_at", "name", "type"],
},
sales_channels: {
fields: [
"id",
"name",
"description",
"is_disabled",
"created_at",
"updated_at",
"deleted_at",
"metadata",
],
},
}

0 comments on commit 87fa07b

Please sign in to comment.