feat: update user search to support multi-word name matching across profiles
This commit is contained in:
@@ -121,6 +121,38 @@ export class UsersService {
|
||||
const where: Prisma.UserWhereInput = {};
|
||||
if (role) where.role = role;
|
||||
if (search) {
|
||||
const searchParts = search.trim().split(/\s+/).filter(Boolean);
|
||||
|
||||
if (searchParts.length > 1) {
|
||||
// Multi-word search (e.g. "Sophie Brown") — match across firstName + lastName
|
||||
where.OR = [
|
||||
// Match all parts across firstName/lastName for userProfile
|
||||
{
|
||||
userProfile: {
|
||||
AND: searchParts.map((part) => ({
|
||||
OR: [
|
||||
{ firstName: { contains: part, mode: 'insensitive' as const } },
|
||||
{ lastName: { contains: part, mode: 'insensitive' as const } },
|
||||
],
|
||||
})),
|
||||
},
|
||||
},
|
||||
// Match all parts across firstName/lastName for agentProfile
|
||||
{
|
||||
agentProfile: {
|
||||
AND: searchParts.map((part) => ({
|
||||
OR: [
|
||||
{ firstName: { contains: part, mode: 'insensitive' as const } },
|
||||
{ lastName: { contains: part, mode: 'insensitive' as const } },
|
||||
],
|
||||
})),
|
||||
},
|
||||
},
|
||||
// Also match the full search string against email
|
||||
{ email: { contains: search, mode: 'insensitive' } },
|
||||
];
|
||||
} else {
|
||||
// Single word — match against any field
|
||||
where.OR = [
|
||||
{ email: { contains: search, mode: 'insensitive' } },
|
||||
{ userProfile: { firstName: { contains: search, mode: 'insensitive' } } },
|
||||
@@ -129,6 +161,7 @@ export class UsersService {
|
||||
{ agentProfile: { lastName: { contains: search, mode: 'insensitive' } } },
|
||||
];
|
||||
}
|
||||
}
|
||||
if (verificationStatus) {
|
||||
where.agentProfile = { ...((where.agentProfile as object) || {}), verificationStatus: verificationStatus as any };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user