feat: Add tax regions table (#6983)
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import * as QueryConfig from "./query-config"
|
||||
|
||||
import { AdminGetTaxRegionsParams, AdminPostTaxRegionsReq } from "./validators"
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import { AdminCreateTaxRegion, AdminGetTaxRegionsParams } from "./validators"
|
||||
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||
|
||||
export const adminTaxRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
@@ -15,13 +16,19 @@ export const adminTaxRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: "POST",
|
||||
matcher: "/admin/tax-regions",
|
||||
middlewares: [transformBody(AdminPostTaxRegionsReq)],
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminCreateTaxRegion),
|
||||
validateAndTransformQuery(
|
||||
AdminGetTaxRegionsParams,
|
||||
QueryConfig.listTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: "GET",
|
||||
matcher: "/admin/tax-regions",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
validateAndTransformQuery(
|
||||
AdminGetTaxRegionsParams,
|
||||
QueryConfig.listTransformQueryConfig
|
||||
),
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
export const defaultAdminTaxRegionRelations = []
|
||||
export const allowedAdminTaxRegionRelations = []
|
||||
export const defaultAdminTaxRegionFields = [
|
||||
export const defaults = [
|
||||
"id",
|
||||
"country_code",
|
||||
"province_code",
|
||||
@@ -14,13 +12,11 @@ export const defaultAdminTaxRegionFields = [
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultAdminTaxRegionFields,
|
||||
defaultRelations: defaultAdminTaxRegionRelations,
|
||||
allowedRelations: allowedAdminTaxRegionRelations,
|
||||
defaults,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listTransformQueryConfig = {
|
||||
defaultLimit: 20,
|
||||
...retrieveTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { createTaxRegionsWorkflow } from "@medusajs/core-flows"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { defaultAdminTaxRegionFields } from "./query-config"
|
||||
import { AdminPostTaxRegionsReq } from "./validators"
|
||||
import { AdminCreateTaxRegionType } from "./validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostTaxRegionsReq>,
|
||||
req: AuthenticatedMedusaRequest<AdminCreateTaxRegionType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { result, errors } = await createTaxRegionsWorkflow(req.scope).run({
|
||||
@@ -30,10 +32,35 @@ export const POST = async (
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "tax_region",
|
||||
variables: { id: result[0].id },
|
||||
fields: defaultAdminTaxRegionFields,
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const [taxRegion] = await remoteQuery(query)
|
||||
|
||||
res.status(200).json({ tax_region: taxRegion })
|
||||
}
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const { rows: tax_regions, metadata } = await remoteQuery(
|
||||
remoteQueryObjectFromString({
|
||||
entryPoint: "tax_regions",
|
||||
variables: {
|
||||
filters: req.filterableFields,
|
||||
...req.remoteQueryConfig.pagination,
|
||||
},
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
)
|
||||
|
||||
res.status(200).json({
|
||||
tax_regions,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,127 +1,45 @@
|
||||
import { OperatorMap } from "@medusajs/types"
|
||||
import { Type } from "class-transformer"
|
||||
import { z } from "zod"
|
||||
import {
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import { extendedFindParamsMixin, FindParams } from "../../../types/common"
|
||||
import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
||||
createFindParams,
|
||||
createOperatorMap,
|
||||
createSelectParams,
|
||||
} from "../../utils/validators"
|
||||
|
||||
export class AdminGetTaxRegionsTaxRegionParams extends FindParams {}
|
||||
export const AdminGetTaxRegionParams = createSelectParams()
|
||||
|
||||
export class AdminGetTaxRegionsParams extends extendedFindParamsMixin({
|
||||
limit: 50,
|
||||
export type AdminCreateTaxRegionsParams = z.infer<
|
||||
typeof AdminGetTaxRegionsParams
|
||||
>
|
||||
export const AdminGetTaxRegionsParams = createFindParams({
|
||||
limit: 20,
|
||||
offset: 0,
|
||||
}) {
|
||||
/**
|
||||
* Search parameter for regions.
|
||||
*/
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
id?: string | string[]
|
||||
}).merge(
|
||||
z.object({
|
||||
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
country_code: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
province_code: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
parent_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
created_by: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
created_at: createOperatorMap().optional(),
|
||||
updated_at: createOperatorMap().optional(),
|
||||
deleted_at: createOperatorMap().optional(),
|
||||
$and: z.lazy(() => AdminGetTaxRegionsParams.array()).optional(),
|
||||
$or: z.lazy(() => AdminGetTaxRegionsParams.array()).optional(),
|
||||
})
|
||||
)
|
||||
|
||||
/**
|
||||
* Filter by country code.
|
||||
*/
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
country_code?: string | string[]
|
||||
|
||||
/**
|
||||
* Filter by province code
|
||||
*/
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
province_code?: string | string[]
|
||||
|
||||
/**
|
||||
* Filter by id of parent Tax Region.
|
||||
*/
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
parent_id?: string | string[]
|
||||
|
||||
/**
|
||||
* Filter by who created the Tax Region.
|
||||
*/
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
created_by?: string | string[]
|
||||
|
||||
/**
|
||||
* Date filters to apply on the Tax Regions' `created_at` date.
|
||||
*/
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => OperatorMapValidator)
|
||||
created_at?: OperatorMap<string>
|
||||
|
||||
/**
|
||||
* Date filters to apply on the Tax Regions' `updated_at` date.
|
||||
*/
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => OperatorMapValidator)
|
||||
updated_at?: OperatorMap<string>
|
||||
|
||||
/**
|
||||
* Date filters to apply on the Tax Regions' `deleted_at` date.
|
||||
*/
|
||||
@ValidateNested()
|
||||
@IsOptional()
|
||||
@Type(() => OperatorMapValidator)
|
||||
deleted_at?: OperatorMap<string>
|
||||
|
||||
// Additional filters from BaseFilterable
|
||||
@IsOptional()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => AdminGetTaxRegionsParams)
|
||||
$and?: AdminGetTaxRegionsParams[]
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => AdminGetTaxRegionsParams)
|
||||
$or?: AdminGetTaxRegionsParams[]
|
||||
}
|
||||
|
||||
class CreateDefaultTaxRate {
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
rate?: number | null
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
code?: string | null
|
||||
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export class AdminPostTaxRegionsReq {
|
||||
@IsString()
|
||||
country_code: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
province_code?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
parent_id?: string
|
||||
|
||||
@ValidateNested()
|
||||
@IsOptional()
|
||||
@Type(() => CreateDefaultTaxRate)
|
||||
default_tax_rate?: CreateDefaultTaxRate
|
||||
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
export type AdminCreateTaxRegionType = z.infer<typeof AdminCreateTaxRegion>
|
||||
export const AdminCreateTaxRegion = z.object({
|
||||
country_code: z.string(),
|
||||
province_code: z.string().optional(),
|
||||
parent_id: z.string().optional(),
|
||||
default_tax_rate: z
|
||||
.object({
|
||||
rate: z.number().optional(),
|
||||
code: z.string().optional(),
|
||||
name: z.string(),
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
})
|
||||
.optional(),
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user