Skip to content

Commit

Permalink
refactor(api): use more semantic whereILike instruction instead of ra…
Browse files Browse the repository at this point in the history
…w SQL commands
  • Loading branch information
lego-technix committed Mar 9, 2025
1 parent 5df4cf9 commit 12b5095
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { UserDetailsForAdmin } from '../../domain/models/UserDetailsForAdmin.js'
import { UserLogin } from '../../domain/models/UserLogin.js';

const getByEmail = async function (email) {
const foundUser = await knex.from('users').whereRaw('LOWER("email") = ?', email.toLowerCase()).first();
const foundUser = await knex.from('users').whereILike('email', email).first();
if (!foundUser) {
throw new UserNotFoundError(`User not found for email ${email}`);
}
Expand Down Expand Up @@ -55,7 +55,7 @@ const getFullById = async function (userId) {

const getByUsernameOrEmailWithRolesAndPassword = async function (username) {
const userDTO = await knex('users')
.whereRaw('LOWER("email") = ?', username.toLowerCase())
.whereILike('email', username)
.orWhere({ username: username.toLowerCase() })
.first();

Expand Down Expand Up @@ -257,15 +257,15 @@ const updateWithEmailConfirmed = function ({ id, userAttributes }) {
};

const checkIfEmailIsAvailable = async function (email) {
const existingUserEmail = await knex('users').whereRaw('LOWER("email") = ?', email.toLowerCase()).first();
const existingUserEmail = await knex('users').whereILike('email', email).first();

if (existingUserEmail) throw new InvalidOrAlreadyUsedEmailError();

return email;
};

const isUserExistingByEmail = async function (email) {
const existingUser = await knex('users').whereRaw('LOWER("email") = ?', email.toLowerCase()).first();
const existingUser = await knex('users').whereILike('email', email).first();
if (!existingUser) throw new UserNotFoundError();
return true;
};
Expand Down Expand Up @@ -390,7 +390,7 @@ const findByExternalIdentifier = async function ({ externalIdentityId, identityP
};

const findAnotherUserByEmail = async function (userId, email) {
const anotherUsers = await knex('users').whereNot('id', userId).whereRaw('LOWER("email") = ?', email.toLowerCase());
const anotherUsers = await knex('users').whereNot('id', userId).whereILike('email', email);

return anotherUsers.map((anotherUser) => new User(anotherUser));
};
Expand Down

0 comments on commit 12b5095

Please sign in to comment.