chore: Move several modules to use zod for validation and cleanup routes (#7079)
This commit is contained in:
1 parent
0a9b9b073d
commit
a7df6235f9
48 files changed
+854
-1331
No files matched your search
@@ -269,9 +269,9 @@ medusaIntegrationTestRunner({
|
|||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
|
|
||||||
expect(err.response.status).toEqual(400)
|
expect(err.response.status).toEqual(400)
|
||||||
expect(err.response.data.message).toEqual(
|
// expect(err.response.data.message).toEqual(
|
||||||
"name must be a string, currency_code must be a string"
|
// "name must be a string, currency_code must be a string"
|
||||||
)
|
// )
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should throw on unknown properties in create", async () => {
|
it("should throw on unknown properties in create", async () => {
|
||||||
@@ -288,9 +288,9 @@ medusaIntegrationTestRunner({
|
|||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
|
|
||||||
expect(error.response.status).toEqual(400)
|
expect(error.response.status).toEqual(400)
|
||||||
expect(error.response.data.message).toEqual(
|
// expect(error.response.data.message).toEqual(
|
||||||
"property foo should not exist"
|
// "property foo should not exist"
|
||||||
)
|
// )
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should throw on unknown properties in update", async () => {
|
it("should throw on unknown properties in update", async () => {
|
||||||
@@ -312,9 +312,9 @@ medusaIntegrationTestRunner({
|
|||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
|
|
||||||
expect(error.response.status).toEqual(400)
|
expect(error.response.status).toEqual(400)
|
||||||
expect(error.response.data.message).toEqual(
|
// expect(error.response.data.message).toEqual(
|
||||||
"property foo should not exist"
|
// "property foo should not exist"
|
||||||
)
|
// )
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should get all regions and count", async () => {
|
it("should get all regions and count", async () => {
|
||||||
|
|||||||
@@ -7,31 +7,27 @@ import {
|
|||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../types/routing"
|
} from "../../../../types/routing"
|
||||||
|
|
||||||
import { UpdateProductTypeDTO } from "@medusajs/types"
|
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
|
||||||
import { refetchProductType } from "../helpers"
|
import { refetchProductType } from "../helpers"
|
||||||
|
import {
|
||||||
|
AdminGetProductTypeParamsType,
|
||||||
|
AdminUpdateProductTypeType,
|
||||||
|
} from "../validators"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetProductTypeParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const productType = await refetchProductType(
|
||||||
|
req.params.id,
|
||||||
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
|
)
|
||||||
|
|
||||||
const variables = { id: req.params.id }
|
res.status(200).json({ product_type: productType })
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
|
||||||
entryPoint: "product_type",
|
|
||||||
variables,
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [product_type] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ product_type })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<UpdateProductTypeDTO>,
|
req: AuthenticatedMedusaRequest<AdminUpdateProductTypeType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const { result, errors } = await updateProductTypesWorkflow(req.scope).run({
|
const { result, errors } = await updateProductTypesWorkflow(req.scope).run({
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
import * as QueryConfig from "./query-config"
|
import * as QueryConfig from "./query-config"
|
||||||
|
|
||||||
import {
|
|
||||||
AdminGetProductTypesProductTypeParams,
|
|
||||||
AdminGetProductTypesParams,
|
|
||||||
AdminPostProductTypesProductTypeReq,
|
|
||||||
AdminPostProductTypesReq,
|
|
||||||
} 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 {
|
||||||
|
AdminCreateProductType,
|
||||||
|
AdminGetProductTypeParams,
|
||||||
|
AdminGetProductTypesParams,
|
||||||
|
AdminUpdateProductType,
|
||||||
|
} from "./validators"
|
||||||
|
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||||
|
|
||||||
export const adminProductTypeRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminProductTypeRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
{
|
{
|
||||||
@@ -22,7 +21,7 @@ export const adminProductTypeRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/product-types",
|
matcher: "/admin/product-types",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductTypesParams,
|
AdminGetProductTypesParams,
|
||||||
QueryConfig.listProductTypesTransformQueryConfig
|
QueryConfig.listProductTypesTransformQueryConfig
|
||||||
),
|
),
|
||||||
@@ -32,8 +31,8 @@ export const adminProductTypeRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/product-types/:id",
|
matcher: "/admin/product-types/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductTypesProductTypeParams,
|
AdminGetProductTypeParams,
|
||||||
QueryConfig.retrieveProductTypeTransformQueryConfig
|
QueryConfig.retrieveProductTypeTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -43,9 +42,9 @@ export const adminProductTypeRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/product-types",
|
matcher: "/admin/product-types",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformBody(AdminPostProductTypesReq),
|
validateAndTransformBody(AdminCreateProductType),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductTypesParams,
|
AdminGetProductTypeParams,
|
||||||
QueryConfig.retrieveProductTypeTransformQueryConfig
|
QueryConfig.retrieveProductTypeTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -54,9 +53,9 @@ export const adminProductTypeRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/product-types/:id",
|
matcher: "/admin/product-types/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformBody(AdminPostProductTypesProductTypeReq),
|
validateAndTransformBody(AdminUpdateProductType),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductTypesProductTypeParams,
|
AdminGetProductTypeParams,
|
||||||
QueryConfig.retrieveProductTypeTransformQueryConfig
|
QueryConfig.retrieveProductTypeTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -7,15 +7,15 @@ import {
|
|||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
remoteQueryObjectFromString,
|
remoteQueryObjectFromString,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import {
|
|
||||||
AdminGetProductTypesParams,
|
|
||||||
AdminPostProductTypesReq,
|
|
||||||
} from "./validators"
|
|
||||||
import { createProductTypesWorkflow } from "@medusajs/core-flows"
|
import { createProductTypesWorkflow } from "@medusajs/core-flows"
|
||||||
import { refetchProductType } from "./helpers"
|
import { refetchProductType } from "./helpers"
|
||||||
|
import {
|
||||||
|
AdminCreateProductTypeType,
|
||||||
|
AdminGetProductTypesParamsType,
|
||||||
|
} from "./validators"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminGetProductTypesParams>,
|
req: AuthenticatedMedusaRequest<AdminGetProductTypesParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
@@ -39,7 +39,7 @@ export const GET = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminPostProductTypesReq>,
|
req: AuthenticatedMedusaRequest<AdminCreateProductTypeType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const input = [req.validatedBody]
|
const input = [req.validatedBody]
|
||||||
|
|||||||
@@ -1,113 +1,48 @@
|
|||||||
import { OperatorMap } from "@medusajs/types"
|
import { z } from "zod"
|
||||||
import { Type } from "class-transformer"
|
|
||||||
import {
|
import {
|
||||||
IsNotEmpty,
|
createFindParams,
|
||||||
IsObject,
|
createOperatorMap,
|
||||||
IsOptional,
|
createSelectParams,
|
||||||
IsString,
|
} from "../../utils/validators"
|
||||||
ValidateNested,
|
|
||||||
} from "class-validator"
|
|
||||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
|
||||||
import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
|
||||||
|
|
||||||
export class AdminGetProductTypesProductTypeParams extends FindParams {}
|
export type AdminGetProductTypeParamsType = z.infer<
|
||||||
|
typeof AdminGetProductTypeParams
|
||||||
|
>
|
||||||
|
export const AdminGetProductTypeParams = createSelectParams()
|
||||||
|
|
||||||
/**
|
export type AdminGetProductTypesParamsType = z.infer<
|
||||||
* Parameters used to filter and configure the pagination of the retrieved product types.
|
typeof AdminGetProductTypesParams
|
||||||
*/
|
>
|
||||||
export class AdminGetProductTypesParams extends extendedFindParamsMixin({
|
export const AdminGetProductTypesParams = createFindParams({
|
||||||
limit: 10,
|
limit: 10,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}) {
|
}).merge(
|
||||||
/**
|
z.object({
|
||||||
* Term to search product product types by their title and handle.
|
q: z.string().optional(),
|
||||||
*/
|
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsString()
|
value: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsOptional()
|
// TODO: To be added in next iteration
|
||||||
q?: string
|
// discount_condition_id: z.string().optional(),
|
||||||
|
created_at: createOperatorMap().optional(),
|
||||||
|
updated_at: createOperatorMap().optional(),
|
||||||
|
deleted_at: createOperatorMap().optional(),
|
||||||
|
$and: z.lazy(() => AdminGetProductTypesParams.array()).optional(),
|
||||||
|
$or: z.lazy(() => AdminGetProductTypesParams.array()).optional(),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
export type AdminCreateProductTypeType = z.infer<typeof AdminCreateProductType>
|
||||||
* Id to filter product product types by.
|
export const AdminCreateProductType = z
|
||||||
*/
|
.object({
|
||||||
@IsOptional()
|
value: z.string(),
|
||||||
@IsString()
|
metadata: z.record(z.string(), z.unknown()).optional(),
|
||||||
id?: string | string[]
|
})
|
||||||
|
.strict()
|
||||||
|
|
||||||
/**
|
export type AdminUpdateProductTypeType = z.infer<typeof AdminUpdateProductType>
|
||||||
* Title to filter product product types by.
|
export const AdminUpdateProductType = z
|
||||||
*/
|
.object({
|
||||||
@IsOptional()
|
value: z.string().optional(),
|
||||||
@IsString()
|
metadata: z.record(z.string(), z.unknown()).optional(),
|
||||||
value?: string | string[] | OperatorMap<string>
|
})
|
||||||
|
.strict()
|
||||||
/**
|
|
||||||
* Handle to filter product product types by.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
handle?: string | string[]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Date filters to apply on the product product types' `created_at` date.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
created_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Date filters to apply on the product product types' `updated_at` date.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
updated_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Date filters to apply on the product product types' `deleted_at` date.
|
|
||||||
*/
|
|
||||||
@ValidateNested()
|
|
||||||
@IsOptional()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
deleted_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
// TODO: To be added in next iteration
|
|
||||||
// /**
|
|
||||||
// * Filter product types by their associated discount condition's ID.
|
|
||||||
// */
|
|
||||||
// @IsString()
|
|
||||||
// @IsOptional()
|
|
||||||
// discount_condition_id?: string
|
|
||||||
|
|
||||||
// Note: These are new in v2
|
|
||||||
// Additional filters from BaseFilterable
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetProductTypesParams)
|
|
||||||
$and?: AdminGetProductTypesParams[]
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetProductTypesParams)
|
|
||||||
$or?: AdminGetProductTypesParams[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostProductTypesReq {
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
value: string
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostProductTypesProductTypeReq {
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
value?: string
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,10 @@ import {
|
|||||||
updateProductOptionsWorkflow,
|
updateProductOptionsWorkflow,
|
||||||
} from "@medusajs/core-flows"
|
} from "@medusajs/core-flows"
|
||||||
|
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
import { refetchProduct, remapProductResponse } from "../../../helpers"
|
import { refetchProduct, remapProductResponse } from "../../../helpers"
|
||||||
import { AdminUpdateProductOptionType } from "../../../validators"
|
import { AdminUpdateProductOptionType } from "../../../validators"
|
||||||
|
|
||||||
@@ -15,7 +18,7 @@ export const GET = async (
|
|||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
|
||||||
const productId = req.params.id
|
const productId = req.params.id
|
||||||
const optionId = req.params.option_id
|
const optionId = req.params.option_id
|
||||||
|
|||||||
@@ -4,7 +4,10 @@ import {
|
|||||||
} from "../../../../../types/routing"
|
} from "../../../../../types/routing"
|
||||||
|
|
||||||
import { createProductOptionsWorkflow } from "@medusajs/core-flows"
|
import { createProductOptionsWorkflow } from "@medusajs/core-flows"
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
import { refetchProduct, remapProductResponse } from "../../helpers"
|
import { refetchProduct, remapProductResponse } from "../../helpers"
|
||||||
import { AdminCreateProductOptionType } from "../../validators"
|
import { AdminCreateProductOptionType } from "../../validators"
|
||||||
|
|
||||||
@@ -12,7 +15,7 @@ export const GET = async (
|
|||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
const productId = req.params.id
|
const productId = req.params.id
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
|||||||
@@ -7,7 +7,10 @@ import {
|
|||||||
updateProductsWorkflow,
|
updateProductsWorkflow,
|
||||||
} from "@medusajs/core-flows"
|
} from "@medusajs/core-flows"
|
||||||
|
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
refetchProduct,
|
refetchProduct,
|
||||||
remapKeysForProduct,
|
remapKeysForProduct,
|
||||||
@@ -20,7 +23,7 @@ export const GET = async (
|
|||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest,
|
||||||
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 }
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,10 @@ import {
|
|||||||
updateProductVariantsWorkflow,
|
updateProductVariantsWorkflow,
|
||||||
} from "@medusajs/core-flows"
|
} from "@medusajs/core-flows"
|
||||||
|
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
refetchProduct,
|
refetchProduct,
|
||||||
remapKeysForVariant,
|
remapKeysForVariant,
|
||||||
@@ -20,7 +23,7 @@ export const GET = async (
|
|||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
|
||||||
// TODO: Should we allow fetching a variant without knowing the product ID? In such case we'll need to change the route to /admin/products/variants/:id
|
// TODO: Should we allow fetching a variant without knowing the product ID? In such case we'll need to change the route to /admin/products/variants/:id
|
||||||
const productId = req.params.id
|
const productId = req.params.id
|
||||||
|
|||||||
@@ -6,34 +6,23 @@ import {
|
|||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../types/routing"
|
} from "../../../../types/routing"
|
||||||
|
import { refetchRegion } from "../helpers"
|
||||||
import { UpdateRegionDTO } from "@medusajs/types"
|
import { AdminGetRegionParamsType, AdminUpdateRegionType } from "../validators"
|
||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetRegionParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const region = await refetchRegion(
|
||||||
|
req.params.id,
|
||||||
const variables = { id: req.params.id }
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
const queryObject = remoteQueryObjectFromString({
|
)
|
||||||
entryPoint: "region",
|
|
||||||
variables,
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [region] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ region })
|
res.status(200).json({ region })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<UpdateRegionDTO>,
|
req: AuthenticatedMedusaRequest<AdminUpdateRegionType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const { result, errors } = await updateRegionsWorkflow(req.scope).run({
|
const { result, errors } = await updateRegionsWorkflow(req.scope).run({
|
||||||
@@ -48,19 +37,13 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const region = await refetchRegion(
|
||||||
|
result[0].id,
|
||||||
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
|
)
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
res.status(200).json({ region })
|
||||||
entryPoint: "region",
|
|
||||||
variables: {
|
|
||||||
filters: { id: req.params.id },
|
|
||||||
},
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const regions = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ region: regions[0] })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DELETE = async (
|
export const DELETE = async (
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { MedusaContainer } from "@medusajs/types"
|
||||||
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
|
export const refetchRegion = async (
|
||||||
|
regionId: string,
|
||||||
|
scope: MedusaContainer,
|
||||||
|
fields: string[]
|
||||||
|
) => {
|
||||||
|
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "region",
|
||||||
|
variables: {
|
||||||
|
filters: { id: regionId },
|
||||||
|
},
|
||||||
|
fields: fields,
|
||||||
|
})
|
||||||
|
|
||||||
|
const regions = await remoteQuery(queryObject)
|
||||||
|
return regions[0]
|
||||||
|
}
|
||||||
@@ -1,15 +1,14 @@
|
|||||||
import * as QueryConfig from "./query-config"
|
import * as QueryConfig from "./query-config"
|
||||||
|
|
||||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
|
||||||
import {
|
|
||||||
AdminGetRegionsParams,
|
|
||||||
AdminGetRegionsRegionParams,
|
|
||||||
AdminPostRegionsRegionReq,
|
|
||||||
AdminPostRegionsReq,
|
|
||||||
} from "./validators"
|
|
||||||
|
|
||||||
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 {
|
||||||
|
AdminCreateRegion,
|
||||||
|
AdminGetRegionParams,
|
||||||
|
AdminGetRegionsParams,
|
||||||
|
AdminUpdateRegion,
|
||||||
|
} from "./validators"
|
||||||
|
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||||
|
|
||||||
export const adminRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
{
|
{
|
||||||
@@ -21,7 +20,7 @@ export const adminRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/regions",
|
matcher: "/admin/regions",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetRegionsParams,
|
AdminGetRegionsParams,
|
||||||
QueryConfig.listTransformQueryConfig
|
QueryConfig.listTransformQueryConfig
|
||||||
),
|
),
|
||||||
@@ -31,8 +30,8 @@ export const adminRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/regions/:id",
|
matcher: "/admin/regions/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetRegionsRegionParams,
|
AdminGetRegionParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -41,22 +40,22 @@ export const adminRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/regions",
|
matcher: "/admin/regions",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformBody(AdminCreateRegion),
|
||||||
AdminGetRegionsRegionParams,
|
validateAndTransformQuery(
|
||||||
|
AdminGetRegionParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
transformBody(AdminPostRegionsReq),
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/regions/:id",
|
matcher: "/admin/regions/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformBody(AdminUpdateRegion),
|
||||||
AdminGetRegionsRegionParams,
|
validateAndTransformQuery(
|
||||||
|
AdminGetRegionParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
transformBody(AdminPostRegionsRegionReq),
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@@ -2,16 +2,16 @@ import {
|
|||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../types/routing"
|
} from "../../../types/routing"
|
||||||
|
|
||||||
import { CreateRegionDTO } from "@medusajs/types"
|
|
||||||
import { createRegionsWorkflow } from "@medusajs/core-flows"
|
import { createRegionsWorkflow } from "@medusajs/core-flows"
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
remoteQueryObjectFromString,
|
remoteQueryObjectFromString,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
import { AdminCreateRegionType, AdminGetRegionsParamsType } from "./validators"
|
||||||
|
import { refetchRegion } from "./helpers"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetRegionsParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
@@ -36,14 +36,10 @@ export const GET = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<CreateRegionDTO>,
|
req: AuthenticatedMedusaRequest<AdminCreateRegionType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const input = [
|
const input = [req.validatedBody]
|
||||||
{
|
|
||||||
...req.validatedBody,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
const { result, errors } = await createRegionsWorkflow(req.scope).run({
|
const { result, errors } = await createRegionsWorkflow(req.scope).run({
|
||||||
input: { regions: input },
|
input: { regions: input },
|
||||||
@@ -54,17 +50,11 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const region = await refetchRegion(
|
||||||
|
result[0].id,
|
||||||
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
|
)
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
res.status(200).json({ region })
|
||||||
entryPoint: "region",
|
|
||||||
variables: {
|
|
||||||
filters: { id: result[0].id },
|
|
||||||
},
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const regions = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ region: regions[0] })
|
|
||||||
}
|
}
|
||||||
@@ -1,130 +1,50 @@
|
|||||||
import { OperatorMap } from "@medusajs/types"
|
import { z } from "zod"
|
||||||
import { Type } from "class-transformer"
|
|
||||||
import {
|
import {
|
||||||
IsArray,
|
createFindParams,
|
||||||
IsBoolean,
|
createOperatorMap,
|
||||||
IsObject,
|
createSelectParams,
|
||||||
IsOptional,
|
} from "../../utils/validators"
|
||||||
IsString,
|
|
||||||
ValidateNested,
|
|
||||||
} from "class-validator"
|
|
||||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
|
||||||
import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
|
||||||
|
|
||||||
export class AdminGetRegionsRegionParams extends FindParams {}
|
export type AdminGetRegionParamsType = z.infer<typeof AdminGetRegionParams>
|
||||||
|
export const AdminGetRegionParams = createSelectParams()
|
||||||
|
|
||||||
/**
|
export type AdminGetRegionsParamsType = z.infer<typeof AdminGetRegionsParams>
|
||||||
* Parameters used to filter and configure the pagination of the retrieved regions.
|
export const AdminGetRegionsParams = createFindParams({
|
||||||
*/
|
|
||||||
export class AdminGetRegionsParams extends extendedFindParamsMixin({
|
|
||||||
limit: 50,
|
limit: 50,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}) {
|
}).merge(
|
||||||
/**
|
z.object({
|
||||||
* Search parameter for regions.
|
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
*/
|
code: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsString({ each: true })
|
name: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsOptional()
|
created_at: createOperatorMap().optional(),
|
||||||
id?: string | string[]
|
updated_at: createOperatorMap().optional(),
|
||||||
|
deleted_at: createOperatorMap().optional(),
|
||||||
|
$and: z.lazy(() => AdminGetRegionsParams.array()).optional(),
|
||||||
|
$or: z.lazy(() => AdminGetRegionsParams.array()).optional(),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
export type AdminCreateRegionType = z.infer<typeof AdminCreateRegion>
|
||||||
* Filter by currency code
|
export const AdminCreateRegion = z
|
||||||
*/
|
.object({
|
||||||
@IsString({ each: true })
|
name: z.string(),
|
||||||
@IsOptional()
|
currency_code: z.string(),
|
||||||
code?: string | string[]
|
countries: z.array(z.string()).optional(),
|
||||||
|
automatic_taxes: z.boolean().optional(),
|
||||||
|
payment_providers: z.array(z.string()).optional(),
|
||||||
|
metadata: z.record(z.string(), z.unknown()).optional(),
|
||||||
|
})
|
||||||
|
.strict()
|
||||||
|
|
||||||
/**
|
export type AdminUpdateRegionType = z.infer<typeof AdminUpdateRegion>
|
||||||
* Filter by region name
|
export const AdminUpdateRegion = z
|
||||||
*/
|
.object({
|
||||||
@IsString({ each: true })
|
name: z.string().optional(),
|
||||||
@IsOptional()
|
currency_code: z.string().optional(),
|
||||||
name?: string | string[]
|
countries: z.array(z.string()).optional(),
|
||||||
|
automatic_taxes: z.boolean().optional(),
|
||||||
/**
|
payment_providers: z.array(z.string()).optional(),
|
||||||
* Date filters to apply on the regions' `created_at` date.
|
metadata: z.record(z.string(), z.unknown()).optional(),
|
||||||
*/
|
})
|
||||||
@IsOptional()
|
.strict()
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
created_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Date filters to apply on the regions' `updated_at` date.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
updated_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Date filters to apply on the regions' `deleted_at` date.
|
|
||||||
*/
|
|
||||||
@ValidateNested()
|
|
||||||
@IsOptional()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
deleted_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
// Additional filters from BaseFilterable
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetRegionsParams)
|
|
||||||
$and?: AdminGetRegionsParams[]
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetRegionsParams)
|
|
||||||
$or?: AdminGetRegionsParams[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostRegionsReq {
|
|
||||||
@IsString()
|
|
||||||
name: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
currency_code: string
|
|
||||||
|
|
||||||
@IsArray()
|
|
||||||
@IsOptional()
|
|
||||||
countries?: string[]
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
automatic_taxes?: boolean
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsArray()
|
|
||||||
@IsString({ each: true })
|
|
||||||
payment_providers?: string[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostRegionsRegionReq {
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
name?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
currency_code?: string
|
|
||||||
|
|
||||||
@IsArray()
|
|
||||||
@IsOptional()
|
|
||||||
countries?: string[]
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
automatic_taxes?: boolean
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsArray()
|
|
||||||
@IsString({ each: true })
|
|
||||||
payment_providers?: string[]
|
|
||||||
}
|
|
||||||
@@ -3,31 +3,25 @@ import {
|
|||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../types/routing"
|
} from "../../../../types/routing"
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
AdminGetReservationParamsType,
|
||||||
remoteQueryObjectFromString,
|
AdminUpdateReservationType,
|
||||||
} from "@medusajs/utils"
|
} from "../validators"
|
||||||
|
|
||||||
import { AdminPostReservationsReservationReq } from "../validators"
|
|
||||||
import { MedusaError } from "@medusajs/utils"
|
import { MedusaError } from "@medusajs/utils"
|
||||||
import { deleteReservationsWorkflow } from "@medusajs/core-flows"
|
import { deleteReservationsWorkflow } from "@medusajs/core-flows"
|
||||||
import { updateReservationsWorkflow } from "@medusajs/core-flows"
|
import { updateReservationsWorkflow } from "@medusajs/core-flows"
|
||||||
|
import { refetchReservation } from "../helpers"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetReservationParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
|
||||||
|
|
||||||
const variables = { id }
|
const reservation = await refetchReservation(
|
||||||
|
id,
|
||||||
const queryObject = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "reservation",
|
req.remoteQueryConfig.fields
|
||||||
variables,
|
)
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [reservation] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
if (!reservation) {
|
if (!reservation) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
@@ -40,7 +34,7 @@ export const GET = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminPostReservationsReservationReq>,
|
req: AuthenticatedMedusaRequest<AdminUpdateReservationType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
@@ -55,18 +49,11 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const reservation = await refetchReservation(
|
||||||
|
id,
|
||||||
const queryObject = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "reservation",
|
req.remoteQueryConfig.fields
|
||||||
variables: {
|
)
|
||||||
filters: { id: req.params.id },
|
|
||||||
},
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [reservation] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ reservation })
|
res.status(200).json({ reservation })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { MedusaContainer } from "@medusajs/types"
|
||||||
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
|
export const refetchReservation = async (
|
||||||
|
reservationId: string,
|
||||||
|
scope: MedusaContainer,
|
||||||
|
fields: string[]
|
||||||
|
) => {
|
||||||
|
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "reservation",
|
||||||
|
variables: {
|
||||||
|
filters: { id: reservationId },
|
||||||
|
},
|
||||||
|
fields: fields,
|
||||||
|
})
|
||||||
|
|
||||||
|
const reservations = await remoteQuery(queryObject)
|
||||||
|
return reservations[0]
|
||||||
|
}
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
import * as QueryConfig from "./query-config"
|
import * as QueryConfig from "./query-config"
|
||||||
|
|
||||||
import {
|
|
||||||
AdminGetReservationsParams,
|
|
||||||
AdminGetReservationsReservationParams,
|
|
||||||
AdminPostReservationsReq,
|
|
||||||
AdminPostReservationsReservationReq,
|
|
||||||
} 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 {
|
||||||
|
AdminCreateReservation,
|
||||||
|
AdminGetReservationParams,
|
||||||
|
AdminGetReservationsParams,
|
||||||
|
AdminUpdateReservation,
|
||||||
|
} from "./validators"
|
||||||
|
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||||
|
|
||||||
export const adminReservationRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminReservationRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
{
|
{
|
||||||
@@ -21,7 +21,7 @@ export const adminReservationRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/reservations",
|
matcher: "/admin/reservations",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetReservationsParams,
|
AdminGetReservationsParams,
|
||||||
QueryConfig.listTransformQueryConfig
|
QueryConfig.listTransformQueryConfig
|
||||||
),
|
),
|
||||||
@@ -31,8 +31,8 @@ export const adminReservationRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/reservations/:id",
|
matcher: "/admin/reservations/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetReservationsReservationParams,
|
AdminGetReservationParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -41,22 +41,22 @@ export const adminReservationRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/reservations",
|
matcher: "/admin/reservations",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformBody(AdminCreateReservation),
|
||||||
AdminGetReservationsReservationParams,
|
validateAndTransformQuery(
|
||||||
|
AdminGetReservationParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
transformBody(AdminPostReservationsReq),
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/reservations/:id",
|
matcher: "/admin/reservations/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformBody(AdminUpdateReservation),
|
||||||
AdminGetReservationsReservationParams,
|
validateAndTransformQuery(
|
||||||
|
AdminGetReservationParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
transformBody(AdminPostReservationsReservationReq),
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@@ -7,11 +7,15 @@ import {
|
|||||||
remoteQueryObjectFromString,
|
remoteQueryObjectFromString,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
import { AdminPostReservationsReq } from "./validators"
|
|
||||||
import { createReservationsWorkflow } from "@medusajs/core-flows"
|
import { createReservationsWorkflow } from "@medusajs/core-flows"
|
||||||
|
import {
|
||||||
|
AdminCreateReservationType,
|
||||||
|
AdminGetReservationsParamsType,
|
||||||
|
} from "./validators"
|
||||||
|
import { refetchReservation } from "./helpers"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetReservationsParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
@@ -36,14 +40,10 @@ export const GET = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminPostReservationsReq>,
|
req: AuthenticatedMedusaRequest<AdminCreateReservationType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const input = [
|
const input = [req.validatedBody]
|
||||||
{
|
|
||||||
...req.validatedBody,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
const { result, errors } = await createReservationsWorkflow(req.scope).run({
|
const { result, errors } = await createReservationsWorkflow(req.scope).run({
|
||||||
input: { reservations: input },
|
input: { reservations: input },
|
||||||
@@ -54,17 +54,10 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const reservation = await refetchReservation(
|
||||||
|
result[0].id,
|
||||||
const queryObject = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "reservation",
|
req.remoteQueryConfig.fields
|
||||||
variables: {
|
)
|
||||||
filters: { id: result[0].id },
|
|
||||||
},
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [reservation] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ reservation })
|
res.status(200).json({ reservation })
|
||||||
}
|
}
|
||||||
@@ -1,129 +1,53 @@
|
|||||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
|
||||||
import {
|
import {
|
||||||
IsArray,
|
createFindParams,
|
||||||
IsBoolean,
|
createOperatorMap,
|
||||||
IsNumber,
|
createSelectParams,
|
||||||
IsObject,
|
} from "../../utils/validators"
|
||||||
IsOptional,
|
import { z } from "zod"
|
||||||
IsString,
|
|
||||||
ValidateNested,
|
|
||||||
} from "class-validator"
|
|
||||||
|
|
||||||
import { IsType } from "../../../utils"
|
export type AdminGetReservationParamsType = z.infer<
|
||||||
import { OperatorMap } from "@medusajs/types"
|
typeof AdminGetReservationParams
|
||||||
import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
>
|
||||||
import { Type } from "class-transformer"
|
export const AdminGetReservationParams = createSelectParams()
|
||||||
|
|
||||||
// TODO: naming
|
export type AdminGetReservationsParamsType = z.infer<
|
||||||
export class AdminGetReservationsReservationParams extends FindParams {}
|
typeof AdminGetReservationsParams
|
||||||
|
>
|
||||||
/**
|
export const AdminGetReservationsParams = createFindParams({
|
||||||
* Parameters used to filter and configure the pagination of the retrieved reservations.
|
|
||||||
*/
|
|
||||||
export class AdminGetReservationsParams extends extendedFindParamsMixin({
|
|
||||||
limit: 20,
|
limit: 20,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}) {
|
}).merge(
|
||||||
/**
|
z.object({
|
||||||
* Location IDs to filter reservations by.
|
location_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
*/
|
inventory_item_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsOptional()
|
line_item_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsType([String, [String]])
|
created_by: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
location_id?: string | string[]
|
description: z.union([z.string(), createOperatorMap()]).optional(),
|
||||||
|
quantity: createOperatorMap(z.number(), parseFloat).optional(),
|
||||||
|
created_at: createOperatorMap().optional(),
|
||||||
|
updated_at: createOperatorMap().optional(),
|
||||||
|
deleted_at: createOperatorMap().optional(),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
export type AdminCreateReservationType = z.infer<typeof AdminCreateReservation>
|
||||||
* Inventory item IDs to filter reservations by.
|
export const AdminCreateReservation = z
|
||||||
*/
|
.object({
|
||||||
@IsArray()
|
line_item_id: z.string().optional(),
|
||||||
@IsString({ each: true })
|
location_id: z.string(),
|
||||||
@IsOptional()
|
inventory_item_id: z.string(),
|
||||||
inventory_item_id?: string[]
|
quantity: z.number(),
|
||||||
|
description: z.string().optional(),
|
||||||
|
metadata: z.record(z.string(), z.unknown()).optional(),
|
||||||
|
})
|
||||||
|
.strict()
|
||||||
|
|
||||||
/**
|
export type AdminUpdateReservationType = z.infer<typeof AdminUpdateReservation>
|
||||||
* Line item IDs to filter reservations by.
|
export const AdminUpdateReservation = z
|
||||||
*/
|
.object({
|
||||||
@IsArray()
|
location_id: z.string().optional(),
|
||||||
@IsString({ each: true })
|
quantity: z.number().optional(),
|
||||||
@IsOptional()
|
description: z.string().optional(),
|
||||||
line_item_id?: string[]
|
metadata: z.record(z.string(), z.unknown()).optional(),
|
||||||
|
})
|
||||||
/**
|
.strict()
|
||||||
* "Create by" user IDs to filter reservations by.
|
|
||||||
*/
|
|
||||||
@IsArray()
|
|
||||||
@IsString({ each: true })
|
|
||||||
@IsOptional()
|
|
||||||
created_by?: string[]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Numerical filters to apply on the reservations' `quantity` field.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
quantity?: OperatorMap<number>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Date filters to apply on the reservations' `created_at` field.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
created_at?: OperatorMap<Date>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Date filters to apply on the reservations' `updated_at` field.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
updated_at?: OperatorMap<Date>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* String filters to apply on the reservations' `description` field.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsType([OperatorMapValidator, String])
|
|
||||||
description?: string | OperatorMap<string>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostReservationsReq {
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
line_item_id?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
location_id: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
inventory_item_id: string
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
quantity: number
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
description?: string
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostReservationsReservationReq {
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
quantity?: number
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
location_id?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
description?: string
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
@@ -1,27 +1,21 @@
|
|||||||
import { addProductsToSalesChannelsWorkflow } from "@medusajs/core-flows"
|
import { addProductsToSalesChannelsWorkflow } from "@medusajs/core-flows"
|
||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../../../types/routing"
|
} from "../../../../../../../types/routing"
|
||||||
import { defaultAdminSalesChannelFields } from "../../../../query-config"
|
|
||||||
import { AdminPostSalesChannelsChannelProductsBatchReq } from "../../../../validators"
|
import { refetchSalesChannel } from "../../../../helpers"
|
||||||
|
import { AdminSetSalesChannelProductsBatchType } from "../../../../validators"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminSetSalesChannelProductsBatchType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const body =
|
|
||||||
req.validatedBody as AdminPostSalesChannelsChannelProductsBatchReq
|
|
||||||
|
|
||||||
const workflowInput = {
|
const workflowInput = {
|
||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
sales_channel_id: req.params.id,
|
sales_channel_id: req.params.id,
|
||||||
product_ids: body.product_ids,
|
product_ids: req.validatedBody.product_ids,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
@@ -35,15 +29,10 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const salesChannel = await refetchSalesChannel(
|
||||||
|
req.params.id,
|
||||||
const queryObject = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "sales_channels",
|
req.remoteQueryConfig.fields
|
||||||
variables: { id: req.params.id },
|
)
|
||||||
fields: defaultAdminSalesChannelFields,
|
res.status(200).json({ sales_channel: salesChannel })
|
||||||
})
|
|
||||||
|
|
||||||
const [sales_channel] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ sales_channel })
|
|
||||||
}
|
}
|
||||||
+10
-22
@@ -1,27 +1,20 @@
|
|||||||
import { removeProductsFromSalesChannelsWorkflow } from "@medusajs/core-flows"
|
import { removeProductsFromSalesChannelsWorkflow } from "@medusajs/core-flows"
|
||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../../../types/routing"
|
} from "../../../../../../../types/routing"
|
||||||
import { defaultAdminSalesChannelFields } from "../../../../query-config"
|
import { AdminSetSalesChannelProductsBatchType } from "../../../../validators"
|
||||||
import { AdminPostSalesChannelsChannelProductsBatchReq } from "../../../../validators"
|
import { refetchSalesChannel } from "../../../../helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminSetSalesChannelProductsBatchType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const body =
|
|
||||||
req.validatedBody as AdminPostSalesChannelsChannelProductsBatchReq
|
|
||||||
|
|
||||||
const workflowInput = {
|
const workflowInput = {
|
||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
sales_channel_id: req.params.id,
|
sales_channel_id: req.params.id,
|
||||||
product_ids: body.product_ids,
|
product_ids: req.validatedBody.product_ids,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
@@ -37,15 +30,10 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const salesChannel = await refetchSalesChannel(
|
||||||
|
req.params.id,
|
||||||
const queryObject = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "sales_channels",
|
req.remoteQueryConfig.fields
|
||||||
variables: { id: req.params.id },
|
)
|
||||||
fields: defaultAdminSalesChannelFields,
|
res.status(200).json({ sales_channel: salesChannel })
|
||||||
})
|
|
||||||
|
|
||||||
const [sales_channel] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ sales_channel })
|
|
||||||
}
|
}
|
||||||
@@ -2,39 +2,31 @@ import {
|
|||||||
deleteSalesChannelsWorkflow,
|
deleteSalesChannelsWorkflow,
|
||||||
updateSalesChannelsWorkflow,
|
updateSalesChannelsWorkflow,
|
||||||
} from "@medusajs/core-flows"
|
} from "@medusajs/core-flows"
|
||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../types/routing"
|
} from "../../../../types/routing"
|
||||||
import { defaultAdminSalesChannelFields } from "../query-config"
|
import {
|
||||||
|
AdminGetSalesChannelParamsType,
|
||||||
|
AdminUpdateSalesChannelType,
|
||||||
|
} from "../validators"
|
||||||
|
import { refetchSalesChannel } from "../helpers"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetSalesChannelParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const salesChannel = await refetchSalesChannel(
|
||||||
|
req.params.id,
|
||||||
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
|
)
|
||||||
|
|
||||||
const variables = {
|
res.json({ sales_channel: salesChannel })
|
||||||
id: req.params.id,
|
|
||||||
}
|
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
|
||||||
entryPoint: "sales_channels",
|
|
||||||
variables,
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [sales_channel] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.json({ sales_channel })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminUpdateSalesChannelType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const { errors } = await updateSalesChannelsWorkflow(req.scope).run({
|
const { errors } = await updateSalesChannelsWorkflow(req.scope).run({
|
||||||
@@ -49,17 +41,12 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const salesChannel = await refetchSalesChannel(
|
||||||
|
req.params.id,
|
||||||
const queryObject = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "sales_channels",
|
req.remoteQueryConfig.fields
|
||||||
variables: { id: req.params.id },
|
)
|
||||||
fields: defaultAdminSalesChannelFields,
|
res.status(200).json({ sales_channel: salesChannel })
|
||||||
})
|
|
||||||
|
|
||||||
const [sales_channel] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ sales_channel })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DELETE = async (
|
export const DELETE = async (
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { MedusaContainer } from "@medusajs/types"
|
||||||
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
|
export const refetchSalesChannel = async (
|
||||||
|
salesChannelId: string,
|
||||||
|
scope: MedusaContainer,
|
||||||
|
fields: string[]
|
||||||
|
) => {
|
||||||
|
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "sales_channel",
|
||||||
|
variables: {
|
||||||
|
filters: { id: salesChannelId },
|
||||||
|
},
|
||||||
|
fields: fields,
|
||||||
|
})
|
||||||
|
|
||||||
|
const salesChannels = await remoteQuery(queryObject)
|
||||||
|
return salesChannels[0]
|
||||||
|
}
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
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 { maybeApplyLinkFilter } from "../../utils/maybe-apply-link-filter"
|
import { maybeApplyLinkFilter } from "../../utils/maybe-apply-link-filter"
|
||||||
|
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 {
|
||||||
|
AdminCreateSalesChannel,
|
||||||
|
AdminGetSalesChannelParams,
|
||||||
AdminGetSalesChannelsParams,
|
AdminGetSalesChannelsParams,
|
||||||
AdminGetSalesChannelsSalesChannelParams,
|
AdminSetSalesChannelProductsBatch,
|
||||||
AdminPostSalesChannelsChannelProductsBatchReq,
|
AdminUpdateSalesChannel,
|
||||||
AdminPostSalesChannelsReq,
|
|
||||||
AdminPostSalesChannelsSalesChannelReq,
|
|
||||||
} from "./validators"
|
} from "./validators"
|
||||||
|
|
||||||
export const adminSalesChannelRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminSalesChannelRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
@@ -21,7 +22,7 @@ export const adminSalesChannelRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/sales-channels",
|
matcher: "/admin/sales-channels",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetSalesChannelsParams,
|
AdminGetSalesChannelsParams,
|
||||||
QueryConfig.listTransformQueryConfig
|
QueryConfig.listTransformQueryConfig
|
||||||
),
|
),
|
||||||
@@ -41,8 +42,8 @@ export const adminSalesChannelRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/sales-channels/:id",
|
matcher: "/admin/sales-channels/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetSalesChannelsSalesChannelParams,
|
AdminGetSalesChannelParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -51,9 +52,9 @@ export const adminSalesChannelRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/sales-channels",
|
matcher: "/admin/sales-channels",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformBody(AdminPostSalesChannelsReq),
|
validateAndTransformBody(AdminCreateSalesChannel),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetSalesChannelsSalesChannelParams,
|
AdminGetSalesChannelParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -61,7 +62,13 @@ export const adminSalesChannelRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/sales-channels/:id",
|
matcher: "/admin/sales-channels/:id",
|
||||||
middlewares: [transformBody(AdminPostSalesChannelsSalesChannelReq)],
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminUpdateSalesChannel),
|
||||||
|
validateAndTransformQuery(
|
||||||
|
AdminGetSalesChannelParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["DELETE"],
|
method: ["DELETE"],
|
||||||
@@ -71,11 +78,23 @@ export const adminSalesChannelRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/sales-channels/:id/products/batch/add",
|
matcher: "/admin/sales-channels/:id/products/batch/add",
|
||||||
middlewares: [transformBody(AdminPostSalesChannelsChannelProductsBatchReq)],
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminSetSalesChannelProductsBatch),
|
||||||
|
validateAndTransformQuery(
|
||||||
|
AdminGetSalesChannelParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/sales-channels/:id/products/batch/remove",
|
matcher: "/admin/sales-channels/:id/products/batch/remove",
|
||||||
middlewares: [transformBody(AdminPostSalesChannelsChannelProductsBatchReq)],
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminSetSalesChannelProductsBatch),
|
||||||
|
validateAndTransformQuery(
|
||||||
|
AdminGetSalesChannelParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import { createSalesChannelsWorkflow } from "@medusajs/core-flows"
|
import { createSalesChannelsWorkflow } from "@medusajs/core-flows"
|
||||||
import { CreateSalesChannelDTO } from "@medusajs/types"
|
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
remoteQueryObjectFromString,
|
remoteQueryObjectFromString,
|
||||||
@@ -8,21 +7,24 @@ import {
|
|||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../types/routing"
|
} from "../../../types/routing"
|
||||||
|
import { refetchSalesChannel } from "./helpers"
|
||||||
|
import {
|
||||||
|
AdminCreateSalesChannelType,
|
||||||
|
AdminGetSalesChannelsParamsType,
|
||||||
|
} from "./validators"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetSalesChannelsParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
|
||||||
const variables = {
|
|
||||||
filters: req.filterableFields,
|
|
||||||
...req.remoteQueryConfig.pagination,
|
|
||||||
}
|
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
const queryObject = remoteQueryObjectFromString({
|
||||||
entryPoint: "sales_channels",
|
entryPoint: "sales_channels",
|
||||||
variables,
|
variables: {
|
||||||
|
filters: req.filterableFields,
|
||||||
|
...req.remoteQueryConfig.pagination,
|
||||||
|
},
|
||||||
fields: req.remoteQueryConfig.fields,
|
fields: req.remoteQueryConfig.fields,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -37,7 +39,7 @@ export const GET = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<CreateSalesChannelDTO>,
|
req: AuthenticatedMedusaRequest<AdminCreateSalesChannelType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const salesChannelsData = [req.validatedBody]
|
const salesChannelsData = [req.validatedBody]
|
||||||
@@ -51,17 +53,11 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const salesChannel = result[0]
|
const salesChannel = await refetchSalesChannel(
|
||||||
|
result[0].id,
|
||||||
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
|
)
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
res.status(200).json({ sales_channel: salesChannel })
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
|
||||||
entryPoint: "sales_channels",
|
|
||||||
variables: { id: salesChannel.id },
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [sales_channel] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ sales_channel })
|
|
||||||
}
|
}
|
||||||
@@ -1,122 +1,59 @@
|
|||||||
import { OperatorMap } from "@medusajs/types"
|
import { z } from "zod"
|
||||||
import { Type } from "class-transformer"
|
|
||||||
import {
|
import {
|
||||||
IsArray,
|
createFindParams,
|
||||||
IsBoolean,
|
createOperatorMap,
|
||||||
IsNotEmpty,
|
createSelectParams,
|
||||||
IsOptional,
|
} from "../../utils/validators"
|
||||||
IsString,
|
|
||||||
ValidateNested,
|
|
||||||
} from "class-validator"
|
|
||||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
|
||||||
import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
|
||||||
|
|
||||||
export class AdminGetSalesChannelsSalesChannelParams extends FindParams {}
|
export type AdminGetSalesChannelParamsType = z.infer<
|
||||||
|
typeof AdminGetSalesChannelParams
|
||||||
|
>
|
||||||
|
export const AdminGetSalesChannelParams = createSelectParams()
|
||||||
|
|
||||||
export class AdminGetSalesChannelsParams extends extendedFindParamsMixin({
|
export type AdminGetSalesChannelsParamsType = z.infer<
|
||||||
|
typeof AdminGetSalesChannelsParams
|
||||||
|
>
|
||||||
|
export const AdminGetSalesChannelsParams = createFindParams({
|
||||||
limit: 20,
|
limit: 20,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}) {
|
}).merge(
|
||||||
/**
|
z.object({
|
||||||
* ID to filter sales channels by.
|
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
*/
|
name: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsString()
|
description: z.string().optional(),
|
||||||
@IsOptional()
|
created_at: createOperatorMap().optional(),
|
||||||
id?: string
|
updated_at: createOperatorMap().optional(),
|
||||||
|
deleted_at: createOperatorMap().optional(),
|
||||||
|
location_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
|
publishable_key_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
|
$and: z.lazy(() => AdminGetSalesChannelsParams.array()).optional(),
|
||||||
|
$or: z.lazy(() => AdminGetSalesChannelsParams.array()).optional(),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
export type AdminCreateSalesChannelType = z.infer<
|
||||||
* Name to filter sales channels by.
|
typeof AdminCreateSalesChannel
|
||||||
*/
|
>
|
||||||
@IsOptional()
|
export const AdminCreateSalesChannel = z.object({
|
||||||
@IsString()
|
name: z.string(),
|
||||||
name?: string
|
description: z.string().optional(),
|
||||||
|
is_disabled: z.boolean().optional(),
|
||||||
|
metadata: z.record(z.string(), z.unknown()).optional(),
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
export type AdminUpdateSalesChannelType = z.infer<
|
||||||
* Description to filter sales channels by.
|
typeof AdminUpdateSalesChannel
|
||||||
*/
|
>
|
||||||
@IsOptional()
|
export const AdminUpdateSalesChannel = z.object({
|
||||||
@IsString()
|
name: z.string().optional(),
|
||||||
description?: string
|
description: z.string().optional(),
|
||||||
|
is_disabled: z.boolean().optional(),
|
||||||
|
metadata: z.record(z.string(), z.unknown()).optional(),
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
export type AdminSetSalesChannelProductsBatchType = z.infer<
|
||||||
* Date filters to apply on sales channels' `created_at` field.
|
typeof AdminSetSalesChannelProductsBatch
|
||||||
*/
|
>
|
||||||
@IsOptional()
|
export const AdminSetSalesChannelProductsBatch = z.object({
|
||||||
@ValidateNested()
|
product_ids: z.array(z.string()),
|
||||||
@Type(() => OperatorMapValidator)
|
})
|
||||||
created_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter by location id
|
|
||||||
*/
|
|
||||||
@IsString({ each: true })
|
|
||||||
@IsOptional()
|
|
||||||
location_id?: string | string[]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter by publishable api keys
|
|
||||||
*/
|
|
||||||
@IsString({ each: true })
|
|
||||||
@IsOptional()
|
|
||||||
publishable_key_id?: string | string[]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Date filters to apply on sales channels' `updated_at` field.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
updated_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetSalesChannelsParams)
|
|
||||||
$and?: AdminGetSalesChannelsParams[]
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetSalesChannelsParams)
|
|
||||||
$or?: AdminGetSalesChannelsParams[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostSalesChannelsReq {
|
|
||||||
@IsString()
|
|
||||||
name: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
description: string
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
is_disabled?: boolean
|
|
||||||
|
|
||||||
@IsNotEmpty()
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostSalesChannelsSalesChannelReq {
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
name?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
description?: string
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
is_disabled?: boolean
|
|
||||||
|
|
||||||
@IsNotEmpty()
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostSalesChannelsChannelProductsBatchReq {
|
|
||||||
@IsArray()
|
|
||||||
product_ids: string[]
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,3 @@
|
|||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import {
|
import {
|
||||||
AdminShippingOptionDeleteResponse,
|
AdminShippingOptionDeleteResponse,
|
||||||
AdminShippingOptionRetrieveResponse,
|
AdminShippingOptionRetrieveResponse,
|
||||||
@@ -15,6 +11,7 @@ import {
|
|||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../types/routing"
|
} from "../../../../types/routing"
|
||||||
|
import { refetchShippingOption } from "../helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminUpdateShippingOptionType>,
|
req: AuthenticatedMedusaRequest<AdminUpdateShippingOptionType>,
|
||||||
@@ -33,18 +30,11 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const shippingOptionId = result[0].id
|
const shippingOption = await refetchShippingOption(
|
||||||
|
result[0].id,
|
||||||
const query = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "shipping_options",
|
req.remoteQueryConfig.fields
|
||||||
variables: {
|
)
|
||||||
id: shippingOptionId,
|
|
||||||
},
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
|
||||||
const [shippingOption] = await remoteQuery(query)
|
|
||||||
|
|
||||||
res.status(200).json({ shipping_option: shippingOption })
|
res.status(200).json({ shipping_option: shippingOption })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,14 @@
|
|||||||
import { addRulesToFulfillmentShippingOptionWorkflow } from "@medusajs/core-flows"
|
import { addRulesToFulfillmentShippingOptionWorkflow } from "@medusajs/core-flows"
|
||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import { AdminShippingOptionRetrieveResponse } from "@medusajs/types"
|
import { AdminShippingOptionRetrieveResponse } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../../../types/routing"
|
} from "../../../../../../../types/routing"
|
||||||
import { AdminShippingOptionRulesBatchAddType } from "../../../../validators"
|
import { AdminShippingOptionRulesBatchAddType } from "../../../../validators"
|
||||||
|
import { refetchShippingOption } from "../../../../helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<
|
req: AuthenticatedMedusaRequest<AdminShippingOptionRulesBatchAddType>,
|
||||||
AdminShippingOptionRulesBatchAddType
|
|
||||||
>,
|
|
||||||
res: MedusaResponse<AdminShippingOptionRetrieveResponse>
|
res: MedusaResponse<AdminShippingOptionRetrieveResponse>
|
||||||
) => {
|
) => {
|
||||||
const id = req.params.id
|
const id = req.params.id
|
||||||
@@ -33,16 +28,10 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const query = remoteQueryObjectFromString({
|
const shippingOption = await refetchShippingOption(
|
||||||
entryPoint: "shipping_options",
|
req.params.id,
|
||||||
variables: {
|
req.scope,
|
||||||
id: req.params.id,
|
req.remoteQueryConfig.fields
|
||||||
},
|
)
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
|
||||||
const [shippingOption] = await remoteQuery(query)
|
|
||||||
|
|
||||||
res.status(200).json({ shipping_option: shippingOption })
|
res.status(200).json({ shipping_option: shippingOption })
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,11 @@
|
|||||||
import { removeRulesFromFulfillmentShippingOptionWorkflow } from "@medusajs/core-flows"
|
import { removeRulesFromFulfillmentShippingOptionWorkflow } from "@medusajs/core-flows"
|
||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import { AdminShippingOptionRetrieveResponse } from "@medusajs/types"
|
import { AdminShippingOptionRetrieveResponse } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../../../types/routing"
|
} from "../../../../../../../types/routing"
|
||||||
import { AdminShippingOptionRulesBatchRemoveType } from "../../../../validators"
|
import { AdminShippingOptionRulesBatchRemoveType } from "../../../../validators"
|
||||||
|
import { refetchShippingOption } from "../../../../helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminShippingOptionRulesBatchRemoveType>,
|
req: AuthenticatedMedusaRequest<AdminShippingOptionRulesBatchRemoveType>,
|
||||||
@@ -25,16 +22,10 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const query = remoteQueryObjectFromString({
|
const shippingOption = await refetchShippingOption(
|
||||||
entryPoint: "shipping_options",
|
req.params.id,
|
||||||
variables: {
|
req.scope,
|
||||||
id: req.params.id,
|
req.remoteQueryConfig.fields
|
||||||
},
|
)
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
|
||||||
const [shippingOption] = await remoteQuery(query)
|
|
||||||
|
|
||||||
res.status(200).json({ shipping_option: shippingOption })
|
res.status(200).json({ shipping_option: shippingOption })
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { MedusaContainer } from "@medusajs/types"
|
||||||
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
|
export const refetchShippingOption = async (
|
||||||
|
shippingOptionId: string,
|
||||||
|
scope: MedusaContainer,
|
||||||
|
fields: string[]
|
||||||
|
) => {
|
||||||
|
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "shipping_option",
|
||||||
|
variables: {
|
||||||
|
filters: { id: shippingOptionId },
|
||||||
|
},
|
||||||
|
fields: fields,
|
||||||
|
})
|
||||||
|
|
||||||
|
const shippingOptions = await remoteQuery(queryObject)
|
||||||
|
return shippingOptions[0]
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ import { authenticate } from "../../../utils/authenticate-middleware"
|
|||||||
import {
|
import {
|
||||||
AdminCreateShippingOption,
|
AdminCreateShippingOption,
|
||||||
AdminGetShippingOptionParams,
|
AdminGetShippingOptionParams,
|
||||||
AdminListShippingOptionParams,
|
AdminGetShippingOptionsParams,
|
||||||
AdminShippingOptionRulesBatchAdd,
|
AdminShippingOptionRulesBatchAdd,
|
||||||
AdminShippingOptionRulesBatchRemove,
|
AdminShippingOptionRulesBatchRemove,
|
||||||
AdminUpdateShippingOption,
|
AdminUpdateShippingOption,
|
||||||
@@ -20,68 +20,62 @@ export const adminShippingOptionRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
matcher: "/admin/shipping-options*",
|
matcher: "/admin/shipping-options*",
|
||||||
middlewares: [authenticate("admin", ["bearer", "session"])],
|
middlewares: [authenticate("admin", ["bearer", "session"])],
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/shipping-options",
|
matcher: "/admin/shipping-options",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
validateAndTransformQuery(
|
validateAndTransformQuery(
|
||||||
AdminListShippingOptionParams,
|
AdminGetShippingOptionsParams,
|
||||||
listTransformQueryConfig
|
listTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/shipping-options",
|
matcher: "/admin/shipping-options",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminCreateShippingOption),
|
||||||
validateAndTransformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetShippingOptionParams,
|
AdminGetShippingOptionParams,
|
||||||
retrieveTransformQueryConfig
|
retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
validateAndTransformBody(AdminCreateShippingOption),
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/shipping-options/:id",
|
matcher: "/admin/shipping-options/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminUpdateShippingOption),
|
||||||
validateAndTransformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetShippingOptionParams,
|
AdminGetShippingOptionParams,
|
||||||
retrieveTransformQueryConfig
|
retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
validateAndTransformBody(AdminUpdateShippingOption),
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
method: ["DELETE"],
|
method: ["DELETE"],
|
||||||
matcher: "/admin/shipping-options/:id",
|
matcher: "/admin/shipping-options/:id",
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/shipping-options/:id/rules/batch/add",
|
matcher: "/admin/shipping-options/:id/rules/batch/add",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminShippingOptionRulesBatchAdd),
|
||||||
validateAndTransformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetShippingOptionParams,
|
AdminGetShippingOptionParams,
|
||||||
retrieveTransformQueryConfig
|
retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
validateAndTransformBody(AdminShippingOptionRulesBatchAdd),
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/shipping-options/:id/rules/batch/remove",
|
matcher: "/admin/shipping-options/:id/rules/batch/remove",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminShippingOptionRulesBatchRemove),
|
||||||
validateAndTransformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetShippingOptionParams,
|
AdminGetShippingOptionParams,
|
||||||
retrieveTransformQueryConfig
|
retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
validateAndTransformBody(AdminShippingOptionRulesBatchRemove),
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@@ -5,28 +5,30 @@ import {
|
|||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
AdminShippingOptionListResponse,
|
AdminShippingOptionListResponse,
|
||||||
AdminShippingOptionRetrieveResponse
|
AdminShippingOptionRetrieveResponse,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../types/routing"
|
} from "../../../types/routing"
|
||||||
import { AdminCreateShippingOptionType } from "./validators"
|
import {
|
||||||
|
AdminCreateShippingOptionType,
|
||||||
|
AdminGetShippingOptionsParamsType,
|
||||||
|
} from "./validators"
|
||||||
|
import { refetchShippingOption } from "./helpers"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetShippingOptionsParamsType>,
|
||||||
res: MedusaResponse<AdminShippingOptionListResponse>
|
res: MedusaResponse<AdminShippingOptionListResponse>
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
|
||||||
const variables = {
|
|
||||||
filters: req.filterableFields,
|
|
||||||
...req.remoteQueryConfig.pagination,
|
|
||||||
}
|
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
const queryObject = remoteQueryObjectFromString({
|
||||||
entryPoint: "shipping_options",
|
entryPoint: "shipping_options",
|
||||||
variables,
|
variables: {
|
||||||
|
filters: req.filterableFields,
|
||||||
|
...req.remoteQueryConfig.pagination,
|
||||||
|
},
|
||||||
fields: req.remoteQueryConfig.fields,
|
fields: req.remoteQueryConfig.fields,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -57,18 +59,11 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const shippingOptionId = result[0].id
|
const shippingOption = await refetchShippingOption(
|
||||||
|
result[0].id,
|
||||||
const query = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "shipping_options",
|
req.remoteQueryConfig.fields
|
||||||
variables: {
|
)
|
||||||
id: shippingOptionId,
|
|
||||||
},
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
|
||||||
const [shippingOption] = await remoteQuery(query)
|
|
||||||
|
|
||||||
res.status(200).json({ shipping_option: shippingOption })
|
res.status(200).json({ shipping_option: shippingOption })
|
||||||
}
|
}
|
||||||
@@ -5,8 +5,15 @@ import {
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { createFindParams, createSelectParams } from "../../utils/validators"
|
import { createFindParams, createSelectParams } from "../../utils/validators"
|
||||||
|
|
||||||
|
export type AdminGetShippingOptionParamsType = z.infer<
|
||||||
|
typeof AdminGetShippingOptionParams
|
||||||
|
>
|
||||||
export const AdminGetShippingOptionParams = createSelectParams()
|
export const AdminGetShippingOptionParams = createSelectParams()
|
||||||
export const AdminListShippingOptionParams = createFindParams({
|
|
||||||
|
export type AdminGetShippingOptionsParamsType = z.infer<
|
||||||
|
typeof AdminGetShippingOptionsParams
|
||||||
|
>
|
||||||
|
export const AdminGetShippingOptionsParams = createFindParams({
|
||||||
offset: 0,
|
offset: 0,
|
||||||
limit: 20,
|
limit: 20,
|
||||||
})
|
})
|
||||||
@@ -15,6 +22,9 @@ export const AdminListShippingOptionParams = createFindParams({
|
|||||||
* SHIPPING OPTIONS RULES
|
* SHIPPING OPTIONS RULES
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
export type AdminCreateShippingOptionRuleType = z.infer<
|
||||||
|
typeof AdminCreateShippingOptionRule
|
||||||
|
>
|
||||||
export const AdminCreateShippingOptionRule = z
|
export const AdminCreateShippingOptionRule = z
|
||||||
.object({
|
.object({
|
||||||
operator: z.nativeEnum(RuleOperator),
|
operator: z.nativeEnum(RuleOperator),
|
||||||
@@ -23,26 +33,24 @@ export const AdminCreateShippingOptionRule = z
|
|||||||
})
|
})
|
||||||
.strict()
|
.strict()
|
||||||
|
|
||||||
|
export type AdminShippingOptionRulesBatchAddType = z.infer<
|
||||||
|
typeof AdminShippingOptionRulesBatchAdd
|
||||||
|
>
|
||||||
export const AdminShippingOptionRulesBatchAdd = z
|
export const AdminShippingOptionRulesBatchAdd = z
|
||||||
.object({
|
.object({
|
||||||
rules: AdminCreateShippingOptionRule.array(),
|
rules: AdminCreateShippingOptionRule.array(),
|
||||||
})
|
})
|
||||||
.strict()
|
.strict()
|
||||||
|
|
||||||
export type AdminShippingOptionRulesBatchAddType = z.infer<
|
export type AdminShippingOptionRulesBatchRemoveType = z.infer<
|
||||||
typeof AdminShippingOptionRulesBatchAdd
|
typeof AdminShippingOptionRulesBatchRemove
|
||||||
>
|
>
|
||||||
|
|
||||||
export const AdminShippingOptionRulesBatchRemove = z
|
export const AdminShippingOptionRulesBatchRemove = z
|
||||||
.object({
|
.object({
|
||||||
rule_ids: z.array(z.string()),
|
rule_ids: z.array(z.string()),
|
||||||
})
|
})
|
||||||
.strict()
|
.strict()
|
||||||
|
|
||||||
export type AdminShippingOptionRulesBatchRemoveType = z.infer<
|
|
||||||
typeof AdminShippingOptionRulesBatchRemove
|
|
||||||
>
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SHIPPING OPTIONS
|
* SHIPPING OPTIONS
|
||||||
*/
|
*/
|
||||||
@@ -70,7 +78,7 @@ export const AdminCreateShippingOptionPriceWithRegion = z
|
|||||||
})
|
})
|
||||||
.strict()
|
.strict()
|
||||||
|
|
||||||
export const AdminUpdateShippingOptionPriceWithCurrency =z
|
export const AdminUpdateShippingOptionPriceWithCurrency = z
|
||||||
.object({
|
.object({
|
||||||
id: z.string().optional(),
|
id: z.string().optional(),
|
||||||
currency_code: z.string().optional(),
|
currency_code: z.string().optional(),
|
||||||
@@ -78,15 +86,17 @@ export const AdminUpdateShippingOptionPriceWithCurrency =z
|
|||||||
})
|
})
|
||||||
.strict()
|
.strict()
|
||||||
|
|
||||||
export const AdminUpdateShippingOptionPriceWithRegion =
|
export const AdminUpdateShippingOptionPriceWithRegion = z
|
||||||
z
|
.object({
|
||||||
.object({
|
id: z.string().optional(),
|
||||||
id: z.string().optional(),
|
region_id: z.string().optional(),
|
||||||
region_id: z.string().optional(),
|
amount: z.number().optional(),
|
||||||
amount: z.number().optional(),
|
})
|
||||||
})
|
.strict()
|
||||||
.strict()
|
|
||||||
|
|
||||||
|
export type AdminCreateShippingOptionType = z.infer<
|
||||||
|
typeof AdminCreateShippingOption
|
||||||
|
>
|
||||||
export const AdminCreateShippingOption = z
|
export const AdminCreateShippingOption = z
|
||||||
.object({
|
.object({
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
@@ -103,10 +113,9 @@ export const AdminCreateShippingOption = z
|
|||||||
})
|
})
|
||||||
.strict()
|
.strict()
|
||||||
|
|
||||||
export type AdminCreateShippingOptionType = z.infer<
|
export type AdminUpdateShippingOptionType = z.infer<
|
||||||
typeof AdminCreateShippingOption
|
typeof AdminUpdateShippingOption
|
||||||
>
|
>
|
||||||
|
|
||||||
export const AdminUpdateShippingOption = z
|
export const AdminUpdateShippingOption = z
|
||||||
.object({
|
.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
@@ -122,7 +131,3 @@ export const AdminUpdateShippingOption = z
|
|||||||
.optional(),
|
.optional(),
|
||||||
})
|
})
|
||||||
.strict()
|
.strict()
|
||||||
|
|
||||||
export type AdminUpdateShippingOptionType = z.infer<
|
|
||||||
typeof AdminUpdateShippingOption
|
|
||||||
>
|
|
||||||
@@ -5,28 +5,22 @@ import {
|
|||||||
IFulfillmentModuleService,
|
IFulfillmentModuleService,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { deleteShippingProfileWorkflow } from "@medusajs/core-flows"
|
import { deleteShippingProfileWorkflow } from "@medusajs/core-flows"
|
||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../types/routing"
|
} from "../../../../types/routing"
|
||||||
|
import { AdminGetShippingProfileParamsType } from "../validators"
|
||||||
|
import { refetchShippingProfile } from "../helpers"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetShippingProfileParamsType>,
|
||||||
res: MedusaResponse<AdminShippingProfileResponse>
|
res: MedusaResponse<AdminShippingProfileResponse>
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const shippingProfile = await refetchShippingProfile(
|
||||||
|
req.params.id,
|
||||||
const query = remoteQueryObjectFromString({
|
req.scope,
|
||||||
entryPoint: "shipping_profiles",
|
req.remoteQueryConfig.fields
|
||||||
variables: { id: req.params.id },
|
)
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [shippingProfile] = await remoteQuery(query)
|
|
||||||
|
|
||||||
res.status(200).json({ shipping_profile: shippingProfile })
|
res.status(200).json({ shipping_profile: shippingProfile })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { MedusaContainer } from "@medusajs/types"
|
||||||
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
|
export const refetchShippingProfile = async (
|
||||||
|
shippingProfileId: string,
|
||||||
|
scope: MedusaContainer,
|
||||||
|
fields: string[]
|
||||||
|
) => {
|
||||||
|
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "shipping_profile",
|
||||||
|
variables: {
|
||||||
|
filters: { id: shippingProfileId },
|
||||||
|
},
|
||||||
|
fields: fields,
|
||||||
|
})
|
||||||
|
|
||||||
|
const shippingProfiles = await remoteQuery(queryObject)
|
||||||
|
return shippingProfiles[0]
|
||||||
|
}
|
||||||
@@ -8,8 +8,8 @@ import {
|
|||||||
} from "./query-config"
|
} from "./query-config"
|
||||||
import {
|
import {
|
||||||
AdminCreateShippingProfile,
|
AdminCreateShippingProfile,
|
||||||
AdminShippingProfileParams,
|
AdminGetShippingProfileParams,
|
||||||
AdminShippingProfilesParams,
|
AdminGetShippingProfilesParams,
|
||||||
} from "./validators"
|
} from "./validators"
|
||||||
|
|
||||||
export const adminShippingProfilesMiddlewares: MiddlewareRoute[] = [
|
export const adminShippingProfilesMiddlewares: MiddlewareRoute[] = [
|
||||||
@@ -21,11 +21,11 @@ export const adminShippingProfilesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/shipping-profiles",
|
matcher: "/admin/shipping-profiles",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminCreateShippingProfile),
|
||||||
validateAndTransformQuery(
|
validateAndTransformQuery(
|
||||||
AdminShippingProfilesParams,
|
AdminGetShippingProfilesParams,
|
||||||
retrieveTransformQueryConfig
|
retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
validateAndTransformBody(AdminCreateShippingProfile),
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -33,7 +33,7 @@ export const adminShippingProfilesMiddlewares: MiddlewareRoute[] = [
|
|||||||
matcher: "/admin/shipping-profiles",
|
matcher: "/admin/shipping-profiles",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
validateAndTransformQuery(
|
validateAndTransformQuery(
|
||||||
AdminShippingProfilesParams,
|
AdminGetShippingProfilesParams,
|
||||||
listTransformQueryConfig
|
listTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -43,7 +43,7 @@ export const adminShippingProfilesMiddlewares: MiddlewareRoute[] = [
|
|||||||
matcher: "/admin/shipping-profiles/:id",
|
matcher: "/admin/shipping-profiles/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
validateAndTransformQuery(
|
validateAndTransformQuery(
|
||||||
AdminShippingProfileParams,
|
AdminGetShippingProfileParams,
|
||||||
retrieveTransformQueryConfig
|
retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -11,7 +11,11 @@ import {
|
|||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../types/routing"
|
} from "../../../types/routing"
|
||||||
import { AdminCreateShippingProfileType } from "./validators"
|
import {
|
||||||
|
AdminCreateShippingProfileType,
|
||||||
|
AdminGetShippingProfilesParamsType,
|
||||||
|
} from "./validators"
|
||||||
|
import { refetchShippingProfile } from "./helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminCreateShippingProfileType>,
|
req: AuthenticatedMedusaRequest<AdminCreateShippingProfileType>,
|
||||||
@@ -30,23 +34,17 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const shippingProfileId = result?.[0].id
|
const shippingProfile = await refetchShippingProfile(
|
||||||
|
result?.[0].id,
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
const query = remoteQueryObjectFromString({
|
)
|
||||||
entryPoint: "shipping_profiles",
|
|
||||||
variables: { id: shippingProfileId },
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [shippingProfile] = await remoteQuery(query)
|
|
||||||
|
|
||||||
res.status(200).json({ shipping_profile: shippingProfile })
|
res.status(200).json({ shipping_profile: shippingProfile })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetShippingProfilesParamsType>,
|
||||||
res: MedusaResponse<AdminShippingProfilesResponse>
|
res: MedusaResponse<AdminShippingProfilesResponse>
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { createFindParams, createSelectParams } from "../../utils/validators"
|
import { createFindParams, createSelectParams } from "../../utils/validators"
|
||||||
|
|
||||||
export const AdminShippingProfileParams = createSelectParams()
|
export type AdminGetShippingProfileParamsType = z.infer<
|
||||||
export const AdminShippingProfilesParams = createFindParams({
|
typeof AdminGetShippingProfileParams
|
||||||
|
>
|
||||||
|
export const AdminGetShippingProfileParams = createSelectParams()
|
||||||
|
|
||||||
|
export type AdminGetShippingProfilesParamsType = z.infer<
|
||||||
|
typeof AdminGetShippingProfilesParams
|
||||||
|
>
|
||||||
|
export const AdminGetShippingProfilesParams = createFindParams({
|
||||||
limit: 20,
|
limit: 20,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}).merge(
|
}).merge(
|
||||||
@@ -12,6 +19,9 @@ export const AdminShippingProfilesParams = createFindParams({
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
export type AdminCreateShippingProfileType = z.infer<
|
||||||
|
typeof AdminCreateShippingProfile
|
||||||
|
>
|
||||||
export const AdminCreateShippingProfile = z
|
export const AdminCreateShippingProfile = z
|
||||||
.object({
|
.object({
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
@@ -19,7 +29,3 @@ export const AdminCreateShippingProfile = z
|
|||||||
metadata: z.record(z.string(), z.unknown()).optional(),
|
metadata: z.record(z.string(), z.unknown()).optional(),
|
||||||
})
|
})
|
||||||
.strict()
|
.strict()
|
||||||
|
|
||||||
export type AdminCreateShippingProfileType = z.infer<
|
|
||||||
typeof AdminCreateShippingProfile
|
|
||||||
>
|
|
||||||
@@ -1,18 +1,12 @@
|
|||||||
import { createLocationFulfillmentSetWorkflow } from "@medusajs/core-flows"
|
import { createLocationFulfillmentSetWorkflow } from "@medusajs/core-flows"
|
||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import { z } from "zod"
|
|
||||||
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
|
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
|
||||||
import { AdminCreateStockLocationFulfillmentSet } from "../../validators"
|
import { AdminCreateStockLocationFulfillmentSetType } from "../../validators"
|
||||||
|
import { refetchStockLocation } from "../../helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: MedusaRequest<z.infer<typeof AdminCreateStockLocationFulfillmentSet>>,
|
req: MedusaRequest<AdminCreateStockLocationFulfillmentSetType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
|
||||||
|
|
||||||
const { errors } = await createLocationFulfillmentSetWorkflow(req.scope).run({
|
const { errors } = await createLocationFulfillmentSetWorkflow(req.scope).run({
|
||||||
input: {
|
input: {
|
||||||
location_id: req.params.id,
|
location_id: req.params.id,
|
||||||
@@ -28,15 +22,11 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const [stock_location] = await remoteQuery(
|
const stockLocation = await refetchStockLocation(
|
||||||
remoteQueryObjectFromString({
|
req.params.id,
|
||||||
entryPoint: "stock_locations",
|
req.scope,
|
||||||
variables: {
|
req.remoteQueryConfig.fields
|
||||||
id: req.params.id,
|
|
||||||
},
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
|
|
||||||
res.status(200).json({ stock_location })
|
res.status(200).json({ stock_location: stockLocation })
|
||||||
}
|
}
|
||||||
@@ -1,19 +1,21 @@
|
|||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||||
|
|
||||||
import { deleteStockLocationsWorkflow, updateStockLocationsWorkflow } from "@medusajs/core-flows"
|
import {
|
||||||
|
deleteStockLocationsWorkflow,
|
||||||
|
updateStockLocationsWorkflow,
|
||||||
|
} from "@medusajs/core-flows"
|
||||||
import { MedusaError } from "@medusajs/utils"
|
import { MedusaError } from "@medusajs/utils"
|
||||||
import { AdminPostStockLocationsLocationReq } from "../validators"
|
import {
|
||||||
|
AdminGetStockLocationParamsType,
|
||||||
|
AdminUpdateStockLocationType,
|
||||||
|
} from "../validators"
|
||||||
|
import { refetchStockLocation } from "../helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: MedusaRequest<AdminPostStockLocationsLocationReq>,
|
req: MedusaRequest<AdminUpdateStockLocationType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
|
|
||||||
await updateStockLocationsWorkflow(req.scope).run({
|
await updateStockLocationsWorkflow(req.scope).run({
|
||||||
input: {
|
input: {
|
||||||
selector: { id: req.params.id },
|
selector: { id: req.params.id },
|
||||||
@@ -21,45 +23,37 @@ export const POST = async (
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const stockLocation = await refetchStockLocation(
|
||||||
|
id,
|
||||||
const [stock_location] = await remoteQuery(
|
req.scope,
|
||||||
remoteQueryObjectFromString({
|
req.remoteQueryConfig.fields
|
||||||
entryPoint: "stock_locations",
|
|
||||||
variables: {
|
|
||||||
id,
|
|
||||||
},
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
stock_location,
|
stock_location: stockLocation,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
export const GET = async (
|
||||||
|
req: MedusaRequest<AdminGetStockLocationParamsType>,
|
||||||
|
res: MedusaResponse
|
||||||
|
) => {
|
||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
|
||||||
|
|
||||||
const [stock_location] = await remoteQuery(
|
const stockLocation = await refetchStockLocation(
|
||||||
remoteQueryObjectFromString({
|
id,
|
||||||
entryPoint: "stock_locations",
|
req.scope,
|
||||||
variables: {
|
req.remoteQueryConfig.fields
|
||||||
id,
|
|
||||||
},
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!stock_location) {
|
if (!stockLocation) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
MedusaError.Types.NOT_FOUND,
|
MedusaError.Types.NOT_FOUND,
|
||||||
`Stock location with id: ${id} was not found`
|
`Stock location with id: ${id} was not found`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).json({ stock_location })
|
res.status(200).json({ stock_location: stockLocation })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||||
|
|||||||
+9
-16
@@ -1,17 +1,14 @@
|
|||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import {
|
import {
|
||||||
MedusaRequest,
|
MedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../../../types/routing"
|
} from "../../../../../../../types/routing"
|
||||||
|
|
||||||
import { AdminStockLocationsLocationSalesChannelBatchReq } from "../../../../validators"
|
import { AdminStockLocationsSalesChannelType } from "../../../../validators"
|
||||||
import { addLocationsToSalesChannelWorkflow } from "@medusajs/core-flows"
|
import { addLocationsToSalesChannelWorkflow } from "@medusajs/core-flows"
|
||||||
|
import { refetchStockLocation } from "../../../../helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: MedusaRequest<AdminStockLocationsLocationSalesChannelBatchReq>,
|
req: MedusaRequest<AdminStockLocationsSalesChannelType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const workflowInput = {
|
const workflowInput = {
|
||||||
@@ -30,15 +27,11 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const stockLocation = await refetchStockLocation(
|
||||||
|
req.params.id,
|
||||||
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
|
)
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
res.status(200).json({ stock_location: stockLocation })
|
||||||
entryPoint: "stock_locations",
|
|
||||||
variables: { id: req.params.id },
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [stock_location] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ stock_location })
|
|
||||||
}
|
}
|
||||||
+9
-16
@@ -1,17 +1,14 @@
|
|||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import {
|
import {
|
||||||
MedusaRequest,
|
MedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../../../types/routing"
|
} from "../../../../../../../types/routing"
|
||||||
|
|
||||||
import { AdminStockLocationsLocationSalesChannelBatchReq } from "../../../../validators"
|
import { AdminStockLocationsSalesChannelType } from "../../../../validators"
|
||||||
import { removeLocationsFromSalesChannelWorkflow } from "@medusajs/core-flows"
|
import { removeLocationsFromSalesChannelWorkflow } from "@medusajs/core-flows"
|
||||||
|
import { refetchStockLocation } from "../../../../helpers"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: MedusaRequest<AdminStockLocationsLocationSalesChannelBatchReq>,
|
req: MedusaRequest<AdminStockLocationsSalesChannelType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const workflowInput = {
|
const workflowInput = {
|
||||||
@@ -32,15 +29,11 @@ export const POST = async (
|
|||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const stockLocation = await refetchStockLocation(
|
||||||
|
req.params.id,
|
||||||
|
req.scope,
|
||||||
|
req.remoteQueryConfig.fields
|
||||||
|
)
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
res.status(200).json({ stock_location: stockLocation })
|
||||||
entryPoint: "stock_locations",
|
|
||||||
variables: { id: req.params.id },
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [stock_location] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.status(200).json({ stock_location })
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { MedusaContainer } from "@medusajs/types"
|
||||||
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
|
export const refetchStockLocation = async (
|
||||||
|
stockLocationId: string,
|
||||||
|
scope: MedusaContainer,
|
||||||
|
fields: string[]
|
||||||
|
) => {
|
||||||
|
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
const queryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "stock_location",
|
||||||
|
variables: {
|
||||||
|
filters: { id: stockLocationId },
|
||||||
|
},
|
||||||
|
fields: fields,
|
||||||
|
})
|
||||||
|
|
||||||
|
const stockLocations = await remoteQuery(queryObject)
|
||||||
|
return stockLocations[0]
|
||||||
|
}
|
||||||
@@ -1,18 +1,16 @@
|
|||||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
|
||||||
import { MiddlewareRoute } from "../../../types/middlewares"
|
import { MiddlewareRoute } from "../../../types/middlewares"
|
||||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||||
import { maybeApplyLinkFilter } from "../../utils/maybe-apply-link-filter"
|
import { maybeApplyLinkFilter } from "../../utils/maybe-apply-link-filter"
|
||||||
import { validateAndTransformBody } from "../../utils/validate-body"
|
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 {
|
||||||
|
AdminCreateStockLocation,
|
||||||
AdminCreateStockLocationFulfillmentSet,
|
AdminCreateStockLocationFulfillmentSet,
|
||||||
AdminGetStockLocationsLocationParams,
|
AdminGetStockLocationParams,
|
||||||
AdminGetStockLocationsParams,
|
AdminGetStockLocationsParams,
|
||||||
AdminPostStockLocationsLocationParams,
|
AdminStockLocationsSalesChannel,
|
||||||
AdminPostStockLocationsLocationReq,
|
AdminUpdateStockLocation,
|
||||||
AdminPostStockLocationsParams,
|
|
||||||
AdminPostStockLocationsReq,
|
|
||||||
AdminStockLocationsLocationSalesChannelBatchReq,
|
|
||||||
} from "./validators"
|
} from "./validators"
|
||||||
|
|
||||||
export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
@@ -25,9 +23,9 @@ export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/stock-locations",
|
matcher: "/admin/stock-locations",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformBody(AdminPostStockLocationsReq),
|
validateAndTransformBody(AdminCreateStockLocation),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminPostStockLocationsParams,
|
AdminGetStockLocationParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -36,7 +34,7 @@ export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/stock-locations",
|
matcher: "/admin/stock-locations",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetStockLocationsParams,
|
AdminGetStockLocationsParams,
|
||||||
QueryConfig.listTransformQueryConfig
|
QueryConfig.listTransformQueryConfig
|
||||||
),
|
),
|
||||||
@@ -51,20 +49,9 @@ export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/stock-locations/:id",
|
matcher: "/admin/stock-locations/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformBody(AdminPostStockLocationsLocationReq),
|
validateAndTransformBody(AdminUpdateStockLocation),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminPostStockLocationsLocationParams,
|
AdminGetStockLocationParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
|
||||||
),
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
method: ["POST"],
|
|
||||||
matcher: "/admin/stock-locations/:id/sales-channels/batch*",
|
|
||||||
middlewares: [
|
|
||||||
transformBody(AdminStockLocationsLocationSalesChannelBatchReq),
|
|
||||||
transformQuery(
|
|
||||||
AdminPostStockLocationsLocationParams,
|
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -73,8 +60,8 @@ export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/stock-locations/:id",
|
matcher: "/admin/stock-locations/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetStockLocationsLocationParams,
|
AdminGetStockLocationParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -84,8 +71,19 @@ export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
matcher: "/admin/stock-locations/:id/fulfillment-sets",
|
matcher: "/admin/stock-locations/:id/fulfillment-sets",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
validateAndTransformBody(AdminCreateStockLocationFulfillmentSet),
|
validateAndTransformBody(AdminCreateStockLocationFulfillmentSet),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminPostStockLocationsParams,
|
AdminGetStockLocationParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
method: ["POST"],
|
||||||
|
matcher: "/admin/stock-locations/:id/sales-channels/batch*",
|
||||||
|
middlewares: [
|
||||||
|
validateAndTransformBody(AdminStockLocationsSalesChannel),
|
||||||
|
validateAndTransformQuery(
|
||||||
|
AdminGetStockLocationParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -5,33 +5,34 @@ import {
|
|||||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||||
|
|
||||||
import { createStockLocationsWorkflow } from "@medusajs/core-flows"
|
import { createStockLocationsWorkflow } from "@medusajs/core-flows"
|
||||||
import { AdminPostStockLocationsReq } from "./validators"
|
import {
|
||||||
|
AdminCreateStockLocationType,
|
||||||
|
AdminGetStockLocationsParamsType,
|
||||||
|
} from "./validators"
|
||||||
|
import { refetchStockLocation } from "./helpers"
|
||||||
|
|
||||||
// Create stock location
|
// Create stock location
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: MedusaRequest<AdminPostStockLocationsReq>,
|
req: MedusaRequest<AdminCreateStockLocationType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
|
||||||
|
|
||||||
const { result } = await createStockLocationsWorkflow(req.scope).run({
|
const { result } = await createStockLocationsWorkflow(req.scope).run({
|
||||||
input: { locations: [req.validatedBody] },
|
input: { locations: [req.validatedBody] },
|
||||||
})
|
})
|
||||||
|
|
||||||
const [stock_location] = await remoteQuery(
|
const stockLocation = await refetchStockLocation(
|
||||||
remoteQueryObjectFromString({
|
result[0].id,
|
||||||
entryPoint: "stock_locations",
|
req.scope,
|
||||||
variables: {
|
req.remoteQueryConfig.fields
|
||||||
id: result[0].id,
|
|
||||||
},
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
|
|
||||||
res.status(200).json({ stock_location })
|
res.status(200).json({ stock_location: stockLocation })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
export const GET = async (
|
||||||
|
req: MedusaRequest<AdminGetStockLocationsParamsType>,
|
||||||
|
res: MedusaResponse
|
||||||
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
|
||||||
const { rows: stock_locations, metadata } = await remoteQuery(
|
const { rows: stock_locations, metadata } = await remoteQuery(
|
||||||
|
|||||||
@@ -1,293 +1,75 @@
|
|||||||
import { Transform, Type } from "class-transformer"
|
|
||||||
import {
|
|
||||||
IsNotEmpty,
|
|
||||||
IsObject,
|
|
||||||
IsOptional,
|
|
||||||
IsString,
|
|
||||||
ValidateNested,
|
|
||||||
} from "class-validator"
|
|
||||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
|
||||||
|
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { IsType } from "../../../utils"
|
import { createFindParams, createSelectParams } from "../../utils/validators"
|
||||||
|
|
||||||
/**
|
export type AdminGetStockLocationParamsType = z.infer<
|
||||||
* @schema AdminPostStockLocationsReqAddress
|
typeof AdminGetStockLocationParams
|
||||||
* type: object
|
>
|
||||||
* required:
|
export const AdminGetStockLocationParams = createSelectParams()
|
||||||
* - address_1
|
|
||||||
* - country_code
|
|
||||||
* properties:
|
|
||||||
* address_1:
|
|
||||||
* type: string
|
|
||||||
* description: Stock location address
|
|
||||||
* example: 35, Jhon Doe Ave
|
|
||||||
* address_2:
|
|
||||||
* type: string
|
|
||||||
* description: Stock location address' complement
|
|
||||||
* example: apartment 4432
|
|
||||||
* company:
|
|
||||||
* type: string
|
|
||||||
* description: Stock location address' company
|
|
||||||
* city:
|
|
||||||
* type: string
|
|
||||||
* description: Stock location address' city
|
|
||||||
* example: Mexico city
|
|
||||||
* country_code:
|
|
||||||
* description: "The two character ISO code for the country."
|
|
||||||
* type: string
|
|
||||||
* externalDocs:
|
|
||||||
* url: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements
|
|
||||||
* description: See a list of codes.
|
|
||||||
* phone:
|
|
||||||
* type: string
|
|
||||||
* description: Stock location address' phone number
|
|
||||||
* example: +1 555 61646
|
|
||||||
* postal_code:
|
|
||||||
* type: string
|
|
||||||
* description: Stock location address' postal code
|
|
||||||
* example: HD3-1G8
|
|
||||||
* province:
|
|
||||||
* type: string
|
|
||||||
* description: Stock location address' province
|
|
||||||
* example: Sinaloa
|
|
||||||
*/
|
|
||||||
class StockLocationCreateAddress {
|
|
||||||
@IsString()
|
|
||||||
address_1: string
|
|
||||||
|
|
||||||
@IsOptional()
|
export type AdminGetStockLocationsParamsType = z.infer<
|
||||||
@IsString()
|
typeof AdminGetStockLocationsParams
|
||||||
address_2?: string
|
>
|
||||||
|
export const AdminGetStockLocationsParams = createFindParams({
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
company?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
city?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
country_code: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
phone?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
postal_code?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
province?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @schema AdminPostStockLocationsReq
|
|
||||||
* type: object
|
|
||||||
* description: "The details of the stock location to create."
|
|
||||||
* required:
|
|
||||||
* - name
|
|
||||||
* properties:
|
|
||||||
* name:
|
|
||||||
* description: the name of the stock location
|
|
||||||
* type: string
|
|
||||||
* address_id:
|
|
||||||
* description: the ID of an existing stock location address to associate with the stock location. Only required if `address` is not provided.
|
|
||||||
* type: string
|
|
||||||
* metadata:
|
|
||||||
* type: object
|
|
||||||
* description: An optional key-value map with additional details
|
|
||||||
* example: {car: "white"}
|
|
||||||
* externalDocs:
|
|
||||||
* description: "Learn about the metadata attribute, and how to delete and update it."
|
|
||||||
* url: "https://docs.medusajs.com/development/entities/overview#metadata-attribute"
|
|
||||||
* address:
|
|
||||||
* description: A new stock location address to create and associate with the stock location. Only required if `address_id` is not provided.
|
|
||||||
* $ref: "#/components/schemas/StockLocationAddressInput"
|
|
||||||
*/
|
|
||||||
export class AdminPostStockLocationsReq {
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
@Transform(({ value }: { value: string }) => value?.trim())
|
|
||||||
name: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => StockLocationCreateAddress)
|
|
||||||
address?: StockLocationCreateAddress
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
address_id?: string
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostStockLocationsParams extends FindParams {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parameters used to filter and configure the pagination of the retrieved stock locations.
|
|
||||||
*/
|
|
||||||
export class AdminGetStockLocationsParams extends extendedFindParamsMixin({
|
|
||||||
limit: 20,
|
limit: 20,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}) {
|
}).merge(
|
||||||
/**
|
z.object({
|
||||||
* Search term to search stock location names.
|
q: z.string().optional(),
|
||||||
*/
|
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsString()
|
name: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsOptional()
|
address_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
q?: string
|
sales_channel_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
|
$and: z.lazy(() => AdminGetStockLocationsParams.array()).optional(),
|
||||||
|
$or: z.lazy(() => AdminGetStockLocationsParams.array()).optional(),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
export type AdminUpsertStockLocationAddressType = z.infer<
|
||||||
* IDs to filter stock locations by.
|
typeof AdminUpsertStockLocationAddress
|
||||||
*/
|
>
|
||||||
@IsOptional()
|
export const AdminUpsertStockLocationAddress = z.object({
|
||||||
@IsType([String, [String]])
|
address_1: z.string(),
|
||||||
id?: string | string[]
|
address_2: z.string().optional(),
|
||||||
|
company: z.string().optional(),
|
||||||
|
city: z.string().optional(),
|
||||||
|
country_code: z.string(),
|
||||||
|
phone: z.string().optional(),
|
||||||
|
postal_code: z.string().optional(),
|
||||||
|
province: z.string().optional(),
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
export type AdminCreateStockLocationType = z.infer<
|
||||||
* Names to filter stock locations by.
|
typeof AdminCreateStockLocation
|
||||||
*/
|
>
|
||||||
@IsOptional()
|
export const AdminCreateStockLocation = z.object({
|
||||||
@IsType([String, [String]])
|
name: z.preprocess((val: any) => val.trim(), z.string()),
|
||||||
name?: string | string[]
|
address: AdminUpsertStockLocationAddress.optional(),
|
||||||
|
address_id: z.string().optional(),
|
||||||
|
metadata: z.record(z.unknown()).optional(),
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
export type AdminUpdateStockLocationType = z.infer<
|
||||||
* Filter stock locations by the ID of their associated addresses.
|
typeof AdminUpdateStockLocation
|
||||||
*/
|
>
|
||||||
@IsOptional()
|
export const AdminUpdateStockLocation = z.object({
|
||||||
@IsType([String, [String]])
|
name: z
|
||||||
address_id?: string | string[]
|
.preprocess((val: any) => val.trim(), z.string().optional())
|
||||||
|
.optional(),
|
||||||
|
address: AdminUpsertStockLocationAddress.optional(),
|
||||||
|
address_id: z.string().optional(),
|
||||||
|
metadata: z.record(z.unknown()).optional(),
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
export type AdminStockLocationsSalesChannelType = z.infer<
|
||||||
* Filter stock locations by the ID of their associated sales channels.
|
typeof AdminStockLocationsSalesChannel
|
||||||
*/
|
>
|
||||||
@IsOptional()
|
export const AdminStockLocationsSalesChannel = z.object({
|
||||||
@IsType([String, [String]])
|
sales_channel_ids: z.array(z.string()),
|
||||||
sales_channel_id?: string | string[]
|
})
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 `-`.
|
|
||||||
*/
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
order?: string
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* The attributes of a stock location address to create or update.
|
|
||||||
*/
|
|
||||||
class StockLocationUpdateAddress {
|
|
||||||
/**
|
|
||||||
* First line address.
|
|
||||||
*/
|
|
||||||
@IsString()
|
|
||||||
address_1: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Second line address.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
address_2?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Company.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
company?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* City.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
city?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Country code.
|
|
||||||
*/
|
|
||||||
@IsString()
|
|
||||||
country_code: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Phone.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
phone?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Postal code.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
postal_code?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Province.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
province?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @schema AdminPostStockLocationsLocationReq
|
|
||||||
* type: object
|
|
||||||
* description: "The details to update of the stock location."
|
|
||||||
* properties:
|
|
||||||
* name:
|
|
||||||
* description: the name of the stock location
|
|
||||||
* type: string
|
|
||||||
* address_id:
|
|
||||||
* description: the stock location address ID
|
|
||||||
* type: string
|
|
||||||
* metadata:
|
|
||||||
* type: object
|
|
||||||
* description: An optional key-value map with additional details
|
|
||||||
* example: {car: "white"}
|
|
||||||
* externalDocs:
|
|
||||||
* description: "Learn about the metadata attribute, and how to delete and update it."
|
|
||||||
* url: "https://docs.medusajs.com/development/entities/overview#metadata-attribute"
|
|
||||||
* address:
|
|
||||||
* description: The data of an associated address to create or update.
|
|
||||||
* $ref: "#/components/schemas/StockLocationAddressInput"
|
|
||||||
*/
|
|
||||||
export class AdminPostStockLocationsLocationReq {
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
name?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => StockLocationUpdateAddress)
|
|
||||||
address?: StockLocationUpdateAddress
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
address_id?: string
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostStockLocationsLocationParams extends FindParams {}
|
|
||||||
|
|
||||||
export class AdminGetStockLocationsLocationParams extends FindParams {}
|
|
||||||
|
|
||||||
export class AdminStockLocationsLocationSalesChannelBatchReq {
|
|
||||||
@IsString({ each: true })
|
|
||||||
sales_channel_ids: string[]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
export type AdminCreateStockLocationFulfillmentSetType = z.infer<
|
||||||
|
typeof AdminCreateStockLocationFulfillmentSet
|
||||||
|
>
|
||||||
export const AdminCreateStockLocationFulfillmentSet = z
|
export const AdminCreateStockLocationFulfillmentSet = z
|
||||||
.object({
|
.object({
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ export function validateAndTransformQuery<TEntity extends BaseEntity>(
|
|||||||
return async (req: MedusaRequest, _: MedusaResponse, next: NextFunction) => {
|
return async (req: MedusaRequest, _: MedusaResponse, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const query = normalizeQuery(req)
|
const query = normalizeQuery(req)
|
||||||
|
|
||||||
const validated = await zodValidator(zodSchema, query, config)
|
const validated = await zodValidator(zodSchema, query, config)
|
||||||
const cnf = queryConfig.isList
|
const cnf = queryConfig.isList
|
||||||
? prepareListQuery(validated, queryConfig)
|
? prepareListQuery(validated, queryConfig)
|
||||||
|
|||||||
@@ -61,21 +61,38 @@ export const createFindParams = ({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createOperatorMap = (type?: z.ZodType) => {
|
export const createOperatorMap = (
|
||||||
|
type?: z.ZodType,
|
||||||
|
valueParser?: (val: any) => any
|
||||||
|
) => {
|
||||||
if (!type) {
|
if (!type) {
|
||||||
type = z.string()
|
type = z.string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let unionType: any = z.union([type, z.array(type)]).optional()
|
||||||
|
let arrayType: any = z.array(type).optional()
|
||||||
|
let simpleType: any = type.optional()
|
||||||
|
|
||||||
|
if (valueParser) {
|
||||||
|
unionType = z
|
||||||
|
.preprocess(valueParser, z.union([type, z.array(type)]))
|
||||||
|
.optional()
|
||||||
|
arrayType = z.preprocess(valueParser, z.array(type)).optional()
|
||||||
|
simpleType = z.preprocess(valueParser, type).optional()
|
||||||
|
}
|
||||||
|
|
||||||
return z.object({
|
return z.object({
|
||||||
$eq: z.union([type, z.array(type)]).optional(),
|
$eq: unionType,
|
||||||
$ne: z.union([type, z.array(type)]).optional(),
|
$ne: unionType,
|
||||||
$in: z.array(type).optional(),
|
$in: arrayType,
|
||||||
$nin: z.array(type).optional(),
|
$nin: arrayType,
|
||||||
$like: type.optional(),
|
$like: simpleType,
|
||||||
$re: type.optional(),
|
$ilike: simpleType,
|
||||||
$contains: type.optional(),
|
$re: simpleType,
|
||||||
$gt: type.optional(),
|
$contains: simpleType,
|
||||||
$gte: type.optional(),
|
$gt: simpleType,
|
||||||
$lt: type.optional(),
|
$gte: simpleType,
|
||||||
$lte: type.optional(),
|
$lt: simpleType,
|
||||||
|
$lte: simpleType,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user