Scheduling infrastructure for absolutely everyone.
feat hardened attendees, availabilites, booking-references, and bookings
Agusti Fernandez Pardo9447bd859de4
diff --git a/pages/api/attendees/index.ts b/pages/api/attendees/index.tsindex 509907bb8d5..59561c736e9 100644--- a/pages/api/attendees/index.ts+++ b/pages/api/attendees/index.ts@@ -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` });}diff --git a/pages/api/availabilities/index.ts b/pages/api/availabilities/index.tsindex 425436bb148..45349b6b3fd 100644--- a/pages/api/availabilities/index.ts+++ b/pages/api/availabilities/index.ts@@ -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" });diff --git a/pages/api/booking-references/[id].ts b/pages/api/booking-references/[id].tsindex 2fac6f79db3..c9527e3f99d 100644--- a/pages/api/booking-references/[id].ts+++ b/pages/api/booking-references/[id].ts@@ -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 itconst 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)) {diff --git a/pages/api/bookings/[id].ts b/pages/api/bookings/[id].tsindex 45f24774e62..54e21b51095 100644--- a/pages/api/bookings/[id].ts+++ b/pages/api/bookings/[id].ts@@ -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<Bookconst 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));diff --git a/pages/api/bookings/index.ts b/pages/api/bookings/index.tsindex fd5f8b19aff..2b2b55bf21e 100644--- a/pages/api/bookings/index.ts+++ b/pages/api/bookings/index.ts@@ -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" });