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
|
2022-04-29 00:49:38 +00:00
|
|
|
* /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
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the eventTypeCustomInput to get
|
2022-04-04 00:02:11 +00:00
|
|
|
* tags:
|
2022-04-29 00:49:38 +00:00
|
|
|
* - custom-inputs
|
2022-04-04 00:02:11 +00:00
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
2022-04-28 23:38:40 +00:00
|
|
|
* 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
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the eventTypeCustomInput to edit
|
2022-04-04 00:02:11 +00:00
|
|
|
* tags:
|
2022-04-29 00:49:38 +00:00
|
|
|
* - custom-inputs
|
2022-04-04 00:02:11 +00:00
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, eventTypeCustomInput edited successfuly
|
|
|
|
* 400:
|
2022-04-28 23:38:40 +00:00
|
|
|
* 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
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the eventTypeCustomInput to delete
|
2022-04-04 00:02:11 +00:00
|
|
|
* tags:
|
2022-04-29 00:49:38 +00:00
|
|
|
* - custom-inputs
|
2022-04-04 00:02:11 +00:00
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, eventTypeCustomInput removed successfuly
|
|
|
|
* 400:
|
2022-04-28 23:38:40 +00:00
|
|
|
* 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>
|
|
|
|
) {
|
2022-04-10 00:10:34 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-04-20 23:08:45 +00:00
|
|
|
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
|
|
|
|
);
|
2022-04-23 00:17:06 +00:00
|
|
|
if (!userEventTypeCustomInputIds.includes(safeQuery.data.id))
|
|
|
|
res.status(401).json({ message: "Unauthorized" });
|
|
|
|
else {
|
2022-04-20 23:08:45 +00:00
|
|
|
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
|
|
|
|
2022-04-20 23:08:45 +00:00
|
|
|
case "PATCH":
|
|
|
|
if (!safeBody.success) {
|
2022-05-17 17:33:18 +00:00
|
|
|
{
|
|
|
|
res.status(400).json({ message: "Invalid request body" });
|
|
|
|
return;
|
|
|
|
}
|
2022-04-20 23:08:45 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2022-04-20 23:08:45 +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
|
|
|
|
2022-04-20 23:08:45 +00:00
|
|
|
default:
|
|
|
|
res.status(405).json({ message: "Method not allowed" });
|
|
|
|
break;
|
|
|
|
}
|
2022-04-23 00:17:06 +00:00
|
|
|
}
|
2022-04-04 00:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(eventTypeById));
|