104 lines
3.5 KiB
TypeScript
104 lines
3.5 KiB
TypeScript
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
import { AvailabilityResponse, AvailabilitiesResponse } from "@lib/types";
|
|
import {
|
|
schemaAvailabilityCreateBodyParams,
|
|
schemaAvailabilityReadPublic,
|
|
} from "@lib/validations/availability";
|
|
|
|
async function createOrlistAllAvailabilities(
|
|
{ method, body, userId }: NextApiRequest,
|
|
res: NextApiResponse<AvailabilitiesResponse | AvailabilityResponse>
|
|
) {
|
|
if (method === "GET") {
|
|
/**
|
|
* @swagger
|
|
* /availabilities:
|
|
* get:
|
|
* operationId: listAvailabilities
|
|
* summary: Find all availabilities
|
|
* tags:
|
|
* - availabilities
|
|
* externalDocs:
|
|
* url: https://docs.cal.com/availability
|
|
* responses:
|
|
* 200:
|
|
* description: OK
|
|
* 401:
|
|
* description: Authorization information is missing or invalid.
|
|
* 404:
|
|
* description: No availabilities were found
|
|
*/
|
|
const data = await prisma.availability.findMany({ where: { userId } });
|
|
const availabilities = data.map((availability) => schemaAvailabilityReadPublic.parse(availability));
|
|
if (availabilities) res.status(200).json({ availabilities });
|
|
else
|
|
(error: Error) =>
|
|
res.status(404).json({
|
|
message: "No Availabilities were found",
|
|
error,
|
|
});
|
|
} else if (method === "POST") {
|
|
/**
|
|
* @swagger
|
|
* /availabilities:
|
|
* post:
|
|
* operationId: addAvailability
|
|
* summary: Creates a new availability
|
|
* requestBody:
|
|
* description: Edit an existing availability related to one of your bookings
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* required:
|
|
* - startTime
|
|
* - endTime
|
|
* properties:
|
|
* days:
|
|
* type: array
|
|
* example: email@example.com
|
|
* startTime:
|
|
* type: string
|
|
* example: 1970-01-01T17:00:00.000Z
|
|
* endTime:
|
|
* type: string
|
|
* example: 1970-01-01T17:00:00.000Z
|
|
* tags:
|
|
* - availabilities
|
|
* externalDocs:
|
|
* url: https://docs.cal.com/availability
|
|
* responses:
|
|
* 201:
|
|
* description: OK, availability created
|
|
* 400:
|
|
* description: Bad request. Availability body is invalid.
|
|
* 401:
|
|
* description: Authorization information is missing or invalid.
|
|
*/
|
|
const safe = schemaAvailabilityCreateBodyParams.safeParse(body);
|
|
if (!safe.success) {
|
|
res.status(400).json({ message: "Your request is invalid", error: safe.error });
|
|
return;
|
|
}
|
|
// FIXME: check for eventTypeId ad scheduleId ownership if passed
|
|
|
|
const data = await prisma.availability.create({ data: { ...safe.data, userId } });
|
|
const availability = schemaAvailabilityReadPublic.parse(data);
|
|
|
|
if (availability) res.status(201).json({ availability, message: "Availability created successfully" });
|
|
else
|
|
(error: Error) =>
|
|
res.status(400).json({
|
|
message: "Could not create new availability",
|
|
error,
|
|
});
|
|
} else res.status(405).json({ message: `Method ${method} not allowed` });
|
|
}
|
|
|
|
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllAvailabilities);
|