fix: update rejectOnNotFound -> find(Fist|Unique)OrThrow (#3829)

* fix: update rejectOnNotFound -> find(Fist|Unique)OrThrow

* fix: add back custom errohandling

* feat: handle notfounderror in getServerErrorFromUnknown

Co-authored-by: Agusti Fernandez Pardo <git@agusti.me>
Co-authored-by: Omar López <zomars@me.com>
This commit is contained in:
authored and GitHub committed 2022-08-31 13:44:47 -06:00
1 parent eeabb53c82
commit 58c4c894fd
26 files changed
+37 -67

No files matched your search

+3 -6
View File
@@ -112,8 +112,7 @@ function isAvailable(busyTimes: BufferedBusyTimes, time: dayjs.ConfigType, lengt
}
const getEventTypesFromDB = async (eventTypeId: number) => {
const eventType = await prisma.eventType.findUnique({
rejectOnNotFound: true,
const eventType = await prisma.eventType.findUniqueOrThrow({
where: {
id: eventTypeId,
},
@@ -662,10 +661,8 @@ async function handler(req: NextApiRequest) {
if (typeof eventType.price === "number" && eventType.price > 0) {
/* Validate if there is any stripe_payment credential for this user */
await prisma.credential.findFirst({
rejectOnNotFound(err) {
throw new HttpError({ statusCode: 400, message: "Missing stripe credentials", cause: err });
},
/* note: removes custom error message about stripe */
await prisma.credential.findFirstOrThrow({
where: {
type: "stripe_payment",
userId: organizerUser.id,
@@ -40,8 +40,7 @@ const rescheduleSchema = z.object({
});
const findUserDataByUserId = async (userId: number) => {
return await prisma.user.findUnique({
rejectOnNotFound: true,
return await prisma.user.findUniqueOrThrow({
where: {
id: userId,
},
@@ -76,7 +75,7 @@ const handler = async (
return res.status(501).end();
}
const bookingToReschedule = await prisma.booking.findFirst({
const bookingToReschedule = await prisma.booking.findFirstOrThrow({
select: {
id: true,
uid: true,
@@ -95,7 +94,6 @@ const handler = async (
dynamicGroupSlugRef: true,
destinationCalendar: true,
},
rejectOnNotFound: true,
where: {
uid: bookingId,
NOT: {
@@ -127,14 +125,13 @@ const handler = async (
if (bookingToReschedule && user) {
let event: Partial<EventType> = {};
if (bookingToReschedule.eventTypeId) {
event = await prisma.eventType.findFirst({
event = await prisma.eventType.findFirstOrThrow({
select: {
title: true,
users: true,
schedulingType: true,
recurringEvent: true,
},
rejectOnNotFound: true,
where: {
id: bookingToReschedule.eventTypeId,
},
+1 -2
View File
@@ -107,7 +107,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
throw new HttpError({ statusCode: 404, message: "User not found" });
}
const organizer = await prisma.user.findFirst({
const organizer = await prisma.user.findFirstOrThrow({
where: {
id: bookingToDelete.userId,
},
@@ -117,7 +117,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
timeZone: true,
locale: true,
},
rejectOnNotFound: true,
});
const attendeesListPromises = bookingToDelete.attendees.map(async (attendee) => {
+1 -2
View File
@@ -15,8 +15,7 @@ type CalendlyEventType = {
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const session = await getSession({ req });
const authenticatedUser = await prisma.user.findFirst({
rejectOnNotFound: true,
const authenticatedUser = await prisma.user.findFirstOrThrow({
where: {
id: session?.user.id,
},
+1 -2
View File
@@ -15,8 +15,7 @@ type SavvyCalEventType = {
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const session = await getSession({ req });
const authenticatedUser = await prisma.user.findFirst({
rejectOnNotFound: true,
const authenticatedUser = await prisma.user.findFirstOrThrow({
where: {
id: session?.user.id,
},
+1 -2
View File
@@ -12,13 +12,12 @@ export async function getHandler(req: NextApiRequest, res: NextApiResponse) {
try {
const session = await getSession({ req });
const userId = session?.user?.id;
const user = await prisma.user.findFirst({
const user = await prisma.user.findFirstOrThrow({
select: {
id: true,
metadata: true,
},
where: { id: userId },
rejectOnNotFound: true,
});
const checkPremiumUsernameResult = await checkUsername(intentUsername);
+1 -2
View File
@@ -15,8 +15,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return;
}
const user = await prisma.user.findUnique({
rejectOnNotFound: true,
const user = await prisma.user.findUniqueOrThrow({
where: {
id: session.user.id,
},
+1 -2
View File
@@ -17,8 +17,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
throw new HttpCode({ statusCode: 405, message: "Method Not Allowed" });
}
const user = await prisma.user.findUnique({
rejectOnNotFound: true,
const user = await prisma.user.findUniqueOrThrow({
where: {
id: session.user.id,
},
+1 -2
View File
@@ -24,8 +24,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(400).json({ message: "No user id provided" });
}
const authenticatedUser = await prisma.user.findFirst({
rejectOnNotFound: true,
const authenticatedUser = await prisma.user.findFirstOrThrow({
where: {
id: session.user.id,
},
+1 -2
View File
@@ -19,8 +19,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (req.method === "DELETE") {
// Get user
const user = await prisma.user.findUnique({
rejectOnNotFound: true,
const user = await prisma.user.findUniqueOrThrow({
where: {
id: session.user?.id,
},
+1 -2
View File
@@ -206,8 +206,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
const id = context.params?.id as string;
try {
const resetPasswordRequest = await prisma.resetPasswordRequest.findUnique({
rejectOnNotFound: true,
const resetPasswordRequest = await prisma.resetPasswordRequest.findUniqueOrThrow({
where: {
id,
},
+1 -2
View File
@@ -61,8 +61,7 @@ export const createUsersFixture = (page: Page, workerInfo: WorkerInfo) => {
length: 30,
},
});
const user = await prisma.user.findUnique({
rejectOnNotFound: true,
const user = await prisma.user.findUniqueOrThrow({
where: { id: _user.id },
include: userIncludes,
});
+1 -2
View File
@@ -10,8 +10,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (req.method === "POST") {
const { username, password } = req.body;
// Get user
const user = await prisma.user.findFirst({
rejectOnNotFound: true,
const user = await prisma.user.findFirstOrThrow({
where: {
id: req.session?.user?.id,
},
+1 -2
View File
@@ -10,8 +10,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (req.method === "POST") {
const { username, password, url } = req.body;
// Get user
const user = await prisma.user.findFirst({
rejectOnNotFound: true,
const user = await prisma.user.findFirstOrThrow({
where: {
id: req.session?.user?.id,
},
@@ -19,8 +19,7 @@ const bodySchema = z
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
const body = bodySchema.parse(req.body);
// Get user
const user = await prisma.user.findFirst({
rejectOnNotFound: true,
const user = await prisma.user.findFirstOrThrow({
where: {
id: req.session?.user?.id,
},
@@ -19,8 +19,7 @@ const bodySchema = z
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
const body = bodySchema.parse(req.body);
// Get user
const user = await prisma.user.findFirst({
rejectOnNotFound: true,
const user = await prisma.user.findFirstOrThrow({
where: {
id: req.session?.user?.id,
},
+1 -2
View File
@@ -16,8 +16,7 @@ async function handler(req: NextApiRequest) {
const { client_id } = await getSlackAppKeys();
// Get user
await prisma.user.findFirst({
rejectOnNotFound: true,
await prisma.user.findFirstOrThrow({
where: {
id: req.session.user.id,
},
@@ -43,8 +43,7 @@ export default async function createEvent(req: NextApiRequest, res: NextApiRespo
// Im sure this query can be made more efficient... The JSON filtering wouldnt work when doing it directly on user.
const foundUser = await db.credential
.findFirst({
rejectOnNotFound: true,
.findFirstOrThrow({
...WhereCredsEqualsId(user.id),
})
.user({
+1 -2
View File
@@ -12,8 +12,7 @@ let base_url = "";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "GET") {
// Get user
await prisma.user.findFirst({
rejectOnNotFound: true,
await prisma.user.findFirstOrThrow({
where: {
id: req.session?.user?.id,
},
+1 -2
View File
@@ -57,8 +57,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
try {
if (event.data.user_id) {
const json = { userVitalId: event.data.user_id as string };
const credential = await prisma.credential.findFirst({
rejectOnNotFound: true,
const credential = await prisma.credential.findFirstOrThrow({
where: {
type: "vital_other",
key: {
+2 -4
View File
@@ -17,7 +17,7 @@ import { getCalendar } from "../../_utils/getCalendar";
type PersonAttendeeCommonFields = Pick<User, "id" | "email" | "name" | "locale" | "timeZone" | "username">;
const Reschedule = async (bookingUid: string, cancellationReason: string) => {
const bookingToReschedule = await prisma.booking.findFirst({
const bookingToReschedule = await prisma.booking.findFirstOrThrow({
select: {
id: true,
uid: true,
@@ -42,7 +42,6 @@ const Reschedule = async (bookingUid: string, cancellationReason: string) => {
},
},
},
rejectOnNotFound: true,
where: {
uid: bookingUid,
NOT: {
@@ -55,13 +54,12 @@ const Reschedule = async (bookingUid: string, cancellationReason: string) => {
if (bookingToReschedule && bookingToReschedule.eventTypeId && bookingToReschedule.user) {
const userOwner = bookingToReschedule.user;
const event = await prisma.eventType.findFirst({
const event = await prisma.eventType.findFirstOrThrow({
select: {
title: true,
users: true,
schedulingType: true,
},
rejectOnNotFound: true,
where: {
id: bookingToReschedule.eventTypeId,
},
@@ -17,7 +17,7 @@ import { getCalendar } from "../../_utils/getCalendar";
type PersonAttendeeCommonFields = Pick<User, "id" | "email" | "name" | "locale" | "timeZone" | "username">;
const Reschedule = async (bookingUid: string, cancellationReason: string) => {
const bookingToReschedule = await prisma.booking.findFirst({
const bookingToReschedule = await prisma.booking.findFirstOrThrow({
select: {
id: true,
uid: true,
@@ -42,7 +42,6 @@ const Reschedule = async (bookingUid: string, cancellationReason: string) => {
},
},
},
rejectOnNotFound: true,
where: {
uid: bookingUid,
NOT: {
@@ -55,13 +54,12 @@ const Reschedule = async (bookingUid: string, cancellationReason: string) => {
if (bookingToReschedule && bookingToReschedule.eventTypeId && bookingToReschedule.user) {
const userOwner = bookingToReschedule.user;
const event = await prisma.eventType.findFirst({
const event = await prisma.eventType.findFirstOrThrow({
select: {
title: true,
users: true,
schedulingType: true,
},
rejectOnNotFound: true,
where: {
id: bookingToReschedule.eventTypeId,
},
+1 -2
View File
@@ -9,8 +9,7 @@ import { getZoomAppKeys } from "../lib";
async function handler(req: NextApiRequest) {
// Get user
await prisma.user.findFirst({
rejectOnNotFound: true,
await prisma.user.findFirstOrThrow({
where: {
id: req.session?.user?.id,
},
@@ -95,8 +95,7 @@ export class CalendarEventBuilder implements ICalendarEventBuilder {
private async getUserById(userId: number) {
let resultUser: User | null;
try {
resultUser = await prisma.user.findUnique({
rejectOnNotFound: true,
resultUser = await prisma.user.findUniqueOrThrow({
where: {
id: userId,
},
@@ -111,8 +110,7 @@ export class CalendarEventBuilder implements ICalendarEventBuilder {
private async getEventFromEventId(eventTypeId: number) {
let resultEventType;
try {
resultEventType = await prisma.eventType.findUnique({
rejectOnNotFound: true,
resultEventType = await prisma.eventType.findUniqueOrThrow({
where: {
id: eventTypeId,
},
@@ -223,8 +221,7 @@ export class CalendarEventBuilder implements ICalendarEventBuilder {
public async setUsersFromId(userId: User["id"]) {
let resultUser: User | null;
try {
resultUser = await prisma.user.findUnique({
rejectOnNotFound: true,
resultUser = await prisma.user.findUniqueOrThrow({
where: {
id: userId,
},
@@ -1,4 +1,4 @@
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
import { PrismaClientKnownRequestError, NotFoundError } from "@prisma/client/runtime";
import Stripe from "stripe";
import { ZodError, ZodIssue } from "zod";
@@ -42,6 +42,9 @@ export function getServerErrorFromUnknown(cause: unknown): HttpError {
if (cause instanceof PrismaClientKnownRequestError) {
return new HttpError({ statusCode: 400, message: cause.message, cause });
}
if (cause instanceof NotFoundError) {
return new HttpError({ statusCode: 404, message: cause.message, cause });
}
if (cause instanceof Stripe.errors.StripeInvalidRequestError) {
return new HttpError({ statusCode: 400, message: cause.message, cause });
}
@@ -96,7 +96,7 @@ export const bookingsRouter = createProtectedRouter()
const { booking } = ctx;
try {
const organizer = await ctx.prisma.user.findFirst({
const organizer = await ctx.prisma.user.findFirstOrThrow({
where: {
id: booking.userId || 0,
},
@@ -106,7 +106,6 @@ export const bookingsRouter = createProtectedRouter()
timeZone: true,
locale: true,
},
rejectOnNotFound: true,
});
const tOrganizer = await getTranslation(organizer.locale ?? "en", "common");