Esempio n. 1
0
        public async Task <ActionResult <PersonViewModel> > RemoveManagerAsync(Guid projectId, Guid userId)
        {
            if (projectId == Guid.Empty || userId == Guid.Empty)
            {
                BadRequest("No valid Ids received.");
            }

            try
            {
                User user = (await personService.GetUserAsync(userId)).Data;
                if (user == null)
                {
                    return(BadRequest("Could not find user"));
                }

                Manager manager = (await personService.GetManagerAsync(projectId, userId)).Data;
                if (manager == null)
                {
                    return(BadRequest("User is not a manager of this project"));
                }

                PersonViewModel viewModel = PersonViewModel.CreateVmFromUserAndPerson(user, manager.Person,
                                                                                      Extensions.GetInstance(b2CExtentionApplicationId));
                if (viewModel == null)
                {
                    return(BadRequest("Unable to create manager"));
                }

                manager.Person  = null;
                manager.Project = null;

                TaskResult <Manager> result = await personService.RemoveManagerAsync(manager);

                TaskResult <List <Manager> > userManagesOtherProjects =
                    await personService.UserManagesOtherProjectsAsync(manager.PersonId);

                if (userManagesOtherProjects?.Data != null &&
                    userManagesOtherProjects.Data.Count == 0)
                {
                    if (viewModel.UserRole != "Boardmember")
                    {
                        await ModAdminAsync(userId, 4); //remove user as a manager in B2C}
                    }
                }
                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                return(Ok(PersonViewModel.CreateVmFromPerson(manager.Person)));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(RemoveManagerAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }