diff --git a/pages/api/availabilities/[id].ts b/pages/api/availabilities/[id].ts index 546fbe0ce1..7d5bfb8b91 100644 --- a/pages/api/availabilities/[id].ts +++ b/pages/api/availabilities/[id].ts @@ -99,7 +99,8 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse const userId = req.userId; const data = await prisma.availability.findMany({ where: { userId } }); const availabiltiesIds = data.map((availability) => availability.id); - if (availabiltiesIds.includes(safeQuery.data.id)) { + if (!availabiltiesIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); + else { switch (method) { case "GET": await prisma.availability @@ -142,8 +143,6 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse res.status(405).json({ message: "Method not allowed" }); break; } - } else { - res.status(401).json({ message: "Unauthorized" }); } } diff --git a/pages/api/booking-references/index.ts b/pages/api/booking-references/index.ts index 03bc700596..74ae942dd2 100644 --- a/pages/api/booking-references/index.ts +++ b/pages/api/booking-references/index.ts @@ -80,7 +80,8 @@ async function createOrlistAllBookingReferences( throw new Error("User not found"); } const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat(); - if (userBookingIds.includes(safe.data.bookingId)) { + if (!userBookingIds.includes(safe.data.bookingId)) res.status(401).json({ message: "Unauthorized" }); + else { const booking_reference = await prisma.bookingReference.create({ data: { ...safe.data }, }); @@ -96,7 +97,7 @@ async function createOrlistAllBookingReferences( error, }); } - } else res.status(401).json({ message: "Unauthorized" }); + } } else res.status(405).json({ message: `Method ${method} not allowed` }); } diff --git a/pages/api/bookings/[id].ts b/pages/api/bookings/[id].ts index f40ae3ccbd..9172b7dc90 100644 --- a/pages/api/bookings/[id].ts +++ b/pages/api/bookings/[id].ts @@ -97,7 +97,8 @@ export async function bookingById(req: NextApiRequest, res: NextApiResponse booking.id).flat(); - if (userBookingIds.includes(safeQuery.data.id)) { + if (!userBookingIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); + else { switch (method) { case "GET": await prisma.booking @@ -151,7 +152,7 @@ export async function bookingById(req: NextApiRequest, res: NextApiResponse dailyEventReference.id ); - if (userBookingDailyEventReferenceIds.includes(safeQuery.data.id)) { + if (!userBookingDailyEventReferenceIds.includes(safeQuery.data.id)) + res.status(401).json({ message: "Unauthorized" }); + else { switch (method) { case "GET": await prisma.dailyEventReference @@ -158,7 +160,7 @@ export async function dailyEventReferenceById( res.status(405).json({ message: "Method not allowed" }); break; } - } else res.status(401).json({ message: "Unauthorized" }); + } } export default withMiddleware("HTTP_GET_DELETE_PATCH")( diff --git a/pages/api/destination-calendars/[id].ts b/pages/api/destination-calendars/[id].ts index 579ba83482..2f643d4120 100644 --- a/pages/api/destination-calendars/[id].ts +++ b/pages/api/destination-calendars/[id].ts @@ -101,7 +101,8 @@ export async function destionationCalendarById( const userDestinationCalendars = data.map((destinationCalendar) => destinationCalendar.id); // FIXME: Should we also check ownership of bokingId and eventTypeId to avoid users cross-pollinating other users calendars. // On a related note, moving from sequential integer IDs to UUIDs would be a good idea. and maybe help avoid having this problem. - if (userDestinationCalendars.includes(safeQuery.data.id)) { + if (userDestinationCalendars.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); + else { switch (method) { case "GET": await prisma.destinationCalendar @@ -154,7 +155,7 @@ export async function destionationCalendarById( res.status(405).json({ message: "Method not allowed" }); break; } - } else res.status(401).json({ message: "Unauthorized" }); + } } export default withMiddleware("HTTP_GET_DELETE_PATCH")( diff --git a/pages/api/event-type-custom-inputs/[id].ts b/pages/api/event-type-custom-inputs/[id].ts index 5ff11ff884..4df7114cef 100644 --- a/pages/api/event-type-custom-inputs/[id].ts +++ b/pages/api/event-type-custom-inputs/[id].ts @@ -102,7 +102,9 @@ async function eventTypeById(req: NextApiRequest, res: NextApiResponse eventTypeCustomInput.id ); - if (userEventTypeCustomInputIds.includes(safeQuery.data.id)) { + if (!userEventTypeCustomInputIds.includes(safeQuery.data.id)) + res.status(401).json({ message: "Unauthorized" }); + else { switch (method) { case "GET": await prisma.eventTypeCustomInput @@ -155,7 +157,7 @@ async function eventTypeById(req: NextApiRequest, res: NextApiResponse eventType.id); - if (userEventTypes.includes(safeQuery.data.id)) { + if (!userEventTypes.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); + else { switch (method) { case "GET": await prisma.eventType diff --git a/pages/api/memberships/[id].ts b/pages/api/memberships/[id].ts index 4c90122f70..acb8f03c0c 100644 --- a/pages/api/memberships/[id].ts +++ b/pages/api/memberships/[id].ts @@ -108,7 +108,8 @@ export async function membershipById(req: NextApiRequest, res: NextApiResponse schemaPaymentPublic.parse(data)) .then((payment) => { - if (userWithBookings?.bookings.map((b) => b.id).includes(payment.bookingId)) { - res.status(200).json({ payment }); - } else { + if (!userWithBookings?.bookings.map((b) => b.id).includes(payment.bookingId)) { res.status(401).json({ message: "Unauthorized" }); + } else { + res.status(200).json({ payment }); } }) .catch((error: Error) => diff --git a/pages/api/schedules/[id].ts b/pages/api/schedules/[id].ts index e0b83f106b..a8fa501412 100644 --- a/pages/api/schedules/[id].ts +++ b/pages/api/schedules/[id].ts @@ -93,7 +93,8 @@ export async function scheduleById(req: NextApiRequest, res: NextApiResponse schedule.id); - if (userScheduleIds.includes(safeQuery.data.id)) { + if (!userScheduleIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); + else { switch (method) { case "GET": await prisma.schedule @@ -144,7 +145,7 @@ export async function scheduleById(req: NextApiRequest, res: NextApiResponse membership.teamId); - if (userTeamIds.includes(safeQuery.data.id)) { + if (!userTeamIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); + else { switch (method) { case "GET": await prisma.team @@ -148,7 +149,7 @@ export async function teamById(req: NextApiRequest, res: NextApiResponse