fix: Switch some modules to using zod for validation, and fix some bugs (#7074)
This commit is contained in:
1 parent
bc081a7777
commit
92b633d1cb
35 files changed
+570
-559
No files matched your search
@@ -4,8 +4,11 @@ import {
|
|||||||
ProductDTO,
|
ProductDTO,
|
||||||
ProductVariantDTO,
|
ProductVariantDTO,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
import {
|
||||||
import { promiseAll, remoteQueryObjectFromString } from "@medusajs/utils"
|
promiseAll,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
const isPricing = (fieldName: string) =>
|
const isPricing = (fieldName: string) =>
|
||||||
fieldName.startsWith("variants.prices") ||
|
fieldName.startsWith("variants.prices") ||
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { createProductsWorkflow } from "@medusajs/core-flows"
|
|||||||
import { CreateProductDTO } from "@medusajs/types"
|
import { CreateProductDTO } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
remoteQueryObjectFromString
|
remoteQueryObjectFromString,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
|
|||||||
@@ -1,25 +1,37 @@
|
|||||||
import { updateStoresWorkflow } from "@medusajs/core-flows"
|
import { updateStoresWorkflow } from "@medusajs/core-flows"
|
||||||
import { UpdateStoreDTO } from "@medusajs/types"
|
import { UpdateStoreDTO } from "@medusajs/types"
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import {
|
||||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
remoteQueryObjectFromString,
|
||||||
import { defaultAdminStoreFields } from "../query-config"
|
ContainerRegistrationKeys,
|
||||||
|
} from "@medusajs/utils"
|
||||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
import {
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
AuthenticatedMedusaRequest,
|
||||||
|
MedusaResponse,
|
||||||
|
} from "../../../../types/routing"
|
||||||
|
import { AdminGetStoreParamsType, AdminUpdateStoreType } from "../validators"
|
||||||
|
import { refetchStore } from "../helpers"
|
||||||
|
|
||||||
|
export const GET = async (
|
||||||
|
req: AuthenticatedMedusaRequest<AdminGetStoreParamsType>,
|
||||||
|
res: MedusaResponse
|
||||||
|
) => {
|
||||||
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
const variables = { id: req.params.id }
|
const variables = { id: req.params.id }
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
const queryObject = remoteQueryObjectFromString({
|
||||||
entryPoint: "store",
|
entryPoint: "store",
|
||||||
variables,
|
variables,
|
||||||
fields: defaultAdminStoreFields,
|
fields: req.remoteQueryConfig.fields,
|
||||||
})
|
})
|
||||||
|
|
||||||
const [store] = await remoteQuery(queryObject)
|
const [store] = await remoteQuery(queryObject)
|
||||||
res.status(200).json({ store })
|
res.status(200).json({ store })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
export const POST = async (
|
||||||
|
req: AuthenticatedMedusaRequest<AdminUpdateStoreType>,
|
||||||
|
res: MedusaResponse
|
||||||
|
) => {
|
||||||
const { result, errors } = await updateStoresWorkflow(req.scope).run({
|
const { result, errors } = await updateStoresWorkflow(req.scope).run({
|
||||||
input: {
|
input: {
|
||||||
selector: { id: req.params.id },
|
selector: { id: req.params.id },
|
||||||
@@ -32,5 +44,11 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).json({ store: result[0] })
|
const store = await refetchStore(
|
||||||
|
result[0].id,
|
||||||
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
|
)
|
||||||
|
|
||||||
|
res.status(200).json({ store })
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { MedusaContainer } from "@medusajs/types"
|
||||||
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
|
export const refetchStore = async (
|
||||||
|
storeId: string,
|
||||||
|
scope: MedusaContainer,
|
||||||
|
fields: string[]
|
||||||
|
) => {
|
||||||
|
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "store",
|
||||||
|
variables: {
|
||||||
|
filters: { id: storeId },
|
||||||
|
},
|
||||||
|
fields: fields,
|
||||||
|
})
|
||||||
|
|
||||||
|
const stores = await remoteQuery(queryObject)
|
||||||
|
return stores[0]
|
||||||
|
}
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
|
||||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||||
|
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||||
|
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||||
import * as QueryConfig from "./query-config"
|
import * as QueryConfig from "./query-config"
|
||||||
import {
|
import {
|
||||||
|
AdminGetStoreParams,
|
||||||
AdminGetStoresParams,
|
AdminGetStoresParams,
|
||||||
AdminGetStoresStoreParams,
|
AdminUpdateStore,
|
||||||
AdminPostStoresStoreReq,
|
|
||||||
} from "./validators"
|
} from "./validators"
|
||||||
|
|
||||||
export const adminStoreRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminStoreRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
@@ -18,7 +19,7 @@ export const adminStoreRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/stores",
|
matcher: "/admin/stores",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetStoresParams,
|
AdminGetStoresParams,
|
||||||
QueryConfig.listTransformQueryConfig
|
QueryConfig.listTransformQueryConfig
|
||||||
),
|
),
|
||||||
@@ -28,8 +29,8 @@ export const adminStoreRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/stores/:id",
|
matcher: "/admin/stores/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetStoresStoreParams,
|
AdminGetStoreParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -37,6 +38,12 @@ export const adminStoreRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/stores/:id",
|
matcher: "/admin/stores/:id",
|
||||||
middlewares: [transformBody(AdminPostStoresStoreReq)],
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminUpdateStore),
|
||||||
|
validateAndTransformQuery(
|
||||||
|
AdminGetStoreParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@@ -20,6 +20,6 @@ export const retrieveTransformQueryConfig = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const listTransformQueryConfig = {
|
export const listTransformQueryConfig = {
|
||||||
defaultLimit: 20,
|
...retrieveTransformQueryConfig,
|
||||||
isList: true,
|
isList: true,
|
||||||
}
|
}
|
||||||
@@ -2,25 +2,28 @@ import {
|
|||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
remoteQueryObjectFromString,
|
remoteQueryObjectFromString,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
import {
|
||||||
import { defaultAdminStoreFields } from "./query-config"
|
AuthenticatedMedusaRequest,
|
||||||
|
MedusaResponse,
|
||||||
|
} from "../../../types/routing"
|
||||||
|
import { AdminGetStoresParamsType } from "./validators"
|
||||||
|
|
||||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
export const GET = async (
|
||||||
|
req: AuthenticatedMedusaRequest<AdminGetStoresParamsType>,
|
||||||
|
res: MedusaResponse
|
||||||
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
const queryObject = remoteQueryObjectFromString({
|
||||||
entryPoint: "store",
|
entryPoint: "store",
|
||||||
variables: {
|
variables: {
|
||||||
filters: req.filterableFields,
|
filters: req.filterableFields,
|
||||||
order: req.listConfig.order,
|
...req.remoteQueryConfig.pagination,
|
||||||
skip: req.listConfig.skip,
|
|
||||||
take: req.listConfig.take,
|
|
||||||
},
|
},
|
||||||
fields: defaultAdminStoreFields,
|
fields: req.remoteQueryConfig.fields,
|
||||||
})
|
})
|
||||||
|
|
||||||
const { rows: stores, metadata } = await remoteQuery(queryObject)
|
const { rows: stores, metadata } = await remoteQuery(queryObject)
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
stores,
|
stores,
|
||||||
count: metadata.count,
|
count: metadata.count,
|
||||||
|
|||||||
@@ -1,75 +1,29 @@
|
|||||||
import { Type } from "class-transformer"
|
import { createFindParams, createSelectParams } from "../../utils/validators"
|
||||||
import {
|
import { z } from "zod"
|
||||||
IsArray,
|
|
||||||
IsObject,
|
|
||||||
IsOptional,
|
|
||||||
IsString,
|
|
||||||
ValidateNested,
|
|
||||||
} from "class-validator"
|
|
||||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
|
||||||
|
|
||||||
export class AdminGetStoresStoreParams extends FindParams {}
|
export type AdminGetStoreParamsType = z.infer<typeof AdminGetStoreParams>
|
||||||
/**
|
export const AdminGetStoreParams = createSelectParams()
|
||||||
* Parameters used to filter and configure the pagination of the retrieved api keys.
|
|
||||||
*/
|
export type AdminGetStoresParamsType = z.infer<typeof AdminGetStoresParams>
|
||||||
export class AdminGetStoresParams extends extendedFindParamsMixin({
|
export const AdminGetStoresParams = createFindParams({
|
||||||
limit: 50,
|
limit: 50,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}) {
|
}).merge(
|
||||||
/**
|
z.object({
|
||||||
* Search parameter for api keys.
|
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
*/
|
name: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsString({ each: true })
|
$and: z.lazy(() => AdminGetStoresParams.array()).optional(),
|
||||||
@IsOptional()
|
$or: z.lazy(() => AdminGetStoresParams.array()).optional(),
|
||||||
id?: string | string[]
|
})
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
export type AdminUpdateStoreType = z.infer<typeof AdminUpdateStore>
|
||||||
* Filter by title
|
export const AdminUpdateStore = z.object({
|
||||||
*/
|
name: z.string().optional(),
|
||||||
@IsString({ each: true })
|
supported_currency_codes: z.array(z.string()).optional(),
|
||||||
@IsOptional()
|
default_currency_code: z.string().optional(),
|
||||||
name?: string | string[]
|
default_sales_channel_id: z.string().optional(),
|
||||||
|
default_region_id: z.string().optional(),
|
||||||
// Additional filters from BaseFilterable
|
default_location_id: z.string().optional(),
|
||||||
@IsOptional()
|
metadata: z.record(z.unknown()).optional(),
|
||||||
@ValidateNested({ each: true })
|
})
|
||||||
@Type(() => AdminGetStoresParams)
|
|
||||||
$and?: AdminGetStoresParams[]
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetStoresParams)
|
|
||||||
$or?: AdminGetStoresParams[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostStoresStoreReq {
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
name?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsArray()
|
|
||||||
supported_currency_codes?: string[]
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
default_currency_code?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
default_sales_channel_id?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
default_region_id?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
default_location_id?: string
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminDeleteStoresStoreReq {}
|
|
||||||
@@ -3,16 +3,22 @@ import {
|
|||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../types/routing"
|
} from "../../../../types/routing"
|
||||||
|
|
||||||
import { defaultAdminTaxRateFields } from "../query-config"
|
import {
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
remoteQueryObjectFromString,
|
||||||
import { AdminPostTaxRatesTaxRateReq } from "../../../../api/routes/admin/tax-rates"
|
ContainerRegistrationKeys,
|
||||||
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
deleteTaxRatesWorkflow,
|
deleteTaxRatesWorkflow,
|
||||||
updateTaxRatesWorkflow,
|
updateTaxRatesWorkflow,
|
||||||
} from "@medusajs/core-flows"
|
} from "@medusajs/core-flows"
|
||||||
|
import {
|
||||||
|
AdminGetTaxRateParamsType,
|
||||||
|
AdminUpdateTaxRateType,
|
||||||
|
} from "../validators"
|
||||||
|
import { refetchTaxRate } from "../helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminPostTaxRatesTaxRateReq>,
|
req: AuthenticatedMedusaRequest<AdminUpdateTaxRateType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const { errors } = await updateTaxRatesWorkflow(req.scope).run({
|
const { errors } = await updateTaxRatesWorkflow(req.scope).run({
|
||||||
@@ -27,35 +33,28 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const taxRate = await refetchTaxRate(
|
||||||
|
req.params.id,
|
||||||
const queryObject = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "tax_rate",
|
req.remoteQueryConfig.fields
|
||||||
variables: { id: req.params.id },
|
)
|
||||||
fields: defaultAdminTaxRateFields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [taxRate] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ tax_rate: taxRate })
|
res.status(200).json({ tax_rate: taxRate })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetTaxRateParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
|
||||||
const variables = { id: req.params.id }
|
const variables = { id: req.params.id }
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
const queryObject = remoteQueryObjectFromString({
|
||||||
entryPoint: "tax_rate",
|
entryPoint: "tax_rate",
|
||||||
variables,
|
variables,
|
||||||
fields: defaultAdminTaxRateFields,
|
fields: req.remoteQueryConfig.fields,
|
||||||
})
|
})
|
||||||
|
|
||||||
const [taxRate] = await remoteQuery(queryObject)
|
const [taxRate] = await remoteQuery(queryObject)
|
||||||
|
|
||||||
res.status(200).json({ tax_rate: taxRate })
|
res.status(200).json({ tax_rate: taxRate })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +63,6 @@ export const DELETE = async (
|
|||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const id = req.params.id
|
const id = req.params.id
|
||||||
|
|
||||||
const { errors } = await deleteTaxRatesWorkflow(req.scope).run({
|
const { errors } = await deleteTaxRatesWorkflow(req.scope).run({
|
||||||
input: { ids: [id] },
|
input: { ids: [id] },
|
||||||
throwOnError: false,
|
throwOnError: false,
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import { deleteTaxRateRulesWorkflow } from "@medusajs/core-flows"
|
import { deleteTaxRateRulesWorkflow } from "@medusajs/core-flows"
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
|
||||||
import { defaultAdminTaxRatesFields } from "../../../../../../api/routes/admin/tax-rates"
|
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../../types/routing"
|
} from "../../../../../../types/routing"
|
||||||
|
import { refetchTaxRate } from "../../../helpers"
|
||||||
|
|
||||||
export const DELETE = async (
|
export const DELETE = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest,
|
||||||
@@ -19,15 +18,16 @@ export const DELETE = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const taxRate = await refetchTaxRate(
|
||||||
|
req.params.id,
|
||||||
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
|
)
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
res.status(200).json({
|
||||||
entryPoint: "tax_rate",
|
id: req.params.rule_id,
|
||||||
variables: { id: req.params.id },
|
object: "tax_rate_rule",
|
||||||
fields: defaultAdminTaxRatesFields,
|
deleted: true,
|
||||||
|
parent: taxRate,
|
||||||
})
|
})
|
||||||
|
|
||||||
const [taxRate] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ tax_rate: taxRate })
|
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,13 @@
|
|||||||
import { createTaxRateRulesWorkflow } from "@medusajs/core-flows"
|
import { createTaxRateRulesWorkflow } from "@medusajs/core-flows"
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
|
||||||
import { defaultAdminTaxRatesFields } from "../../../../../api/routes/admin/tax-rates"
|
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../types/routing"
|
} from "../../../../../types/routing"
|
||||||
import { AdminPostTaxRatesTaxRateRulesReq } from "../../validators"
|
import { AdminCreateTaxRateRuleType } from "../../validators"
|
||||||
|
import { refetchTaxRate } from "../../helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminPostTaxRatesTaxRateRulesReq>,
|
req: AuthenticatedMedusaRequest<AdminCreateTaxRateRuleType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const { errors } = await createTaxRateRulesWorkflow(req.scope).run({
|
const { errors } = await createTaxRateRulesWorkflow(req.scope).run({
|
||||||
@@ -28,15 +27,10 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const taxRate = await refetchTaxRate(
|
||||||
|
req.params.id,
|
||||||
const queryObject = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "tax_rate",
|
req.remoteQueryConfig.fields
|
||||||
variables: { id: req.params.id },
|
)
|
||||||
fields: defaultAdminTaxRatesFields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [taxRate] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ tax_rate: taxRate })
|
res.status(200).json({ tax_rate: taxRate })
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { MedusaContainer } from "@medusajs/types"
|
||||||
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
|
export const refetchTaxRate = async (
|
||||||
|
taxRateId: string,
|
||||||
|
scope: MedusaContainer,
|
||||||
|
fields: string[]
|
||||||
|
) => {
|
||||||
|
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "tax_rate",
|
||||||
|
variables: {
|
||||||
|
filters: { id: taxRateId },
|
||||||
|
},
|
||||||
|
fields: fields,
|
||||||
|
})
|
||||||
|
|
||||||
|
const taxRates = await remoteQuery(queryObject)
|
||||||
|
return taxRates[0]
|
||||||
|
}
|
||||||
@@ -1,15 +1,17 @@
|
|||||||
import * as QueryConfig from "./query-config"
|
import * as QueryConfig from "./query-config"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
AdminGetTaxRatesTaxRateParams,
|
AdminCreateTaxRate,
|
||||||
AdminPostTaxRatesReq,
|
AdminCreateTaxRateRule,
|
||||||
AdminPostTaxRatesTaxRateReq,
|
AdminGetTaxRateParams,
|
||||||
AdminPostTaxRatesTaxRateRulesReq,
|
AdminGetTaxRatesParams,
|
||||||
|
AdminUpdateTaxRate,
|
||||||
} from "./validators"
|
} from "./validators"
|
||||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
|
||||||
|
|
||||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||||
|
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||||
|
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||||
|
|
||||||
export const adminTaxRateRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminTaxRateRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
{
|
{
|
||||||
@@ -20,19 +22,41 @@ export const adminTaxRateRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
matcher: "/admin/tax-rates",
|
matcher: "/admin/tax-rates",
|
||||||
middlewares: [transformBody(AdminPostTaxRatesReq)],
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminCreateTaxRate),
|
||||||
|
validateAndTransformQuery(
|
||||||
|
AdminGetTaxRateParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
matcher: "/admin/tax-rates/:id",
|
matcher: "/admin/tax-rates/:id",
|
||||||
middlewares: [transformBody(AdminPostTaxRatesTaxRateReq)],
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminUpdateTaxRate),
|
||||||
|
validateAndTransformQuery(
|
||||||
|
AdminGetTaxRateParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
method: "GET",
|
||||||
|
matcher: "/admin/tax-rates",
|
||||||
|
middlewares: [
|
||||||
|
validateAndTransformQuery(
|
||||||
|
AdminGetTaxRatesParams,
|
||||||
|
QueryConfig.listTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
matcher: "/admin/tax-rates/:id",
|
matcher: "/admin/tax-rates/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetTaxRatesTaxRateParams,
|
AdminGetTaxRateParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -40,6 +64,22 @@ export const adminTaxRateRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
matcher: "/admin/tax-rates/:id/rules",
|
matcher: "/admin/tax-rates/:id/rules",
|
||||||
middlewares: [transformBody(AdminPostTaxRatesTaxRateRulesReq)],
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminCreateTaxRateRule),
|
||||||
|
validateAndTransformQuery(
|
||||||
|
AdminGetTaxRateParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
method: "DELETE",
|
||||||
|
matcher: "/admin/tax-rates/:id/rules/:rule_id",
|
||||||
|
middlewares: [
|
||||||
|
validateAndTransformQuery(
|
||||||
|
AdminGetTaxRateParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@@ -16,13 +16,11 @@ export const defaultAdminTaxRateFields = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
export const retrieveTransformQueryConfig = {
|
export const retrieveTransformQueryConfig = {
|
||||||
defaultFields: defaultAdminTaxRateFields,
|
defaults: defaultAdminTaxRateFields,
|
||||||
defaultRelations: defaultAdminTaxRateRelations,
|
|
||||||
allowedRelations: allowedAdminTaxRateRelations,
|
|
||||||
isList: false,
|
isList: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const listTransformQueryConfig = {
|
export const listTransformQueryConfig = {
|
||||||
defaultLimit: 20,
|
defaults: defaultAdminTaxRateFields,
|
||||||
isList: true,
|
isList: true,
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,20 @@
|
|||||||
import { createTaxRatesWorkflow } from "@medusajs/core-flows"
|
import { createTaxRatesWorkflow } from "@medusajs/core-flows"
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import {
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../types/routing"
|
} from "../../../types/routing"
|
||||||
import { defaultAdminTaxRateFields } from "./query-config"
|
import {
|
||||||
import { AdminPostTaxRatesReq } from "./validators"
|
AdminCreateTaxRateType,
|
||||||
|
AdminGetTaxRatesParamsType,
|
||||||
|
} from "./validators"
|
||||||
|
import { refetchTaxRate } from "./helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminPostTaxRatesReq>,
|
req: AuthenticatedMedusaRequest<AdminCreateTaxRateType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const { result, errors } = await createTaxRatesWorkflow(req.scope).run({
|
const { result, errors } = await createTaxRatesWorkflow(req.scope).run({
|
||||||
@@ -25,34 +31,34 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const taxRate = await refetchTaxRate(
|
||||||
|
result[0].id,
|
||||||
const query = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "tax_rate",
|
req.remoteQueryConfig.fields
|
||||||
variables: { id: result[0].id },
|
)
|
||||||
fields: defaultAdminTaxRateFields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [taxRate] = await remoteQuery(query)
|
|
||||||
|
|
||||||
res.status(200).json({ tax_rate: taxRate })
|
res.status(200).json({ tax_rate: taxRate })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetTaxRatesParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
const { rows: tax_rates, metadata } = await remoteQuery(
|
||||||
const variables = { id: req.params.id }
|
remoteQueryObjectFromString({
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
|
||||||
entryPoint: "tax_rate",
|
entryPoint: "tax_rate",
|
||||||
variables,
|
variables: {
|
||||||
fields: defaultAdminTaxRateFields,
|
filters: req.filterableFields,
|
||||||
|
...req.remoteQueryConfig.pagination,
|
||||||
|
},
|
||||||
|
fields: req.remoteQueryConfig.fields,
|
||||||
})
|
})
|
||||||
|
)
|
||||||
|
|
||||||
const rates = await remoteQuery(queryObject)
|
res.status(200).json({
|
||||||
|
tax_rates,
|
||||||
res.status(200).json({ tax_rates: rates })
|
count: metadata.count,
|
||||||
|
offset: metadata.skip,
|
||||||
|
limit: metadata.take,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
@@ -1,85 +1,40 @@
|
|||||||
import { Type } from "class-transformer"
|
import { createFindParams, createSelectParams } from "../../utils/validators"
|
||||||
import {
|
import { z } from "zod"
|
||||||
IsBoolean,
|
|
||||||
IsNumber,
|
|
||||||
IsObject,
|
|
||||||
IsOptional,
|
|
||||||
IsString,
|
|
||||||
ValidateNested,
|
|
||||||
} from "class-validator"
|
|
||||||
import { FindParams } from "../../../types/common"
|
|
||||||
|
|
||||||
export class AdminGetTaxRatesTaxRateParams extends FindParams {}
|
export type AdminGetTaxRateParamsType = z.infer<typeof AdminGetTaxRateParams>
|
||||||
|
export const AdminGetTaxRateParams = createSelectParams()
|
||||||
|
|
||||||
class CreateTaxRateRule {
|
export type AdminGetTaxRatesParamsType = z.infer<typeof AdminGetTaxRatesParams>
|
||||||
@IsString()
|
export const AdminGetTaxRatesParams = createFindParams({
|
||||||
reference: string
|
limit: 20,
|
||||||
|
offset: 0,
|
||||||
|
})
|
||||||
|
|
||||||
@IsString()
|
export type AdminCreateTaxRateRuleType = z.infer<typeof AdminCreateTaxRateRule>
|
||||||
reference_id: string
|
export const AdminCreateTaxRateRule = z.object({
|
||||||
}
|
reference: z.string(),
|
||||||
|
reference_id: z.string(),
|
||||||
|
})
|
||||||
|
|
||||||
export class AdminPostTaxRatesReq {
|
export type AdminCreateTaxRateType = z.infer<typeof AdminCreateTaxRate>
|
||||||
@IsOptional()
|
export const AdminCreateTaxRate = z.object({
|
||||||
@IsNumber()
|
rate: z.number().optional(),
|
||||||
rate?: number | null
|
code: z.string().optional(),
|
||||||
|
rules: z.array(AdminCreateTaxRateRule).optional(),
|
||||||
|
name: z.string(),
|
||||||
|
is_default: z.boolean().optional(),
|
||||||
|
is_combinable: z.boolean().optional(),
|
||||||
|
tax_region_id: z.string(),
|
||||||
|
metadata: z.record(z.unknown()).optional(),
|
||||||
|
})
|
||||||
|
|
||||||
@IsOptional()
|
export type AdminUpdateTaxRateType = z.infer<typeof AdminUpdateTaxRate>
|
||||||
@IsString()
|
export const AdminUpdateTaxRate = z.object({
|
||||||
code?: string | null
|
rate: z.number().optional(),
|
||||||
|
code: z.string().optional(),
|
||||||
@ValidateNested({ each: true })
|
rules: z.array(AdminCreateTaxRateRule).optional(),
|
||||||
@IsOptional()
|
name: z.string().optional(),
|
||||||
@Type(() => CreateTaxRateRule)
|
is_default: z.boolean().optional(),
|
||||||
rules?: CreateTaxRateRule[]
|
is_combinable: z.boolean().optional(),
|
||||||
|
metadata: z.record(z.unknown()).optional(),
|
||||||
@IsString()
|
})
|
||||||
name: string
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
is_default?: boolean
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
is_combinable?: boolean
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
tax_region_id: string
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostTaxRatesTaxRateReq {
|
|
||||||
@IsOptional()
|
|
||||||
@IsNumber()
|
|
||||||
rate?: number | null
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
code?: string | null
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
name?: string
|
|
||||||
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => CreateTaxRateRule)
|
|
||||||
rules: CreateTaxRateRule[]
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
is_default?: boolean
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
is_combinable?: boolean
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostTaxRatesTaxRateRulesReq extends CreateTaxRateRule {}
|
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { MedusaContainer } from "@medusajs/types"
|
||||||
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
|
export const refetchTaxRegion = async (
|
||||||
|
taxRegionId: string,
|
||||||
|
scope: MedusaContainer,
|
||||||
|
fields: string[]
|
||||||
|
) => {
|
||||||
|
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "tax_region",
|
||||||
|
variables: {
|
||||||
|
filters: { id: taxRegionId },
|
||||||
|
},
|
||||||
|
fields: fields,
|
||||||
|
})
|
||||||
|
|
||||||
|
const taxRegions = await remoteQuery(queryObject)
|
||||||
|
return taxRegions[0]
|
||||||
|
}
|
||||||
@@ -7,7 +7,11 @@ import {
|
|||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../types/routing"
|
} from "../../../types/routing"
|
||||||
import { AdminCreateTaxRegionType } from "./validators"
|
import {
|
||||||
|
AdminCreateTaxRegionType,
|
||||||
|
AdminGetTaxRegionsParamsType,
|
||||||
|
} from "./validators"
|
||||||
|
import { refetchTaxRegion } from "./helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminCreateTaxRegionType>,
|
req: AuthenticatedMedusaRequest<AdminCreateTaxRegionType>,
|
||||||
@@ -27,21 +31,16 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const taxRegion = await refetchTaxRegion(
|
||||||
|
result[0].id,
|
||||||
const query = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "tax_region",
|
req.remoteQueryConfig.fields
|
||||||
variables: { id: result[0].id },
|
)
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [taxRegion] = await remoteQuery(query)
|
|
||||||
|
|
||||||
res.status(200).json({ tax_region: taxRegion })
|
res.status(200).json({ tax_region: taxRegion })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetTaxRegionsParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
|||||||
@@ -5,9 +5,12 @@ import {
|
|||||||
createSelectParams,
|
createSelectParams,
|
||||||
} from "../../utils/validators"
|
} from "../../utils/validators"
|
||||||
|
|
||||||
|
export type AdminGetTaxRegionParamsType = z.infer<
|
||||||
|
typeof AdminGetTaxRegionParams
|
||||||
|
>
|
||||||
export const AdminGetTaxRegionParams = createSelectParams()
|
export const AdminGetTaxRegionParams = createSelectParams()
|
||||||
|
|
||||||
export type AdminCreateTaxRegionsParams = z.infer<
|
export type AdminGetTaxRegionsParamsType = z.infer<
|
||||||
typeof AdminGetTaxRegionsParams
|
typeof AdminGetTaxRegionsParams
|
||||||
>
|
>
|
||||||
export const AdminGetTaxRegionsParams = createFindParams({
|
export const AdminGetTaxRegionsParams = createFindParams({
|
||||||
|
|||||||
@@ -1,31 +1,46 @@
|
|||||||
import { deleteUsersWorkflow, updateUsersWorkflow } from "@medusajs/core-flows"
|
import { deleteUsersWorkflow, updateUsersWorkflow } from "@medusajs/core-flows"
|
||||||
import { IUserModuleService, UpdateUserDTO } from "@medusajs/types"
|
import { UpdateUserDTO } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../types/routing"
|
} from "../../../../types/routing"
|
||||||
|
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import {
|
||||||
import { AdminUpdateUserRequest } from "../validators"
|
ContainerRegistrationKeys,
|
||||||
|
MedusaError,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
import { AdminUpdateUserType } from "../validators"
|
||||||
|
import { refetchUser } from "../helpers"
|
||||||
|
|
||||||
// Get user
|
// Get user
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
|
|
||||||
const moduleService: IUserModuleService = req.scope.resolve(
|
const query = remoteQueryObjectFromString({
|
||||||
ModuleRegistrationName.USER
|
entryPoint: "user",
|
||||||
|
variables: { id },
|
||||||
|
fields: req.remoteQueryConfig.fields,
|
||||||
|
})
|
||||||
|
|
||||||
|
const [user] = await remoteQuery(query)
|
||||||
|
if (!user) {
|
||||||
|
throw new MedusaError(
|
||||||
|
MedusaError.Types.NOT_FOUND,
|
||||||
|
`User with id: ${id} was not found`
|
||||||
)
|
)
|
||||||
const user = await moduleService.retrieve(id, req.retrieveConfig)
|
}
|
||||||
|
|
||||||
res.status(200).json({ user })
|
res.status(200).json({ user })
|
||||||
}
|
}
|
||||||
|
|
||||||
// update user
|
// update user
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminUpdateUserRequest>,
|
req: AuthenticatedMedusaRequest<AdminUpdateUserType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const workflow = updateUsersWorkflow(req.scope)
|
const workflow = updateUsersWorkflow(req.scope)
|
||||||
@@ -41,7 +56,11 @@ export const POST = async (
|
|||||||
|
|
||||||
const { result } = await workflow.run({ input })
|
const { result } = await workflow.run({ input })
|
||||||
|
|
||||||
const [user] = result
|
const user = await refetchUser(
|
||||||
|
req.params.id,
|
||||||
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
|
)
|
||||||
|
|
||||||
res.status(200).json({ user })
|
res.status(200).json({ user })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { MedusaContainer } from "@medusajs/types"
|
||||||
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
|
export const refetchUser = async (
|
||||||
|
userId: string,
|
||||||
|
scope: MedusaContainer,
|
||||||
|
fields: string[]
|
||||||
|
) => {
|
||||||
|
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "user",
|
||||||
|
variables: {
|
||||||
|
filters: { id: userId },
|
||||||
|
},
|
||||||
|
fields: fields,
|
||||||
|
})
|
||||||
|
|
||||||
|
const users = await remoteQuery(queryObject)
|
||||||
|
return users[0]
|
||||||
|
}
|
||||||
@@ -22,7 +22,7 @@ export const GET = async (
|
|||||||
const query = remoteQueryObjectFromString({
|
const query = remoteQueryObjectFromString({
|
||||||
entryPoint: "user",
|
entryPoint: "user",
|
||||||
variables: { id },
|
variables: { id },
|
||||||
fields: req.retrieveConfig.select as string[],
|
fields: req.remoteQueryConfig.fields,
|
||||||
})
|
})
|
||||||
|
|
||||||
const [user] = await remoteQuery(query)
|
const [user] = await remoteQuery(query)
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
import * as QueryConfig from "./query-config"
|
import * as QueryConfig from "./query-config"
|
||||||
|
|
||||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
|
||||||
import {
|
import {
|
||||||
AdminCreateUserRequest,
|
AdminCreateUser,
|
||||||
|
AdminGetUserParams,
|
||||||
AdminGetUsersParams,
|
AdminGetUsersParams,
|
||||||
AdminGetUsersUserParams,
|
AdminUpdateUser,
|
||||||
AdminUpdateUserRequest,
|
|
||||||
} from "./validators"
|
} from "./validators"
|
||||||
|
|
||||||
import { MiddlewareRoute } from "../../../types/middlewares"
|
import { MiddlewareRoute } from "../../../types/middlewares"
|
||||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||||
|
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||||
|
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||||
|
|
||||||
export const adminUserRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminUserRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
{
|
{
|
||||||
@@ -17,7 +18,10 @@ export const adminUserRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
matcher: "/admin/users",
|
matcher: "/admin/users",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
authenticate("admin", ["bearer", "session"]),
|
authenticate("admin", ["bearer", "session"]),
|
||||||
transformQuery(AdminGetUsersParams, QueryConfig.listTransformQueryConfig),
|
validateAndTransformQuery(
|
||||||
|
AdminGetUsersParams,
|
||||||
|
QueryConfig.listTransformQueryConfig
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -25,7 +29,11 @@ export const adminUserRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
matcher: "/admin/users",
|
matcher: "/admin/users",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
authenticate("admin", ["bearer", "session"], { allowUnregistered: true }),
|
authenticate("admin", ["bearer", "session"], { allowUnregistered: true }),
|
||||||
transformBody(AdminCreateUserRequest),
|
validateAndTransformBody(AdminCreateUser),
|
||||||
|
validateAndTransformQuery(
|
||||||
|
AdminGetUserParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -33,8 +41,8 @@ export const adminUserRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
matcher: "/admin/users/:id",
|
matcher: "/admin/users/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
authenticate("admin", ["bearer", "session"]),
|
authenticate("admin", ["bearer", "session"]),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetUsersUserParams,
|
AdminGetUserParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -44,8 +52,8 @@ export const adminUserRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
matcher: "/admin/users/me",
|
matcher: "/admin/users/me",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
authenticate("admin", ["bearer", "session"]),
|
authenticate("admin", ["bearer", "session"]),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetUsersUserParams,
|
AdminGetUserParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -55,7 +63,16 @@ export const adminUserRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
matcher: "/admin/users/:id",
|
matcher: "/admin/users/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
authenticate("admin", ["bearer", "session"]),
|
authenticate("admin", ["bearer", "session"]),
|
||||||
transformBody(AdminUpdateUserRequest),
|
validateAndTransformBody(AdminUpdateUser),
|
||||||
|
validateAndTransformQuery(
|
||||||
|
AdminGetUserParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
method: ["DELETE"],
|
||||||
|
matcher: "/admin/users/:id",
|
||||||
|
middlewares: [authenticate("admin", ["bearer", "session"])],
|
||||||
|
},
|
||||||
]
|
]
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
export const defaultAdminUserRelations = []
|
|
||||||
export const allowedAdminUserRelations = []
|
|
||||||
export const defaultAdminUserFields = [
|
export const defaultAdminUserFields = [
|
||||||
"id",
|
"id",
|
||||||
"first_name",
|
"first_name",
|
||||||
@@ -13,9 +11,7 @@ export const defaultAdminUserFields = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
export const retrieveTransformQueryConfig = {
|
export const retrieveTransformQueryConfig = {
|
||||||
defaultFields: defaultAdminUserFields,
|
defaults: defaultAdminUserFields,
|
||||||
defaultRelations: defaultAdminUserRelations,
|
|
||||||
allowedRelations: allowedAdminUserRelations,
|
|
||||||
isList: false,
|
isList: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { createUserAccountWorkflow } from "@medusajs/core-flows"
|
import { createUserAccountWorkflow } from "@medusajs/core-flows"
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import { CreateUserDTO } from "@medusajs/types"
|
||||||
import { CreateUserDTO, IAuthModuleService } from "@medusajs/types"
|
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
MedusaError,
|
MedusaError,
|
||||||
@@ -11,6 +10,7 @@ import {
|
|||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../types/routing"
|
} from "../../../types/routing"
|
||||||
|
import { refetchUser } from "./helpers"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest,
|
||||||
@@ -22,17 +22,12 @@ export const GET = async (
|
|||||||
entryPoint: "user",
|
entryPoint: "user",
|
||||||
variables: {
|
variables: {
|
||||||
filters: req.filterableFields,
|
filters: req.filterableFields,
|
||||||
order: req.listConfig.order,
|
...req.remoteQueryConfig.pagination,
|
||||||
skip: req.listConfig.skip,
|
|
||||||
take: req.listConfig.take,
|
|
||||||
},
|
},
|
||||||
fields: req.listConfig.select as string[],
|
fields: req.remoteQueryConfig.fields,
|
||||||
})
|
|
||||||
|
|
||||||
const { rows: users, metadata } = await remoteQuery({
|
|
||||||
...query,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const { rows: users, metadata } = await remoteQuery(query)
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
users,
|
users,
|
||||||
count: metadata.count,
|
count: metadata.count,
|
||||||
@@ -45,10 +40,6 @@ export const POST = async (
|
|||||||
req: AuthenticatedMedusaRequest<CreateUserDTO>,
|
req: AuthenticatedMedusaRequest<CreateUserDTO>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const authModuleService = req.scope.resolve<IAuthModuleService>(
|
|
||||||
ModuleRegistrationName.AUTH
|
|
||||||
)
|
|
||||||
|
|
||||||
// If `actor_id` is present, the request carries authentication for an existing user
|
// If `actor_id` is present, the request carries authentication for an existing user
|
||||||
if (req.auth.actor_id) {
|
if (req.auth.actor_id) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
@@ -65,10 +56,16 @@ export const POST = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { result } = await createUserAccountWorkflow(req.scope).run(input)
|
const { result } = await createUserAccountWorkflow(req.scope).run(input)
|
||||||
|
const user = await refetchUser(
|
||||||
|
req.auth.auth_user_id,
|
||||||
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
|
)
|
||||||
|
|
||||||
const { jwt_secret } = req.scope.resolve("configModule").projectConfig
|
const { jwt_secret } = req.scope.resolve(
|
||||||
const authUser = await authModuleService.retrieve(req.auth.auth_user_id)
|
ContainerRegistrationKeys.CONFIG_MODULE
|
||||||
const token = jwt.sign(authUser, jwt_secret)
|
).projectConfig
|
||||||
|
const token = jwt.sign(user, jwt_secret)
|
||||||
|
|
||||||
res.status(200).json({ user: result, token })
|
res.status(200).json({ user, token })
|
||||||
}
|
}
|
||||||
@@ -1,112 +1,39 @@
|
|||||||
import { Type } from "class-transformer"
|
|
||||||
import { IsEmail, IsOptional, IsString, ValidateNested } from "class-validator"
|
|
||||||
import {
|
import {
|
||||||
DateComparisonOperator,
|
createFindParams,
|
||||||
FindParams,
|
createOperatorMap,
|
||||||
extendedFindParamsMixin,
|
createSelectParams,
|
||||||
} from "../../../types/common"
|
} from "../../utils/validators"
|
||||||
import { IsType } from "../../../utils"
|
import { z } from "zod"
|
||||||
|
|
||||||
export class AdminGetUsersUserParams extends FindParams {}
|
export const AdminGetUserParams = createSelectParams()
|
||||||
|
|
||||||
export class AdminGetUsersParams extends extendedFindParamsMixin({
|
export type AdminGetUsersParamsType = z.infer<typeof AdminGetUsersParams>
|
||||||
limit: 50,
|
export const AdminGetUsersParams = createFindParams({
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}) {
|
limit: 50,
|
||||||
/**
|
}).merge(
|
||||||
* IDs to filter users by.
|
z.object({
|
||||||
*/
|
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsOptional()
|
created_at: createOperatorMap().optional(),
|
||||||
@IsType([String, [String]])
|
updated_at: createOperatorMap().optional(),
|
||||||
id?: string | string[]
|
deleted_at: createOperatorMap().optional(),
|
||||||
|
email: z.string().optional(),
|
||||||
|
first_name: z.string().optional(),
|
||||||
|
last_name: z.string().optional(),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
export type AdminCreateUserType = z.infer<typeof AdminCreateUser>
|
||||||
* The field to sort the data by. By default, the sort order is ascending. To change the order to descending, prefix the field name with `-`.
|
export const AdminCreateUser = z.object({
|
||||||
*/
|
email: z.string(),
|
||||||
@IsString()
|
first_name: z.string().optional(),
|
||||||
@IsOptional()
|
last_name: z.string().optional(),
|
||||||
order?: string
|
avatar_url: z.string().optional(),
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
export type AdminUpdateUserType = z.infer<typeof AdminUpdateUser>
|
||||||
* Date filters to apply on the users' `update_at` date.
|
export const AdminUpdateUser = z.object({
|
||||||
*/
|
first_name: z.string().optional(),
|
||||||
@IsOptional()
|
last_name: z.string().optional(),
|
||||||
@ValidateNested()
|
avatar_url: z.string().optional(),
|
||||||
@Type(() => DateComparisonOperator)
|
})
|
||||||
updated_at?: DateComparisonOperator
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Date filters to apply on the customer users' `created_at` date.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => DateComparisonOperator)
|
|
||||||
created_at?: DateComparisonOperator
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Date filters to apply on the users' `deleted_at` date.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => DateComparisonOperator)
|
|
||||||
deleted_at?: DateComparisonOperator
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter to apply on the users' `email` field.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
email?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter to apply on the users' `first_name` field.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
first_name?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter to apply on the users' `last_name` field.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
last_name?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Comma-separated fields that should be included in the returned users.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
fields?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminCreateUserRequest {
|
|
||||||
@IsEmail()
|
|
||||||
email: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
first_name?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
last_name?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
avatar_url: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminUpdateUserRequest {
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
first_name?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
last_name?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
avatar_url: string
|
|
||||||
}
|
|
||||||
@@ -3,25 +3,25 @@ import {
|
|||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../types/routing"
|
} from "../../../../types/routing"
|
||||||
|
|
||||||
import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
|
import { AdminGetWorkflowExecutionDetailsParamsType } from "../validators"
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetWorkflowExecutionDetailsParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
ModuleRegistrationName.WORKFLOW_ENGINE
|
const variables = { id: req.params.id }
|
||||||
)
|
|
||||||
|
|
||||||
const { id } = req.params
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "workflow_execution",
|
||||||
const execution = await workflowEngineService.retrieveWorkflowExecution(id, {
|
variables,
|
||||||
select: req.retrieveConfig.select,
|
fields: req.remoteQueryConfig.fields,
|
||||||
relations: req.retrieveConfig.relations,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({
|
const [workflowExecution] = await remoteQuery(queryObject)
|
||||||
workflow_execution: execution,
|
res.status(200).json({ workflow_execution: workflowExecution })
|
||||||
})
|
|
||||||
}
|
}
|
||||||
+16
-19
@@ -3,31 +3,28 @@ import {
|
|||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../types/routing"
|
} from "../../../../../types/routing"
|
||||||
|
|
||||||
import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
|
import {
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
import { AdminGetWorkflowExecutionDetailsParamsType } from "../../validators"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetWorkflowExecutionDetailsParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
ModuleRegistrationName.WORKFLOW_ENGINE
|
|
||||||
)
|
|
||||||
|
|
||||||
const { workflow_id, transaction_id } = req.params
|
const { workflow_id, transaction_id } = req.params
|
||||||
|
const variables = { workflow_id, transaction_id }
|
||||||
|
|
||||||
const execution = await workflowEngineService.retrieveWorkflowExecution(
|
const queryObject = remoteQueryObjectFromString({
|
||||||
{
|
entryPoint: "workflow_execution",
|
||||||
workflow_id,
|
variables,
|
||||||
transaction_id,
|
fields: req.remoteQueryConfig.fields,
|
||||||
},
|
|
||||||
{
|
|
||||||
select: req.retrieveConfig.select,
|
|
||||||
relations: req.retrieveConfig.relations,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
workflow_execution: execution,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const [workflowExecution] = await remoteQuery(queryObject)
|
||||||
|
|
||||||
|
res.status(200).json({ workflow_execution: workflowExecution })
|
||||||
}
|
}
|
||||||
@@ -7,11 +7,11 @@ import {
|
|||||||
WorkflowOrchestratorTypes,
|
WorkflowOrchestratorTypes,
|
||||||
} from "@medusajs/workflows-sdk"
|
} from "@medusajs/workflows-sdk"
|
||||||
|
|
||||||
import { AdminPostWorkflowsRunReq } from "../../validators"
|
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
|
import { AdminCreateWorkflowsRunType } from "../../validators"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminPostWorkflowsRunReq>,
|
req: AuthenticatedMedusaRequest<AdminCreateWorkflowsRunType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
|
const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
|
||||||
|
|||||||
+2
-2
@@ -5,11 +5,11 @@ import {
|
|||||||
import { IWorkflowEngineService, StepResponse } from "@medusajs/workflows-sdk"
|
import { IWorkflowEngineService, StepResponse } from "@medusajs/workflows-sdk"
|
||||||
import { TransactionHandlerType, isDefined } from "@medusajs/utils"
|
import { TransactionHandlerType, isDefined } from "@medusajs/utils"
|
||||||
|
|
||||||
import { AdminPostWorkflowsAsyncResponseReq } from "../../../validators"
|
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
|
import { AdminCreateWorkflowsAsyncResponseType } from "../../../validators"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminPostWorkflowsAsyncResponseReq>,
|
req: AuthenticatedMedusaRequest<AdminCreateWorkflowsAsyncResponseType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
|
const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
|
||||||
|
|||||||
+2
-2
@@ -5,11 +5,11 @@ import {
|
|||||||
import { IWorkflowEngineService, StepResponse } from "@medusajs/workflows-sdk"
|
import { IWorkflowEngineService, StepResponse } from "@medusajs/workflows-sdk"
|
||||||
import { TransactionHandlerType, isDefined } from "@medusajs/utils"
|
import { TransactionHandlerType, isDefined } from "@medusajs/utils"
|
||||||
|
|
||||||
import { AdminPostWorkflowsAsyncResponseReq } from "../../../validators"
|
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
|
import { AdminCreateWorkflowsAsyncResponseType } from "../../../validators"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminPostWorkflowsAsyncResponseReq>,
|
req: AuthenticatedMedusaRequest<AdminCreateWorkflowsAsyncResponseType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
|
const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
|
||||||
|
|||||||
@@ -1,27 +1,28 @@
|
|||||||
import * as QueryConfig from "./query-config"
|
import * as QueryConfig from "./query-config"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
AdminCreateWorkflowsAsyncResponse,
|
||||||
|
AdminCreateWorkflowsRun,
|
||||||
AdminGetWorkflowExecutionDetailsParams,
|
AdminGetWorkflowExecutionDetailsParams,
|
||||||
AdminGetWorkflowExecutionsParams,
|
AdminGetWorkflowExecutionsParams,
|
||||||
AdminPostWorkflowsAsyncResponseReq,
|
|
||||||
AdminPostWorkflowsRunReq,
|
|
||||||
} from "./validators"
|
} from "./validators"
|
||||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
|
||||||
|
|
||||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||||
|
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||||
|
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||||
|
|
||||||
export const adminWorkflowsExecutionsMiddlewares: MiddlewareRoute[] = [
|
export const adminWorkflowsExecutionsMiddlewares: MiddlewareRoute[] = [
|
||||||
{
|
{
|
||||||
method: ["ALL"],
|
method: ["ALL"],
|
||||||
matcher: "/admin/workflows-executions*",
|
matcher: "/admin/workflows-executions*",
|
||||||
middlewares: [authenticate("admin", ["bearer", "session"])],
|
middlewares: [authenticate("admin", ["bearer", "session", "api-key"])],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/workflows-executions",
|
matcher: "/admin/workflows-executions",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetWorkflowExecutionsParams,
|
AdminGetWorkflowExecutionsParams,
|
||||||
QueryConfig.listTransformQueryConfig
|
QueryConfig.listTransformQueryConfig
|
||||||
),
|
),
|
||||||
@@ -31,7 +32,7 @@ export const adminWorkflowsExecutionsMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/workflows-executions/:id",
|
matcher: "/admin/workflows-executions/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetWorkflowExecutionDetailsParams,
|
AdminGetWorkflowExecutionDetailsParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
@@ -41,7 +42,7 @@ export const adminWorkflowsExecutionsMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/workflows-executions/:workflow_id/:transaction_id",
|
matcher: "/admin/workflows-executions/:workflow_id/:transaction_id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetWorkflowExecutionDetailsParams,
|
AdminGetWorkflowExecutionDetailsParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
@@ -50,17 +51,17 @@ export const adminWorkflowsExecutionsMiddlewares: MiddlewareRoute[] = [
|
|||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/workflows-executions/:id/run",
|
matcher: "/admin/workflows-executions/:id/run",
|
||||||
middlewares: [transformBody(AdminPostWorkflowsRunReq)],
|
middlewares: [validateAndTransformBody(AdminCreateWorkflowsRun)],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
|
|
||||||
matcher: "/admin/workflows-executions/:id/steps/success",
|
matcher: "/admin/workflows-executions/:id/steps/success",
|
||||||
middlewares: [transformBody(AdminPostWorkflowsAsyncResponseReq)],
|
middlewares: [validateAndTransformBody(AdminCreateWorkflowsAsyncResponse)],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/workflows-executions/:id/steps/failure",
|
matcher: "/admin/workflows-executions/:id/steps/failure",
|
||||||
middlewares: [transformBody(AdminPostWorkflowsAsyncResponseReq)],
|
middlewares: [validateAndTransformBody(AdminCreateWorkflowsAsyncResponse)],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@@ -1,15 +1,3 @@
|
|||||||
export const defaultAdminWorkflowExecutionsRelations = []
|
|
||||||
export const allowedAdminWorkflowExecutionsRelations = []
|
|
||||||
export const defaultAdminWorkflowExecutionsFields = [
|
|
||||||
"id",
|
|
||||||
"workflow_id",
|
|
||||||
"transaction_id",
|
|
||||||
"state",
|
|
||||||
"created_at",
|
|
||||||
"updated_at",
|
|
||||||
"deleted_at",
|
|
||||||
]
|
|
||||||
|
|
||||||
export const defaultAdminWorkflowExecutionDetailFields = [
|
export const defaultAdminWorkflowExecutionDetailFields = [
|
||||||
"id",
|
"id",
|
||||||
"workflow_id",
|
"workflow_id",
|
||||||
@@ -22,15 +10,23 @@ export const defaultAdminWorkflowExecutionDetailFields = [
|
|||||||
"deleted_at",
|
"deleted_at",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
export const defaultAdminWorkflowExecutionsFields = [
|
||||||
|
"id",
|
||||||
|
"workflow_id",
|
||||||
|
"transaction_id",
|
||||||
|
"state",
|
||||||
|
"created_at",
|
||||||
|
"updated_at",
|
||||||
|
"deleted_at",
|
||||||
|
]
|
||||||
|
|
||||||
export const retrieveTransformQueryConfig = {
|
export const retrieveTransformQueryConfig = {
|
||||||
defaultFields: defaultAdminWorkflowExecutionDetailFields,
|
defaults: defaultAdminWorkflowExecutionDetailFields,
|
||||||
defaultRelations: defaultAdminWorkflowExecutionsRelations,
|
|
||||||
allowedRelations: allowedAdminWorkflowExecutionsRelations,
|
|
||||||
isList: false,
|
isList: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const listTransformQueryConfig = {
|
export const listTransformQueryConfig = {
|
||||||
...retrieveTransformQueryConfig,
|
...retrieveTransformQueryConfig,
|
||||||
defaultFields: defaultAdminWorkflowExecutionsFields,
|
defaults: defaultAdminWorkflowExecutionsFields,
|
||||||
isList: true,
|
isList: true,
|
||||||
}
|
}
|
||||||
@@ -3,34 +3,33 @@ import {
|
|||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../types/routing"
|
} from "../../../types/routing"
|
||||||
|
|
||||||
import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
|
import { AdminGetWorkflowExecutionsParamsType } from "./validators"
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetWorkflowExecutionsParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
ModuleRegistrationName.WORKFLOW_ENGINE
|
|
||||||
)
|
|
||||||
|
|
||||||
const listConfig = req.listConfig
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "workflow_execution",
|
||||||
|
variables: {
|
||||||
|
filters: req.filterableFields,
|
||||||
|
...req.remoteQueryConfig.pagination,
|
||||||
|
},
|
||||||
|
fields: req.remoteQueryConfig.fields,
|
||||||
|
})
|
||||||
|
|
||||||
const [workflow_executions, count] =
|
const { rows: workflowExecutions, metadata } = await remoteQuery(queryObject)
|
||||||
await workflowEngineService.listAndCountWorkflowExecution(
|
|
||||||
req.filterableFields,
|
|
||||||
{
|
|
||||||
select: req.listConfig.select,
|
|
||||||
relations: req.listConfig.relations,
|
|
||||||
skip: listConfig.skip,
|
|
||||||
take: listConfig.take,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
workflow_executions,
|
workflow_executions: workflowExecutions,
|
||||||
count,
|
count: metadata.count,
|
||||||
offset: listConfig.skip,
|
offset: metadata.skip,
|
||||||
limit: listConfig.take,
|
limit: metadata.take,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -1,54 +1,46 @@
|
|||||||
import { TransactionHandlerType } from "@medusajs/utils"
|
import { TransactionHandlerType } from "@medusajs/utils"
|
||||||
import { Transform } from "class-transformer"
|
import { createFindParams, createSelectParams } from "../../utils/validators"
|
||||||
import { IsEnum, IsOptional, IsString } from "class-validator"
|
import { z } from "zod"
|
||||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
|
||||||
import { IsType } from "../../../utils"
|
|
||||||
|
|
||||||
export class AdminGetWorkflowExecutionDetailsParams extends FindParams {}
|
export type AdminGetWorkflowExecutionDetailsParamsType = z.infer<
|
||||||
|
typeof AdminGetWorkflowExecutionDetailsParams
|
||||||
|
>
|
||||||
|
|
||||||
export class AdminGetWorkflowExecutionsParams extends extendedFindParamsMixin({
|
export const AdminGetWorkflowExecutionDetailsParams = createSelectParams()
|
||||||
limit: 100,
|
|
||||||
|
export type AdminGetWorkflowExecutionsParamsType = z.infer<
|
||||||
|
typeof AdminGetWorkflowExecutionsParams
|
||||||
|
>
|
||||||
|
export const AdminGetWorkflowExecutionsParams = createFindParams({
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}) {
|
limit: 100,
|
||||||
/**
|
}).merge(
|
||||||
* transaction id(s) to filter workflow executions by transaction_id.
|
z.object({
|
||||||
*/
|
transaction_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsOptional()
|
workflow_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsType([String, [String]])
|
})
|
||||||
transaction_id?: string | string[]
|
)
|
||||||
|
|
||||||
/**
|
export type AdminCreateWorkflowsRunType = z.infer<
|
||||||
* workflow id(s) to filter workflow executions by workflow_id
|
typeof AdminCreateWorkflowsRun
|
||||||
*/
|
>
|
||||||
@IsOptional()
|
export const AdminCreateWorkflowsRun = z.object({
|
||||||
@IsType([String, [String]])
|
input: z.any().optional(),
|
||||||
workflow_id?: string | string[]
|
transaction_id: z.string().optional(),
|
||||||
}
|
})
|
||||||
|
|
||||||
export class AdminPostWorkflowsRunReq {
|
export type AdminCreateWorkflowsAsyncResponseType = z.infer<
|
||||||
@IsOptional()
|
typeof AdminCreateWorkflowsAsyncResponse
|
||||||
input?: unknown
|
>
|
||||||
|
export const AdminCreateWorkflowsAsyncResponse = z.object({
|
||||||
@IsOptional()
|
transaction_id: z.string(),
|
||||||
@IsString()
|
step_id: z.string(),
|
||||||
transaction_id?: string
|
response: z.any().optional(),
|
||||||
}
|
compensate_input: z.any().optional(),
|
||||||
|
action: z
|
||||||
export class AdminPostWorkflowsAsyncResponseReq {
|
.preprocess(
|
||||||
@IsString()
|
(val: any) => (val + "").toLowerCase(),
|
||||||
transaction_id: string
|
z.nativeEnum(TransactionHandlerType).optional()
|
||||||
|
)
|
||||||
@IsString()
|
.optional(),
|
||||||
step_id: string
|
})
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
response?: unknown
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
compensate_input?: unknown
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@Transform(({ value }) => (value + "").toLowerCase())
|
|
||||||
@IsEnum(TransactionHandlerType)
|
|
||||||
action?: TransactionHandlerType
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user