Added an API to delete existing contact category

This commit is contained in:
ashutosh.nehete 2025-05-17 12:15:23 +05:30
parent 326ceecdbf
commit 54d6d5f1bd
2 changed files with 35 additions and 1 deletions

View File

@ -713,7 +713,8 @@ namespace Marco.Pms.Services.Controllers
[HttpDelete("contact-category/{id}")] [HttpDelete("contact-category/{id}")]
public async Task<IActionResult> DeletecontactCategoryMaster(Guid id) public async Task<IActionResult> DeletecontactCategoryMaster(Guid id)
{ {
return Ok(); var response = await _masterHelper.DeleteContactCategory(id);
return Ok(response);
} }
// -------------------------------- Contact Tag -------------------------------- // -------------------------------- Contact Tag --------------------------------

View File

@ -63,6 +63,39 @@ namespace Marco.Pms.Services.Helpers
_logger.LogInfo("{count} contact categoires are fetched by Employee with ID {LoggedInEmployeeId}", contactCategories.Count, LoggedInEmployee.Id); _logger.LogInfo("{count} contact categoires are fetched by Employee with ID {LoggedInEmployeeId}", contactCategories.Count, LoggedInEmployee.Id);
return ApiResponse<object>.SuccessResponse(contactCategories, System.String.Format("{0} contact categories fetched successfully", contactCategories.Count), 200); return ApiResponse<object>.SuccessResponse(contactCategories, System.String.Format("{0} contact categories fetched successfully", contactCategories.Count), 200);
} }
public async Task<ApiResponse<object>> DeleteContactCategory(Guid id)
{
Guid tenantId = _userHelper.GetTenantId();
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
ContactCategoryMaster? contactCategory = await _context.ContactCategoryMasters.FirstOrDefaultAsync(c => c.Id == id && c.TenantId == tenantId);
if (contactCategory != null)
{
List<Contact>? existingContacts = await _context.Contacts.AsNoTracking().Where(c => c.ContactCategoryId == contactCategory.Id).ToListAsync();
if (existingContacts.Count > 0)
{
List<Contact>? contacts = new List<Contact>();
foreach (var contact in existingContacts)
{
contact.ContactCategoryId = null;
contacts.Add(contact);
}
_context.Contacts.UpdateRange(contacts);
}
_context.ContactCategoryMasters.Remove(contactCategory);
_context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
{
RefereanceId = id,
UpdatedById = LoggedInEmployee.Id,
UpdateAt = DateTime.UtcNow
});
await _context.SaveChangesAsync();
_logger.LogInfo("Employee {EmployeeId} deleted contact category {ContactCategoryId}", LoggedInEmployee.Id, id);
}
_logger.LogWarning("Employee {EmployeeId} tries to delete Category {CategoryId} but not found in database", LoggedInEmployee.Id, id);
return ApiResponse<object>.SuccessResponse(new { }, "Category deleted successfully", 200);
}
// -------------------------------- Contact Tag -------------------------------- // -------------------------------- Contact Tag --------------------------------