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

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

            int userInvitationId = await _projectGateway.CheckUserInvitation(model.UserId, model.ProjectId);

            if (userInvitationId == 0)
            {
                return(BadRequest("Invitation not exists"));
            }

            Result resultAssignProject = await _projectGateway.AssignUserToProject(model.UserId, model.ProjectId, (int)EnumProjectAccessRight.Voyeur);

            if (resultAssignProject.ErrorMessage == "The project is already assigned to user")
            {
                return(BadRequest(resultAssignProject.ErrorMessage));
            }

            Result resultDeleteInvitationProject = await _projectGateway.DeleteUserInvitationInProjectByUserInvitedAndProject(model.UserId, 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.ReceiveUserJoinedProject(userInProject.ToList(), Convert.ToString(model.ProjectId), userName, nameProject);

            return(Ok("Assign project success"));
        }