ASC.Data.Reassigns: reassigning data for an array of users

This commit is contained in:
Andrey Savihin 2023-07-12 14:52:03 +03:00
parent 0afeb7ba4e
commit f55cc13913
4 changed files with 41 additions and 30 deletions

View File

@ -103,11 +103,11 @@ public class QueueWorkerReassign : QueueWorker<ReassignProgressItem>
{
}
public ReassignProgressItem Start(int tenantId, Guid fromUserId, Guid toUserId, Guid currentUserId, bool deleteProfile)
public ReassignProgressItem Start(int tenantId, Guid fromUserId, Guid toUserId, Guid currentUserId, bool notify, bool deleteProfile)
{
var result = _serviceProvider.GetService<ReassignProgressItem>();
result.Init(_httpHeaders, tenantId, fromUserId, toUserId, currentUserId, deleteProfile);
result.Init(_httpHeaders, tenantId, fromUserId, toUserId, currentUserId, notify, deleteProfile);
return Start(tenantId, fromUserId, result);
}

View File

@ -39,6 +39,7 @@ public class ReassignProgressItem : DistributedTaskProgress
private IDictionary<string, StringValues> _httpHeaders;
private int _tenantId;
private Guid _currentUserId;
private bool _notify;
private bool _deleteProfile;
public ReassignProgressItem(IServiceScopeFactory serviceScopeFactory)
@ -48,13 +49,14 @@ public class ReassignProgressItem : DistributedTaskProgress
//_projectsReassign = new ProjectsReassign();
}
public void Init(IDictionary<string, StringValues> httpHeaders, int tenantId, Guid fromUserId, Guid toUserId, Guid currentUserId, bool deleteProfile)
public void Init(IDictionary<string, StringValues> httpHeaders, int tenantId, Guid fromUserId, Guid toUserId, Guid currentUserId, bool notify, bool deleteProfile)
{
_httpHeaders = httpHeaders;
_tenantId = tenantId;
FromUser = fromUserId;
ToUser = toUserId;
_currentUserId = currentUserId;
_notify = notify;
_deleteProfile = deleteProfile;
Id = QueueWorkerReassign.GetProgressItemId(tenantId, fromUserId);
Status = DistributedTaskStatus.Created;
@ -145,7 +147,10 @@ public class ReassignProgressItem : DistributedTaskProgress
var fromUser = await userManager.GetUsersAsync(FromUser);
var toUser = await userManager.GetUsersAsync(ToUser);
await studioNotifyService.SendMsgReassignsCompletedAsync(_currentUserId, fromUser, toUser);
if (_notify)
{
await studioNotifyService.SendMsgReassignsCompletedAsync(_currentUserId, fromUser, toUser);
}
var fromUserName = fromUser.DisplayUserName(false, displayUserSettingsHelper);
var toUserName = toUser.DisplayUserName(false, displayUserSettingsHelper);

View File

@ -64,37 +64,43 @@ public class ReassignController : ApiControllerBase
}
[HttpPost("reassign/start")]
public async Task<TaskProgressResponseDto> StartReassignAsync(StartReassignRequestDto inDto)
public async Task<IEnumerable<TaskProgressResponseDto>> StartReassignAsync(StartReassignRequestDto inDto)
{
await _permissionContext.DemandPermissionsAsync(Constants.Action_EditUser);
var toUser = await _userManager.GetUsersAsync(inDto.ToUserId);
var fromUser = await _userManager.GetUsersAsync(inDto.FromUserId);
if (fromUser == null || fromUser.Id == Constants.LostUser.Id)
{
throw new ArgumentException("User with id = " + inDto.FromUserId + " not found");
}
if (fromUser.IsOwner(Tenant) || fromUser.IsMe(_authContext) || fromUser.Status != EmployeeStatus.Terminated)
{
throw new ArgumentException("Can not delete user with id = " + inDto.FromUserId);
}
var toUser = await _userManager.GetUsersAsync(inDto.ToUserId);
if (toUser == null || toUser.Id == Constants.LostUser.Id)
{
throw new ArgumentException("User with id = " + inDto.ToUserId + " not found");
}
if (await _userManager.IsUserAsync(toUser) || toUser.Status == EmployeeStatus.Terminated)
if (_userManager.IsSystemUser(toUser.Id)
|| await _userManager.IsUserAsync(toUser)
|| toUser.Status == EmployeeStatus.Terminated)
{
throw new ArgumentException("Can not reassign data to user with id = " + inDto.ToUserId);
}
var progressItem = _queueWorkerReassign.Start(Tenant.Id, inDto.FromUserId, inDto.ToUserId, _securityContext.CurrentAccount.ID, inDto.DeleteProfile);
}
return TaskProgressResponseDto.Get(progressItem);
foreach (var fromUserId in inDto.FromUserIds)
{
var fromUser = await _userManager.GetUsersAsync(fromUserId);
if (_userManager.IsSystemUser(fromUser.Id)
|| fromUser.IsOwner(Tenant)
|| fromUser.IsMe(_authContext)
|| await _userManager.IsUserAsync(toUser)
|| fromUser.Status != EmployeeStatus.Terminated)
{
throw new ArgumentException("Can not reassign data from user with id = " + fromUserId);
}
}
var notify = inDto.FromUserIds.Count() == 1;
var result = new List<TaskProgressResponseDto>();
foreach (var fromUserId in inDto.FromUserIds)
{
var progressItem = _queueWorkerReassign.Start(Tenant.Id, fromUserId, inDto.ToUserId, _securityContext.CurrentAccount.ID, notify, inDto.DeleteProfile);
result.Add(TaskProgressResponseDto.Get(progressItem));
}
return result;
}
[HttpPut("reassign/terminate")]

View File

@ -28,7 +28,7 @@ namespace ASC.People.ApiModels.RequestDto;
public class StartReassignRequestDto
{
public Guid FromUserId { get; set; }
public IEnumerable<Guid> FromUserIds { get; set; }
public Guid ToUserId { get; set; }
public bool DeleteProfile { get; set; }
}