From ac5884d4db25d2251ce53fe247763bfa7d0098f4 Mon Sep 17 00:00:00 2001 From: Prakash Date: Fri, 25 Aug 2023 18:52:14 +0530 Subject: [PATCH] add try catch to handle for getUserData --- controllers/auth.ts | 12 +++++++----- services/authService.ts | 37 +++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/controllers/auth.ts b/controllers/auth.ts index 8d02cb6..8e4827f 100644 --- a/controllers/auth.ts +++ b/controllers/auth.ts @@ -77,11 +77,13 @@ const googleAuthCallback = ( let rCalUiUrl = new URL(config.get('services.rCalUi.baseUrl')); - try { - rCalUiUrl = new URL(redirectURL); - } catch (error) { - logger.error('Invalid redirect URL provided'); - logger.error(error); + if (redirectURL) { + try { + rCalUiUrl = new URL(redirectURL); + } catch (error) { + logger.error('Invalid redirect URL provided'); + logger.error(error); + } } try { diff --git a/services/authService.ts b/services/authService.ts index 44155e3..73abae5 100644 --- a/services/authService.ts +++ b/services/authService.ts @@ -47,24 +47,29 @@ const decodeAuthToken = (token: string): any => { */ const getUserData = async ( email: string -): Promise => { - const user = await prisma.users.findUniqueOrThrow({ - where: { - email, - }, - include: { - Calendar: { - where: { - isDeleted: false, - isPrimary: true, - }, - select: { - id: true, +): Promise<(Users & { calendarId: number }) | undefined> => { + try { + const user = await prisma.users.findUniqueOrThrow({ + where: { + email, + }, + include: { + Calendar: { + where: { + isDeleted: false, + isPrimary: true, + }, + select: { + id: true, + }, }, }, - }, - }); - return { ...user, calendarId: user.Calendar[0].id }; + }); + return { ...user, calendarId: user.Calendar[0].id }; + } catch (error) { + logger.error('Could not find user', error); + return undefined; + } }; /**