feat hardened attendees, availabilites, booking-references, and bookings
This commit is contained in:
@@ -65,20 +65,39 @@ async function createOrlistAllAttendees(
|
||||
throw new Error("Invalid request body", safe.error);
|
||||
}
|
||||
const bookingId = safe.data.bookingId;
|
||||
delete safe.data.bookingId;
|
||||
const noBookingId = safe.data;
|
||||
const data = await prisma.attendee.create({
|
||||
data: { ...noBookingId, booking: { connect: { id: parseInt(bookingId as string) } } },
|
||||
const userId = await getCalcomUserId(res);
|
||||
const userWithBookings = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
include: { bookings: true },
|
||||
});
|
||||
const attendee = schemaAttendeePublic.parse(data);
|
||||
if (!userWithBookings) {
|
||||
throw new Error("User not found");
|
||||
}
|
||||
const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat();
|
||||
if (userBookingIds.includes(bookingId)) {
|
||||
delete safe.data.bookingId;
|
||||
const noBookingId = safe.data;
|
||||
const data = await prisma.attendee.create({
|
||||
data: {
|
||||
...noBookingId,
|
||||
booking: { connect: { id: bookingId } },
|
||||
},
|
||||
});
|
||||
const attendee = schemaAttendeePublic.parse(data);
|
||||
|
||||
if (attendee) res.status(201).json({ attendee, message: "Attendee created successfully" });
|
||||
else
|
||||
(error: Error) =>
|
||||
res.status(400).json({
|
||||
message: "Could not create new attendee",
|
||||
error,
|
||||
if (attendee) {
|
||||
res.status(201).json({
|
||||
attendee,
|
||||
message: "Attendee created successfully",
|
||||
});
|
||||
} else {
|
||||
(error: Error) =>
|
||||
res.status(400).json({
|
||||
message: "Could not create new attendee",
|
||||
error,
|
||||
});
|
||||
}
|
||||
} else res.status(401).json({ message: "Unauthorized" });
|
||||
} else res.status(405).json({ message: `Method ${method} not allowed` });
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ async function createOrlistAllAvailabilities(
|
||||
const safe = schemaAvailabilityBodyParams.safeParse(req.body);
|
||||
if (!safe.success) throw new Error("Invalid request body");
|
||||
|
||||
const data = await prisma.availability.create({ data: safe.data });
|
||||
const data = await prisma.availability.create({ data: { ...safe.data, userId } });
|
||||
const availability = schemaAvailabilityPublic.parse(data);
|
||||
|
||||
if (availability) res.status(201).json({ availability, message: "Availability created successfully" });
|
||||
|
||||
@@ -92,7 +92,6 @@ export async function bookingReferenceById(
|
||||
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
||||
const safeBody = schemaBookingReferenceBodyParams.safeParse(body);
|
||||
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
|
||||
// FIXME: Allow only userId owner of booking ref to edit it
|
||||
const userId = await getCalcomUserId(res);
|
||||
const userWithBookings = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
@@ -100,7 +99,6 @@ export async function bookingReferenceById(
|
||||
});
|
||||
if (!userWithBookings) throw new Error("User not found");
|
||||
const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat();
|
||||
console.log(userBookingIds);
|
||||
const bookingReference = await prisma.bookingReference.findUnique({ where: { id: safeQuery.data.id } });
|
||||
if (!bookingReference) throw new Error("BookingReference not found");
|
||||
if (userBookingIds.includes(bookingReference.bookingId)) {
|
||||
|
||||
+60
-38
@@ -4,6 +4,7 @@ import prisma from "@calcom/prisma";
|
||||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import type { BookingResponse } from "@lib/types";
|
||||
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
|
||||
import { schemaBookingBodyParams, schemaBookingPublic } from "@lib/validations/booking";
|
||||
import {
|
||||
schemaQueryIdParseInt,
|
||||
@@ -84,47 +85,68 @@ export async function bookingById(req: NextApiRequest, res: NextApiResponse<Book
|
||||
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
||||
const safeBody = schemaBookingBodyParams.safeParse(body);
|
||||
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
|
||||
// FIXME: Allow only userId owner of booking to edit it
|
||||
switch (method) {
|
||||
case "GET":
|
||||
await prisma.booking
|
||||
.findUnique({ where: { id: safeQuery.data.id } })
|
||||
.then((data) => schemaBookingPublic.parse(data))
|
||||
.then((booking) => res.status(200).json({ booking }))
|
||||
.catch((error: Error) =>
|
||||
res.status(404).json({ message: `Booking with id: ${safeQuery.data.id} not found`, error })
|
||||
);
|
||||
break;
|
||||
const userId = await getCalcomUserId(res);
|
||||
const userWithBookings = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
include: { bookings: true },
|
||||
});
|
||||
if (!userWithBookings) throw new Error("User not found");
|
||||
const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat();
|
||||
if (userBookingIds.includes(safeQuery.data.id)) {
|
||||
switch (method) {
|
||||
case "GET":
|
||||
await prisma.booking
|
||||
.findUnique({ where: { id: safeQuery.data.id } })
|
||||
.then((data) => schemaBookingPublic.parse(data))
|
||||
.then((booking) => res.status(200).json({ booking }))
|
||||
.catch((error: Error) =>
|
||||
res.status(404).json({
|
||||
message: `Booking with id: ${safeQuery.data.id} not found`,
|
||||
error,
|
||||
})
|
||||
);
|
||||
break;
|
||||
|
||||
case "PATCH":
|
||||
if (!safeBody.success) throw new Error("Invalid request body");
|
||||
await prisma.booking
|
||||
.update({
|
||||
where: { id: safeQuery.data.id },
|
||||
data: safeBody.data,
|
||||
})
|
||||
.then((data) => schemaBookingPublic.parse(data))
|
||||
.then((booking) => res.status(200).json({ booking }))
|
||||
.catch((error: Error) =>
|
||||
res.status(404).json({ message: `Booking with id: ${safeQuery.data.id} not found`, error })
|
||||
);
|
||||
break;
|
||||
case "PATCH":
|
||||
if (!safeBody.success) {
|
||||
throw new Error("Invalid request body");
|
||||
}
|
||||
await prisma.booking
|
||||
.update({
|
||||
where: { id: safeQuery.data.id },
|
||||
data: safeBody.data,
|
||||
})
|
||||
.then((data) => schemaBookingPublic.parse(data))
|
||||
.then((booking) => res.status(200).json({ booking }))
|
||||
.catch((error: Error) =>
|
||||
res.status(404).json({
|
||||
message: `Booking with id: ${safeQuery.data.id} not found`,
|
||||
error,
|
||||
})
|
||||
);
|
||||
break;
|
||||
|
||||
case "DELETE":
|
||||
await prisma.booking
|
||||
.delete({ where: { id: safeQuery.data.id } })
|
||||
.then(() =>
|
||||
res.status(200).json({ message: `Booking with id: ${safeQuery.data.id} deleted successfully` })
|
||||
)
|
||||
.catch((error: Error) =>
|
||||
res.status(404).json({ message: `Booking with id: ${safeQuery.data.id} not found`, error })
|
||||
);
|
||||
break;
|
||||
case "DELETE":
|
||||
await prisma.booking
|
||||
.delete({ where: { id: safeQuery.data.id } })
|
||||
.then(() =>
|
||||
res.status(200).json({
|
||||
message: `Booking with id: ${safeQuery.data.id} deleted successfully`,
|
||||
})
|
||||
)
|
||||
.catch((error: Error) =>
|
||||
res.status(404).json({
|
||||
message: `Booking 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;
|
||||
}
|
||||
} else res.status(401).json({ message: "Unauthorized" });
|
||||
}
|
||||
|
||||
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(bookingById));
|
||||
|
||||
@@ -4,6 +4,7 @@ import prisma from "@calcom/prisma";
|
||||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import { BookingResponse, BookingsResponse } from "@lib/types";
|
||||
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
|
||||
import { schemaBookingBodyParams, schemaBookingPublic, withValidBooking } from "@lib/validations/booking";
|
||||
|
||||
/**
|
||||
@@ -38,10 +39,10 @@ async function createOrlistAllBookings(
|
||||
res: NextApiResponse<BookingsResponse | BookingResponse>
|
||||
) {
|
||||
const { method } = req;
|
||||
// FIXME: List only bookings owner by userId
|
||||
const userId = await getCalcomUserId(res);
|
||||
|
||||
if (method === "GET") {
|
||||
const data = await prisma.booking.findMany();
|
||||
const data = await prisma.booking.findMany({ where: { userId } });
|
||||
const bookings = data.map((booking) => schemaBookingPublic.parse(booking));
|
||||
if (bookings) res.status(200).json({ bookings });
|
||||
else
|
||||
@@ -54,7 +55,7 @@ async function createOrlistAllBookings(
|
||||
const safe = schemaBookingBodyParams.safeParse(req.body);
|
||||
if (!safe.success) throw new Error("Invalid request body");
|
||||
|
||||
const data = await prisma.booking.create({ data: safe.data });
|
||||
const data = await prisma.booking.create({ data: { ...safe.data, userId } });
|
||||
const booking = schemaBookingPublic.parse(data);
|
||||
|
||||
if (booking) res.status(201).json({ booking, message: "Booking created successfully" });
|
||||
|
||||
Reference in New Issue
Block a user