feat: update user search to support multi-word name matching across profiles
This commit is contained in:
@@ -121,13 +121,46 @@ export class UsersService {
|
|||||||
const where: Prisma.UserWhereInput = {};
|
const where: Prisma.UserWhereInput = {};
|
||||||
if (role) where.role = role;
|
if (role) where.role = role;
|
||||||
if (search) {
|
if (search) {
|
||||||
where.OR = [
|
const searchParts = search.trim().split(/\s+/).filter(Boolean);
|
||||||
{ email: { contains: search, mode: 'insensitive' } },
|
|
||||||
{ userProfile: { firstName: { contains: search, mode: 'insensitive' } } },
|
if (searchParts.length > 1) {
|
||||||
{ userProfile: { lastName: { contains: search, mode: 'insensitive' } } },
|
// Multi-word search (e.g. "Sophie Brown") — match across firstName + lastName
|
||||||
{ agentProfile: { firstName: { contains: search, mode: 'insensitive' } } },
|
where.OR = [
|
||||||
{ agentProfile: { lastName: { contains: search, mode: 'insensitive' } } },
|
// 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' } } },
|
||||||
|
{ userProfile: { lastName: { contains: search, mode: 'insensitive' } } },
|
||||||
|
{ agentProfile: { firstName: { contains: search, mode: 'insensitive' } } },
|
||||||
|
{ agentProfile: { lastName: { contains: search, mode: 'insensitive' } } },
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (verificationStatus) {
|
if (verificationStatus) {
|
||||||
where.agentProfile = { ...((where.agentProfile as object) || {}), verificationStatus: verificationStatus as any };
|
where.agentProfile = { ...((where.agentProfile as object) || {}), verificationStatus: verificationStatus as any };
|
||||||
|
|||||||
Reference in New Issue
Block a user