From f55cc13913f35f1dc00d8116600a226dc718e2e6 Mon Sep 17 00:00:00 2001 From: Andrey Savihin Date: Wed, 12 Jul 2023 14:52:03 +0300 Subject: [PATCH] ASC.Data.Reassigns: reassigning data for an array of users --- common/ASC.Data.Reassigns/QueueWorker.cs | 4 +- .../ReassignProgressItem.cs | 9 ++- .../Server/Api/ReassignController.cs | 56 ++++++++++--------- .../RequestDto/StartReassignRequestDto.cs | 2 +- 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/common/ASC.Data.Reassigns/QueueWorker.cs b/common/ASC.Data.Reassigns/QueueWorker.cs index 7e8838dd3c..e6e8877fc4 100644 --- a/common/ASC.Data.Reassigns/QueueWorker.cs +++ b/common/ASC.Data.Reassigns/QueueWorker.cs @@ -103,11 +103,11 @@ public class QueueWorkerReassign : QueueWorker { } - 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(); - result.Init(_httpHeaders, tenantId, fromUserId, toUserId, currentUserId, deleteProfile); + result.Init(_httpHeaders, tenantId, fromUserId, toUserId, currentUserId, notify, deleteProfile); return Start(tenantId, fromUserId, result); } diff --git a/common/ASC.Data.Reassigns/ReassignProgressItem.cs b/common/ASC.Data.Reassigns/ReassignProgressItem.cs index 2afaeda0f7..103cc76a62 100644 --- a/common/ASC.Data.Reassigns/ReassignProgressItem.cs +++ b/common/ASC.Data.Reassigns/ReassignProgressItem.cs @@ -39,6 +39,7 @@ public class ReassignProgressItem : DistributedTaskProgress private IDictionary _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 httpHeaders, int tenantId, Guid fromUserId, Guid toUserId, Guid currentUserId, bool deleteProfile) + public void Init(IDictionary 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); diff --git a/products/ASC.People/Server/Api/ReassignController.cs b/products/ASC.People/Server/Api/ReassignController.cs index e95cc08487..bf90d590f4 100644 --- a/products/ASC.People/Server/Api/ReassignController.cs +++ b/products/ASC.People/Server/Api/ReassignController.cs @@ -64,37 +64,43 @@ public class ReassignController : ApiControllerBase } [HttpPost("reassign/start")] - public async Task StartReassignAsync(StartReassignRequestDto inDto) + public async Task> 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(); + + 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")] diff --git a/products/ASC.People/Server/ApiModels/RequestDto/StartReassignRequestDto.cs b/products/ASC.People/Server/ApiModels/RequestDto/StartReassignRequestDto.cs index 94e34adcc7..bbb237f787 100644 --- a/products/ASC.People/Server/ApiModels/RequestDto/StartReassignRequestDto.cs +++ b/products/ASC.People/Server/ApiModels/RequestDto/StartReassignRequestDto.cs @@ -28,7 +28,7 @@ namespace ASC.People.ApiModels.RequestDto; public class StartReassignRequestDto { - public Guid FromUserId { get; set; } + public IEnumerable FromUserIds { get; set; } public Guid ToUserId { get; set; } public bool DeleteProfile { get; set; } }