feat(tax): add endpoints to create tax regions and tax rates (#6533)
**What** Adds: - POST /admin/tax-regions - POST /admin/tax-rates - GET /admin/tax-rates - `createTaxRegionsWorkflow` - `createTaxRatesWorkflow`
This commit is contained in:
@@ -1,19 +1,27 @@
|
||||
import * as QueryConfig from "./query-config"
|
||||
|
||||
import { AdminGetTaxRatesTaxRateParams } from "./validators"
|
||||
import { transformQuery } from "../../../api/middlewares"
|
||||
import {
|
||||
AdminGetTaxRatesTaxRateParams,
|
||||
AdminPostTaxRatesReq,
|
||||
} from "./validators"
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
|
||||
export const adminTaxRateRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["ALL"],
|
||||
method: "ALL",
|
||||
matcher: "/admin/tax-rates*",
|
||||
middlewares: [authenticate("admin", ["bearer", "session", "api-key"])],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
method: "POST",
|
||||
matcher: "/admin/tax-rates",
|
||||
middlewares: [transformBody(AdminPostTaxRatesReq)],
|
||||
},
|
||||
{
|
||||
method: "GET",
|
||||
matcher: "/admin/tax-rates/:id",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
|
||||
@@ -6,6 +6,8 @@ export const defaultAdminTaxRateFields = [
|
||||
"code",
|
||||
"rate",
|
||||
"tax_region_id",
|
||||
"is_default",
|
||||
"is_combinable",
|
||||
"created_by",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import { createTaxRatesWorkflow } from "@medusajs/core-flows"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { defaultAdminTaxRateFields } from "./query-config"
|
||||
import { AdminPostTaxRatesReq } from "./validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostTaxRatesReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { result, errors } = await createTaxRatesWorkflow(req.scope).run({
|
||||
input: [
|
||||
{
|
||||
...req.validatedBody,
|
||||
created_by: req.auth.actor_id,
|
||||
},
|
||||
],
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "tax_rate",
|
||||
variables: { id: result[0].id },
|
||||
fields: defaultAdminTaxRateFields,
|
||||
})
|
||||
|
||||
const [taxRate] = await remoteQuery(query)
|
||||
|
||||
res.status(200).json({ tax_rate: taxRate })
|
||||
}
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const variables = { id: req.params.id }
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "tax_rate",
|
||||
variables,
|
||||
fields: defaultAdminTaxRateFields,
|
||||
})
|
||||
|
||||
const rates = await remoteQuery(queryObject)
|
||||
|
||||
res.status(200).json({ tax_rates: rates })
|
||||
}
|
||||
@@ -1,3 +1,54 @@
|
||||
// HEAD
|
||||
import { Type } from "class-transformer"
|
||||
import {
|
||||
IsBoolean,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import { FindParams } from "../../../types/common"
|
||||
|
||||
export class AdminGetTaxRatesTaxRateParams extends FindParams {}
|
||||
|
||||
class CreateTaxRateRule {
|
||||
@IsString()
|
||||
reference: string
|
||||
|
||||
@IsString()
|
||||
reference_id: string
|
||||
}
|
||||
|
||||
export class AdminPostTaxRatesReq {
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
rate?: number | null
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
code?: string | null
|
||||
|
||||
@ValidateNested({ each: true })
|
||||
@IsOptional()
|
||||
@Type(() => CreateTaxRateRule)
|
||||
rules?: CreateTaxRateRule[]
|
||||
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
is_default?: boolean
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
is_combinable?: boolean
|
||||
|
||||
@IsString()
|
||||
tax_region_id: string
|
||||
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import * as QueryConfig from "./query-config"
|
||||
|
||||
import { AdminGetTaxRegionsParams, AdminPostTaxRegionsReq } from "./validators"
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
|
||||
export const adminTaxRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["ALL"],
|
||||
matcher: "/admin/tax-regions*",
|
||||
middlewares: [authenticate("admin", ["bearer", "session", "api-key"])],
|
||||
},
|
||||
{
|
||||
method: "POST",
|
||||
matcher: "/admin/tax-regions",
|
||||
middlewares: [transformBody(AdminPostTaxRegionsReq)],
|
||||
},
|
||||
{
|
||||
method: "GET",
|
||||
matcher: "/admin/tax-regions",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetTaxRegionsParams,
|
||||
QueryConfig.listTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,26 @@
|
||||
export const defaultAdminTaxRegionRelations = []
|
||||
export const allowedAdminTaxRegionRelations = []
|
||||
export const defaultAdminTaxRegionFields = [
|
||||
"id",
|
||||
"country_code",
|
||||
"province_code",
|
||||
"parent_id",
|
||||
"provider_id",
|
||||
"created_by",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
"metadata",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultAdminTaxRegionFields,
|
||||
defaultRelations: defaultAdminTaxRegionRelations,
|
||||
allowedRelations: allowedAdminTaxRegionRelations,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listTransformQueryConfig = {
|
||||
defaultLimit: 20,
|
||||
isList: true,
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { createTaxRegionsWorkflow } from "@medusajs/core-flows"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { defaultAdminTaxRegionFields } from "./query-config"
|
||||
import { AdminPostTaxRegionsReq } from "./validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostTaxRegionsReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { result, errors } = await createTaxRegionsWorkflow(req.scope).run({
|
||||
input: [
|
||||
{
|
||||
...req.validatedBody,
|
||||
created_by: req.auth.actor_id,
|
||||
},
|
||||
],
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "tax_region",
|
||||
variables: { id: result[0].id },
|
||||
fields: defaultAdminTaxRegionFields,
|
||||
})
|
||||
|
||||
const [taxRegion] = await remoteQuery(query)
|
||||
|
||||
res.status(200).json({ tax_region: taxRegion })
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
import { OperatorMap } from "@medusajs/types"
|
||||
import { Type } from "class-transformer"
|
||||
import {
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import { extendedFindParamsMixin, FindParams } from "../../../types/common"
|
||||
import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
||||
|
||||
export class AdminGetTaxRegionsTaxRegionParams extends FindParams {}
|
||||
|
||||
export class AdminGetTaxRegionsParams extends extendedFindParamsMixin({
|
||||
limit: 50,
|
||||
offset: 0,
|
||||
}) {
|
||||
/**
|
||||
* Search parameter for regions.
|
||||
*/
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
id?: string | string[]
|
||||
|
||||
/**
|
||||
* 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>
|
||||
}
|
||||
Reference in New Issue
Block a user