feat(region): Region create, delete, update admin endpoints (#6332)
**What** Add `POST /admin/regions` Add `POST /admin/regions/:id` Add `DELETE /admin/regions/:id` All are added for v2 using API Routes and workflows. In follow-up PRs, I will add support for passing countries in update and create. Update: First follow-up PR is #6372
This commit is contained in:
1 parent
1d91b7429b
commit
691f68c3b8
22 files changed
+586
-169
No files matched your search
@@ -2,6 +2,7 @@ import {
|
||||
Context,
|
||||
CreateRegionDTO,
|
||||
DAL,
|
||||
FilterableRegionProps,
|
||||
InternalModuleDeclaration,
|
||||
IRegionModuleService,
|
||||
ModuleJoinerConfig,
|
||||
@@ -9,11 +10,14 @@ import {
|
||||
RegionCountryDTO,
|
||||
RegionCurrencyDTO,
|
||||
RegionDTO,
|
||||
UpdatableRegionFields,
|
||||
UpdateRegionDTO,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
isObject,
|
||||
isString,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
ModulesSdkUtils,
|
||||
@@ -188,26 +192,68 @@ export default class RegionModuleService<
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdateRegionDTO[],
|
||||
selector: FilterableRegionProps,
|
||||
data: UpdatableRegionFields,
|
||||
sharedContext?: Context
|
||||
): Promise<RegionDTO[]>
|
||||
async update(
|
||||
data: UpdateRegionDTO,
|
||||
regionId: string,
|
||||
data: UpdatableRegionFields,
|
||||
sharedContext?: Context
|
||||
): Promise<RegionDTO>
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async update(data: UpdateRegionDTO[]): Promise<RegionDTO[]>
|
||||
@InjectManager("baseRepository_")
|
||||
async update(
|
||||
data: UpdateRegionDTO | UpdateRegionDTO[],
|
||||
idOrSelectorOrData: string | FilterableRegionProps | UpdateRegionDTO[],
|
||||
data?: UpdatableRegionFields,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<RegionDTO | RegionDTO[]> {
|
||||
const result = await this.regionService_.update(data, sharedContext)
|
||||
const result = await this.update_(idOrSelectorOrData, data, sharedContext)
|
||||
|
||||
return await this.baseRepository_.serialize<RegionDTO[]>(
|
||||
Array.isArray(data) ? result : result[0],
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
const regions = await this.baseRepository_.serialize<
|
||||
RegionDTO[] | RegionDTO
|
||||
>(result)
|
||||
|
||||
return isString(idOrSelectorOrData) ? regions[0] : regions
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async update_(
|
||||
idOrSelectorOrData: string | FilterableRegionProps | UpdateRegionDTO[],
|
||||
data?: UpdatableRegionFields,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<Region[]> {
|
||||
let result: Region[] = []
|
||||
if (isString(idOrSelectorOrData)) {
|
||||
result = await this.regionService_.update(
|
||||
[{ id: idOrSelectorOrData, ...data }],
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
if (Array.isArray(idOrSelectorOrData)) {
|
||||
result = await this.regionService_.update(
|
||||
idOrSelectorOrData,
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
if (isObject(idOrSelectorOrData)) {
|
||||
let toUpdate: Partial<UpdateRegionDTO>[] = []
|
||||
const regions = await this.regionService_.list(
|
||||
{ ...idOrSelectorOrData },
|
||||
{},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
regions.forEach((region) => {
|
||||
toUpdate.push({ id: region.id, ...data })
|
||||
})
|
||||
|
||||
result = await this.regionService_.update(toUpdate, sharedContext)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
|
||||
Reference in new issue
Block a user