feat: event type custom input endpoitns ready
parent
18c1a2f026
commit
d8bfff9525
|
@ -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)
|
||||||
|
|
|
@ -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,7 +94,16 @@ 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) {
|
switch (method) {
|
||||||
case "GET":
|
case "GET":
|
||||||
await prisma.eventTypeCustomInput
|
await prisma.eventTypeCustomInput
|
||||||
|
@ -101,32 +111,44 @@ async function eventTypeById(req: NextApiRequest, res: NextApiResponse<EventType
|
||||||
.then((data) => schemaEventTypeCustomInputPublic.parse(data))
|
.then((data) => schemaEventTypeCustomInputPublic.parse(data))
|
||||||
.then((event_type_custom_input) => res.status(200).json({ event_type_custom_input }))
|
.then((event_type_custom_input) => res.status(200).json({ event_type_custom_input }))
|
||||||
.catch((error: Error) =>
|
.catch((error: Error) =>
|
||||||
res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error })
|
res.status(404).json({
|
||||||
|
message: `EventType with id: ${safeQuery.data.id} not found`,
|
||||||
|
error,
|
||||||
|
})
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "PATCH":
|
case "PATCH":
|
||||||
if (!safeBody.success) throw new Error("Invalid request body");
|
if (!safeBody.success) {
|
||||||
|
throw new Error("Invalid request body");
|
||||||
|
}
|
||||||
await prisma.eventTypeCustomInput
|
await prisma.eventTypeCustomInput
|
||||||
.update({
|
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
|
||||||
where: { id: safeQuery.data.id },
|
|
||||||
data: safeBody.data,
|
|
||||||
})
|
|
||||||
.then((data) => schemaEventTypeCustomInputPublic.parse(data))
|
.then((data) => schemaEventTypeCustomInputPublic.parse(data))
|
||||||
.then((event_type_custom_input) => res.status(200).json({ event_type_custom_input }))
|
.then((event_type_custom_input) => res.status(200).json({ event_type_custom_input }))
|
||||||
.catch((error: Error) =>
|
.catch((error: Error) =>
|
||||||
res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error })
|
res.status(404).json({
|
||||||
|
message: `EventType with id: ${safeQuery.data.id} not found`,
|
||||||
|
error,
|
||||||
|
})
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
await prisma.eventTypeCustomInput
|
await prisma.eventTypeCustomInput
|
||||||
.delete({ where: { id: safeQuery.data.id } })
|
.delete({
|
||||||
|
where: { id: safeQuery.data.id },
|
||||||
|
})
|
||||||
.then(() =>
|
.then(() =>
|
||||||
res.status(200).json({ message: `CustomInputEventType with id: ${safeQuery.data.id} deleted` })
|
res.status(200).json({
|
||||||
|
message: `CustomInputEventType with id: ${safeQuery.data.id} deleted`,
|
||||||
|
})
|
||||||
)
|
)
|
||||||
.catch((error: Error) =>
|
.catch((error: Error) =>
|
||||||
res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error })
|
res.status(404).json({
|
||||||
|
message: `EventType with id: ${safeQuery.data.id} not found`,
|
||||||
|
error,
|
||||||
|
})
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -134,6 +156,7 @@ async function eventTypeById(req: NextApiRequest, res: NextApiResponse<EventType
|
||||||
res.status(405).json({ message: "Method not allowed" });
|
res.status(405).json({ message: "Method not allowed" });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else res.status(401).json({ message: "Unauthorized" });
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(eventTypeById));
|
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(eventTypeById));
|
||||||
|
|
|
@ -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
|
||||||
|
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);
|
const event_type_custom_input = schemaEventTypeCustomInputPublic.parse(data);
|
||||||
|
|
||||||
if (event_type_custom_input)
|
if (event_type_custom_input)
|
||||||
res.status(201).json({ event_type_custom_input, message: "EventTypeCustomInput created successfully" });
|
res
|
||||||
else
|
.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",
|
||||||
|
|
Loading…
Reference in New Issue