cal.pub0.org/pages/api/custom-inputs/index.ts

90 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-03-30 12:17:55 +00:00
import type { NextApiRequest, NextApiResponse } from "next";
2022-03-30 12:17:55 +00:00
import prisma from "@calcom/prisma";
2022-04-01 23:55:41 +00:00
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { EventTypeCustomInputResponse, EventTypeCustomInputsResponse } from "@lib/types";
import {
schemaEventTypeCustomInputBodyParams,
schemaEventTypeCustomInputPublic,
} from "@lib/validations/event-type-custom-input";
2022-04-01 23:55:41 +00:00
/**
* @swagger
* /custom-inputs:
2022-04-01 23:55:41 +00:00
* get:
* summary: Find all eventTypeCustomInputs
2022-04-01 23:55:41 +00:00
* tags:
* - custom-inputs
2022-04-01 23:55:41 +00:00
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No eventTypeCustomInputs were found
* post:
* summary: Creates a new eventTypeCustomInput
* tags:
* - custom-inputs
* responses:
* 201:
* description: OK, eventTypeCustomInput created
* 400:
* description: Bad request. EventTypeCustomInput body is invalid.
* 401:
* description: Authorization information is missing or invalid.
2022-04-01 23:55:41 +00:00
*/
async function createOrlistAllEventTypeCustomInputs(
req: NextApiRequest,
res: NextApiResponse<EventTypeCustomInputsResponse | EventTypeCustomInputResponse>
2022-04-01 23:55:41 +00:00
) {
const { method } = req;
2022-04-22 01:36:33 +00:00
const userId = req.userId;
const data = await prisma.eventType.findMany({ where: { userId } });
const userEventTypes = data.map((eventType) => eventType.id);
2022-04-23 00:22:43 +00:00
if (method === "GET") {
const data = await prisma.eventTypeCustomInput.findMany({ where: { eventType: userEventTypes } });
const event_type_custom_inputs = data.map((eventTypeCustomInput) =>
schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput)
);
if (event_type_custom_inputs) res.status(200).json({ event_type_custom_inputs });
else
(error: Error) =>
res.status(404).json({
message: "No EventTypeCustomInputs were found",
error,
});
} else if (method === "POST") {
const safe = schemaEventTypeCustomInputBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body");
// Since we're supporting a create or connect relation on eventType, we need to treat them differently
// When using connect on event type, check if userId is the owner of the event
if (safe.data.eventType.connect && !userEventTypes.includes(safe.data.eventType.connect.id as number)) {
const data = await prisma.eventTypeCustomInput.create({ data: { ...safe.data } });
const event_type_custom_input = schemaEventTypeCustomInputPublic.parse(data);
if (event_type_custom_input)
res
.status(201)
.json({ event_type_custom_input, message: "EventTypeCustomInput created successfully" });
// When creating, no need
// FIXME: we might want to pass userId to the new created/linked eventType, though.
} else if (safe.data.eventType.create) {
const data = await prisma.eventTypeCustomInput.create({ data: { ...safe.data } });
const event_type_custom_input = schemaEventTypeCustomInputPublic.parse(data);
if (event_type_custom_input)
res
.status(201)
.json({ event_type_custom_input, message: "EventTypeCustomInput created successfully" });
} else
(error: Error) =>
res.status(400).json({
message: "Could not create new eventTypeCustomInput",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
2022-04-01 23:55:41 +00:00
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllEventTypeCustomInputs);