fix: Move default country loading for region to the loader, fix a bug with cascade (#6559)
This commit is contained in:
@@ -1,13 +1,30 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IRegionModuleService, LoaderOptions } from "@medusajs/types"
|
||||
import { Logger } from "@medusajs/types"
|
||||
import { LoaderOptions, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
import { DefaultsUtils } from "@medusajs/utils"
|
||||
import { Country } from "@models"
|
||||
|
||||
export default async ({ container }: LoaderOptions): Promise<void> => {
|
||||
const service: IRegionModuleService = container.resolve(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
// TODO: Add default logger to the container when running tests
|
||||
const logger =
|
||||
container.resolve<Logger>(ContainerRegistrationKeys.LOGGER) ?? console
|
||||
const countryService_: ModulesSdkTypes.InternalModuleService<Country> =
|
||||
container.resolve("countryService")
|
||||
|
||||
// TODO: Remove when legacy modules have been migrated
|
||||
if (!!process.env.MEDUSA_FF_MEDUSA_V2) {
|
||||
await service.createDefaultCountries()
|
||||
try {
|
||||
const normalizedCountries = DefaultsUtils.defaultCountries.map((c) => ({
|
||||
iso_2: c.alpha2.toLowerCase(),
|
||||
iso_3: c.alpha3.toLowerCase(),
|
||||
num_code: c.numeric,
|
||||
name: c.name.toUpperCase(),
|
||||
display_name: c.name,
|
||||
}))
|
||||
|
||||
const resp = await countryService_.upsert(normalizedCountries)
|
||||
logger.info(`Loaded ${resp.length} countries`)
|
||||
} catch (error) {
|
||||
logger.warn(
|
||||
`Failed to load countries, skipping loader. Original error: ${error.message}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
{
|
||||
"namespaces": ["public"],
|
||||
"namespaces": [
|
||||
"public"
|
||||
],
|
||||
"name": "public",
|
||||
"tables": [
|
||||
{
|
||||
@@ -77,7 +79,9 @@
|
||||
"schema": "public",
|
||||
"indexes": [
|
||||
{
|
||||
"columnNames": ["deleted_at"],
|
||||
"columnNames": [
|
||||
"deleted_at"
|
||||
],
|
||||
"composite": false,
|
||||
"keyName": "IDX_region_deleted_at",
|
||||
"primary": false,
|
||||
@@ -85,7 +89,9 @@
|
||||
},
|
||||
{
|
||||
"keyName": "region_pkey",
|
||||
"columnNames": ["id"],
|
||||
"columnNames": [
|
||||
"id"
|
||||
],
|
||||
"composite": false,
|
||||
"primary": true,
|
||||
"unique": true
|
||||
@@ -165,7 +171,9 @@
|
||||
"indexes": [
|
||||
{
|
||||
"keyName": "region_country_pkey",
|
||||
"columnNames": ["id"],
|
||||
"columnNames": [
|
||||
"id"
|
||||
],
|
||||
"composite": false,
|
||||
"primary": true,
|
||||
"unique": true
|
||||
@@ -175,9 +183,13 @@
|
||||
"foreignKeys": {
|
||||
"region_country_region_id_foreign": {
|
||||
"constraintName": "region_country_region_id_foreign",
|
||||
"columnNames": ["region_id"],
|
||||
"columnNames": [
|
||||
"region_id"
|
||||
],
|
||||
"localTableName": "public.region_country",
|
||||
"referencedColumnNames": ["id"],
|
||||
"referencedColumnNames": [
|
||||
"id"
|
||||
],
|
||||
"referencedTableName": "public.region",
|
||||
"deleteRule": "set null",
|
||||
"updateRule": "cascade"
|
||||
|
||||
@@ -38,7 +38,7 @@ CREATE TABLE IF NOT EXISTS "region_country" (
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "IDX_region_country_region_id_iso_2_unique" ON "region_country" (region_id, iso_2);
|
||||
-- Adjust foreign keys for "region_country"
|
||||
ALTER TABLE "region_country" DROP CONSTRAINT IF EXISTS "FK_91f88052197680f9790272aaf5b";
|
||||
ALTER TABLE "region_country" ADD CONSTRAINT "region_country_region_id_foreign" FOREIGN KEY ("region_id") REFERENCES "region" ("id") ON UPDATE CASCADE;
|
||||
ALTER TABLE "region_country" ADD CONSTRAINT "region_country_region_id_foreign" FOREIGN KEY ("region_id") REFERENCES "region" ("id") ON UPDATE CASCADE ON DELETE SET NULL;
|
||||
`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,9 @@ export default class Country {
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => Region,
|
||||
fieldName: "region_id",
|
||||
nullable: true,
|
||||
onDelete: "set null",
|
||||
})
|
||||
region?: Region | null
|
||||
|
||||
|
||||
@@ -21,18 +21,15 @@ import {
|
||||
MedusaError,
|
||||
ModulesSdkUtils,
|
||||
promiseAll,
|
||||
DefaultsUtils,
|
||||
removeUndefined,
|
||||
getDuplicates,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { Country, Region } from "@models"
|
||||
|
||||
import { CreateCountryDTO, UpdateRegionInput } from "@types"
|
||||
import { UpdateRegionInput } from "@types"
|
||||
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||
|
||||
const COUNTRIES_LIMIT = 1000
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
regionService: ModulesSdkTypes.InternalModuleService<any>
|
||||
@@ -280,7 +277,7 @@ export default class RegionModuleService<
|
||||
|
||||
const countriesInDb = await this.countryService_.list(
|
||||
{ iso_2: uniqueCountries },
|
||||
{ select: ["iso_2", "region_id"] },
|
||||
{ select: ["iso_2", "region_id"], take: null },
|
||||
sharedContext
|
||||
)
|
||||
const countryCodesInDb = countriesInDb.map((c) => c.iso_2.toLowerCase())
|
||||
@@ -311,43 +308,4 @@ export default class RegionModuleService<
|
||||
|
||||
return countriesInDb
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
public async createDefaultCountries(
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.maybeCreateCountries(sharedContext)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
private async maybeCreateCountries(
|
||||
@MedusaContext() sharedContext: Context
|
||||
): Promise<void> {
|
||||
const [countries, count] = await this.countryService_.listAndCount(
|
||||
{},
|
||||
{ select: ["iso_2"], take: COUNTRIES_LIMIT },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
let countsToCreate: CreateCountryDTO[] = []
|
||||
if (count !== DefaultsUtils.defaultCountries.length) {
|
||||
const countriesInDb = new Set(countries.map((c) => c.iso_2))
|
||||
|
||||
const countriesToAdd = DefaultsUtils.defaultCountries.filter(
|
||||
(c) => !countriesInDb.has(c.alpha2.toLowerCase())
|
||||
)
|
||||
|
||||
countsToCreate = countriesToAdd.map((c) => ({
|
||||
iso_2: c.alpha2.toLowerCase(),
|
||||
iso_3: c.alpha3.toLowerCase(),
|
||||
num_code: c.numeric,
|
||||
name: c.name.toUpperCase(),
|
||||
display_name: c.name,
|
||||
}))
|
||||
}
|
||||
|
||||
if (countsToCreate.length) {
|
||||
await this.countryService_.create(countsToCreate, sharedContext)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user