feat: add admin endpoint support for event-types id

pull/9078/head
Agusti Fernandez Pardo 2022-06-04 01:32:05 +02:00
parent aadde45bb7
commit 514a98f9e0
1 changed files with 5 additions and 2 deletions

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 { isAdminGuard } from "@lib/utils/isAdmin";
import { schemaEventTypeEditBodyParams, schemaEventTypeReadPublic } from "@lib/validations/event-type"; import { schemaEventTypeEditBodyParams, schemaEventTypeReadPublic } from "@lib/validations/event-type";
import { import {
schemaQueryIdParseInt, schemaQueryIdParseInt,
@ -14,19 +15,21 @@ export async function eventTypeById(
{ method, query, body, userId }: NextApiRequest, { method, query, body, userId }: NextApiRequest,
res: NextApiResponse<EventTypeResponse> res: NextApiResponse<EventTypeResponse>
) { ) {
const isAdmin = await isAdminGuard(userId);
const safeQuery = schemaQueryIdParseInt.safeParse(query); const safeQuery = schemaQueryIdParseInt.safeParse(query);
if (!safeQuery.success) { if (!safeQuery.success) {
res.status(400).json({ message: "Your query was invalid" }); res.status(400).json({ message: "Your query was invalid" });
return; return;
} }
const data = await await prisma.user.findUnique({ const data = await prisma.user.findUnique({
where: { id: userId }, where: { id: userId },
rejectOnNotFound: true, rejectOnNotFound: true,
select: { eventTypes: true }, select: { eventTypes: true },
}); });
const userEventTypes = data.eventTypes.map((eventType) => eventType.id); const userEventTypes = data.eventTypes.map((eventType) => eventType.id);
if (!userEventTypes.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); if (!isAdmin || !userEventTypes.includes(safeQuery.data.id))
res.status(401).json({ message: "Unauthorized" });
else { else {
switch (method) { switch (method) {
/** /**