Implemented filtering functionality for Get Contact List API
This commit is contained in:
parent
a9d20b8077
commit
62f9b2256e
9
Marco.Pms.Model/Dtos/Directory/ContactFilterDto.cs
Normal file
9
Marco.Pms.Model/Dtos/Directory/ContactFilterDto.cs
Normal 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; }
|
||||
|
||||
}
|
||||
}
|
@ -25,9 +25,9 @@ namespace Marco.Pms.Services.Controllers
|
||||
}
|
||||
|
||||
[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)
|
||||
|
@ -640,7 +640,8 @@ namespace Marco.Pms.Services.Controllers
|
||||
_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));
|
||||
}
|
||||
|
||||
_logger.LogError("User sent empyt payload");
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("User sent Empty payload", "User sent Empty payload", 400));
|
||||
}
|
||||
|
||||
[HttpDelete("work-category/{id}")]
|
||||
@ -781,7 +782,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
if (updateContactTagDto != null && updateContactTagDto.Id != 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);
|
||||
_context.ContactTagMasters.Update(contactTag);
|
||||
|
@ -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();
|
||||
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||
List<EmployeeBucketMapping>? employeeBuckets = await _context.EmployeeBucketMappings.Where(eb => eb.EmployeeId == LoggedInEmployee.Id).ToListAsync();
|
||||
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<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 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();
|
||||
|
||||
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>();
|
||||
|
||||
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 bucketMapping = contactBuckets.Where(b => b.ContactId == contact.Id).ToList();
|
||||
|
||||
|
||||
if (emails != null && emails.Count > 0)
|
||||
{
|
||||
foreach (var email in emails)
|
||||
@ -1005,5 +1031,14 @@ namespace Marco.Pms.Services.Helpers
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user