Example #1
0
        public async Task <IActionResult> SendInvitationProject([FromBody] SendInvitationProjectViewModel model)
        {
            if (model.UserAuthorId == 0)
            {
                model.UserAuthorId = Convert.ToInt32(HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier));
            }

            if (model.UserAuthorId == model.UserInvitedId)
            {
                return(BadRequest("User author cannot invite himself"));
            }
            int userInProject = await _projectGateway.CheckUserInProject(model.UserInvitedId, model.ProjectId);

            if (userInProject != 0)
            {
                return(BadRequest("User invited already member of this project"));
            }

            if (!HttpContext.User.IsInRole("admin"))
            {
                if (!_getAccessUser.UserCookieIs(HttpContext, model.UserAuthorId.ToString()))
                {
                    return(StatusCode(403, "Access Denied !"));
                }
                EnumProjectAccessRight projectAccessRight = await _getAccessUser.GetUserAccessRightProject(Convert.ToInt32(HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)), model.ProjectId);

                if (projectAccessRight != EnumProjectAccessRight.Admin)
                {
                    return(StatusCode(403, "Access Denied !"));
                }
            }

            Result resultSendInvitationProject = await _projectGateway.CreateUserInvitationInProject(model.UserAuthorId, model.UserInvitedId, model.ProjectId);

            if (resultSendInvitationProject.ErrorMessage == "An invitation made by the user with this project and user invited already exist")
            {
                return(BadRequest(resultSendInvitationProject.ErrorMessage));
            }

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

            await _projectHub.ReceiveInvitationInProject(Convert.ToString(model.UserInvitedId), Convert.ToString(model.UserAuthorId), Convert.ToString(model.ProjectId), HttpContext.User.Identity.Name, nameProject);

            return(Ok("The invitation has been sent"));
        }