feat(region): Add store region get + list endpoints (#6342)
This commit is contained in:
@@ -7,6 +7,7 @@ import { adminRegionRoutesMiddlewares } from "./admin/regions/middlewares"
|
|||||||
import { authRoutesMiddlewares } from "./auth/middlewares"
|
import { authRoutesMiddlewares } from "./auth/middlewares"
|
||||||
import { storeCartRoutesMiddlewares } from "./store/carts/middlewares"
|
import { storeCartRoutesMiddlewares } from "./store/carts/middlewares"
|
||||||
import { storeCustomerRoutesMiddlewares } from "./store/customers/middlewares"
|
import { storeCustomerRoutesMiddlewares } from "./store/customers/middlewares"
|
||||||
|
import { storeRegionRoutesMiddlewares } from "./store/regions/middlewares"
|
||||||
|
|
||||||
export const config: MiddlewaresConfig = {
|
export const config: MiddlewaresConfig = {
|
||||||
routes: [
|
routes: [
|
||||||
@@ -17,6 +18,7 @@ export const config: MiddlewaresConfig = {
|
|||||||
...storeCustomerRoutesMiddlewares,
|
...storeCustomerRoutesMiddlewares,
|
||||||
...storeCartRoutesMiddlewares,
|
...storeCartRoutesMiddlewares,
|
||||||
...authRoutesMiddlewares,
|
...authRoutesMiddlewares,
|
||||||
|
...storeRegionRoutesMiddlewares,
|
||||||
...adminRegionRoutesMiddlewares,
|
...adminRegionRoutesMiddlewares,
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||||
|
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||||
|
import { defaultStoreRegionFields } 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: defaultStoreRegionFields,
|
||||||
|
})
|
||||||
|
|
||||||
|
const [region] = await remoteQuery(queryObject)
|
||||||
|
|
||||||
|
res.status(200).json({ region })
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { transformQuery } from "../../../api/middlewares"
|
||||||
|
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||||
|
import * as QueryConfig from "./query-config"
|
||||||
|
import { StoreGetRegionsParams, StoreRegionsRegionParams } from "./validators"
|
||||||
|
|
||||||
|
export const storeRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
|
{
|
||||||
|
method: ["GET"],
|
||||||
|
matcher: "/store/regions",
|
||||||
|
middlewares: [
|
||||||
|
transformQuery(
|
||||||
|
StoreGetRegionsParams,
|
||||||
|
QueryConfig.listTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
method: ["GET"],
|
||||||
|
matcher: "/store/regions/:id",
|
||||||
|
middlewares: [
|
||||||
|
transformQuery(
|
||||||
|
StoreRegionsRegionParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
export const defaultStoreRegionFields = [
|
||||||
|
"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 { defaultStoreRegionFields } 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: defaultStoreRegionFields,
|
||||||
|
})
|
||||||
|
|
||||||
|
// TODO: Add count, offset, limit
|
||||||
|
const regions = await remoteQuery(queryObject)
|
||||||
|
|
||||||
|
res.json({ regions })
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
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 StoreRegionsRegionParams extends FindParams {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parameters used to filter and configure the pagination of the retrieved regions.
|
||||||
|
*/
|
||||||
|
export class StoreGetRegionsParams extends extendedFindParamsMixin({
|
||||||
|
limit: 50,
|
||||||
|
offset: 0,
|
||||||
|
}) {
|
||||||
|
/**
|
||||||
|
* 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>
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@ValidateNested({ each: true })
|
||||||
|
@Type(() => StoreGetRegionsParams)
|
||||||
|
$and?: StoreGetRegionsParams[]
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@ValidateNested({ each: true })
|
||||||
|
@Type(() => StoreGetRegionsParams)
|
||||||
|
$or?: StoreGetRegionsParams[]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user