Feat(medusa, core-flows, types): add reservation endpoints (#7018)
* add reservation endpoints * add changeset
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { AdminPostReservationsReservationReq } from "../validators"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import { deleteReservationsWorkflow } from "@medusajs/core-flows"
|
||||
import { updateReservationsWorkflow } from "@medusajs/core-flows"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { id } = req.params
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const variables = { id }
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "reservation",
|
||||
variables,
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const [reservation] = await remoteQuery(queryObject)
|
||||
|
||||
if (!reservation) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Reservation with id: ${id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
res.status(200).json({ reservation })
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostReservationsReservationReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { id } = req.params
|
||||
const { errors } = await updateReservationsWorkflow(req.scope).run({
|
||||
input: {
|
||||
updates: [{ ...req.validatedBody, id }],
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "reservation",
|
||||
variables: {
|
||||
filters: { id: req.params.id },
|
||||
},
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const [reservation] = await remoteQuery(queryObject)
|
||||
|
||||
res.status(200).json({ reservation })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
|
||||
const { errors } = await deleteReservationsWorkflow(req.scope).run({
|
||||
input: { ids: [id] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
object: "reservation",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
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 { authenticate } from "../../../utils/authenticate-middleware"
|
||||
|
||||
export const adminReservationRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["ALL"],
|
||||
matcher: "/admin/reservations*",
|
||||
middlewares: [authenticate("admin", ["bearer", "session", "api-key"])],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/reservations",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetReservationsParams,
|
||||
QueryConfig.listTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/reservations/:id",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetReservationsReservationParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/reservations",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetReservationsReservationParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
transformBody(AdminPostReservationsReq),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/reservations/:id",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetReservationsReservationParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
transformBody(AdminPostReservationsReservationReq),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,21 @@
|
||||
export const defaultAdminReservationFields = [
|
||||
"id",
|
||||
"location_id",
|
||||
"inventory_item_id",
|
||||
"quantity",
|
||||
"line_item_id",
|
||||
"description",
|
||||
"metadata",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaults: defaultAdminReservationFields,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listTransformQueryConfig = {
|
||||
...retrieveTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { AdminPostReservationsReq } from "./validators"
|
||||
import { createReservationsWorkflow } from "@medusajs/core-flows"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "reservation",
|
||||
variables: {
|
||||
filters: req.filterableFields,
|
||||
...req.remoteQueryConfig.pagination,
|
||||
},
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const { rows: reservations, metadata } = await remoteQuery(queryObject)
|
||||
|
||||
res.json({
|
||||
reservations,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
})
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostReservationsReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const input = [
|
||||
{
|
||||
...req.validatedBody,
|
||||
},
|
||||
]
|
||||
|
||||
const { result, errors } = await createReservationsWorkflow(req.scope).run({
|
||||
input: { reservations: input },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "reservation",
|
||||
variables: {
|
||||
filters: { id: result[0].id },
|
||||
},
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const [reservation] = await remoteQuery(queryObject)
|
||||
|
||||
res.status(200).json({ reservation })
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
||||
import {
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
|
||||
import { IsType } from "../../../utils"
|
||||
import { OperatorMap } from "@medusajs/types"
|
||||
import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
||||
import { Type } from "class-transformer"
|
||||
|
||||
// TODO: naming
|
||||
export class AdminGetReservationsReservationParams extends FindParams {}
|
||||
|
||||
/**
|
||||
* Parameters used to filter and configure the pagination of the retrieved reservations.
|
||||
*/
|
||||
export class AdminGetReservationsParams extends extendedFindParamsMixin({
|
||||
limit: 20,
|
||||
offset: 0,
|
||||
}) {
|
||||
/**
|
||||
* Location IDs to filter reservations by.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsType([String, [String]])
|
||||
location_id?: string | string[]
|
||||
|
||||
/**
|
||||
* Inventory item IDs to filter reservations by.
|
||||
*/
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
inventory_item_id?: string[]
|
||||
|
||||
/**
|
||||
* Line item IDs to filter reservations by.
|
||||
*/
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
@IsOptional()
|
||||
line_item_id?: string[]
|
||||
|
||||
/**
|
||||
* "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>
|
||||
}
|
||||
@@ -13,10 +13,11 @@ import { adminPaymentRoutesMiddlewares } from "./admin/payments/middlewares"
|
||||
import { adminPriceListsRoutesMiddlewares } from "./admin/price-lists/middlewares"
|
||||
import { adminPricingRoutesMiddlewares } from "./admin/pricing/middlewares"
|
||||
import { adminProductCategoryRoutesMiddlewares } from "./admin/product-categories/middlewares"
|
||||
import { adminProductTypeRoutesMiddlewares } from "./admin/product-types/middlewares"
|
||||
import { adminProductRoutesMiddlewares } from "./admin/products/middlewares"
|
||||
import { adminProductTypeRoutesMiddlewares } from "./admin/product-types/middlewares"
|
||||
import { adminPromotionRoutesMiddlewares } from "./admin/promotions/middlewares"
|
||||
import { adminRegionRoutesMiddlewares } from "./admin/regions/middlewares"
|
||||
import { adminReservationRoutesMiddlewares } from "./admin/reservations/middlewares"
|
||||
import { adminSalesChannelRoutesMiddlewares } from "./admin/sales-channels/middlewares"
|
||||
import { adminShippingOptionRoutesMiddlewares } from "./admin/shipping-options/middlewares"
|
||||
import { adminShippingProfilesMiddlewares } from "./admin/shipping-profiles/middlewares"
|
||||
@@ -70,6 +71,7 @@ export const config: MiddlewaresConfig = {
|
||||
...adminUploadRoutesMiddlewares,
|
||||
...adminFulfillmentSetsRoutesMiddlewares,
|
||||
...adminProductCategoryRoutesMiddlewares,
|
||||
...adminReservationRoutesMiddlewares,
|
||||
...adminShippingProfilesMiddlewares,
|
||||
],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user