feat: Update signature of region module to match latest spec (#6487)

This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-02-23 15:56:16 +00:00
1 parent c86e27bd0d
commit 5b85c3103e
8 files changed
+174 -87

No files matched your search

@@ -167,6 +167,68 @@ describe("Region Module Service", () => {
)
})
it("should upsert the region successfully", async () => {
const createdRegion = await service.upsert({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await service.upsert({
id: createdRegion.id,
name: "Americas",
currency_code: "MXN",
countries: ["us", "mx"],
})
const latestRegion = await service.retrieve(createdRegion.id, {
relations: ["currency", "countries"],
})
expect(latestRegion).toMatchObject({
id: createdRegion.id,
name: "Americas",
currency_code: "mxn",
})
expect(latestRegion.countries.map((c) => c.iso_2)).toEqual(["mx", "us"])
})
it("should allow mixing create and update operations in upsert", async () => {
const createdRegion = await service.upsert({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
const upserted = await service.upsert([
{
id: createdRegion.id,
name: "Americas",
currency_code: "USD",
countries: ["us", "ca"],
},
{
name: "Central America",
currency_code: "MXN",
countries: ["mx"],
},
])
expect(upserted).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: createdRegion.id,
name: "Americas",
currency_code: "usd",
}),
expect.objectContaining({
name: "Central America",
currency_code: "mxn",
}),
])
)
})
it("should update the region successfully", async () => {
const createdRegion = await service.create({
name: "North America",
+64 -42
View File
@@ -10,14 +10,13 @@ import {
RegionCountryDTO,
RegionCurrencyDTO,
RegionDTO,
UpdatableRegionFields,
UpdateRegionDTO,
UpsertRegionDTO,
} from "@medusajs/types"
import {
arrayDifference,
InjectManager,
InjectTransactionManager,
isObject,
isString,
MedusaContext,
MedusaError,
@@ -30,7 +29,7 @@ import {
import { Country, Currency, Region } from "@models"
import { CreateCountryDTO, CreateCurrencyDTO } from "@types"
import { CreateCountryDTO, CreateCurrencyDTO, UpdateRegionInput } from "@types"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
const COUNTRIES_LIMIT = 1000
@@ -143,54 +142,64 @@ export default class RegionModuleService<
return await this.regionService_.create(normalizedDbRegions, sharedContext)
}
async update(
selector: FilterableRegionProps,
data: UpdatableRegionFields,
async upsert(
data: UpsertRegionDTO[],
sharedContext?: Context
): Promise<RegionDTO[]>
async update(
regionId: string,
data: UpdatableRegionFields,
async upsert(
data: UpsertRegionDTO,
sharedContext?: Context
): Promise<RegionDTO>
async update(data: UpdateRegionDTO[]): Promise<RegionDTO[]>
@InjectManager("baseRepository_")
async update(
idOrSelectorOrData: string | FilterableRegionProps | UpdateRegionDTO[],
data?: UpdatableRegionFields,
@InjectTransactionManager("baseRepository_")
async upsert(
data: UpsertRegionDTO | UpsertRegionDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<RegionDTO | RegionDTO[]> {
const updateResult = await this.update_(
idOrSelectorOrData,
data,
sharedContext
const input = Array.isArray(data) ? data : [data]
const forUpdate = input.filter(
(region): region is UpdateRegionInput => !!region.id
)
const forCreate = input.filter(
(region): region is CreateRegionDTO => !region.id
)
const regions = await this.baseRepository_.serialize<
RegionDTO[] | RegionDTO
>(updateResult)
const operations: Promise<Region[]>[] = []
return isString(idOrSelectorOrData) ? regions[0] : regions
if (forCreate.length) {
operations.push(this.create_(forCreate, sharedContext))
}
if (forUpdate.length) {
operations.push(this.update_(forUpdate, sharedContext))
}
const result = (await promiseAll(operations)).flat()
return await this.baseRepository_.serialize<RegionDTO[] | RegionDTO>(
Array.isArray(data) ? result : result[0]
)
}
@InjectTransactionManager("baseRepository_")
protected async update_(
idOrSelectorOrData: string | FilterableRegionProps | UpdateRegionDTO[],
data?: UpdatableRegionFields,
async update(
id: string,
data: UpdateRegionDTO,
sharedContext?: Context
): Promise<RegionDTO>
async update(
selector: FilterableRegionProps,
data: UpdateRegionDTO,
sharedContext?: Context
): Promise<RegionDTO[]>
@InjectManager("baseRepository_")
async update(
idOrSelector: string | FilterableRegionProps,
data: UpdateRegionDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<Region[]> {
let normalizedInput: UpdateRegionDTO[] = []
if (isString(idOrSelectorOrData)) {
normalizedInput = [{ id: idOrSelectorOrData, ...data }]
}
if (Array.isArray(idOrSelectorOrData)) {
normalizedInput = idOrSelectorOrData
}
if (isObject(idOrSelectorOrData)) {
): Promise<RegionDTO | RegionDTO[]> {
let normalizedInput: UpdateRegionInput[] = []
if (isString(idOrSelector)) {
normalizedInput = [{ id: idOrSelector, ...data }]
} else {
const regions = await this.regionService_.list(
idOrSelectorOrData,
idOrSelector,
{},
sharedContext
)
@@ -200,7 +209,22 @@ export default class RegionModuleService<
...data,
}))
}
normalizedInput = RegionModuleService.normalizeInput(normalizedInput)
const updateResult = await this.update_(normalizedInput, sharedContext)
const regions = await this.baseRepository_.serialize<
RegionDTO[] | RegionDTO
>(updateResult)
return isString(idOrSelector) ? regions[0] : regions
}
@InjectTransactionManager("baseRepository_")
protected async update_(
data: UpdateRegionInput[],
@MedusaContext() sharedContext: Context = {}
): Promise<Region[]> {
const normalizedInput = RegionModuleService.normalizeInput(data)
// If countries are being updated for a region, first make previously set countries' region to null to get to a clean slate.
// Somewhat less efficient, but region operations will be very rare, so it is better to go with a simple solution
@@ -245,9 +269,7 @@ export default class RegionModuleService<
return await this.regionService_.update(normalizedDbRegions, sharedContext)
}
private static normalizeInput<T extends UpdatableRegionFields>(
regions: T[]
): T[] {
private static normalizeInput<T extends UpdateRegionDTO>(regions: T[]): T[] {
return regions.map((region) =>
removeUndefined({
...region,
+3 -1
View File
@@ -1,4 +1,4 @@
import { Logger } from "@medusajs/types"
import { Logger, UpdateRegionDTO } from "@medusajs/types"
import { Country } from "@models"
export type InitializeModuleInjectableDependencies = {
@@ -24,3 +24,5 @@ export type CreateCountryDTO = {
name: string
display_name: string
}
export type UpdateRegionInput = UpdateRegionDTO & { id: string }