Merge pull request 'Implemented filtering functionality for Get Contact List API' (#67) from AshutoshEnhancement#332_Added_Filter_To_Contact_List into Feature_Directory

Reviewed-on: #67
This commit is contained in:
Vikas Nale 2025-05-22 13:19:27 +00:00
commit e0af0b5ba3
4 changed files with 55 additions and 9 deletions

View File

@ -0,0 +1,9 @@
namespace Marco.Pms.Model.Dtos.Directory
{
public class ContactFilterDto
{
public List<Guid>? BucketIds { get; set; }
public List<Guid>? CategoryIds { get; set; }
}
}

View File

@ -25,9 +25,9 @@ namespace Marco.Pms.Services.Controllers
} }
[HttpGet] [HttpGet]
public async Task<IActionResult> GetContactList() public async Task<IActionResult> GetContactList([FromQuery] string? search, [FromBody] ContactFilterDto? filterDto, [FromQuery] bool active = true)
{ {
var response = await _directoryHelper.GetListOfContacts(); var response = await _directoryHelper.GetListOfContacts(search, active, filterDto);
if (response.StatusCode == 200) if (response.StatusCode == 200)

View File

@ -640,7 +640,8 @@ namespace Marco.Pms.Services.Controllers
_logger.LogError("Work category master {WorkCategoryId} not found in database", workCategoryMasterDto.Id ?? Guid.Empty); _logger.LogError("Work category master {WorkCategoryId} not found in database", workCategoryMasterDto.Id ?? Guid.Empty);
return NotFound(ApiResponse<object>.ErrorResponse("Work category master not found", "Work category master not found", 404)); return NotFound(ApiResponse<object>.ErrorResponse("Work category master not found", "Work category master not found", 404));
} }
_logger.LogError("User sent empyt payload");
return BadRequest(ApiResponse<object>.ErrorResponse("User sent Empty payload", "User sent Empty payload", 400));
} }
[HttpDelete("work-category/{id}")] [HttpDelete("work-category/{id}")]
@ -781,7 +782,7 @@ namespace Marco.Pms.Services.Controllers
if (updateContactTagDto != null && updateContactTagDto.Id != id) if (updateContactTagDto != null && updateContactTagDto.Id != id)
{ {
ContactTagMaster? contactTag = await _context.ContactTagMasters.AsNoTracking().FirstOrDefaultAsync(t => t.TenantId == tenantId && t.Id == updateContactTagDto.Id); ContactTagMaster? contactTag = await _context.ContactTagMasters.AsNoTracking().FirstOrDefaultAsync(t => t.TenantId == tenantId && t.Id == updateContactTagDto.Id);
if(contactTag != null) if (contactTag != null)
{ {
contactTag = updateContactTagDto.ToContactTagMasterFromUpdateContactTagDto(tenantId); contactTag = updateContactTagDto.ToContactTagMasterFromUpdateContactTagDto(tenantId);
_context.ContactTagMasters.Update(contactTag); _context.ContactTagMasters.Update(contactTag);
@ -796,8 +797,8 @@ namespace Marco.Pms.Services.Controllers
ContactTagVM contactTagVm = contactTag.ToContactTagVMFromContactTagMaster(); ContactTagVM contactTagVm = contactTag.ToContactTagVMFromContactTagMaster();
_logger.LogInfo("Work category master {ConatctTagId} updated successfully from tenant {tenantId}", contactTagVm.Id, tenantId); _logger.LogInfo("Work category master {ConatctTagId} updated successfully from tenant {tenantId}", contactTagVm.Id, tenantId);
return Ok(ApiResponse<object>.SuccessResponse(contactTagVm, "Contact Tag master updated successfully", 200)); return Ok(ApiResponse<object>.SuccessResponse(contactTagVm, "Contact Tag master updated successfully", 200));
} }

View File

@ -29,17 +29,29 @@ namespace Marco.Pms.Services.Helpers
public async Task<ApiResponse<object>> GetListOfContacts() public async Task<ApiResponse<object>> GetListOfContacts(string? search, bool active, ContactFilterDto? filterDto)
{ {
Guid tenantId = _userHelper.GetTenantId(); Guid tenantId = _userHelper.GetTenantId();
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
List<EmployeeBucketMapping>? employeeBuckets = await _context.EmployeeBucketMappings.Where(eb => eb.EmployeeId == LoggedInEmployee.Id).ToListAsync(); List<EmployeeBucketMapping>? employeeBuckets = await _context.EmployeeBucketMappings.Where(eb => eb.EmployeeId == LoggedInEmployee.Id).ToListAsync();
List<Guid> bucketIds = employeeBuckets.Select(c => c.BucketId).ToList(); List<Guid> bucketIds = employeeBuckets.Select(c => c.BucketId).ToList();
if (filterDto != null && filterDto.BucketIds != null && filterDto.BucketIds.Count > 0)
{
bucketIds = filterDto.BucketIds;
}
List<ContactBucketMapping>? contactBuckets = await _context.ContactBucketMappings.Where(cb => bucketIds.Contains(cb.BucketId)).ToListAsync(); List<ContactBucketMapping>? contactBuckets = await _context.ContactBucketMappings.Where(cb => bucketIds.Contains(cb.BucketId)).ToListAsync();
List<Guid> contactIds = contactBuckets.Select(cb => cb.ContactId).ToList(); List<Guid> contactIds = contactBuckets.Select(cb => cb.ContactId).ToList();
var contacts = await _context.Contacts.Include(c => c.ContactCategory).Where(c => contactIds.Contains(c.Id) && c.TenantId == tenantId && c.IsActive).ToListAsync(); List<Contact> contacts = new List<Contact>();
if (filterDto != null && filterDto.CategoryIds != null && filterDto.CategoryIds.Count > 0)
{
var categoryIds = filterDto.CategoryIds;
contacts = await _context.Contacts.Include(c => c.ContactCategory).Where(c => contactIds.Contains(c.Id) && categoryIds.Contains(c.ContactCategoryId ?? Guid.Empty) && c.TenantId == tenantId && c.IsActive == active).ToListAsync();
}
else
{
contacts = await _context.Contacts.Include(c => c.ContactCategory).Where(c => contactIds.Contains(c.Id) && c.TenantId == tenantId && c.IsActive == active).ToListAsync();
}
var phoneNo = await _context.ContactsPhones.Where(p => contactIds.Contains(p.ContactId)).ToListAsync(); var phoneNo = await _context.ContactsPhones.Where(p => contactIds.Contains(p.ContactId)).ToListAsync();
var Emails = await _context.ContactsEmails.Where(E => contactIds.Contains(E.ContactId)).ToListAsync(); var Emails = await _context.ContactsEmails.Where(E => contactIds.Contains(E.ContactId)).ToListAsync();
@ -50,6 +62,19 @@ namespace Marco.Pms.Services.Helpers
var TagList = await _context.ContactTagMasters.Where(t => TagIds.Contains(t.Id)).ToListAsync(); var TagList = await _context.ContactTagMasters.Where(t => TagIds.Contains(t.Id)).ToListAsync();
if (search != null && search != string.Empty)
{
List<Guid> filteredContactIds = new List<Guid>();
phoneNo = phoneNo.Where(p => Compare(p.PhoneNumber, search)).ToList();
filteredContactIds = phoneNo.Select(p => p.ContactId).ToList();
Emails = Emails.Where(e => Compare(e.EmailAddress, search)).ToList();
filteredContactIds.AddRange(Emails.Select(e => e.ContactId).ToList());
filteredContactIds = filteredContactIds.Distinct().ToList();
contacts = contacts.Where(c => Compare(c.Name, search) || Compare(c.Organization, search) || filteredContactIds.Contains(c.Id)).ToList();
}
List<ContactVM> list = new List<ContactVM>(); List<ContactVM> list = new List<ContactVM>();
foreach (var contact in contacts) foreach (var contact in contacts)
@ -66,6 +91,7 @@ namespace Marco.Pms.Services.Helpers
var projectMapping = contactProjects.Where(p => p.ContactId == contact.Id).ToList(); var projectMapping = contactProjects.Where(p => p.ContactId == contact.Id).ToList();
var bucketMapping = contactBuckets.Where(b => b.ContactId == contact.Id).ToList(); var bucketMapping = contactBuckets.Where(b => b.ContactId == contact.Id).ToList();
if (emails != null && emails.Count > 0) if (emails != null && emails.Count > 0)
{ {
foreach (var email in emails) foreach (var email in emails)
@ -944,5 +970,15 @@ namespace Marco.Pms.Services.Helpers
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id); _logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400); return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
} }
private bool Compare(string sentence, string search)
{
sentence = sentence.Trim().ToLower();
search = search.Trim().ToLower();
// Check for exact substring
bool result = sentence.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0;
return result;
}
} }
} }