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:
co-authored by
Oliver Windall Juhl
parent
0fa5042e35
commit
ea460b4e0b
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa): add `q` param to PKs sales channels retrieval
|
||||||
@@ -499,6 +499,7 @@ describe("[MEDUSA_FF_PUBLISHABLE_API_KEYS] Publishable API keys", () => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toBe(200)
|
expect(response.status).toBe(200)
|
||||||
|
expect(response.data.sales_channels.length).toEqual(2)
|
||||||
expect(response.data.sales_channels).toEqual(
|
expect(response.data.sales_channels).toEqual(
|
||||||
expect.arrayContaining([
|
expect.arrayContaining([
|
||||||
expect.objectContaining({
|
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", () => {
|
describe("GET /store/products", () => {
|
||||||
|
|||||||
+13
-2
@@ -1,6 +1,8 @@
|
|||||||
import { Request, Response } from "express"
|
import { Request, Response } from "express"
|
||||||
|
import { IsOptional, IsString } from "class-validator"
|
||||||
|
|
||||||
import PublishableApiKeyService from "../../../../services/publishable-api-key"
|
import PublishableApiKeyService from "../../../../services/publishable-api-key"
|
||||||
|
import { extendedFindParamsMixin } from "../../../../types/common"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [get] /publishable-api-keys/{id}/sales-channels
|
* @oas [get] /publishable-api-keys/{id}/sales-channels
|
||||||
@@ -10,6 +12,7 @@ import PublishableApiKeyService from "../../../../services/publishable-api-key"
|
|||||||
* x-authenticated: true
|
* x-authenticated: true
|
||||||
* parameters:
|
* parameters:
|
||||||
* - (path) id=* {string} The ID of the Publishable Api Key.
|
* - (path) id=* {string} The ID of the Publishable Api Key.
|
||||||
|
* - (query) q {string} Query used for searching sales channels' names and descriptions.
|
||||||
* x-codeSamples:
|
* x-codeSamples:
|
||||||
* - lang: JavaScript
|
* - lang: JavaScript
|
||||||
* label: JS Client
|
* label: JS Client
|
||||||
@@ -61,11 +64,19 @@ export default async (req: Request, res: Response) => {
|
|||||||
"publishableApiKeyService"
|
"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({
|
return res.json({
|
||||||
sales_channels: salesChannels,
|
sales_channels: salesChannels,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GetPublishableApiKeySalesChannelsParams {}
|
export class GetPublishableApiKeySalesChannelsParams extends extendedFindParamsMixin() {
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
q?: string
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { EntityRepository, In, Repository } from "typeorm"
|
import { Brackets, EntityRepository, In, Repository } from "typeorm"
|
||||||
|
|
||||||
import { PublishableApiKeySalesChannel, SalesChannel } from "../models"
|
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
|
* 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 publishableApiKeyId - id of the key to retrieve channels for
|
||||||
|
* @param config - querying params
|
||||||
*/
|
*/
|
||||||
public async findSalesChannels(
|
public async findSalesChannels(
|
||||||
publishableApiKeyId: string
|
publishableApiKeyId: string,
|
||||||
|
config?: { q?: string }
|
||||||
): Promise<SalesChannel[]> {
|
): Promise<SalesChannel[]> {
|
||||||
const data = await this.createQueryBuilder("PublishableKeySalesChannel")
|
const query = this.createQueryBuilder("PublishableKeySalesChannel")
|
||||||
.select("PublishableKeySalesChannel.sales_channel_id")
|
.select("PublishableKeySalesChannel.sales_channel_id")
|
||||||
.innerJoinAndMapOne(
|
.innerJoinAndMapOne(
|
||||||
"PublishableKeySalesChannel.sales_channel_id",
|
"PublishableKeySalesChannel.sales_channel_id",
|
||||||
@@ -26,9 +28,20 @@ export class PublishableApiKeySalesChannelRepository extends Repository<Publisha
|
|||||||
publishableApiKeyId,
|
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
|
(record) => record.sales_channel_id as unknown as SalesChannel
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -302,16 +302,21 @@ class PublishableApiKeyService extends TransactionBaseService {
|
|||||||
* List SalesChannels associated with the PublishableKey
|
* List SalesChannels associated with the PublishableKey
|
||||||
*
|
*
|
||||||
* @param publishableApiKeyId - id of the key SalesChannels are listed for
|
* @param publishableApiKeyId - id of the key SalesChannels are listed for
|
||||||
|
* @param config - querying params
|
||||||
*/
|
*/
|
||||||
async listSalesChannels(
|
async listSalesChannels(
|
||||||
publishableApiKeyId: string
|
publishableApiKeyId: string,
|
||||||
|
config?: { q?: string }
|
||||||
): Promise<SalesChannel[]> {
|
): Promise<SalesChannel[]> {
|
||||||
const manager = this.manager_
|
const manager = this.manager_
|
||||||
const pubKeySalesChannelRepo = manager.getCustomRepository(
|
const pubKeySalesChannelRepo = manager.getCustomRepository(
|
||||||
this.publishableApiKeySalesChannelRepository_
|
this.publishableApiKeySalesChannelRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
return await pubKeySalesChannelRepo.findSalesChannels(publishableApiKeyId)
|
return await pubKeySalesChannelRepo.findSalesChannels(
|
||||||
|
publishableApiKeyId,
|
||||||
|
config
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user