fix: dont throw err on bad query
This commit is contained in:
@@ -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 },
|
||||
|
||||
@@ -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 },
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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" });
|
||||
|
||||
@@ -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" });
|
||||
|
||||
@@ -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" });
|
||||
|
||||
@@ -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" });
|
||||
|
||||
@@ -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" });
|
||||
|
||||
@@ -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 },
|
||||
});
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user