feat(region): Add admin region get + list endpoints (#6322)
**What** Add `GET /admin/regions` Add `GET /admin/regions/:id` Blocked by #6320 Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
co-authored by
Adrien de Peretti
parent
b91a1ca5b8
commit
95d0e58d31
@@ -0,0 +1,19 @@
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
import { defaultAdminRegionFields } from "../query-config"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const variables = { id: req.params.id }
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "region",
|
||||
variables,
|
||||
fields: defaultAdminRegionFields,
|
||||
})
|
||||
|
||||
const [region] = await remoteQuery(queryObject)
|
||||
|
||||
res.status(200).json({ region })
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
AdminGetRegionsParams,
|
||||
AdminGetRegionsRegionParams,
|
||||
} from "./validators"
|
||||
|
||||
export const adminRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/regions",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetRegionsParams,
|
||||
QueryConfig.listTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/regions/:id",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetRegionsRegionParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,27 @@
|
||||
export const defaultAdminRegionFields = [
|
||||
"id",
|
||||
"name",
|
||||
"currency_code",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
"metadata",
|
||||
"countries.id",
|
||||
"countries.iso_2",
|
||||
"countries.iso_3",
|
||||
"countries.num_code",
|
||||
"countries.name",
|
||||
"currency.code",
|
||||
"currency.symbol",
|
||||
"currency.symbol_native",
|
||||
"currency.name",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listTransformQueryConfig = {
|
||||
defaultLimit: 20,
|
||||
isList: true,
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||
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,
|
||||
fields: defaultAdminRegionFields,
|
||||
})
|
||||
|
||||
// TODO: Add count, offset, limit
|
||||
const regions = await remoteQuery(queryObject)
|
||||
|
||||
res.json({ regions })
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { OperatorMap } from "@medusajs/types"
|
||||
import { Type } from "class-transformer"
|
||||
import { IsOptional, IsString, ValidateNested } from "class-validator"
|
||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
||||
import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
||||
|
||||
export class AdminGetRegionsRegionParams extends FindParams {}
|
||||
|
||||
/**
|
||||
* Parameters used to filter and configure the pagination of the retrieved regions.
|
||||
*/
|
||||
export class AdminGetRegionsParams extends extendedFindParamsMixin({
|
||||
limit: 50,
|
||||
offset: 0,
|
||||
}) {
|
||||
/**
|
||||
* Search parameter for regions.
|
||||
*/
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
id?: string | string[]
|
||||
|
||||
/**
|
||||
* Filter by currency code
|
||||
*/
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
code?: string | string[]
|
||||
|
||||
/**
|
||||
* Filter by region name
|
||||
*/
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
name?: string | string[]
|
||||
|
||||
/**
|
||||
* Date filters to apply on the regions' `created_at` date.
|
||||
*/
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => OperatorMapValidator)
|
||||
created_at?: OperatorMap<string>
|
||||
|
||||
/**
|
||||
* Date filters to apply on the regions' `updated_at` date.
|
||||
*/
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => OperatorMapValidator)
|
||||
updated_at?: OperatorMap<string>
|
||||
|
||||
/**
|
||||
* Date filters to apply on the regions' `deleted_at` date.
|
||||
*/
|
||||
@ValidateNested()
|
||||
@IsOptional()
|
||||
@Type(() => OperatorMapValidator)
|
||||
deleted_at?: OperatorMap<string>
|
||||
|
||||
// Additional filters from BaseFilterable
|
||||
@IsOptional()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => AdminGetRegionsParams)
|
||||
$and?: AdminGetRegionsParams[]
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => AdminGetRegionsParams)
|
||||
$or?: AdminGetRegionsParams[]
|
||||
}
|
||||
Reference in New Issue
Block a user