cal.pub0.org/pages/api/custom-inputs/[id].ts

152 lines
4.8 KiB
TypeScript
Raw Normal View History

2022-04-04 00:02:11 +00:00
import type { NextApiRequest, NextApiResponse } from "next";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { EventTypeCustomInputResponse } from "@lib/types";
import {
schemaEventTypeCustomInputBodyParams,
schemaEventTypeCustomInputPublic,
} from "@lib/validations/event-type-custom-input";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
/**
* @swagger
* /custom-inputs/{id}:
2022-04-04 00:02:11 +00:00
* get:
2022-04-29 15:29:57 +00:00
* summary: Find a eventTypeCustomInput
2022-04-04 00:02:11 +00:00
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: ID of the eventTypeCustomInput to get
2022-04-04 00:02:11 +00:00
* tags:
* - custom-inputs
2022-04-04 00:02:11 +00:00
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: EventType was not found
2022-04-04 00:02:11 +00:00
* patch:
* summary: Edit an existing eventTypeCustomInput
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: ID of the eventTypeCustomInput to edit
2022-04-04 00:02:11 +00:00
* tags:
* - custom-inputs
2022-04-04 00:02:11 +00:00
* responses:
* 201:
* description: OK, eventTypeCustomInput edited successfuly
* 400:
* description: Bad request. EventType body is invalid.
2022-04-04 00:02:11 +00:00
* 401:
* description: Authorization information is missing or invalid.
* delete:
* summary: Remove an existing eventTypeCustomInput
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: ID of the eventTypeCustomInput to delete
2022-04-04 00:02:11 +00:00
* tags:
* - custom-inputs
2022-04-04 00:02:11 +00:00
* responses:
* 201:
* description: OK, eventTypeCustomInput removed successfuly
* 400:
* description: Bad request. EventType id is invalid.
2022-04-04 00:02:11 +00:00
* 401:
* description: Authorization information is missing or invalid.
*/
2022-04-29 15:29:57 +00:00
async function eventTypeById(
2022-06-06 16:17:10 +00:00
{ method, query, body, userId, prisma }: NextApiRequest,
2022-04-29 15:29:57 +00:00
res: NextApiResponse<EventTypeCustomInputResponse>
) {
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaEventTypeCustomInputBodyParams.safeParse(body);
2022-05-18 12:27:30 +00:00
if (!safeQuery.success) {
res.status(400).json({ message: "Your query was invalid" });
return;
}
const data = await prisma.eventType.findMany({ where: { userId } });
const userEventTypes = data.map((eventType) => eventType.id);
const userEventTypeCustomInputs = await prisma.eventTypeCustomInput.findMany({
where: { eventType: userEventTypes },
});
const userEventTypeCustomInputIds = userEventTypeCustomInputs.map(
(eventTypeCustomInput) => eventTypeCustomInput.id
);
if (!userEventTypeCustomInputIds.includes(safeQuery.data.id))
res.status(401).json({ message: "Unauthorized" });
else {
switch (method) {
case "GET":
await prisma.eventTypeCustomInput
.findUnique({ where: { id: safeQuery.data.id } })
.then((data) => schemaEventTypeCustomInputPublic.parse(data))
.then((event_type_custom_input) => res.status(200).json({ event_type_custom_input }))
.catch((error: Error) =>
res.status(404).json({
message: `EventType with id: ${safeQuery.data.id} not found`,
error,
})
);
break;
2022-04-04 00:02:11 +00:00
case "PATCH":
if (!safeBody.success) {
2022-05-17 17:33:18 +00:00
{
res.status(400).json({ message: "Invalid request body" });
return;
}
}
await prisma.eventTypeCustomInput
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
.then((data) => schemaEventTypeCustomInputPublic.parse(data))
.then((event_type_custom_input) => res.status(200).json({ event_type_custom_input }))
.catch((error: Error) =>
res.status(404).json({
message: `EventType with id: ${safeQuery.data.id} not found`,
error,
})
);
break;
2022-04-04 00:02:11 +00:00
case "DELETE":
await prisma.eventTypeCustomInput
.delete({
where: { id: safeQuery.data.id },
})
.then(() =>
res.status(200).json({
message: `CustomInputEventType with id: ${safeQuery.data.id} deleted`,
})
)
.catch((error: Error) =>
res.status(404).json({
message: `EventType with id: ${safeQuery.data.id} not found`,
error,
})
);
break;
2022-04-04 00:02:11 +00:00
default:
res.status(405).json({ message: "Method not allowed" });
break;
}
}
2022-04-04 00:02:11 +00:00
}
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(eventTypeById));