feat: add search functionality to getAllHostApplications
This commit is contained in:
@@ -35,8 +35,11 @@ export const handler = safeHandler(async (
|
||||
throw new ApiError(404, 'User not found');
|
||||
}
|
||||
|
||||
// Get search query from query parameters
|
||||
const search = event.queryStringParameters?.search || '';
|
||||
|
||||
// Get all host applications from service based on user role
|
||||
const hostApplications = await minglarService.getAllHostApplications(user.id, user.roleXid);
|
||||
const hostApplications = await minglarService.getAllHostApplications(user.id, user.roleXid, search);
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
|
||||
@@ -376,7 +376,7 @@ export class MinglarService {
|
||||
})
|
||||
}
|
||||
|
||||
async getAllHostApplications(userId: number, userRoleXid: number) {
|
||||
async getAllHostApplications(userId: number, userRoleXid: number, search?: string) {
|
||||
// Build where clause based on user role
|
||||
const whereClause: any = {
|
||||
isActive: true,
|
||||
@@ -387,6 +387,29 @@ export class MinglarService {
|
||||
}
|
||||
};
|
||||
|
||||
// Add search filter if search query is provided
|
||||
if (search && search.trim() !== '') {
|
||||
const searchTerm = search.trim();
|
||||
|
||||
// Check if search term is a number (for ID search)
|
||||
const isNumeric = /^\d+$/.test(searchTerm);
|
||||
|
||||
if (isNumeric) {
|
||||
// Search by host ID
|
||||
whereClause.id = parseInt(searchTerm);
|
||||
} else {
|
||||
// Search by email or name
|
||||
whereClause.user = {
|
||||
...whereClause.user,
|
||||
OR: [
|
||||
{ emailAddress: { contains: searchTerm, mode: 'insensitive' } },
|
||||
{ firstName: { contains: searchTerm, mode: 'insensitive' } },
|
||||
{ lastName: { contains: searchTerm, mode: 'insensitive' } }
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// If user is Co_Admin or Account_Manager, filter by assigned hosts only
|
||||
if (userRoleXid === ROLE.CO_ADMIN || userRoleXid === ROLE.ACCOUNT_MANAGER) {
|
||||
whereClause.accountManagerXid = userId;
|
||||
@@ -433,6 +456,7 @@ export class MinglarService {
|
||||
accountManager: host.accountManager || null
|
||||
}));
|
||||
}
|
||||
|
||||
async getAllCoadminAndAM() {
|
||||
return await this.prisma.user.findMany({
|
||||
where: {
|
||||
|
||||
Reference in New Issue
Block a user