From 71ed21de4abe12bfcee37aa5ce4f103aebe944ad Mon Sep 17 00:00:00 2001 From: Stevche Radevski Date: Fri, 1 Mar 2024 16:43:47 +0100 Subject: [PATCH] feat: Add supported currencies to store model (#6562) I ultimately went with having a flat list of settings for the store. It wouldn't be very difficult to change it if we wish to do so, but for now this keeps the codebase simpler --- .../modules/__tests__/store/admin/store.spec.ts | 6 +++++- .../medusa/src/api-v2/admin/stores/query-config.ts | 1 + .../medusa/src/api-v2/admin/stores/validators.ts | 12 +++++++++++- .../store/integration-tests/__fixtures__/index.ts | 1 + .../__tests__/store-module-service.spec.ts | 1 + .../store/src/migrations/.snapshot-medusa-store.json | 10 ++++++++++ .../src/migrations/InitialSetup20240227075933.ts | 5 ++++- packages/store/src/models/store.ts | 3 +++ packages/types/src/store/common/store.ts | 1 + packages/types/src/store/mutations/store.ts | 3 +++ 10 files changed, 40 insertions(+), 3 deletions(-) diff --git a/integration-tests/modules/__tests__/store/admin/store.spec.ts b/integration-tests/modules/__tests__/store/admin/store.spec.ts index 8b3d5c2709..e1c57090c3 100644 --- a/integration-tests/modules/__tests__/store/admin/store.spec.ts +++ b/integration-tests/modules/__tests__/store/admin/store.spec.ts @@ -46,11 +46,15 @@ describe("Store - Admin", () => { it("should correctly implement the entire lifecycle of a store", async () => { const api = useApi() as any - const createdStore = await service.create({ name: "Test store" }) + const createdStore = await service.create({ + name: "Test store", + supported_currency_codes: ["usd"], + }) expect(createdStore).toEqual( expect.objectContaining({ id: createdStore.id, + supported_currency_codes: ["usd"], name: "Test store", }) ) diff --git a/packages/medusa/src/api-v2/admin/stores/query-config.ts b/packages/medusa/src/api-v2/admin/stores/query-config.ts index 9140c2f213..908cab05a0 100644 --- a/packages/medusa/src/api-v2/admin/stores/query-config.ts +++ b/packages/medusa/src/api-v2/admin/stores/query-config.ts @@ -3,6 +3,7 @@ export const allowedAdminStoreRelations = [] export const defaultAdminStoreFields = [ "id", "name", + "supported_currency_codes", "default_sales_channel_id", "default_region_id", "default_location_id", diff --git a/packages/medusa/src/api-v2/admin/stores/validators.ts b/packages/medusa/src/api-v2/admin/stores/validators.ts index 39784fe71d..8a8295d9f8 100644 --- a/packages/medusa/src/api-v2/admin/stores/validators.ts +++ b/packages/medusa/src/api-v2/admin/stores/validators.ts @@ -1,5 +1,11 @@ import { Type } from "class-transformer" -import { IsObject, IsOptional, IsString, ValidateNested } from "class-validator" +import { + IsArray, + IsObject, + IsOptional, + IsString, + ValidateNested, +} from "class-validator" import { FindParams, extendedFindParamsMixin } from "../../../types/common" export class AdminGetStoresStoreParams extends FindParams {} @@ -41,6 +47,10 @@ export class AdminPostStoresStoreReq { @IsString() name?: string + @IsOptional() + @IsArray() + supported_currency_codes?: string[] + @IsOptional() @IsString() default_sales_channel_id?: string diff --git a/packages/store/integration-tests/__fixtures__/index.ts b/packages/store/integration-tests/__fixtures__/index.ts index 39b33936be..fa59befca4 100644 --- a/packages/store/integration-tests/__fixtures__/index.ts +++ b/packages/store/integration-tests/__fixtures__/index.ts @@ -2,6 +2,7 @@ import { StoreTypes } from "@medusajs/types" export const createStoreFixture: StoreTypes.CreateStoreDTO = { name: "Test store", + supported_currency_codes: ["usd", "eur"], default_sales_channel_id: "test-sales-channel", default_region_id: "test-region", metadata: { diff --git a/packages/store/integration-tests/__tests__/store-module-service.spec.ts b/packages/store/integration-tests/__tests__/store-module-service.spec.ts index 5c8ca1b894..470f236731 100644 --- a/packages/store/integration-tests/__tests__/store-module-service.spec.ts +++ b/packages/store/integration-tests/__tests__/store-module-service.spec.ts @@ -19,6 +19,7 @@ moduleIntegrationTestRunner({ expect(store).toEqual( expect.objectContaining({ name: "Test store", + supported_currency_codes: expect.arrayContaining(["eur", "usd"]), default_sales_channel_id: "test-sales-channel", default_region_id: "test-region", metadata: { diff --git a/packages/store/src/migrations/.snapshot-medusa-store.json b/packages/store/src/migrations/.snapshot-medusa-store.json index 072267583f..5faa1a6990 100644 --- a/packages/store/src/migrations/.snapshot-medusa-store.json +++ b/packages/store/src/migrations/.snapshot-medusa-store.json @@ -25,6 +25,16 @@ "default": "'Medusa Store'", "mappedType": "text" }, + "supported_currency_codes": { + "name": "supported_currency_codes", + "type": "text[]", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "'{}'", + "mappedType": "array" + }, "default_sales_channel_id": { "name": "default_sales_channel_id", "type": "text", diff --git a/packages/store/src/migrations/InitialSetup20240227075933.ts b/packages/store/src/migrations/InitialSetup20240227075933.ts index ca30f4ac80..668c1ab991 100644 --- a/packages/store/src/migrations/InitialSetup20240227075933.ts +++ b/packages/store/src/migrations/InitialSetup20240227075933.ts @@ -24,6 +24,9 @@ export class InitialSetup20240226130829 extends Migration { this.addSql( `alter table "store" add column "deleted_at" timestamptz null;` ) + this.addSql( + `alter table "store" add column "supported_currency_codes" text[] not null default \'{}\';` + ) this.addSql( 'create index if not exists "IDX_store_deleted_at" on "store" (deleted_at) where deleted_at is not null;' @@ -35,7 +38,7 @@ export class InitialSetup20240226130829 extends Migration { // this.addSql(`alter table "store" drop column "invite_link_template";`) } else { this.addSql(`create table if not exists "store" - ("id" text not null, "name" text not null default \'Medusa Store\', + ("id" text not null, "name" text not null default \'Medusa Store\', "supported_currency_codes" text[] not null default \'{}\', "default_sales_channel_id" text null, "default_region_id" text null, "default_location_id" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "store_pkey" primary key ("id"));`) diff --git a/packages/store/src/models/store.ts b/packages/store/src/models/store.ts index d3d48ef17f..8c9015fac2 100644 --- a/packages/store/src/models/store.ts +++ b/packages/store/src/models/store.ts @@ -35,6 +35,9 @@ export default class Store { @Property({ columnType: "text", default: "Medusa Store" }) name: string + @Property({ type: "array", default: "{}" }) + supported_currency_codes: string[] = [] + @Property({ columnType: "text", nullable: true }) default_sales_channel_id: string | null = null diff --git a/packages/types/src/store/common/store.ts b/packages/types/src/store/common/store.ts index d013612e5b..a852771d5f 100644 --- a/packages/types/src/store/common/store.ts +++ b/packages/types/src/store/common/store.ts @@ -3,6 +3,7 @@ import { BaseFilterable } from "../../dal" export interface StoreDTO { id: string name: string + supported_currency_codes: string[] default_sales_channel_id?: string default_region_id?: string default_location_id?: string diff --git a/packages/types/src/store/mutations/store.ts b/packages/types/src/store/mutations/store.ts index 59b6475252..eaf391b3e0 100644 --- a/packages/types/src/store/mutations/store.ts +++ b/packages/types/src/store/mutations/store.ts @@ -1,5 +1,6 @@ export interface CreateStoreDTO { name?: string + supported_currency_codes?: string[] default_sales_channel_id?: string default_region_id?: string default_location_id?: string @@ -9,6 +10,7 @@ export interface CreateStoreDTO { export interface UpsertStoreDTO { id?: string name?: string + supported_currency_codes?: string[] default_sales_channel_id?: string default_region_id?: string default_location_id?: string @@ -17,6 +19,7 @@ export interface UpsertStoreDTO { export interface UpdateStoreDTO { name?: string + supported_currency_codes?: string[] default_sales_channel_id?: string default_region_id?: string default_location_id?: string