feat: adds availabilitesi and attendees endpoints, some cleanup less try/catch more if/else
This commit is contained in:
1 parent
ac307f7161
commit
936572e7e1
48 files changed
+1365
-172
No files matched your search
@@ -11,6 +11,7 @@ type ResponseData = {
|
||||
export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryIdAsString.safeParse(query);
|
||||
|
||||
if (method === "DELETE" && safe.success) {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
await prisma.apiKey
|
||||
@@ -23,10 +24,8 @@ export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseD
|
||||
// This catches the error thrown by prisma.apiKey.delete() if the resource is not found.
|
||||
res.status(404).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only DELETE Method allowed" });
|
||||
}
|
||||
// Reject any other HTTP method than POST
|
||||
} else res.status(405).json({ message: "Only DELETE Method allowed" });
|
||||
}
|
||||
|
||||
export default withValidQueryIdString(apiKey);
|
||||
@@ -26,10 +26,8 @@ export async function editApiKey(req: NextApiRequest, res: NextApiResponse<Respo
|
||||
}).catch(error => {
|
||||
res.status(404).json({ message: `apiKey with ID ${safeQuery.data.id} not found and wasn't updated`, error })
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only PATCH Method allowed for updating API keys" });
|
||||
}
|
||||
// Reject any other HTTP method than POST
|
||||
} else res.status(405).json({ message: "Only PATCH Method allowed for updating API keys" });
|
||||
}
|
||||
|
||||
export default withValidQueryIdString(withValidApiKey(editApiKey));
|
||||
@@ -18,10 +18,8 @@ export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseD
|
||||
const apiKey = await prisma.apiKey.findUnique({ where: { id: safe.data.id } });
|
||||
if (!apiKey) res.status(404).json({ message: "API key was not found" });
|
||||
else res.status(200).json({ data: apiKey });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
// Reject any other HTTP method than POST
|
||||
} else res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -12,17 +12,7 @@ type ResponseData = {
|
||||
export default async function apiKeys(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { method } = req;
|
||||
if (method === "GET") {
|
||||
// try {
|
||||
const apiKeys = await prisma.apiKey.findMany({});
|
||||
res.status(200).json({ data: { ...apiKeys } });
|
||||
// Without any params this never fails. not sure how to force test unavailable prisma query
|
||||
// } catch (error) {
|
||||
// // FIXME: Add zod for validation/error handling
|
||||
// res.status(400).json({ error: error });
|
||||
// }
|
||||
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
} else res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
@@ -15,21 +15,13 @@ async function createApiKey(req: NextApiRequest, res: NextApiResponse<ResponseDa
|
||||
const { body, method } = req;
|
||||
const safe = schemaApiKey.safeParse(body);
|
||||
if (method === "POST" && safe.success) {
|
||||
const apiKey = await prisma.apiKey
|
||||
.create({
|
||||
data: {
|
||||
...safe.data, user: { connect: { id: 1 } }
|
||||
}
|
||||
})
|
||||
if (apiKey) {
|
||||
res.status(201).json({ data: apiKey });
|
||||
} else {
|
||||
res.status(404).json({message: "API Key not created"});
|
||||
}
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ error: "Only POST Method allowed" });
|
||||
}
|
||||
const apiKey = await prisma.apiKey
|
||||
.create({ data: { ...safe.data, user: { connect: { id: 1 } } } })
|
||||
|
||||
if (apiKey) res.status(201).json({ data: apiKey });
|
||||
else res.status(404).json({ message: "API Key not created" });
|
||||
|
||||
} else res.status(405).json({ error: "Only POST Method allowed" });
|
||||
}
|
||||
|
||||
export default withValidApiKey(createApiKey);
|
||||
@@ -0,0 +1,35 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
|
||||
type ResponseData = {
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function attendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
|
||||
if (method === "DELETE" && safe.success) {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
prisma.attendee
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
.then(() => {
|
||||
// We only remove the attendee type from the database if there's an existing resource.
|
||||
res.status(200).json({ message: `attendee-type with id: ${safe.data.id} deleted successfully` });
|
||||
})
|
||||
.catch((error) => {
|
||||
// This catches the error thrown by prisma.attendee.delete() if the resource is not found.
|
||||
res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only DELETE Method allowed in /attendee-types/[id]/delete endpoint" });
|
||||
}
|
||||
}
|
||||
|
||||
export default withValidQueryIdTransformParseInt(attendee);
|
||||
@@ -0,0 +1,33 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { Attendee } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaAttendee, withValidAttendee } from "@lib/validations/attendee";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Attendee;
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function editAttendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, body, method } = req;
|
||||
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
|
||||
const safeBody = await schemaAttendee.safeParse(body);
|
||||
|
||||
if (method === "PATCH" && safeQuery.success && safeBody.success) {
|
||||
await prisma.attendee.update({
|
||||
where: { id: safeQuery.data.id },
|
||||
data: safeBody.data,
|
||||
}).then(attendee => {
|
||||
res.status(200).json({ data: attendee });
|
||||
}).catch(error => {
|
||||
res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error })
|
||||
});
|
||||
// Reject any other HTTP method than POST
|
||||
} else res.status(405).json({ message: "Only PATCH Method allowed for updating attendees" });
|
||||
}
|
||||
|
||||
export default withValidQueryIdTransformParseInt(withValidAttendee(editAttendee));
|
||||
@@ -0,0 +1,27 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { Attendee } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Attendee;
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function attendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
|
||||
if (method === "GET" && safe.success) {
|
||||
const attendee = await prisma.attendee.findUnique({ where: { id: safe.data.id } });
|
||||
|
||||
if (attendee) res.status(200).json({ data: attendee });
|
||||
if (!attendee) res.status(404).json({ message: "Event type not found" });
|
||||
// Reject any other HTTP method than POST
|
||||
} else res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
|
||||
export default withValidQueryIdTransformParseInt(attendee);
|
||||
@@ -0,0 +1,19 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { Attendee } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Attendee[];
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export default async function attendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
try {
|
||||
const attendees = await prisma.attendee.findMany();
|
||||
res.status(200).json({ data: { ...attendees } });
|
||||
} catch (error) {
|
||||
// FIXME: Add zod for validation/error handling
|
||||
res.status(400).json({ error: error });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { Attendee } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaAttendee, withValidAttendee } from "@lib/validations/attendee";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Attendee;
|
||||
message?: string;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
async function createAttendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { body, method } = req;
|
||||
const safe = schemaAttendee.safeParse(body);
|
||||
|
||||
if (method === "POST" && safe.success) {
|
||||
await prisma.attendee
|
||||
.create({ data: safe.data })
|
||||
.then((attendee) => res.status(201).json({ data: attendee }))
|
||||
.catch((error) => res.status(400).json({ message: "Could not create attendee type", error: error }));
|
||||
// Reject any other HTTP method than POST
|
||||
} else res.status(405).json({ error: "Only POST Method allowed" });
|
||||
}
|
||||
|
||||
export default withValidAttendee(createAttendee);
|
||||
@@ -0,0 +1,27 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
|
||||
type ResponseData = {
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function availability(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
if (method === "DELETE" && safe.success && safe.data) {
|
||||
const availability = await prisma.availability
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
// We only remove the availability type from the database if there's an existing resource.
|
||||
if (availability) res.status(200).json({ message: `availability with id: ${safe.data.id} deleted successfully` });
|
||||
// This catches the error thrown by prisma.availability.delete() if the resource is not found.
|
||||
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`});
|
||||
// Reject any other HTTP method than POST
|
||||
} else res.status(405).json({ message: "Only DELETE Method allowed in /availabilities/[id]/delete endpoint" });
|
||||
}
|
||||
|
||||
export default withValidQueryIdTransformParseInt(availability);
|
||||
@@ -0,0 +1,33 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { Availability } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaAvailability, withValidAvailability,} from "@lib/validations/availability";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Availability;
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function editAvailability(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, body, method } = req;
|
||||
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
|
||||
const safeBody = await schemaAvailability.safeParse(body);
|
||||
|
||||
if (method === "PATCH" && safeQuery.success && safeBody.success) {
|
||||
await prisma.availability.update({
|
||||
where: { id: safeQuery.data.id },
|
||||
data: safeBody.data,
|
||||
}).then(availability => {
|
||||
res.status(200).json({ data: availability });
|
||||
}).catch(error => {
|
||||
res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error })
|
||||
});
|
||||
// Reject any other HTTP method than PATCH
|
||||
} else res.status(405).json({ message: "Only PATCH Method allowed for updating availabilities" });
|
||||
}
|
||||
|
||||
export default withValidQueryIdTransformParseInt(withValidAvailability(editAvailability));
|
||||
@@ -0,0 +1,28 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { Availability } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Availability;
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function availability(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
|
||||
if (method === "GET" && safe.success) {
|
||||
const availability = await prisma.availability.findUnique({ where: { id: safe.data.id } });
|
||||
|
||||
if (availability) res.status(200).json({ data: availability });
|
||||
if (!availability) res.status(404).json({ message: "Event type not found" });
|
||||
// Reject any other HTTP method than POST
|
||||
} else res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
|
||||
|
||||
export default withValidQueryIdTransformParseInt(availability);
|
||||
@@ -0,0 +1,19 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { Availability } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Availability[];
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export default async function availability(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
try {
|
||||
const availabilities = await prisma.availability.findMany();
|
||||
res.status(200).json({ data: { ...availabilities } });
|
||||
} catch (error) {
|
||||
// FIXME: Add zod for validation/error handling
|
||||
res.status(400).json({ error: error });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { Availability } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaAvailability, withValidAvailability } from "@lib/validations/availability";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Availability;
|
||||
message?: string;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
async function createAvailability(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { body, method } = req;
|
||||
if (method === "POST") {
|
||||
const safe = schemaAvailability.safeParse(body);
|
||||
if (safe.success && safe.data) {
|
||||
await prisma.availability
|
||||
.create({ data: safe.data })
|
||||
.then((availability) => res.status(201).json({ data: availability }))
|
||||
.catch((error) => res.status(400).json({ message: "Could not create availability type", error: error }));
|
||||
}
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ error: "Only POST Method allowed" });
|
||||
}
|
||||
}
|
||||
|
||||
export default withValidAvailability(createAvailability);
|
||||
@@ -2,7 +2,7 @@ import prisma from "@calcom/prisma";
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
|
||||
type ResponseData = {
|
||||
@@ -12,24 +12,23 @@ type ResponseData = {
|
||||
|
||||
export async function booking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "DELETE") {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
prisma.booking
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
.then(() => {
|
||||
// We only remove the booking type from the database if there's an existing resource.
|
||||
res.status(200).json({ message: `booking-type with id: ${safe.data.id} deleted successfully` });
|
||||
})
|
||||
.catch((error) => {
|
||||
// This catches the error thrown by prisma.booking.delete() if the resource is not found.
|
||||
res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only DELETE Method allowed in /booking-types/[id]/delete endpoint" });
|
||||
}
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
|
||||
if (method === "DELETE" && safe.success) {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
prisma.booking
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
.then(() => {
|
||||
// We only remove the booking type from the database if there's an existing resource.
|
||||
res.status(200).json({ message: `booking-type with id: ${safe.data.id} deleted successfully` });
|
||||
})
|
||||
.catch((error) => {
|
||||
// This catches the error thrown by prisma.booking.delete() if the resource is not found.
|
||||
res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only DELETE Method allowed in /booking-types/[id]/delete endpoint" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Booking } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaBooking, withValidBooking } from "@lib/validations/booking";
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Booking;
|
||||
@@ -14,7 +14,7 @@ type ResponseData = {
|
||||
|
||||
export async function editBooking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, body, method } = req;
|
||||
const safeQuery = await schemaQueryId.safeParse(query);
|
||||
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
|
||||
const safeBody = await schemaBooking.safeParse(body);
|
||||
|
||||
if (method === "PATCH") {
|
||||
|
||||
@@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
|
||||
import { Booking } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Booking;
|
||||
@@ -13,17 +13,16 @@ type ResponseData = {
|
||||
|
||||
export async function booking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "GET") {
|
||||
const booking = await prisma.booking.findUnique({ where: { id: safe.data.id } });
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
|
||||
if (booking) res.status(200).json({ data: booking });
|
||||
if (!booking) res.status(404).json({ message: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
if (method === "GET" && safe.success) {
|
||||
const booking = await prisma.booking.findUnique({ where: { id: safe.data.id } });
|
||||
|
||||
if (booking) res.status(200).json({ data: booking });
|
||||
if (!booking) res.status(404).json({ message: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import prisma from "@calcom/prisma";
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
|
||||
type ResponseData = {
|
||||
@@ -12,24 +12,22 @@ type ResponseData = {
|
||||
|
||||
export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "DELETE") {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
prisma.eventType
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
.then(() => {
|
||||
// We only remove the event type from the database if there's an existing resource.
|
||||
res.status(200).json({ message: `event-type with id: ${safe.data.id} deleted successfully` });
|
||||
})
|
||||
.catch((error) => {
|
||||
// This catches the error thrown by prisma.eventType.delete() if the resource is not found.
|
||||
res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only DELETE Method allowed in /event-types/[id]/delete endpoint" });
|
||||
}
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
if (method === "DELETE" && safe.success) {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
prisma.eventType
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
.then(() => {
|
||||
// We only remove the event type from the database if there's an existing resource.
|
||||
res.status(200).json({ message: `event-type with id: ${safe.data.id} deleted successfully` });
|
||||
})
|
||||
.catch((error) => {
|
||||
// This catches the error thrown by prisma.eventType.delete() if the resource is not found.
|
||||
res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only DELETE Method allowed in /event-types/[id]/delete endpoint" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { EventType } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaEventType, withValidEventType } from "@lib/validations/eventType";
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: EventType;
|
||||
@@ -14,7 +14,7 @@ type ResponseData = {
|
||||
|
||||
export async function editEventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, body, method } = req;
|
||||
const safeQuery = await schemaQueryId.safeParse(query);
|
||||
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
|
||||
const safeBody = await schemaEventType.safeParse(body);
|
||||
|
||||
if (method === "PATCH") {
|
||||
|
||||
@@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
|
||||
import { EventType } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: EventType;
|
||||
@@ -13,17 +13,16 @@ type ResponseData = {
|
||||
|
||||
export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "GET") {
|
||||
const event = await prisma.eventType.findUnique({ where: { id: safe.data.id } });
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
|
||||
if (event) res.status(200).json({ data: event });
|
||||
if (!event) res.status(404).json({ message: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
if (method === "GET" && safe.success) {
|
||||
const event = await prisma.eventType.findUnique({ where: { id: safe.data.id } });
|
||||
|
||||
if (event) res.status(200).json({ data: event });
|
||||
if (!event) res.status(404).json({ message: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import prisma from "@calcom/prisma";
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
|
||||
type ResponseData = {
|
||||
@@ -12,24 +12,22 @@ type ResponseData = {
|
||||
|
||||
export async function team(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "DELETE") {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
prisma.team
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
.then(() => {
|
||||
// We only remove the team type from the database if there's an existing resource.
|
||||
res.status(200).json({ message: `team-type with id: ${safe.data.id} deleted successfully` });
|
||||
})
|
||||
.catch((error) => {
|
||||
// This catches the error thrown by prisma.team.delete() if the resource is not found.
|
||||
res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only DELETE Method allowed in /team-types/[id]/delete endpoint" });
|
||||
}
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
if (method === "DELETE" && safe.success) {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
prisma.team
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
.then(() => {
|
||||
// We only remove the team type from the database if there's an existing resource.
|
||||
res.status(200).json({ message: `team-type with id: ${safe.data.id} deleted successfully` });
|
||||
})
|
||||
.catch((error) => {
|
||||
// This catches the error thrown by prisma.team.delete() if the resource is not found.
|
||||
res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only DELETE Method allowed in /team-types/[id]/delete endpoint" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Team } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaTeam, withValidTeam } from "@lib/validations/team";
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Team;
|
||||
@@ -14,7 +14,7 @@ type ResponseData = {
|
||||
|
||||
export async function editTeam(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, body, method } = req;
|
||||
const safeQuery = await schemaQueryId.safeParse(query);
|
||||
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
|
||||
const safeBody = await schemaTeam.safeParse(body);
|
||||
|
||||
if (method === "PATCH") {
|
||||
|
||||
@@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
|
||||
import { Team } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Team;
|
||||
@@ -13,17 +13,16 @@ type ResponseData = {
|
||||
|
||||
export async function team(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "GET") {
|
||||
const team = await prisma.team.findUnique({ where: { id: safe.data.id } });
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
|
||||
if (method === "GET" && safe.success) {
|
||||
const team = await prisma.team.findUnique({ where: { id: safe.data.id } });
|
||||
|
||||
if (team) res.status(200).json({ data: team });
|
||||
if (!team) res.status(404).json({ message: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
if (team) res.status(200).json({ data: team });
|
||||
if (!team) res.status(404).json({ message: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import prisma from "@calcom/prisma";
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
|
||||
type ResponseData = {
|
||||
@@ -12,24 +12,22 @@ type ResponseData = {
|
||||
|
||||
export async function user(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "DELETE") {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
prisma.user
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
.then(() => {
|
||||
// We only remove the user type from the database if there's an existing resource.
|
||||
res.status(200).json({ message: `user-type with id: ${safe.data.id} deleted successfully` });
|
||||
})
|
||||
.catch((error) => {
|
||||
// This catches the error thrown by prisma.user.delete() if the resource is not found.
|
||||
res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only DELETE Method allowed in /user-types/[id]/delete endpoint" });
|
||||
}
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
if (method === "DELETE" && safe.success) {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
prisma.user
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
.then(() => {
|
||||
// We only remove the user type from the database if there's an existing resource.
|
||||
res.status(200).json({ message: `user-type with id: ${safe.data.id} deleted successfully` });
|
||||
})
|
||||
.catch((error) => {
|
||||
// This catches the error thrown by prisma.user.delete() if the resource is not found.
|
||||
res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only DELETE Method allowed in /user-types/[id]/delete endpoint" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { User } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaUser, withValidUser } from "@lib/validations/user";
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: User;
|
||||
@@ -14,7 +14,7 @@ type ResponseData = {
|
||||
|
||||
export async function editUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, body, method } = req;
|
||||
const safeQuery = await schemaQueryId.safeParse(query);
|
||||
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
|
||||
const safeBody = await schemaUser.safeParse(body);
|
||||
|
||||
if (method === "PATCH") {
|
||||
|
||||
@@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
|
||||
import { User } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: User;
|
||||
@@ -13,17 +13,15 @@ type ResponseData = {
|
||||
|
||||
export async function user(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "GET") {
|
||||
const user = await prisma.user.findUnique({ where: { id: safe.data.id } });
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
if (method === "GET" && safe.success) {
|
||||
const user = await prisma.user.findUnique({ where: { id: safe.data.id } });
|
||||
|
||||
if (user) res.status(200).json({ data: user });
|
||||
if (!user) res.status(404).json({ message: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
if (user) res.status(200).json({ data: user });
|
||||
if (!user) res.status(404).json({ message: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user