Scheduling infrastructure for absolutely everyone.
fix: dont throw err on bad query
Agusti Fernandez Pardo0ca974867497
diff --git a/pages/api/booking-references/[id].ts b/pages/api/booking-references/[id].tsindex 8041617bb3f..702b77c1a58 100644--- a/pages/api/booking-references/[id].ts+++ b/pages/api/booking-references/[id].ts@@ -18,7 +18,10 @@ export async function bookingReferenceById(res: NextApiResponse<BookingReferenceResponse>) {const safeQuery = schemaQueryIdParseInt.safeParse(query);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ if (!safeQuery.success) {+ res.status(400).json({ message: "Your query was invalid" });+ return;+ }// const userWithBookings = await prisma.user.findUnique({// where: { id: userId },// include: { bookings: true },diff --git a/pages/api/bookings/[id].ts b/pages/api/bookings/[id].tsindex b6a37122c12..7e17c9613a4 100644--- a/pages/api/bookings/[id].ts+++ b/pages/api/bookings/[id].ts@@ -15,7 +15,10 @@ export async function bookingById(res: NextApiResponse<BookingResponse>) {const safeQuery = schemaQueryIdParseInt.safeParse(query);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ if (!safeQuery.success) {+ res.status(400).json({ message: "Your query was invalid" });+ return;+ }const userWithBookings = await prisma.user.findUnique({where: { id: userId },include: { bookings: true },diff --git a/pages/api/custom-inputs/[id].ts b/pages/api/custom-inputs/[id].tsindex 8c20771a45e..7423a10b996 100644--- a/pages/api/custom-inputs/[id].ts+++ b/pages/api/custom-inputs/[id].ts@@ -77,7 +77,10 @@ async function eventTypeById() {const safeQuery = schemaQueryIdParseInt.safeParse(query);const safeBody = schemaEventTypeCustomInputBodyParams.safeParse(body);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ if (!safeQuery.success) {+ res.status(400).json({ message: "Your query was invalid" });+ return;+ }const data = await prisma.eventType.findMany({ where: { userId } });const userEventTypes = data.map((eventType) => eventType.id);const userEventTypeCustomInputs = await prisma.eventTypeCustomInput.findMany({diff --git a/pages/api/destination-calendars/[id].ts b/pages/api/destination-calendars/[id].tsindex 675f8c2e286..528b9e5f2d1 100644--- a/pages/api/destination-calendars/[id].ts+++ b/pages/api/destination-calendars/[id].ts@@ -19,7 +19,10 @@ export async function destionationCalendarById() {const safeQuery = schemaQueryIdParseInt.safeParse(query);const safeBody = schemaDestinationCalendarEditBodyParams.safeParse(body);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ if (!safeQuery.success) {+ res.status(400).json({ message: "Your query was invalid" });+ return;+ }const data = await prisma.destinationCalendar.findMany({ where: { userId } });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.diff --git a/pages/api/event-references/[id].ts b/pages/api/event-references/[id].tsindex 97bb53bc827..7a72fea4c77 100644--- a/pages/api/event-references/[id].ts+++ b/pages/api/event-references/[id].ts@@ -19,7 +19,10 @@ export async function dailyEventReferenceById() {const safeQuery = schemaQueryIdParseInt.safeParse(query);const safeBody = schemaDailyEventReferenceEditBodyParams.safeParse(body);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ if (!safeQuery.success) {+ res.status(400).json({ message: "Your query was invalid" });+ return;+ }const userBookings = await prisma.booking.findMany({ where: { userId } });const userBookingIds: number[] = userBookings.map((booking) => booking.id);const userBookingDailyEventReferences = await prisma.dailyEventReference.findMany({diff --git a/pages/api/event-types/[id].ts b/pages/api/event-types/[id].tsindex 224a7c9995a..6319b48b9e2 100644--- a/pages/api/event-types/[id].ts+++ b/pages/api/event-types/[id].ts@@ -15,7 +15,10 @@ export async function eventTypeById(res: NextApiResponse<EventTypeResponse>) {const safeQuery = schemaQueryIdParseInt.safeParse(query);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ if (!safeQuery.success) {+ res.status(400).json({ message: "Your query was invalid" });+ return;+ }const data = await prisma.eventType.findMany({ where: { userId } });const userEventTypes = data.map((eventType) => eventType.id);if (!userEventTypes.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" });diff --git a/pages/api/hooks/[id].ts b/pages/api/hooks/[id].tsindex cc017d8e8f7..dbdfc1a17e6 100644--- a/pages/api/hooks/[id].ts+++ b/pages/api/hooks/[id].ts@@ -12,7 +12,10 @@ export async function WebhookById(res: NextApiResponse<WebhookResponse>) {const safeQuery = schemaQueryIdAsString.safeParse(query);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ if (!safeQuery.success) {+ res.status(400).json({ message: "Your query was invalid" });+ return;+ }const data = await prisma.webhook.findMany({ where: { userId } });const userWebhooks = data.map((webhook) => webhook.id);if (!userWebhooks.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" });diff --git a/pages/api/memberships/[id].ts b/pages/api/memberships/[id].tsindex 1b9ceaada81..ea99860d147 100644--- a/pages/api/memberships/[id].ts+++ b/pages/api/memberships/[id].ts@@ -12,7 +12,10 @@ export async function membershipById(res: NextApiResponse<MembershipResponse>) {const safeQuery = schemaQueryIdAsString.safeParse(query);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ if (!safeQuery.success) {+ res.status(400).json({ message: "Your query was invalid" });+ return;+ }// This is how we set the userId and teamId in the query for managing compoundId.const [paramUserId, teamId] = safeQuery.data.id.split("_");if (parseInt(paramUserId) !== userId) res.status(401).json({ message: "Unauthorized" });diff --git a/pages/api/schedules/[id].ts b/pages/api/schedules/[id].tsindex a6e65a87a3e..305cc5c51ba 100644--- a/pages/api/schedules/[id].ts+++ b/pages/api/schedules/[id].ts@@ -16,7 +16,10 @@ export async function scheduleById() {const safeQuery = schemaQueryIdParseInt.safeParse(query);const safeBody = schemaScheduleBodyParams.safeParse(body);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ if (!safeQuery.success) {+ res.status(400).json({ message: "Your query was invalid" });+ return;+ }const userSchedules = await prisma.schedule.findMany({ where: { userId } });const userScheduleIds = userSchedules.map((schedule) => schedule.id);if (!userScheduleIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" });diff --git a/pages/api/selected-calendars/[id].ts b/pages/api/selected-calendars/[id].tsindex 5081da3d87e..88dca61806b 100644--- a/pages/api/selected-calendars/[id].ts+++ b/pages/api/selected-calendars/[id].ts@@ -16,7 +16,10 @@ export async function selectedCalendarById() {const safeQuery = schemaQueryIdAsString.safeParse(query);const safeBody = schemaSelectedCalendarBodyParams.safeParse(body);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ if (!safeQuery.success) {+ res.status(400).json({ message: "Your query was invalid" });+ return;+ }// This is how we set the userId and externalId in the query for managing compoundId.const [paramUserId, integration, externalId] = safeQuery.data.id.split("_");if (userId !== parseInt(paramUserId)) res.status(401).json({ message: "Unauthorized" });diff --git a/pages/api/teams/[id].ts b/pages/api/teams/[id].tsindex 4c8262efdc4..2cfd3717492 100644--- a/pages/api/teams/[id].ts+++ b/pages/api/teams/[id].ts@@ -77,7 +77,10 @@ export async function teamById() {const safeQuery = schemaQueryIdParseInt.safeParse(query);const safeBody = schemaTeamBodyParams.safeParse(body);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ if (!safeQuery.success) {+ res.status(400).json({ message: "Your query was invalid" });+ return;+ }const userWithMemberships = await prisma.membership.findMany({where: { userId: userId },});diff --git a/pages/api/users/[id].ts b/pages/api/users/[id].tsindex b73abaf5207..7539d513b87 100644--- a/pages/api/users/[id].ts+++ b/pages/api/users/[id].ts@@ -16,7 +16,10 @@ export async function userById() {const safeQuery = schemaQueryIdParseInt.safeParse(query);console.log(body);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ if (!safeQuery.success) {+ res.status(400).json({ message: "Your query was invalid" });+ return;+ }if (safeQuery.data.id !== userId) res.status(401).json({ message: "Unauthorized" });else {switch (method) {