Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Use single query to find user email
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Apr 19, 2020
1 parent c1f3868 commit a0167ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/rest/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const login = async (
emails: {
some: {
email,
isVerified: false,
},
},
},
Expand Down Expand Up @@ -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 };
};
Expand Down
18 changes: 12 additions & 6 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};

/**
Expand Down

0 comments on commit a0167ee

Please sign in to comment.