From b5f248f1544489f0bc709fa6c9b55721216b19e7 Mon Sep 17 00:00:00 2001 From: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Wed, 26 Jun 2024 18:44:16 +0200 Subject: [PATCH] fix(region): Soft deleting region unsets region on countries (#7786) **What** Unset `region_id` on countries when soft deleting the associated region. Otherwise, countries cannot be added to new or other regions Alternatively, we hard delete the region, which will trigger the DB level cascade In both cases, we cannot easily restore the region countries. --- .../__tests__/region-module.spec.ts | 24 ++++++++++++++++++- .../region/src/services/region-module.ts | 21 ++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/packages/modules/region/integration-tests/__tests__/region-module.spec.ts b/packages/modules/region/integration-tests/__tests__/region-module.spec.ts index f0d9a3b9b9..1683e3c5bd 100644 --- a/packages/modules/region/integration-tests/__tests__/region-module.spec.ts +++ b/packages/modules/region/integration-tests/__tests__/region-module.spec.ts @@ -1,6 +1,6 @@ import { IRegionModuleService } from "@medusajs/types" -import { moduleIntegrationTestRunner } from "medusa-test-utils" import { Modules } from "@medusajs/utils" +import { moduleIntegrationTestRunner } from "medusa-test-utils" jest.setTimeout(30000) @@ -368,6 +368,28 @@ moduleIntegrationTestRunner({ expect(resp.countries).toHaveLength(2) }) + + it("should unset the region ID on the country when soft deleting a region", async () => { + const createdRegion = await service.createRegions({ + name: "North America", + currency_code: "USD", + countries: ["us", "ca"], + }) + + await service.softDeleteRegions([createdRegion.id]) + + const newRegion = await service.createRegions({ + name: "North America", + currency_code: "USD", + countries: ["us", "ca"], + }) + + const resp = await service.retrieveRegion(newRegion.id, { + relations: ["countries"], + }) + + expect(resp.countries).toHaveLength(2) + }) }) }, }) diff --git a/packages/modules/region/src/services/region-module.ts b/packages/modules/region/src/services/region-module.ts index 7d3ddbba38..b7fda5cb48 100644 --- a/packages/modules/region/src/services/region-module.ts +++ b/packages/modules/region/src/services/region-module.ts @@ -9,6 +9,7 @@ import { ModulesSdkTypes, RegionCountryDTO, RegionDTO, + SoftDeleteReturn, UpdateRegionDTO, UpsertRegionDTO, } from "@medusajs/types" @@ -129,6 +130,26 @@ export default class RegionModuleService return result } + @InjectManager("baseRepository_") + // @ts-ignore + async softDeleteRegions( + ids: string | object | string[] | object[], + config?: SoftDeleteReturn, + @MedusaContext() sharedContext: Context = {} + ): Promise | void> { + const result = await super.softDeleteRegions(ids, config, sharedContext) + // Note: You cannot revert the state of a region by simply restoring it. The association with countries is lost. + await super.updateCountries( + { + selector: { region_id: ids }, + data: { region_id: null }, + }, + sharedContext + ) + + return result + } + async upsertRegions( data: UpsertRegionDTO[], sharedContext?: Context