fix: add event-types admin endpoints
parent
3c0f8b5b6d
commit
aadde45bb7
|
@ -4,12 +4,14 @@ 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 { isAdminGuard } from "@lib/utils/isAdmin";
|
||||||
import { schemaEventTypeCreateBodyParams, schemaEventTypeReadPublic } from "@lib/validations/event-type";
|
import { schemaEventTypeCreateBodyParams, schemaEventTypeReadPublic } from "@lib/validations/event-type";
|
||||||
|
|
||||||
async function createOrlistAllEventTypes(
|
async function createOrlistAllEventTypes(
|
||||||
{ method, body, userId }: NextApiRequest,
|
{ method, body, userId }: NextApiRequest,
|
||||||
res: NextApiResponse<EventTypesResponse | EventTypeResponse>
|
res: NextApiResponse<EventTypesResponse | EventTypeResponse>
|
||||||
) {
|
) {
|
||||||
|
const isAdmin = await isAdminGuard(userId);
|
||||||
if (method === "GET") {
|
if (method === "GET") {
|
||||||
/**
|
/**
|
||||||
* @swagger
|
* @swagger
|
||||||
|
@ -29,20 +31,27 @@ async function createOrlistAllEventTypes(
|
||||||
* 404:
|
* 404:
|
||||||
* description: No event types were found
|
* description: No event types were found
|
||||||
*/
|
*/
|
||||||
const data = await prisma.user
|
if (!isAdmin) {
|
||||||
.findUnique({
|
const data = await prisma.user
|
||||||
where: { id: userId },
|
.findUnique({
|
||||||
rejectOnNotFound: true,
|
where: { id: userId },
|
||||||
select: { eventTypes: true },
|
rejectOnNotFound: true,
|
||||||
})
|
select: { eventTypes: true },
|
||||||
.catch((error) => res.status(404).json({ message: "No event types were found", error }));
|
})
|
||||||
if (data) res.status(200).json({ event_types: data.eventTypes });
|
.catch((error) => res.status(404).json({ message: "No event types were found", error }));
|
||||||
else
|
// @todo: add validations back schemaReadEventType.parse
|
||||||
(error: Error) =>
|
if (data) res.status(200).json({ event_types: data.eventTypes });
|
||||||
res.status(404).json({
|
else
|
||||||
message: "No EventTypes were found",
|
(error: Error) =>
|
||||||
error,
|
res.status(404).json({
|
||||||
});
|
message: "No EventTypes were found",
|
||||||
|
error,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const data = await prisma.eventType.findMany({});
|
||||||
|
const event_types = data.map((eventType) => schemaEventTypeReadPublic.parse(eventType));
|
||||||
|
if (event_types) res.status(200).json({ event_types });
|
||||||
|
}
|
||||||
} else if (method === "POST") {
|
} else if (method === "POST") {
|
||||||
/**
|
/**
|
||||||
* @swagger
|
* @swagger
|
||||||
|
@ -92,17 +101,16 @@ async function createOrlistAllEventTypes(
|
||||||
res.status(400).json({ message: "Invalid request body", error: safe.error });
|
res.status(400).json({ message: "Invalid request body", error: safe.error });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!isAdmin) {
|
||||||
const data = await prisma.eventType.create({ data: { ...safe.data, userId } });
|
const data = await prisma.eventType.create({ data: { ...safe.data, userId } });
|
||||||
const event_type = schemaEventTypeReadPublic.parse(data);
|
const event_type = schemaEventTypeReadPublic.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" });
|
} else {
|
||||||
else
|
// if admin don't re-set userId from input
|
||||||
(error: Error) =>
|
const data = await prisma.eventType.create({ data: { ...safe.data } });
|
||||||
res.status(400).json({
|
const event_type = schemaEventTypeReadPublic.parse(data);
|
||||||
message: "Could not create new event type",
|
if (data) res.status(201).json({ event_type, message: "EventType created successfully" });
|
||||||
error,
|
}
|
||||||
});
|
|
||||||
} else res.status(405).json({ message: `Method ${method} not allowed` });
|
} else res.status(405).json({ message: `Method ${method} not allowed` });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue