feat(medusa): add q param to PKs sales channels retrieval (#2810)

**What**
- enable to filter sales channels of a Publishable API Key with a free text search param

Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Frane Polić
2022-12-15 20:12:44 +01:00
committed by GitHub
parent 0fa5042e35
commit ea460b4e0b
5 changed files with 67 additions and 9 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
feat(medusa): add `q` param to PKs sales channels retrieval

View File

@@ -499,6 +499,7 @@ describe("[MEDUSA_FF_PUBLISHABLE_API_KEYS] Publishable API keys", () => {
)
expect(response.status).toBe(200)
expect(response.data.sales_channels.length).toEqual(2)
expect(response.data.sales_channels).toEqual(
expect.arrayContaining([
expect.objectContaining({
@@ -518,6 +519,29 @@ describe("[MEDUSA_FF_PUBLISHABLE_API_KEYS] Publishable API keys", () => {
])
)
})
it("list sales channels from the publishable api key with free text search filter", async () => {
const api = useApi()
const response = await api.get(
`/admin/publishable-api-keys/${pubKeyId}/sales-channels?q=2`,
adminHeaders
)
expect(response.status).toBe(200)
expect(response.data.sales_channels.length).toEqual(1)
expect(response.data.sales_channels).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: salesChannel2.id,
deleted_at: null,
name: "test name 2",
description: "test description 2",
is_disabled: false,
}),
])
)
})
})
describe("GET /store/products", () => {

View File

@@ -1,6 +1,8 @@
import { Request, Response } from "express"
import { IsOptional, IsString } from "class-validator"
import PublishableApiKeyService from "../../../../services/publishable-api-key"
import { extendedFindParamsMixin } from "../../../../types/common"
/**
* @oas [get] /publishable-api-keys/{id}/sales-channels
@@ -10,6 +12,7 @@ import PublishableApiKeyService from "../../../../services/publishable-api-key"
* x-authenticated: true
* parameters:
* - (path) id=* {string} The ID of the Publishable Api Key.
* - (query) q {string} Query used for searching sales channels' names and descriptions.
* x-codeSamples:
* - lang: JavaScript
* label: JS Client
@@ -61,11 +64,19 @@ export default async (req: Request, res: Response) => {
"publishableApiKeyService"
)
const salesChannels = await publishableApiKeyService.listSalesChannels(id)
const filterableFields = req.filterableFields
const salesChannels = await publishableApiKeyService.listSalesChannels(id, {
q: filterableFields.q as string | undefined,
})
return res.json({
sales_channels: salesChannels,
})
}
export class GetPublishableApiKeySalesChannelsParams {}
export class GetPublishableApiKeySalesChannelsParams extends extendedFindParamsMixin() {
@IsOptional()
@IsString()
q?: string
}

View File

@@ -1,4 +1,4 @@
import { EntityRepository, In, Repository } from "typeorm"
import { Brackets, EntityRepository, In, Repository } from "typeorm"
import { PublishableApiKeySalesChannel, SalesChannel } from "../models"
@@ -8,11 +8,13 @@ export class PublishableApiKeySalesChannelRepository extends Repository<Publisha
* Query a list of sales channels that are assigned to the publishable key scope
*
* @param publishableApiKeyId - id of the key to retrieve channels for
* @param config - querying params
*/
public async findSalesChannels(
publishableApiKeyId: string
publishableApiKeyId: string,
config?: { q?: string }
): Promise<SalesChannel[]> {
const data = await this.createQueryBuilder("PublishableKeySalesChannel")
const query = this.createQueryBuilder("PublishableKeySalesChannel")
.select("PublishableKeySalesChannel.sales_channel_id")
.innerJoinAndMapOne(
"PublishableKeySalesChannel.sales_channel_id",
@@ -26,9 +28,20 @@ export class PublishableApiKeySalesChannelRepository extends Repository<Publisha
publishableApiKeyId,
}
)
.getMany()
return data.map(
if (config?.q) {
query.andWhere(
new Brackets((qb) => {
qb.where(`SalesChannel.description ILIKE :q`, {
q: `%${config.q}%`,
}).orWhere(`SalesChannel.name ILIKE :q`, { q: `%${config.q}%` })
})
)
}
const records = await query.getMany()
return records.map(
(record) => record.sales_channel_id as unknown as SalesChannel
)
}

View File

@@ -302,16 +302,21 @@ class PublishableApiKeyService extends TransactionBaseService {
* List SalesChannels associated with the PublishableKey
*
* @param publishableApiKeyId - id of the key SalesChannels are listed for
* @param config - querying params
*/
async listSalesChannels(
publishableApiKeyId: string
publishableApiKeyId: string,
config?: { q?: string }
): Promise<SalesChannel[]> {
const manager = this.manager_
const pubKeySalesChannelRepo = manager.getCustomRepository(
this.publishableApiKeySalesChannelRepository_
)
return await pubKeySalesChannelRepo.findSalesChannels(publishableApiKeyId)
return await pubKeySalesChannelRepo.findSalesChannels(
publishableApiKeyId,
config
)
}
/**