fix(medusa): Support q search in currencies (#6201)

Closes #6175
This commit is contained in:
Oli Juhl
2024-01-24 13:31:17 +01:00
committed by GitHub
parent 8ad7539ebc
commit 489b7effb0
5 changed files with 74 additions and 2 deletions

View File

@@ -0,0 +1,6 @@
---
"@medusajs/medusa": patch
"@medusajs/client-types": patch
---
fix(medusa): Support q search in currencies

View File

@@ -53,6 +53,35 @@ describe("/admin/currencies", () => {
expect(response.data).toMatchSnapshot()
})
it("should retrieve the currencies filtered with q param", async () => {
const api = useApi()
const response = await api.get(
`/admin/currencies?q=us&order=code`,
adminReqConfig
)
const { currencies } = response.data
expect(currencies).toEqual([
expect.objectContaining({
code: "aud",
name: "Australian Dollar",
}),
expect.objectContaining({
code: "byn",
name: "Belarusian Ruble",
}),
expect.objectContaining({
code: "rub",
name: "Russian Ruble",
}),
expect.objectContaining({
code: "usd",
name: "US Dollar",
}),
])
})
})
})
describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/currencies", () => {

View File

@@ -16,6 +16,10 @@ export interface AdminGetCurrenciesParams {
* A field to sort order the retrieved currencies by.
*/
order?: string
/**
* Term used to search currencies' name and code.
*/
q?: string
/**
* The number of currencies to skip when retrieving the currencies.
*/

View File

@@ -21,6 +21,7 @@ import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators
* type: boolean
* x-featureFlag: "tax_inclusive_pricing"
* - (query) order {string} A field to sort order the retrieved currencies by.
* - (query) q {string} Term used to search currencies' name and code.
* - (query) offset=0 {number} The number of currencies to skip when retrieving the currencies.
* - (query) limit=20 {number} The number of currencies to return.
* x-codegen:
@@ -128,6 +129,13 @@ export class AdminGetCurrenciesParams extends FindPaginationParams {
@IsOptional()
code?: string
/**
* Search parameter for currencies.
*/
@IsString()
@IsOptional()
q?: string
/**
* Filter currencies by whether they include tax.
*

View File

@@ -1,6 +1,6 @@
import { FlagRouter } from "@medusajs/utils"
import { MedusaError } from "medusa-core-utils"
import { EntityManager } from "typeorm"
import { EntityManager, FindOptionsWhere, ILike } from "typeorm"
import { TransactionBaseService } from "../interfaces"
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"
import { Currency } from "../models"
@@ -76,7 +76,7 @@ export default class CurrencyService extends TransactionBaseService {
* as the second element.
*/
async listAndCount(
selector: Selector<Currency>,
selector: Selector<Currency> & { q?: string },
config: FindConfig<Currency> = {
skip: 0,
take: 20,
@@ -86,8 +86,33 @@ export default class CurrencyService extends TransactionBaseService {
this.currencyRepository_
)
let q: string | undefined
if (selector.q) {
q = selector.q
delete selector.q
}
const query = buildQuery(selector, config)
if (q) {
const where = query.where as FindOptionsWhere<Currency>
delete where.code
delete where.name
query.where = [
{
...where,
code: ILike(`%${q}%`),
},
{
...where,
name: ILike(`%${q}%`),
},
]
}
return await productRepo.findAndCount(query)
}