Merge pull request 'Ashutosh_Task#269_Delete_Bucket' (#77) from Ashutosh_Task#269_Delete_Bucket into Feature_Directory
Reviewed-on: #77
This commit is contained in:
commit
b42b8b726a
@ -137,12 +137,12 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
|
|
||||||
if (dateFrom != null && DateTime.TryParse(dateFrom, out fromDate) == false)
|
if (dateFrom != null && DateTime.TryParse(dateFrom, out fromDate) == false)
|
||||||
{
|
{
|
||||||
_logger.LogError("User sent Invalid from Date while featching attendance logs");
|
_logger.LogError("User sent Invalid fromDate while featching attendance logs");
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid Date", "Invalid Date", 400));
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid Date", "Invalid Date", 400));
|
||||||
}
|
}
|
||||||
if (dateTo != null && DateTime.TryParse(dateTo, out toDate) == false)
|
if (dateTo != null && DateTime.TryParse(dateTo, out toDate) == false)
|
||||||
{
|
{
|
||||||
_logger.LogError("User sent Invalid to Date while featching attendance logs");
|
_logger.LogError("User sent Invalid toDate while featching attendance logs");
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid Date", "Invalid Date", 400));
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid Date", "Invalid Date", 400));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,9 +139,9 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
public async Task<IActionResult> DeleteContact(Guid id)
|
public async Task<IActionResult> DeleteContact(Guid id, [FromQuery] bool? active)
|
||||||
{
|
{
|
||||||
var response = await _directoryHelper.DeleteContact(id);
|
var response = await _directoryHelper.DeleteContact(id, active ?? false);
|
||||||
if (response.StatusCode == 200)
|
if (response.StatusCode == 200)
|
||||||
{
|
{
|
||||||
return Ok(response);
|
return Ok(response);
|
||||||
@ -214,9 +214,9 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("note/{id}")]
|
[HttpDelete("note/{id}")]
|
||||||
public async Task<IActionResult> DeleteContactNote(Guid id)
|
public async Task<IActionResult> DeleteContactNote(Guid id, [FromQuery] bool? active)
|
||||||
{
|
{
|
||||||
var response = await _directoryHelper.DeleteContactNote(id);
|
var response = await _directoryHelper.DeleteContactNote(id, active ?? false);
|
||||||
return Ok(response);
|
return Ok(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -315,5 +315,27 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
return BadRequest(response);
|
return BadRequest(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpDelete("bucket/{id}")]
|
||||||
|
public async Task<IActionResult> DeleteBucket(Guid id)
|
||||||
|
{
|
||||||
|
var response = await _directoryHelper.DeleteBucket(id);
|
||||||
|
if (response.StatusCode == 200)
|
||||||
|
{
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
else if (response.StatusCode == 404)
|
||||||
|
{
|
||||||
|
return NotFound(response);
|
||||||
|
}
|
||||||
|
else if (response.StatusCode == 401)
|
||||||
|
{
|
||||||
|
return Unauthorized(response);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return BadRequest(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -862,7 +862,7 @@ 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)
|
public async Task<ApiResponse<object>> DeleteContact(Guid id, bool active)
|
||||||
{
|
{
|
||||||
Guid tenantId = _userHelper.GetTenantId();
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
@ -874,7 +874,7 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} tries to delete contact with ID {ContactId} is not found in database", LoggedInEmployee.Id);
|
_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);
|
return ApiResponse<object>.ErrorResponse("Contact not found", "Contact not found", 404);
|
||||||
}
|
}
|
||||||
contact.IsActive = false;
|
contact.IsActive = active;
|
||||||
|
|
||||||
_context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
|
_context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
|
||||||
{
|
{
|
||||||
@ -900,7 +900,15 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
Contact? contact = await _context.Contacts.FirstOrDefaultAsync(c => c.Id == id && c.IsActive && c.TenantId == tenantId);
|
Contact? contact = await _context.Contacts.FirstOrDefaultAsync(c => c.Id == id && c.IsActive && c.TenantId == tenantId);
|
||||||
if (contact != null)
|
if (contact != null)
|
||||||
{
|
{
|
||||||
List<ContactNote>? notes = await _context.ContactNotes.Where(n => n.ContactId == contact.Id && n.IsActive == active).ToListAsync();
|
List<ContactNote> notes = new List<ContactNote>();
|
||||||
|
if (active)
|
||||||
|
{
|
||||||
|
notes = await _context.ContactNotes.Where(n => n.ContactId == contact.Id && n.IsActive && n.TenantId == tenantId).ToListAsync();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
notes = await _context.ContactNotes.Where(n => n.ContactId == contact.Id && n.TenantId == tenantId).ToListAsync();
|
||||||
|
}
|
||||||
List<ContactNoteVM>? noteVMs = new List<ContactNoteVM>();
|
List<ContactNoteVM>? noteVMs = new List<ContactNoteVM>();
|
||||||
foreach (var note in notes)
|
foreach (var note in notes)
|
||||||
{
|
{
|
||||||
@ -973,15 +981,15 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
|
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
|
||||||
return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
|
return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
|
||||||
}
|
}
|
||||||
public async Task<ApiResponse<object>> DeleteContactNote(Guid id)
|
public async Task<ApiResponse<object>> DeleteContactNote(Guid id, bool active)
|
||||||
{
|
{
|
||||||
Guid tenentId = _userHelper.GetTenantId();
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
|
|
||||||
ContactNote? note = await _context.ContactNotes.FirstOrDefaultAsync(n => n.Id == id && n.TenantId == tenentId);
|
ContactNote? note = await _context.ContactNotes.FirstOrDefaultAsync(n => n.Id == id && n.TenantId == tenantId);
|
||||||
if (note != null)
|
if (note != null)
|
||||||
{
|
{
|
||||||
note.IsActive = false;
|
note.IsActive = active;
|
||||||
_context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
|
_context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
|
||||||
{
|
{
|
||||||
RefereanceId = id,
|
RefereanceId = id,
|
||||||
@ -1248,6 +1256,66 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
|
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
|
||||||
return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
|
return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
|
||||||
}
|
}
|
||||||
|
public async Task<ApiResponse<object>> DeleteBucket(Guid id)
|
||||||
|
{
|
||||||
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
|
|
||||||
|
Bucket? bucket = await _context.Buckets.FirstOrDefaultAsync(n => n.Id == id && n.TenantId == tenantId);
|
||||||
|
|
||||||
|
if (bucket != null)
|
||||||
|
{
|
||||||
|
List<EmployeeBucketMapping>? employeeBuckets = await _context.EmployeeBucketMappings.Where(eb => eb.BucketId == id).ToListAsync();
|
||||||
|
List<ContactBucketMapping>? contactBuckets = await _context.ContactBucketMappings.Where(eb => eb.BucketId == id).ToListAsync();
|
||||||
|
|
||||||
|
if (contactBuckets.Any())
|
||||||
|
{
|
||||||
|
_logger.LogInfo("Employee {EmployeeId} attempted to deleted bucket {BucketId},but bucket have contacts in it.", LoggedInEmployee.Id, id);
|
||||||
|
return ApiResponse<object>.ErrorResponse("This bucket can not be deleted", "This bucket can not be deleted", 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
var assignedRoleIds = await _context.EmployeeRoleMappings.Where(r => r.EmployeeId == LoggedInEmployee.Id).Select(r => r.RoleId).ToListAsync();
|
||||||
|
var permissionIds = await _context.RolePermissionMappings.Where(rp => assignedRoleIds.Contains(rp.ApplicationRoleId)).Select(rp => rp.FeaturePermissionId).Distinct().ToListAsync();
|
||||||
|
var bucketIds = employeeBuckets.Where(eb => eb.EmployeeId == LoggedInEmployee.Id).Select(eb => eb.BucketId).ToList();
|
||||||
|
|
||||||
|
Bucket? accessableBucket = null;
|
||||||
|
if (permissionIds.Contains(directoryAdmin))
|
||||||
|
{
|
||||||
|
accessableBucket = bucket;
|
||||||
|
}
|
||||||
|
else if (permissionIds.Contains(directoryManager) && bucketIds.Contains(id))
|
||||||
|
{
|
||||||
|
accessableBucket = bucket;
|
||||||
|
}
|
||||||
|
else if (permissionIds.Contains(directoryUser))
|
||||||
|
{
|
||||||
|
if (bucket.CreatedByID == LoggedInEmployee.Id)
|
||||||
|
{
|
||||||
|
accessableBucket = bucket;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (accessableBucket == null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Employee {EmployeeId} attempted to access bucket {BucketId} without the necessary permissions.", LoggedInEmployee.Id, bucket.Id);
|
||||||
|
return ApiResponse<object>.ErrorResponse("You don't have permission to access this bucket", "You don't have permission to access this bucket", 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
_context.EmployeeBucketMappings.RemoveRange(employeeBuckets);
|
||||||
|
_context.Buckets.Remove(bucket);
|
||||||
|
_context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
|
||||||
|
{
|
||||||
|
RefereanceId = id,
|
||||||
|
UpdatedById = LoggedInEmployee.Id,
|
||||||
|
UpdateAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
_logger.LogInfo("Employee {EmployeeId} deleted bucket {BucketId} and related entries", LoggedInEmployee.Id, id);
|
||||||
|
return ApiResponse<object>.SuccessResponse(new { }, "Bucket deleted successfully", 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogWarning("Employee {EmployeeId} tries to delete bucket {BucketId} but not found in database", LoggedInEmployee.Id, id);
|
||||||
|
return ApiResponse<object>.SuccessResponse(new { }, "Bucket deleted successfully", 200);
|
||||||
|
}
|
||||||
private bool Compare(string sentence, string search)
|
private bool Compare(string sentence, string search)
|
||||||
{
|
{
|
||||||
sentence = sentence.Trim().ToLower();
|
sentence = sentence.Trim().ToLower();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user