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,3 +1,8 @@
|
||||
import {
|
||||
deleteRegionsWorkflow,
|
||||
updateRegionsWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import { UpdatableRegionFields } from "@medusajs/types"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
import { defaultAdminRegionFields } from "../query-config"
|
||||
@@ -17,3 +22,38 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
|
||||
res.status(200).json({ region })
|
||||
}
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { result, errors } = await updateRegionsWorkflow(req.scope).run({
|
||||
input: {
|
||||
selector: { id: req.params.id },
|
||||
update: req.validatedBody as UpdatableRegionFields,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ region: result[0] })
|
||||
}
|
||||
|
||||
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const id = req.params.id
|
||||
|
||||
const { errors } = await deleteRegionsWorkflow(req.scope).run({
|
||||
input: { ids: [id] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
object: "region",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { transformQuery } from "../../../api/middlewares"
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
AdminGetRegionsParams,
|
||||
AdminGetRegionsRegionParams,
|
||||
AdminPostRegionsRegionReq,
|
||||
AdminPostRegionsReq,
|
||||
} from "./validators"
|
||||
|
||||
export const adminRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
@@ -27,4 +29,14 @@ export const adminRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/regions",
|
||||
middlewares: [transformBody(AdminPostRegionsReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/regions/:id",
|
||||
middlewares: [transformBody(AdminPostRegionsRegionReq)],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
export const defaultAdminRegionRelations = ["countries", "currency"]
|
||||
export const allowedAdminRegionRelations = ["countries", "currency"]
|
||||
export const defaultAdminRegionFields = [
|
||||
"id",
|
||||
"name",
|
||||
@@ -18,6 +20,9 @@ export const defaultAdminRegionFields = [
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultAdminRegionFields,
|
||||
defaultRelations: defaultAdminRegionRelations,
|
||||
allowedRelations: allowedAdminRegionRelations,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { createRegionsWorkflow } from "@medusajs/core-flows"
|
||||
import { CreateRegionDTO } from "@medusajs/types"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||
import { defaultAdminRegionFields } from "./query-config"
|
||||
@@ -5,16 +7,42 @@ import { defaultAdminRegionFields } from "./query-config"
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const variables = { filters: req.filterableFields }
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "region",
|
||||
variables,
|
||||
variables: {
|
||||
filters: req.filterableFields,
|
||||
order: req.listConfig.order,
|
||||
skip: req.listConfig.skip,
|
||||
take: req.listConfig.take,
|
||||
},
|
||||
fields: defaultAdminRegionFields,
|
||||
})
|
||||
|
||||
// TODO: Add count, offset, limit
|
||||
const regions = await remoteQuery(queryObject)
|
||||
const { rows: regions, metadata } = await remoteQuery(queryObject)
|
||||
|
||||
res.json({ regions })
|
||||
res.json({
|
||||
regions,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
})
|
||||
}
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const input = [
|
||||
{
|
||||
...(req.validatedBody as CreateRegionDTO),
|
||||
},
|
||||
]
|
||||
|
||||
const { result, errors } = await createRegionsWorkflow(req.scope).run({
|
||||
input: { regionsData: input },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ region: result[0] })
|
||||
}
|
||||
|
||||
@@ -69,3 +69,21 @@ export class AdminGetRegionsParams extends extendedFindParamsMixin({
|
||||
@Type(() => AdminGetRegionsParams)
|
||||
$or?: AdminGetRegionsParams[]
|
||||
}
|
||||
|
||||
export class AdminPostRegionsReq {
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@IsString()
|
||||
currency_code: string
|
||||
}
|
||||
|
||||
export class AdminPostRegionsRegionReq {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
name?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
currency_code?: string
|
||||
}
|
||||
|
||||
@@ -10,11 +10,11 @@ import {
|
||||
import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column"
|
||||
|
||||
import { BaseEntity } from "../interfaces/models/base-entity"
|
||||
import { generateEntityId } from "../utils/generate-entity-id"
|
||||
import { Cart } from "./cart"
|
||||
import { Currency } from "./currency"
|
||||
import { Order } from "./order"
|
||||
import { Swap } from "./swap"
|
||||
import { generateEntityId } from "../utils/generate-entity-id"
|
||||
|
||||
@Index(["cart_id"], { where: "canceled_at IS NOT NULL" })
|
||||
@Index("UniquePaymentActive", ["cart_id"], {
|
||||
|
||||
Reference in New Issue
Block a user