From 5d75e16b1f9300244fc8b04edb95c79f73401c72 Mon Sep 17 00:00:00 2001 From: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Thu, 15 Sep 2022 16:29:14 +0200 Subject: [PATCH] fix(medusa): Expose list-currencies endpoint by removing feature flag guard (#2216) --- .../admin/__snapshots__/currency.js.snap | 132 +++++++++++++++++- .../api/__tests__/admin/currency.js | 87 +++++++++++- .../src/api/routes/admin/currencies/index.ts | 5 +- .../admin/currencies/list-currencies.ts | 10 +- 4 files changed, 222 insertions(+), 12 deletions(-) diff --git a/integration-tests/api/__tests__/admin/__snapshots__/currency.js.snap b/integration-tests/api/__tests__/admin/__snapshots__/currency.js.snap index a559d18dd2..a3b1f61f9b 100644 --- a/integration-tests/api/__tests__/admin/__snapshots__/currency.js.snap +++ b/integration-tests/api/__tests__/admin/__snapshots__/currency.js.snap @@ -1,6 +1,136 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`/admin/currencies GET /admin/currencies should retrieve the currencies 1`] = ` +Object { + "count": 120, + "currencies": Array [ + Object { + "code": "aed", + "name": "United Arab Emirates Dirham", + "symbol": "AED", + "symbol_native": "د.إ.‏", + }, + Object { + "code": "afn", + "name": "Afghan Afghani", + "symbol": "Af", + "symbol_native": "؋", + }, + Object { + "code": "all", + "name": "Albanian Lek", + "symbol": "ALL", + "symbol_native": "Lek", + }, + Object { + "code": "amd", + "name": "Armenian Dram", + "symbol": "AMD", + "symbol_native": "դր.", + }, + Object { + "code": "ars", + "name": "Argentine Peso", + "symbol": "AR$", + "symbol_native": "$", + }, + Object { + "code": "aud", + "name": "Australian Dollar", + "symbol": "AU$", + "symbol_native": "$", + }, + Object { + "code": "azn", + "name": "Azerbaijani Manat", + "symbol": "man.", + "symbol_native": "ман.", + }, + Object { + "code": "bam", + "name": "Bosnia-Herzegovina Convertible Mark", + "symbol": "KM", + "symbol_native": "KM", + }, + Object { + "code": "bdt", + "name": "Bangladeshi Taka", + "symbol": "Tk", + "symbol_native": "৳", + }, + Object { + "code": "bgn", + "name": "Bulgarian Lev", + "symbol": "BGN", + "symbol_native": "лв.", + }, + Object { + "code": "bhd", + "name": "Bahraini Dinar", + "symbol": "BD", + "symbol_native": "د.ب.‏", + }, + Object { + "code": "bif", + "name": "Burundian Franc", + "symbol": "FBu", + "symbol_native": "FBu", + }, + Object { + "code": "bnd", + "name": "Brunei Dollar", + "symbol": "BN$", + "symbol_native": "$", + }, + Object { + "code": "bob", + "name": "Bolivian Boliviano", + "symbol": "Bs", + "symbol_native": "Bs", + }, + Object { + "code": "brl", + "name": "Brazilian Real", + "symbol": "R$", + "symbol_native": "R$", + }, + Object { + "code": "bwp", + "name": "Botswanan Pula", + "symbol": "BWP", + "symbol_native": "P", + }, + Object { + "code": "byn", + "name": "Belarusian Ruble", + "symbol": "Br", + "symbol_native": "руб.", + }, + Object { + "code": "bzd", + "name": "Belize Dollar", + "symbol": "BZ$", + "symbol_native": "$", + }, + Object { + "code": "cad", + "name": "Canadian Dollar", + "symbol": "CA$", + "symbol_native": "$", + }, + Object { + "code": "cdf", + "name": "Congolese Franc", + "symbol": "CDF", + "symbol_native": "FrCD", + }, + ], + "limit": 20, + "offset": 0, +} +`; + +exports[`[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/currencies GET /admin/currencies should retrieve the currencies 1`] = ` Object { "count": 120, "currencies": Array [ @@ -150,7 +280,7 @@ Object { } `; -exports[`/admin/currencies POST /admin/currencies/:code should update currency includes_tax 1`] = ` +exports[`[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/currencies POST /admin/currencies/:code should update currency includes_tax 1`] = ` Object { "currency": Object { "code": "aed", diff --git a/integration-tests/api/__tests__/admin/currency.js b/integration-tests/api/__tests__/admin/currency.js index 5587b9d6a9..a3080ccac2 100644 --- a/integration-tests/api/__tests__/admin/currency.js +++ b/integration-tests/api/__tests__/admin/currency.js @@ -1,9 +1,10 @@ const path = require("path") +const setupServer = require("../../../helpers/setup-server") const startServerWithEnvironment = require("../../../helpers/start-server-with-environment").default const { useApi } = require("../../../helpers/use-api") -const { useDb } = require("../../../helpers/use-db") -const adminSeeder = require("../../helpers/admin-seeder"); +const { useDb, initDb } = require("../../../helpers/use-db") +const adminSeeder = require("../../helpers/admin-seeder") const adminReqConfig = { headers: { @@ -16,12 +17,86 @@ describe("/admin/currencies", () => { let medusaProcess let dbConnection + beforeAll(async () => { + const cwd = path.resolve(path.join(__dirname, "..", "..")) + dbConnection = await initDb({ cwd }) + medusaProcess = await setupServer({ cwd }) + }) + + afterAll(async () => { + const db = useDb() + await db.shutdown() + + medusaProcess.kill() + }) + + describe("GET /admin/currencies", function () { + beforeEach(async () => { + try { + await adminSeeder(dbConnection) + } catch (e) { + console.error(e) + } + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("should retrieve the currencies", async () => { + const api = useApi() + const response = await api.get( + `/admin/currencies?order=code`, + adminReqConfig + ) + + expect(response.data).toMatchSnapshot() + }) + }) + + describe("POST /admin/currencies/:code", function () { + beforeEach(async () => { + try { + await adminSeeder(dbConnection) + } catch (e) { + console.error(e) + } + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("should fail when attempting to update includes_tax", async () => { + const api = useApi() + + try { + await api.post( + `/admin/currencies/aed`, + { + includes_tax: true, + }, + adminReqConfig + ) + } catch (error) { + expect(error.response.data.message).toBe( + "property includes_tax should not exist" + ) + } + }) + }) +}) +describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/currencies", () => { + let medusaProcess + let dbConnection + beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) const [process, connection] = await startServerWithEnvironment({ cwd, env: { MEDUSA_FF_TAX_INCLUSIVE_PRICING: true }, - verbose: false, }) dbConnection = connection medusaProcess = process @@ -57,7 +132,7 @@ describe("/admin/currencies", () => { expect(response.data).toMatchSnapshot() }) - }); + }) describe("POST /admin/currencies/:code", function () { beforeEach(async () => { @@ -78,12 +153,12 @@ describe("/admin/currencies", () => { const response = await api.post( `/admin/currencies/aed`, { - includes_tax: true + includes_tax: true, }, adminReqConfig ) expect(response.data).toMatchSnapshot() }) - }); + }) }) diff --git a/packages/medusa/src/api/routes/admin/currencies/index.ts b/packages/medusa/src/api/routes/admin/currencies/index.ts index b222985739..9b0fd72126 100644 --- a/packages/medusa/src/api/routes/admin/currencies/index.ts +++ b/packages/medusa/src/api/routes/admin/currencies/index.ts @@ -4,7 +4,7 @@ import TaxInclusivePricingFeatureFlag from "../../../../loaders/feature-flags/ta import { PaginatedResponse } from "../../../../types/common" import middlewares, { transformBody, - transformQuery, + transformQuery } from "../../../middlewares" import { isFeatureFlagEnabled } from "../../../middlewares/feature-flag-enabled" import { AdminGetCurrenciesParams } from "./list-currencies" @@ -14,7 +14,6 @@ export default (app) => { const route = Router() app.use( "/currencies", - isFeatureFlagEnabled(TaxInclusivePricingFeatureFlag.key), route ) @@ -29,6 +28,7 @@ export default (app) => { route.post( "/:code", transformBody(AdminPostCurrenciesCurrencyReq), + isFeatureFlagEnabled(TaxInclusivePricingFeatureFlag.key), middlewares.wrap(require("./update-currency").default) ) @@ -45,3 +45,4 @@ export type AdminCurrenciesRes = { export * from "./list-currencies" export * from "./update-currency" + 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 4e92ed311f..ecdc325591 100644 --- a/packages/medusa/src/api/routes/admin/currencies/list-currencies.ts +++ b/packages/medusa/src/api/routes/admin/currencies/list-currencies.ts @@ -1,8 +1,10 @@ import { IsBoolean, IsOptional, IsString } from "class-validator" +import TaxInclusivePricingFeatureFlag from "../../../../loaders/feature-flags/tax-inclusive-pricing" import { Currency } from "../../../../models" import { CurrencyService } from "../../../../services" +import { FindPaginationParams } from "../../../../types/common" import { ExtendedRequest } from "../../../../types/global" -import { FindConfig, FindPaginationParams } from "../../../../types/common" +import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators" /** * @oas [get] /currencies @@ -66,8 +68,10 @@ export class AdminGetCurrenciesParams extends FindPaginationParams { @IsOptional() code?: string - @IsBoolean() - @IsOptional() + @FeatureFlagDecorators(TaxInclusivePricingFeatureFlag.key, [ + IsBoolean(), + IsOptional(), + ]) includes_tax?: boolean @IsString()