Added an API to delete existing contact category
This commit is contained in:
parent
57d430be1e
commit
a97b95eb00
@ -127,6 +127,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 --------------------------------
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user