make event types return only userId data

pull/9078/head
Agusti Fernandez Pardo 2022-04-19 05:19:59 +02:00
parent f9b7cebe37
commit 29666493d6
3 changed files with 70 additions and 39 deletions

View File

@ -12,8 +12,8 @@ const schemaEventTypeRequiredParams = z.object({
}); });
export const schemaEventTypeBodyParams = schemaEventTypeBaseBodyParams.merge(schemaEventTypeRequiredParams); export const schemaEventTypeBodyParams = schemaEventTypeBaseBodyParams.merge(schemaEventTypeRequiredParams);
// Omitting those two (locations/ metadata) because the validations where choking at array and JSON object. add them later if needed
export const schemaEventTypePublic = EventType.omit({}); export const schemaEventTypePublic = EventType.omit({ locations: true, metadata: true });
export const withValidEventType = withValidation({ export const withValidEventType = withValidation({
schema: schemaEventTypeBodyParams, schema: schemaEventTypeBodyParams,

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 { EventTypeResponse } from "@lib/types"; import type { EventTypeResponse } from "@lib/types";
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validations/event-type"; import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validations/event-type";
import { import {
schemaQueryIdParseInt, schemaQueryIdParseInt,
@ -26,6 +27,8 @@ import {
* - ApiKeyAuth: [] * - ApiKeyAuth: []
* tags: * tags:
* - event-types * - event-types
* externalDocs:
* url: https://docs.cal.com/event-types
* responses: * responses:
* 200: * 200:
* description: OK * description: OK
@ -55,6 +58,8 @@ import {
* - ApiKeyAuth: [] * - ApiKeyAuth: []
* tags: * tags:
* - event-types * - event-types
* externalDocs:
* url: https://docs.cal.com/event-types
* responses: * responses:
* 201: * 201:
* description: OK, eventType edited successfuly * description: OK, eventType edited successfuly
@ -76,6 +81,8 @@ import {
* - ApiKeyAuth: [] * - ApiKeyAuth: []
* tags: * tags:
* - event-types * - event-types
* externalDocs:
* url: https://docs.cal.com/event-types
* responses: * responses:
* 201: * 201:
* description: OK, eventType removed successfuly * description: OK, eventType removed successfuly
@ -90,7 +97,11 @@ export async function eventTypeById(req: NextApiRequest, res: NextApiResponse<Ev
const safeQuery = schemaQueryIdParseInt.safeParse(query); const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaEventTypeBodyParams.safeParse(body); const safeBody = schemaEventTypeBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const userId = await getCalcomUserId(res);
const data = await prisma.eventType.findMany({ where: { userId } });
const userEventTypes = data.map((eventType) => eventType.id);
if (userEventTypes.includes(safeQuery.data.id)) {
switch (method) { switch (method) {
case "GET": case "GET":
await prisma.eventType await prisma.eventType
@ -98,30 +109,42 @@ export async function eventTypeById(req: NextApiRequest, res: NextApiResponse<Ev
.then((data) => schemaEventTypePublic.parse(data)) .then((data) => schemaEventTypePublic.parse(data))
.then((event_type) => res.status(200).json({ event_type })) .then((event_type) => res.status(200).json({ event_type }))
.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.eventType await prisma.eventType
.update({ .update({ where: { id: safeQuery.data.id }, data: safeBody.data })
where: { id: safeQuery.data.id },
data: safeBody.data,
})
.then((data) => schemaEventTypePublic.parse(data)) .then((data) => schemaEventTypePublic.parse(data))
.then((event_type) => res.status(200).json({ event_type })) .then((event_type) => res.status(200).json({ event_type }))
.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.eventType await prisma.eventType
.delete({ where: { id: safeQuery.data.id } }) .delete({ where: { id: safeQuery.data.id } })
.then(() => res.status(200).json({ message: `EventType with id: ${safeQuery.data.id} deleted` })) .then(() =>
res.status(200).json({
message: `EventType 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;
@ -129,6 +152,7 @@ export async function eventTypeById(req: NextApiRequest, res: NextApiResponse<Ev
res.status(405).json({ message: "Method not allowed" }); res.status(405).json({ message: "Method not allowed" });
break; break;
} }
}
} }
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(eventTypeById)); export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(eventTypeById));

View File

@ -4,6 +4,7 @@ import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware"; import { withMiddleware } from "@lib/helpers/withMiddleware";
import { EventTypeResponse, EventTypesResponse } from "@lib/types"; import { EventTypeResponse, EventTypesResponse } from "@lib/types";
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validations/event-type"; import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validations/event-type";
/** /**
@ -15,6 +16,8 @@ import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validatio
* - ApiKeyAuth: [] * - ApiKeyAuth: []
* tags: * tags:
* - event-types * - event-types
* externalDocs:
* url: https://docs.cal.com/event-types
* responses: * responses:
* 200: * 200:
* description: OK * description: OK
@ -28,6 +31,8 @@ import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validatio
* - ApiKeyAuth: [] * - ApiKeyAuth: []
* tags: * tags:
* - event-types * - event-types
* externalDocs:
* url: https://docs.cal.com/event-types
* responses: * responses:
* 201: * 201:
* description: OK, event type created * description: OK, event type created
@ -42,8 +47,10 @@ async function createOrlistAllEventTypes(
res: NextApiResponse<EventTypesResponse | EventTypeResponse> res: NextApiResponse<EventTypesResponse | EventTypeResponse>
) { ) {
const { method } = req; const { method } = req;
const userId = await getCalcomUserId(res);
if (method === "GET") { if (method === "GET") {
const data = await prisma.eventType.findMany(); const data = await prisma.eventType.findMany({ where: { userId } });
const event_types = data.map((eventType) => schemaEventTypePublic.parse(eventType)); const event_types = data.map((eventType) => schemaEventTypePublic.parse(eventType));
if (event_types) res.status(200).json({ event_types }); if (event_types) res.status(200).json({ event_types });
else else
@ -56,7 +63,7 @@ async function createOrlistAllEventTypes(
const safe = schemaEventTypeBodyParams.safeParse(req.body); const safe = schemaEventTypeBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body"); if (!safe.success) throw new Error("Invalid request body");
const data = await prisma.eventType.create({ data: safe.data }); const data = await prisma.eventType.create({ data: { ...safe.data, userId } });
const event_type = schemaEventTypePublic.parse(data); const event_type = schemaEventTypePublic.parse(data);
if (data) res.status(201).json({ event_type, message: "EventType created successfully" }); if (data) res.status(201).json({ event_type, message: "EventType created successfully" });