Exemplo n.º 1
0
        public async Task <IActionResult> UnassignUserToProject([FromBody] UnassignUserToProjectViewModel model)
        {
            if (model.UserId == 0)
            {
                model.UserId = Convert.ToInt32(HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier));
            }

            EnumProjectAccessRight projectAccessRight = await _getAccessUser.GetUserAccessRightProject(Convert.ToInt32(HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)), model.ProjectId);

            if (!HttpContext.User.IsInRole("admin") && !_getAccessUser.UserCookieIs(HttpContext, model.UserId.ToString()) && projectAccessRight != EnumProjectAccessRight.Admin)
            {
                return(StatusCode(403, "Access Denied !"));
            }

            Result resultUnassignUserToProject = await _projectGateway.UnassignUserToProject(model.UserId, model.ProjectId);

            if (resultUnassignUserToProject.ErrorMessage == "User is not in the project")
            {
                return(BadRequest(resultUnassignUserToProject.ErrorMessage));
            }

            int nbOfAdminInProject = await _projectGateway.GetNumberAccessRightInProject(model.ProjectId, (int)EnumProjectAccessRight.Admin);

            if (nbOfAdminInProject <= 0)
            {
                _dGraphGateway.DeleteProject(model.ProjectId);
                await _projectGateway.DeleteProject(model.ProjectId);
            }

            string nameProject = await _getAccessUser.GetProjectName(model.ProjectId);

            string userName = await _getAccessUser.GetUserName(model.UserId);

            IEnumerable <string> userInProject = await _projectGateway.GetIdUserInProject(model.ProjectId);

            await _projectHub.ReceiveUserLeavedProject(userInProject.ToList(), Convert.ToString(model.ProjectId), userName, nameProject);

            return(Ok("Unassign success"));
        }