From 489b7effb013b2ffb38693ace14fb8cce2dd7ab4 Mon Sep 17 00:00:00 2001 From: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Wed, 24 Jan 2024 13:31:17 +0100 Subject: [PATCH] fix(medusa): Support q search in currencies (#6201) Closes #6175 --- .changeset/dry-meals-love.md | 6 ++++ .../api/__tests__/admin/currency.js | 29 +++++++++++++++++++ .../lib/models/AdminGetCurrenciesParams.ts | 4 +++ .../admin/currencies/list-currencies.ts | 8 +++++ packages/medusa/src/services/currency.ts | 29 +++++++++++++++++-- 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 .changeset/dry-meals-love.md diff --git a/.changeset/dry-meals-love.md b/.changeset/dry-meals-love.md new file mode 100644 index 0000000000..57ea2d4520 --- /dev/null +++ b/.changeset/dry-meals-love.md @@ -0,0 +1,6 @@ +--- +"@medusajs/medusa": patch +"@medusajs/client-types": patch +--- + +fix(medusa): Support q search in currencies diff --git a/integration-tests/api/__tests__/admin/currency.js b/integration-tests/api/__tests__/admin/currency.js index b8634ec93e..48963b3359 100644 --- a/integration-tests/api/__tests__/admin/currency.js +++ b/integration-tests/api/__tests__/admin/currency.js @@ -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", () => { diff --git a/packages/generated/client-types/src/lib/models/AdminGetCurrenciesParams.ts b/packages/generated/client-types/src/lib/models/AdminGetCurrenciesParams.ts index 7e6edb2b0d..5946d0dac7 100644 --- a/packages/generated/client-types/src/lib/models/AdminGetCurrenciesParams.ts +++ b/packages/generated/client-types/src/lib/models/AdminGetCurrenciesParams.ts @@ -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. */ diff --git a/packages/medusa/src/api/routes/admin/currencies/list-currencies.ts b/packages/medusa/src/api/routes/admin/currencies/list-currencies.ts index 4889ac0525..0a605a1a24 100644 --- a/packages/medusa/src/api/routes/admin/currencies/list-currencies.ts +++ b/packages/medusa/src/api/routes/admin/currencies/list-currencies.ts @@ -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. * diff --git a/packages/medusa/src/services/currency.ts b/packages/medusa/src/services/currency.ts index 4a0512a8f6..bf056a0890 100644 --- a/packages/medusa/src/services/currency.ts +++ b/packages/medusa/src/services/currency.ts @@ -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, + selector: Selector & { q?: string }, config: FindConfig = { 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 + + delete where.code + delete where.name + + query.where = [ + { + ...where, + code: ILike(`%${q}%`), + }, + { + ...where, + name: ILike(`%${q}%`), + }, + ] + } + return await productRepo.findAndCount(query) }