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,44 +97,61 @@ 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) {
case "GET":
await prisma.eventType
.findUnique({ where: { id: safeQuery.data.id } })
.then((data) => schemaEventTypePublic.parse(data))
.then((event_type) => res.status(200).json({ event_type }))
.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.eventType throw new Error("Invalid request body");
.findUnique({ where: { id: safeQuery.data.id } }) }
.then((data) => schemaEventTypePublic.parse(data)) await prisma.eventType
.then((event_type) => res.status(200).json({ event_type })) .update({ where: { id: safeQuery.data.id }, data: safeBody.data })
.catch((error: Error) => .then((data) => schemaEventTypePublic.parse(data))
res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) .then((event_type) => res.status(200).json({ event_type }))
); .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.eventType
await prisma.eventType .delete({ where: { id: safeQuery.data.id } })
.update({ .then(() =>
where: { id: safeQuery.data.id }, res.status(200).json({
data: safeBody.data, message: `EventType with id: ${safeQuery.data.id} deleted`,
}) })
.then((data) => schemaEventTypePublic.parse(data)) )
.then((event_type) => res.status(200).json({ event_type })) .catch((error: Error) =>
.catch((error: Error) => res.status(404).json({
res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) message: `EventType with id: ${safeQuery.data.id} not found`,
); error,
break; })
);
break;
case "DELETE": default:
await prisma.eventType res.status(405).json({ message: "Method not allowed" });
.delete({ where: { id: safeQuery.data.id } }) break;
.then(() => res.status(200).json({ message: `EventType with id: ${safeQuery.data.id} deleted` })) }
.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;
} }
} }

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" });