From fe5e58dce03cde1066485c2de4ab1009563b17f1 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Wed, 21 May 2025 17:29:20 +0530 Subject: [PATCH] Implemented an API to suspend a Contact --- .../Controllers/DirectoryController.cs | 22 +++++++++++++++++++ Marco.Pms.Services/Helpers/DirectoryHelper.cs | 20 +++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/Marco.Pms.Services/Controllers/DirectoryController.cs b/Marco.Pms.Services/Controllers/DirectoryController.cs index 8f2cb5c..843aacb 100644 --- a/Marco.Pms.Services/Controllers/DirectoryController.cs +++ b/Marco.Pms.Services/Controllers/DirectoryController.cs @@ -107,6 +107,10 @@ namespace Marco.Pms.Services.Controllers { return Ok(response); } + else if (response.StatusCode == 404) + { + return NotFound(response); + } else { return BadRequest(response); @@ -120,6 +124,24 @@ namespace Marco.Pms.Services.Controllers return Ok(response); } + [HttpDelete("{id}")] + public async Task 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 -------------------------------- [HttpPost("note")] diff --git a/Marco.Pms.Services/Helpers/DirectoryHelper.cs b/Marco.Pms.Services/Helpers/DirectoryHelper.cs index 51ca0d8..df5fbc9 100644 --- a/Marco.Pms.Services/Helpers/DirectoryHelper.cs +++ b/Marco.Pms.Services/Helpers/DirectoryHelper.cs @@ -752,6 +752,26 @@ namespace Marco.Pms.Services.Helpers _logger.LogInfo("Employee {EmployeeId} fetched list of organizations in a tenant {TenantId}", LoggedInEmployee.Id, tenantId); return ApiResponse.SuccessResponse(organizationList, $"{organizationList.Count} records of organization names fetched from contacts", 200); } + public async Task> 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.ErrorResponse("Contact not found", "Contact not found", 404); + } + contact.IsActive = false; + await _context.SaveChangesAsync(); + _logger.LogInfo("Contact {ContactId} has been deleted by Employee {Employee}", id, LoggedInEmployee.Id); + return ApiResponse.SuccessResponse(new { }, "Contact is deleted Successfully", 200); + } + _logger.LogInfo("Employee ID {EmployeeId} sent an empty contact id", LoggedInEmployee.Id); + return ApiResponse.ErrorResponse("Contact ID is empty", "Contact ID is empty", 400); + } // -------------------------------- Contact Notes --------------------------------