From a0167ee5acd22f49aca58cbb51aa5314d8dc0b95 Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Mon, 20 Apr 2020 03:17:32 +0530 Subject: [PATCH] :recycle: Use single query to find user email --- src/rest/auth.ts | 3 ++- src/services/user.service.ts | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/rest/auth.ts b/src/rest/auth.ts index e75331bce..605233b0f 100644 --- a/src/rest/auth.ts +++ b/src/rest/auth.ts @@ -73,6 +73,7 @@ export const login = async ( emails: { some: { email, + isVerified: false, }, }, }, @@ -153,7 +154,7 @@ export const register = async ( : {}), }) ).id; - if (email) await createEmail(userId, email, emailVerified); + if (email) await createEmail(userId, email, !emailVerified); if (locals) await addApprovedLocation(userId, locals.ipAddress); return { userId }; }; diff --git a/src/services/user.service.ts b/src/services/user.service.ts index 63c3cba53..90e5493f9 100644 --- a/src/services/user.service.ts +++ b/src/services/user.service.ts @@ -102,13 +102,19 @@ export const createUser = async ( * Get the details of a user by their email */ export const getUserByEmail = async (email: string, secureOrigin = false) => { - const emailObject = await prisma.emails.findMany({ - where: { email, isVerified: true }, + const users = await prisma.users.findMany({ + where: { + emails: { + some: { + email, + isVerified: true, + }, + }, + }, }); - if (!emailObject.length) throw new Error(USER_NOT_FOUND); - const user = await getUserById(emailObject[0].id); - if (!secureOrigin) return deleteSensitiveInfoUser(user); - return user; + if (!users.length) throw new Error(USER_NOT_FOUND); + if (!secureOrigin) return deleteSensitiveInfoUser(users[0]); + return users[0]; }; /**