Skip to content

Commit

Permalink
Improve user search to allow searching by first and middle name, and …
Browse files Browse the repository at this point in the history
…typing last name first
  • Loading branch information
AleksTeresh committed Sep 30, 2024
1 parent 9d8b0a5 commit 12fafa2
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/server/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,35 @@ usersRouter.get('/', async (req, res) => {
const users = await User.findAll({
attributes: userFields,
where: {
firstName: {
[Op.iLike]: `${firstName}%`,
},
lastName: {
[Op.iLike]: `${lastName}%`,
},
[Op.or]: [
// assume that the first word is the first name and the second word is the last name
{
firstName: {
[Op.iLike]: `${firstName}%`,
},
lastName: {
[Op.iLike]: `${lastName}%`,
},
},
// assume that both words are the "first name", this is because
// first name includes middle names as well. Thus this allows
// for searching with first and middle names
{
firstName: {
[Op.iLike]: `${trimmedSearch}%`,
},
},
// sometimes, users might first type in last name and then first name
// so we need to account for that as well
{
firstName: {
[Op.iLike]: `${lastName}%`,
},
lastName: {
[Op.iLike]: `${firstName}%`,
},
},
],
...whereClauses,
},
limit: 100,
Expand Down

0 comments on commit 12fafa2

Please sign in to comment.