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 schemaEventTypePublic = EventType.omit({});
// 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({ locations: true, metadata: true });
export const withValidEventType = withValidation({
schema: schemaEventTypeBodyParams,

View File

@ -4,6 +4,7 @@ import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { EventTypeResponse } from "@lib/types";
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validations/event-type";
import {
schemaQueryIdParseInt,
@ -26,6 +27,8 @@ import {
* - ApiKeyAuth: []
* tags:
* - event-types
* externalDocs:
* url: https://docs.cal.com/event-types
* responses:
* 200:
* description: OK
@ -55,6 +58,8 @@ import {
* - ApiKeyAuth: []
* tags:
* - event-types
* externalDocs:
* url: https://docs.cal.com/event-types
* responses:
* 201:
* description: OK, eventType edited successfuly
@ -76,6 +81,8 @@ import {
* - ApiKeyAuth: []
* tags:
* - event-types
* externalDocs:
* url: https://docs.cal.com/event-types
* responses:
* 201:
* description: OK, eventType removed successfuly
@ -90,44 +97,61 @@ export async function eventTypeById(req: NextApiRequest, res: NextApiResponse<Ev
const safeQuery = schemaQueryIdParseInt.safeParse(query);
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);
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 "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;
case "PATCH":
if (!safeBody.success) {
throw new Error("Invalid request body");
}
await prisma.eventType
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
.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;
case "PATCH":
if (!safeBody.success) throw new Error("Invalid request body");
await prisma.eventType
.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
})
.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;
case "DELETE":
await prisma.eventType
.delete({ where: { id: safeQuery.data.id } })
.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;
case "DELETE":
await prisma.eventType
.delete({ where: { id: safeQuery.data.id } })
.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;
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 { EventTypeResponse, EventTypesResponse } from "@lib/types";
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validations/event-type";
/**
@ -15,6 +16,8 @@ import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validatio
* - ApiKeyAuth: []
* tags:
* - event-types
* externalDocs:
* url: https://docs.cal.com/event-types
* responses:
* 200:
* description: OK
@ -28,6 +31,8 @@ import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validatio
* - ApiKeyAuth: []
* tags:
* - event-types
* externalDocs:
* url: https://docs.cal.com/event-types
* responses:
* 201:
* description: OK, event type created
@ -42,8 +47,10 @@ async function createOrlistAllEventTypes(
res: NextApiResponse<EventTypesResponse | EventTypeResponse>
) {
const { method } = req;
const userId = await getCalcomUserId(res);
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));
if (event_types) res.status(200).json({ event_types });
else
@ -56,7 +63,7 @@ async function createOrlistAllEventTypes(
const safe = schemaEventTypeBodyParams.safeParse(req.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);
if (data) res.status(201).json({ event_type, message: "EventType created successfully" });