Implemented an API to suspend a Contact #66

Merged
vikas.nale merged 2 commits from Ashutosh_Task#263_Delete_Contact into Feature_Directory 2025-05-22 06:13:50 +00:00
2 changed files with 50 additions and 0 deletions

View File

@ -107,6 +107,10 @@ namespace Marco.Pms.Services.Controllers
{ {
return Ok(response); return Ok(response);
} }
else if (response.StatusCode == 404)
{
return NotFound(response);
}
else else
{ {
return BadRequest(response); return BadRequest(response);
@ -120,6 +124,24 @@ namespace Marco.Pms.Services.Controllers
return Ok(response); return Ok(response);
} }
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteContact(Guid id)
{
var response = await _directoryHelper.DeleteContact(id);
if (response.StatusCode == 200)
{
return Ok(response);
}
else if (response.StatusCode == 404)
{
return NotFound(response);
}
else
{
return BadRequest(response);
}
}
// -------------------------------- Contact Notes -------------------------------- // -------------------------------- Contact Notes --------------------------------
[HttpPost("note")] [HttpPost("note")]

View File

@ -752,6 +752,34 @@ namespace Marco.Pms.Services.Helpers
_logger.LogInfo("Employee {EmployeeId} fetched list of organizations in a tenant {TenantId}", LoggedInEmployee.Id, tenantId); _logger.LogInfo("Employee {EmployeeId} fetched list of organizations in a tenant {TenantId}", LoggedInEmployee.Id, tenantId);
return ApiResponse<object>.SuccessResponse(organizationList, $"{organizationList.Count} records of organization names fetched from contacts", 200); return ApiResponse<object>.SuccessResponse(organizationList, $"{organizationList.Count} records of organization names fetched from contacts", 200);
} }
public async Task<ApiResponse<object>> DeleteContact(Guid id)
{
Guid tenantId = _userHelper.GetTenantId();
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
if (id != Guid.Empty)
{
Contact? contact = await _context.Contacts.FirstOrDefaultAsync(c => c.Id == id && c.TenantId == tenantId);
if (contact == null)
{
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} tries to delete contact with ID {ContactId} is not found in database", LoggedInEmployee.Id);
return ApiResponse<object>.ErrorResponse("Contact not found", "Contact not found", 404);
}
contact.IsActive = false;
_context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
{
RefereanceId = contact.Id,
UpdatedById = LoggedInEmployee.Id,
UpdateAt = DateTime.UtcNow
});
await _context.SaveChangesAsync();
_logger.LogInfo("Contact {ContactId} has been deleted by Employee {Employee}", id, LoggedInEmployee.Id);
return ApiResponse<object>.SuccessResponse(new { }, "Contact is deleted Successfully", 200);
}
_logger.LogInfo("Employee ID {EmployeeId} sent an empty contact id", LoggedInEmployee.Id);
return ApiResponse<object>.ErrorResponse("Contact ID is empty", "Contact ID is empty", 400);
}
// -------------------------------- Contact Notes -------------------------------- // -------------------------------- Contact Notes --------------------------------