Merge pull request 'Implement API to Assign Bucket to Employees' (#76) from Ashutosh_Task#387_Assign_Bucket into Feature_Directory

Reviewed-on: #76
This commit is contained in:
ashutosh.nehete 2025-05-27 11:09:51 +00:00
commit 6036415ef4
5 changed files with 162 additions and 5 deletions

View File

@ -0,0 +1,8 @@
namespace Marco.Pms.Model.Dtos.Directory
{
public class AssignBucketDto
{
public Guid EmployeeId { get; set; }
public bool IsActive { get; set; }
}
}

View File

@ -202,6 +202,15 @@ namespace Marco.Pms.Model.Mapper
Description = bucket.Description Description = bucket.Description
}; };
} }
public static AssignBucketVM ToAssignBucketVMFromBucket(this Bucket bucket)
{
return new AssignBucketVM
{
Id = bucket.Id,
Name = bucket.Name,
Description = bucket.Description
};
}
//Contact Note //Contact Note
public static ContactNote ToContactNoteFromCreateContactNoteDto(this CreateContactNoteDto noteDto, Guid tenantId, Guid employeeId) public static ContactNote ToContactNoteFromCreateContactNoteDto(this CreateContactNoteDto noteDto, Guid tenantId, Guid employeeId)

View File

@ -0,0 +1,10 @@
namespace Marco.Pms.Model.ViewModels.Directory
{
public class AssignBucketVM
{
public Guid Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public List<Guid>? EmployeeIds { get; set; }
}
}

View File

@ -177,7 +177,7 @@ namespace Marco.Pms.Services.Controllers
} }
} }
[HttpGet("note/{ContactId}")] [HttpGet("notes/{ContactId}")]
public async Task<IActionResult> GetNoteListByContactId(Guid contactId, [FromQuery] bool active = true) public async Task<IActionResult> GetNoteListByContactId(Guid contactId, [FromQuery] bool active = true)
{ {
var response = await _directoryHelper.GetNoteListByContactId(contactId, active); var response = await _directoryHelper.GetNoteListByContactId(contactId, active);
@ -293,5 +293,27 @@ namespace Marco.Pms.Services.Controllers
return BadRequest(response); return BadRequest(response);
} }
} }
[HttpPost("assign-bucket/{bucketId}")]
public async Task<IActionResult> AssignBucket(Guid bucketId, [FromBody] List<AssignBucketDto> assignBuckets)
{
var response = await _directoryHelper.AssignBucket(bucketId, assignBuckets);
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);
}
}
} }
} }

View File

@ -1023,15 +1023,21 @@ namespace Marco.Pms.Services.Helpers
return ApiResponse<object>.ErrorResponse("You don't have permission", "You don't have permission", 401); return ApiResponse<object>.ErrorResponse("You don't have permission", "You don't have permission", 401);
} }
List<BucketVM> bucketVMs = new List<BucketVM>(); List<AssignBucketVM> bucketVMs = new List<AssignBucketVM>();
if (bucketList.Any()) if (bucketList.Any())
{ {
foreach (var bucket in bucketList) foreach (var bucket in bucketList)
{ {
BucketVM bucketVM = bucket.ToBucketVMFromBucket(); List<EmployeeBucketMapping> employeeBucketMappings = employeeBuckets.Where(eb => eb.BucketId == bucket.Id).ToList();
var emplyeeIds = employeeBucketMappings.Select(eb => eb.EmployeeId).ToList();
AssignBucketVM bucketVM = bucket.ToAssignBucketVMFromBucket();
bucketVM.EmployeeIds = emplyeeIds;
bucketVMs.Add(bucketVM); bucketVMs.Add(bucketVM);
} }
} }
_logger.LogInfo("{count} Buckets are fetched by Employee with ID {LoggedInEmployeeId}", bucketVMs.Count, LoggedInEmployee.Id); _logger.LogInfo("{count} Buckets are fetched by Employee with ID {LoggedInEmployeeId}", bucketVMs.Count, LoggedInEmployee.Id);
return ApiResponse<object>.SuccessResponse(bucketVMs, $"{bucketVMs.Count} buckets fetched successfully", 200); return ApiResponse<object>.SuccessResponse(bucketVMs, $"{bucketVMs.Count} buckets fetched successfully", 200);
} }
@ -1090,7 +1096,8 @@ namespace Marco.Pms.Services.Helpers
{ {
var assignedRoleIds = await _context.EmployeeRoleMappings.Where(r => r.EmployeeId == LoggedInEmployee.Id).Select(r => r.RoleId).ToListAsync(); 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 permissionIds = await _context.RolePermissionMappings.Where(rp => assignedRoleIds.Contains(rp.ApplicationRoleId)).Select(rp => rp.FeaturePermissionId).Distinct().ToListAsync();
var bucketIds = await _context.EmployeeBucketMappings.Where(eb => eb.EmployeeId == LoggedInEmployee.Id).Select(eb => eb.BucketId).ToListAsync(); var employeeBuckets = await _context.EmployeeBucketMappings.Where(eb => eb.BucketId == id).ToListAsync();
var bucketIds = employeeBuckets.Where(eb => eb.EmployeeId == LoggedInEmployee.Id).Select(eb => eb.BucketId).ToList();
Bucket? bucket = await _context.Buckets.FirstOrDefaultAsync(b => b.Id == bucketDto.Id && b.TenantId == tenantId); Bucket? bucket = await _context.Buckets.FirstOrDefaultAsync(b => b.Id == bucketDto.Id && b.TenantId == tenantId);
if (bucket == null) if (bucket == null)
@ -1133,13 +1140,114 @@ namespace Marco.Pms.Services.Helpers
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
BucketVM bucketVM = bucket.ToBucketVMFromBucket(); AssignBucketVM bucketVM = bucket.ToAssignBucketVMFromBucket();
List<EmployeeBucketMapping> employeeBucketMappings = employeeBuckets.Where(eb => eb.BucketId == bucket.Id).ToList();
var employeeIds = employeeBucketMappings.Select(eb => eb.EmployeeId).ToList();
bucketVM.EmployeeIds = employeeIds;
_logger.LogInfo("Employee Id {LoggedInEmployeeId} Updated new bucket {BucketId}", LoggedInEmployee.Id, bucket.Id); _logger.LogInfo("Employee Id {LoggedInEmployeeId} Updated new bucket {BucketId}", LoggedInEmployee.Id, bucket.Id);
return ApiResponse<object>.SuccessResponse(bucketVM, "Bucket update successFully", 200); return ApiResponse<object>.SuccessResponse(bucketVM, "Bucket update successFully", 200);
} }
_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>> AssignBucket(Guid bucketId, List<AssignBucketDto> assignBuckets)
{
Guid tenantId = _userHelper.GetTenantId();
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
if (assignBuckets != null && bucketId != Guid.Empty)
{
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();
Bucket? bucket = await _context.Buckets.FirstOrDefaultAsync(b => b.Id == bucketId && b.TenantId == tenantId);
if (bucket == null)
{
_logger.LogWarning("Employee ID {LoggedInEmployeeId} attempted to update a bucket but not found in database.", LoggedInEmployee.Id);
return ApiResponse<object>.ErrorResponse("Bucket not found", "Bucket not found", 404);
}
var employeeBuckets = await _context.EmployeeBucketMappings.Where(eb => eb.BucketId == bucketId).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(bucketId))
{
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);
}
var employeeIds = await _context.Employees.Where(e => e.TenantId == tenantId && e.IsActive).Select(e => e.Id).ToListAsync();
int assignedEmployee = 0;
int removededEmployee = 0;
foreach (var assignBucket in assignBuckets)
{
if (employeeIds.Contains(assignBucket.EmployeeId))
{
if (assignBucket.IsActive)
{
EmployeeBucketMapping employeeBucketMapping = new EmployeeBucketMapping
{
EmployeeId = assignBucket.EmployeeId,
BucketId = bucketId
};
_context.EmployeeBucketMappings.Add(employeeBucketMapping);
assignedEmployee += 1;
}
else
{
EmployeeBucketMapping? employeeBucketMapping = employeeBuckets.FirstOrDefault(eb => eb.BucketId == bucketId && eb.EmployeeId == assignBucket.EmployeeId);
if (employeeBucketMapping != null)
{
_context.EmployeeBucketMappings.Remove(employeeBucketMapping);
removededEmployee += 1;
}
}
}
}
_context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
{
RefereanceId = bucketId,
UpdatedById = LoggedInEmployee.Id,
UpdateAt = DateTime.UtcNow
});
await _context.SaveChangesAsync();
AssignBucketVM bucketVM = bucket.ToAssignBucketVMFromBucket();
List<EmployeeBucketMapping> employeeBucketMappings = await _context.EmployeeBucketMappings.Where(eb => eb.BucketId == bucket.Id).ToListAsync();
employeeIds = employeeBucketMappings.Select(eb => eb.EmployeeId).ToList();
bucketVM.EmployeeIds = employeeIds;
if (assignedEmployee > 0)
{
_logger.LogInfo("Employee {EmployeeId} assigned bucket {BucketId} to {conut} number of employees", LoggedInEmployee.Id, bucketId, assignedEmployee);
}
if (removededEmployee > 0)
{
_logger.LogError("Employee {EmployeeId} removed {conut} number of employees from bucket {BucketId}", LoggedInEmployee.Id, removededEmployee, bucketId);
}
return ApiResponse<object>.SuccessResponse(bucketVM, "Details updated successfully", 200);
}
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
}
private bool Compare(string sentence, string search) private bool Compare(string sentence, string search)
{ {
sentence = sentence.Trim().ToLower(); sentence = sentence.Trim().ToLower();