Esempio n. 1
0
        public HttpResponseMessage RespondInvitation([FromUri] bool accept)
        {
            //Gets the headers
            var            headers = Request.Headers;
            CustomResponse response;

            //if http packet does not include notification Token
            if (!headers.Contains("Actiontoken"))
            {
                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.BadRequest, true, null, ConstantResponse.TEAM_RESPOND_MISSING_ACTIONTOKEN);
                return(Request.CreateResponse <CustomResponse>(HttpStatusCode.BadRequest, response));
            }

            //Notification token
            var actionToken = headers.GetValues("Actiontoken").First();


            //Gets the Receiver from Auth token
            var token = Request.Headers.Authorization.Parameter;

            //Gets the user who sent the request
            string loginUserEmail = UserUtility.GetEmailByToken(token);
            User   loginUser      = _userRepository.GetByEmail(loginUserEmail);


            //Gets the related notification from db by using unique actionToken
            Notification notification = _notificationRepository.getByToken(actionToken);

            //invalid notification check
            if (notification == null || !notification.isActive || notification.receivedBy.id != loginUser.id)
            {
                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.Forbidden, true, null, ConstantResponse.TEAM_RESPOND_INVALID_NOTIFICATION);
                return(Request.CreateResponse <CustomResponse>(HttpStatusCode.Forbidden, response));
            }


            //Rejection check
            if (!accept)
            {
                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.OK, false, ConstantResponse.REJECTED, ConstantResponse.TEAM_RESPOND_REJECTED);
            }
            else
            {
                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.OK, false, ConstantResponse.ACCEPTED, ConstantResponse.TEAM_RESPOND_ACCEPTED);
                //receiver is added to the team where sender is the leader
                _repository.AcceptTeamInvitation(loginUserEmail, notification.sentBy.teamId);
                //Makes notification passive
            }
            _notificationRepository.Update(notification, false);
            return(Request.CreateResponse <CustomResponse>(HttpStatusCode.OK, response));
        }