chore(tests): Try to use the api integration tests for v2 (#6588)

Few things to keep in mind:
1. You need to set MEDUSA_FF_MEDUSA_V2 to true before running the tests to run with the v2 API
2. You can use the `breaking` function to differentiate between v1 and v2 differences. This can help us identify what was breaking pretty quickly afterwards
3. You will need to run specific tests for now instead of all if you want to target v2. I think that's fine though, as we don't really need these to run on every PR until we have feature parity (and by then, all tests would be both v1 and v2 compatible)


**note: Adrien** 
- add a new way to load modules only to run their loaders comparable to the way to run the migrations only
- improve tests runner to cleanup the data properly as well as re running all loaders and core defaults

Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
Stevche Radevski
2024-03-07 08:05:43 +00:00
committed by GitHub
co-authored by Adrien de Peretti
parent 000eb61e33
commit 12b035cb18
76 changed files with 773 additions and 371 deletions
@@ -35,6 +35,15 @@
"default": "'{}'",
"mappedType": "array"
},
"default_currency_code": {
"name": "default_currency_code",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
"default_sales_channel_id": {
"name": "default_sales_channel_id",
"type": "text",
@@ -13,6 +13,12 @@ export class InitialSetup20240226130829 extends Migration {
this.addSql(
`alter table "store" alter column "name" SET DEFAULT 'Medusa Store';`
)
this.addSql(
`alter table "store" alter column "default_currency_code" TYPE text;`
)
this.addSql(
`alter table "store" alter column "default_currency_code" drop not null;`
)
this.addSql(
`alter table "store" alter column "default_sales_channel_id" TYPE text;`
)
@@ -20,7 +26,9 @@ export class InitialSetup20240226130829 extends Migration {
`alter table "store" alter column "default_location_id" TYPE text;`
)
this.addSql(`alter table "store" add column "default_region_id" text;`)
this.addSql(
`alter table "store" add column "default_region_id" text null;`
)
this.addSql(
`alter table "store" add column "deleted_at" timestamptz null;`
)
@@ -32,6 +40,13 @@ export class InitialSetup20240226130829 extends Migration {
'create index if not exists "IDX_store_deleted_at" on "store" (deleted_at) where deleted_at is not null;'
)
this.addSql(
`alter table "store" drop constraint if exists "FK_61b0f48cccbb5f41c750bac7286";`
)
this.addSql(
`alter table "store" drop constraint if exists "FK_55beebaa09e947cccca554af222";`
)
// this.addSql(`alter table "store" drop column "default_currency_code";`)
// this.addSql(`alter table "store" drop column "swap_link_template";`)
// this.addSql(`alter table "store" drop column "payment_link_template";`)
@@ -39,7 +54,7 @@ export class InitialSetup20240226130829 extends Migration {
} else {
this.addSql(`create table if not exists "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,
"default_currency_code" text null, "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"));`)
+3
View File
@@ -38,6 +38,9 @@ export default class Store {
@Property({ type: "array", default: "{}" })
supported_currency_codes: string[] = []
@Property({ columnType: "text", nullable: true })
default_currency_code: string | null = null
@Property({ columnType: "text", nullable: true })
default_sales_channel_id: string | null = null
@@ -11,6 +11,7 @@ import {
InjectManager,
InjectTransactionManager,
MedusaContext,
MedusaError,
ModulesSdkUtils,
isString,
promiseAll,
@@ -83,6 +84,8 @@ export default class StoreModuleService<TEntity extends Store = Store>
@MedusaContext() sharedContext: Context = {}
): Promise<Store[]> {
let normalizedInput = StoreModuleService.normalizeInput(data)
StoreModuleService.validateCreateRequest(normalizedInput)
return await this.storeService_.create(normalizedInput, sharedContext)
}
@@ -169,6 +172,7 @@ export default class StoreModuleService<TEntity extends Store = Store>
@MedusaContext() sharedContext: Context = {}
): Promise<Store[]> {
const normalizedInput = StoreModuleService.normalizeInput(data)
await this.validateUpdateRequest(normalizedInput)
return await this.storeService_.update(normalizedInput, sharedContext)
}
@@ -182,4 +186,80 @@ export default class StoreModuleService<TEntity extends Store = Store>
})
)
}
private static validateCreateRequest(stores: StoreTypes.CreateStoreDTO[]) {
for (const store of stores) {
// If we are setting the default currency code on creating, make sure it is supported
if (store.default_currency_code) {
if (
!store.supported_currency_codes?.includes(
store.default_currency_code ?? ""
)
) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Store does not have currency: ${store.default_currency_code}`
)
}
}
}
}
private async validateUpdateRequest(stores: UpdateStoreInput[]) {
const dbStores = await this.storeService_.list(
{ id: stores.map((s) => s.id) },
{ take: null }
)
const dbStoresMap = new Map<string, Store>(
dbStores.map((dbStore) => [dbStore.id, dbStore])
)
for (const store of stores) {
const dbStore = dbStoresMap.get(store.id)
// If it is updating both the supported currency codes and the default one, look in that list
if (store.supported_currency_codes && store.default_currency_code) {
if (
!store.supported_currency_codes.includes(
store.default_currency_code ?? ""
)
) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Store does not have currency: ${store.default_currency_code}`
)
}
return
}
// If it is updating only the default currency code, look in the db store
if (store.default_currency_code) {
if (
!dbStore?.supported_currency_codes?.includes(
store.default_currency_code
)
) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Store does not have currency: ${store.default_currency_code}`
)
}
}
// If it is updating only the supported currency codes, make sure one of them is not set as a default one
if (store.supported_currency_codes) {
if (
!store.supported_currency_codes.includes(
dbStore?.default_currency_code ?? ""
)
) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"You are not allowed to remove default currency from store currencies without replacing it as well"
)
}
}
}
}
}