2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-26 23:58:22 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-26 23:58:22 +00:00
|
|
|
|
2022-04-01 20:05:10 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
2022-04-11 10:03:15 +00:00
|
|
|
import { AvailabilityResponse, AvailabilitiesResponse } from "@lib/types";
|
2022-04-26 20:48:15 +00:00
|
|
|
import {
|
|
|
|
schemaAvailabilityCreateBodyParams,
|
|
|
|
schemaAvailabilityReadPublic,
|
|
|
|
} from "@lib/validations/availability";
|
2022-03-26 23:58:22 +00:00
|
|
|
|
2022-04-11 10:03:15 +00:00
|
|
|
async function createOrlistAllAvailabilities(
|
2022-04-30 18:53:19 +00:00
|
|
|
{ method, body, userId }: NextApiRequest,
|
2022-04-11 10:03:15 +00:00
|
|
|
res: NextApiResponse<AvailabilitiesResponse | AvailabilityResponse>
|
|
|
|
) {
|
|
|
|
if (method === "GET") {
|
2022-04-29 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /availabilities:
|
|
|
|
* get:
|
|
|
|
* 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
|
|
|
|
*/
|
2022-04-11 13:26:06 +00:00
|
|
|
const data = await prisma.availability.findMany({ where: { userId } });
|
2022-04-26 20:23:33 +00:00
|
|
|
const availabilities = data.map((availability) => schemaAvailabilityReadPublic.parse(availability));
|
2022-04-11 10:03:15 +00:00
|
|
|
if (availabilities) res.status(200).json({ availabilities });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: "No Availabilities were found",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else if (method === "POST") {
|
2022-04-29 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /availabilities:
|
|
|
|
* post:
|
|
|
|
* summary: Creates a new availability
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-04-30 18:53:19 +00:00
|
|
|
const safe = schemaAvailabilityCreateBodyParams.safeParse(body);
|
2022-04-11 10:03:15 +00:00
|
|
|
if (!safe.success) throw new Error("Invalid request body");
|
|
|
|
|
2022-04-11 14:47:01 +00:00
|
|
|
const data = await prisma.availability.create({ data: { ...safe.data, userId } });
|
2022-04-26 20:23:33 +00:00
|
|
|
const availability = schemaAvailabilityReadPublic.parse(data);
|
2022-04-01 20:05:10 +00:00
|
|
|
|
2022-04-11 10:03:15 +00:00
|
|
|
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` });
|
2022-03-26 23:58:22 +00:00
|
|
|
}
|
2022-04-01 20:05:10 +00:00
|
|
|
|
2022-04-11 10:03:15 +00:00
|
|
|
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllAvailabilities);
|