Implement API to Assign Bucket to Employees
This commit is contained in:
parent
c1cc8d5d34
commit
049189024a
8
Marco.Pms.Model/Dtos/Directory/AssignBucketDto.cs
Normal file
8
Marco.Pms.Model/Dtos/Directory/AssignBucketDto.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace Marco.Pms.Model.Dtos.Directory
|
||||||
|
{
|
||||||
|
public class AssignBucketDto
|
||||||
|
{
|
||||||
|
public Guid EmployeeId { get; set; }
|
||||||
|
public bool IsActive { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
10
Marco.Pms.Model/ViewModels/Directory/AssignBucketVM.cs
Normal file
10
Marco.Pms.Model/ViewModels/Directory/AssignBucketVM.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1043,15 +1043,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);
|
||||||
}
|
}
|
||||||
@ -1110,7 +1116,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)
|
||||||
@ -1153,13 +1160,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();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user