feat: event type custom input endpoitns ready

pull/9078/head
Agusti Fernandez Pardo 2022-04-21 01:08:45 +02:00
parent 18c1a2f026
commit d8bfff9525
3 changed files with 93 additions and 51 deletions

View File

@ -134,7 +134,6 @@ tests/endpoint/resource.new.test.ts - Create new resource
| event-types | ✅ | ✅ | ✅ | ✅ | ✅ | | event-types | ✅ | ✅ | ✅ | ✅ | ✅ |
| memberships | ✅ | ✅ | ✅ | ✅ | ✅ | | memberships | ✅ | ✅ | ✅ | ✅ | ✅ |
| payments | ✅ | ✅ | ❌ | ❌ | ❌ | | payments | ✅ | ✅ | ❌ | ❌ | ❌ |
| reminder-mails | ✅ | ✅ | ✅ | ✅ | ✅ |
| schedules | ✅ | ✅ | ✅ | ✅ | ✅ | | schedules | ✅ | ✅ | ✅ | ✅ | ✅ |
| selected-calendars | ✅ | ✅ | ✅ | ✅ | ✅ | | selected-calendars | ✅ | ✅ | ✅ | ✅ | ✅ |
| teams | ✅ | ✅ | ✅ | ✅ | ✅ | | teams | ✅ | ✅ | ✅ | ✅ | ✅ |
@ -144,9 +143,7 @@ tests/endpoint/resource.new.test.ts - Create new resource
- daily-event-references - daily-event-references
- destination-calendars - destination-calendars
- event-types-custom-input
- memberships
- reminder-mails
## Models from database that are not exposed ## Models from database that are not exposed
@ -157,7 +154,8 @@ mostly because they're deemed too sensitive can be revisited if needed. most are
- [ ] Webhooks - [ ] Webhooks
- [ ] ResetPasswordRequest - [ ] ResetPasswordRequest
- [ ] VerificationToken - [ ] VerificationToken
- [ ] ReminderMail
- [ ]
## Documentation (OpenAPI) ## Documentation (OpenAPI)
You will see that each endpoint has a comment at the top with the annotation `@swagger` with the documentation of the endpoint, **please update it if you change the code!** This is what auto-generates the OpenAPI spec by collecting the YAML in each endpoint and parsing it in /docs alongside the json-schema (auto-generated from prisma package, not added to code but manually for now, need to fix later) You will see that each endpoint has a comment at the top with the annotation `@swagger` with the documentation of the endpoint, **please update it if you change the code!** This is what auto-generates the OpenAPI spec by collecting the YAML in each endpoint and parsing it in /docs alongside the json-schema (auto-generated from prisma package, not added to code but manually for now, need to fix later)

View File

@ -4,6 +4,7 @@ import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware"; import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { EventTypeCustomInputResponse } from "@lib/types"; import type { EventTypeCustomInputResponse } from "@lib/types";
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
import { import {
schemaEventTypeCustomInputBodyParams, schemaEventTypeCustomInputBodyParams,
schemaEventTypeCustomInputPublic, schemaEventTypeCustomInputPublic,
@ -93,47 +94,69 @@ async function eventTypeById(req: NextApiRequest, res: NextApiResponse<EventType
const safeQuery = schemaQueryIdParseInt.safeParse(query); const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaEventTypeCustomInputBodyParams.safeParse(body); const safeBody = schemaEventTypeCustomInputBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const userId = getCalcomUserId(res);
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)) {
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;
switch (method) { case "PATCH":
case "GET": if (!safeBody.success) {
await prisma.eventTypeCustomInput throw new Error("Invalid request body");
.findUnique({ where: { id: safeQuery.data.id } }) }
.then((data) => schemaEventTypeCustomInputPublic.parse(data)) await prisma.eventTypeCustomInput
.then((event_type_custom_input) => res.status(200).json({ event_type_custom_input })) .update({ where: { id: safeQuery.data.id }, data: safeBody.data })
.catch((error: Error) => .then((data) => schemaEventTypeCustomInputPublic.parse(data))
res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) .then((event_type_custom_input) => res.status(200).json({ event_type_custom_input }))
); .catch((error: Error) =>
break; res.status(404).json({
message: `EventType with id: ${safeQuery.data.id} not found`,
error,
})
);
break;
case "PATCH": case "DELETE":
if (!safeBody.success) throw new Error("Invalid request body"); await prisma.eventTypeCustomInput
await prisma.eventTypeCustomInput .delete({
.update({ where: { id: safeQuery.data.id },
where: { id: safeQuery.data.id }, })
data: safeBody.data, .then(() =>
}) res.status(200).json({
.then((data) => schemaEventTypeCustomInputPublic.parse(data)) message: `CustomInputEventType with id: ${safeQuery.data.id} deleted`,
.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 }) .catch((error: Error) =>
); res.status(404).json({
break; message: `EventType with id: ${safeQuery.data.id} not found`,
error,
})
);
break;
case "DELETE": default:
await prisma.eventTypeCustomInput res.status(405).json({ message: "Method not allowed" });
.delete({ where: { id: safeQuery.data.id } }) break;
.then(() => }
res.status(200).json({ message: `CustomInputEventType with id: ${safeQuery.data.id} deleted` }) } else res.status(401).json({ message: "Unauthorized" });
)
.catch((error: Error) =>
res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error })
);
break;
default:
res.status(405).json({ message: "Method not allowed" });
break;
}
} }
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(eventTypeById)); export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(eventTypeById));

View File

@ -45,8 +45,17 @@ async function createOrlistAllEventTypeCustomInputs(
res: NextApiResponse<EventTypeCustomInputsResponse | EventTypeCustomInputResponse> res: NextApiResponse<EventTypeCustomInputsResponse | EventTypeCustomInputResponse>
) { ) {
const { method } = req; const { method } = req;
const userId = getCalcomUserId(res);
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 (method === "GET") { if (method === "GET") {
const data = await prisma.eventTypeCustomInput.findMany(); const data = await prisma.eventTypeCustomInput.findMany({ where: { eventType: userEventTypes } });
const event_type_custom_inputs = data.map((eventTypeCustomInput) => const event_type_custom_inputs = data.map((eventTypeCustomInput) =>
schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput) schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput)
); );
@ -60,13 +69,25 @@ async function createOrlistAllEventTypeCustomInputs(
} else if (method === "POST") { } else if (method === "POST") {
const safe = schemaEventTypeCustomInputBodyParams.safeParse(req.body); const safe = schemaEventTypeCustomInputBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request 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
const data = await prisma.eventTypeCustomInput.create({ data: safe.data }); // When using connect on event type, check if userId is the owner of the event
const event_type_custom_input = schemaEventTypeCustomInputPublic.parse(data); if (safe.data.eventType.connect && !userEventTypes.includes(safe.data.eventType.connect.id as number)) {
const data = await prisma.eventTypeCustomInput.create({ data: { ...safe.data } });
if (event_type_custom_input) const event_type_custom_input = schemaEventTypeCustomInputPublic.parse(data);
res.status(201).json({ event_type_custom_input, message: "EventTypeCustomInput created successfully" }); if (event_type_custom_input)
else 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) => (error: Error) =>
res.status(400).json({ res.status(400).json({
message: "Could not create new eventTypeCustomInput", message: "Could not create new eventTypeCustomInput",