feat(medusa, stock-location): Sales channel filtering when listing locations (#3324)
* only add ordering select if not already selected * add integration test * add changeset * remove catch * linting and suggestion from adrien * add sales channel filtering when listing locations * add changeset * add exception back into sales channel location service --------- Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Oliver Windall Juhl
parent
cbbf3ca054
commit
a1e59313c9
@@ -148,6 +148,8 @@ export default async (req: Request, res: Response) => {
|
||||
const { filterableFields, listConfig } = req
|
||||
const { skip, take } = listConfig
|
||||
|
||||
const filterOnSalesChannel = !!filterableFields.sales_channel_id
|
||||
|
||||
const includeSalesChannels =
|
||||
!!listConfig.relations?.includes("sales_channels")
|
||||
|
||||
@@ -157,6 +159,18 @@ export default async (req: Request, res: Response) => {
|
||||
)
|
||||
}
|
||||
|
||||
if (filterOnSalesChannel) {
|
||||
const ids: string[] = Array.isArray(filterableFields.sales_channel_id)
|
||||
? filterableFields.sales_channel_id
|
||||
: [filterableFields.sales_channel_id]
|
||||
|
||||
delete filterableFields.sales_channel_id
|
||||
|
||||
const locationIds = await channelLocationService.listLocationIds(ids)
|
||||
|
||||
filterableFields.id = [...new Set(locationIds.flat())]
|
||||
}
|
||||
|
||||
let [locations, count] = await stockLocationService.listAndCount(
|
||||
filterableFields,
|
||||
listConfig
|
||||
@@ -193,4 +207,8 @@ export class AdminGetStockLocationsParams extends extendedFindParamsMixin({
|
||||
@IsOptional()
|
||||
@IsType([String, [String]])
|
||||
address_id?: string | string[]
|
||||
|
||||
@IsOptional()
|
||||
@IsType([String, [String]])
|
||||
sales_channel_id?: string | string[]
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { EntityManager } from "typeorm"
|
||||
import { EntityManager, In } from "typeorm"
|
||||
import { IStockLocationService, TransactionBaseService } from "../interfaces"
|
||||
import { EventBusService, SalesChannelService } from "./"
|
||||
|
||||
import { SalesChannelLocation } from "../models/sales-channel-location"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
stockLocationService: IStockLocationService
|
||||
@@ -97,13 +98,24 @@ class SalesChannelLocationService extends TransactionBaseService {
|
||||
* @param salesChannelId - The ID of the sales channel.
|
||||
* @returns A promise that resolves with an array of location IDs.
|
||||
*/
|
||||
async listLocationIds(salesChannelId: string): Promise<string[]> {
|
||||
const salesChannel = await this.salesChannelService_
|
||||
async listLocationIds(salesChannelId: string | string[]): Promise<string[]> {
|
||||
const ids = Array.isArray(salesChannelId)
|
||||
? salesChannelId
|
||||
: [salesChannelId]
|
||||
|
||||
const [salesChannels, count] = await this.salesChannelService_
|
||||
.withTransaction(this.activeManager_)
|
||||
.retrieve(salesChannelId)
|
||||
.listAndCount({ id: ids }, { select: ["id"], skip: 0 })
|
||||
|
||||
if (!count) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Sales channel with id: ${ids.join(", ")} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
const locations = await this.activeManager_.find(SalesChannelLocation, {
|
||||
where: { sales_channel_id: salesChannel.id },
|
||||
where: { sales_channel_id: In(salesChannels.map((sc) => sc.id)) },
|
||||
select: ["location_id"],
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user