Пример #1
0
        public async Task <IActionResult> CloseTicket(int id = -1, string reference = null, string system = null)
        {
            // Get user name who wants to get info
            var re          = Request;
            var headers     = re.Headers;
            var tokenString = headers["Authorization"];

            var handler = new JwtSecurityTokenHandler();
            var token   = handler.ReadJwtToken(tokenString[0].Split(' ')[1]);

            var claims        = token.Claims;
            var usernameClaim = claims.Where(x => x.Type == ClaimTypes.Name).FirstOrDefault();
            var user          = await _dbHandler.GetUserAsync(usernameClaim.Value);

            if (user == null)
            {
                return(BadRequest(new GeneralMessage()
                {
                    Message = "User does not exist"
                }));
            }

            // Close the ticket
            if (id != -1)
            {
                // Get ticket, return with BadRequest of requestor can't close the ticket
                var ticket = await _dbHandler.GetTicketAsync(id);

                if (ticket == null)
                {
                    return(BadRequest(new GeneralMessage()
                    {
                        Message = "Ticket does not exist"
                    }));
                }

                if (ticket.UserId != user.Id && user.Role != UserRole.Admin)
                {
                    return(BadRequest(new GeneralMessage()
                    {
                        Message = "Only Admin or ticket owner can close ticket"
                    }));
                }

                // Close the ticket
                var respond = await _dbHandler.CloseTicketAsync(id, user);

                if (respond.MessageType == MessageType.NOK)
                {
                    return(BadRequest(new GeneralMessage()
                    {
                        Message = respond.MessageText
                    }));
                }

                return(Ok(new GeneralMessage()
                {
                    Message = respond.MessageText
                }));
            }
            else if (reference != null && system != null)
            {
                // Get ticket, return with BadRequest of requestor can't close the ticket
                var ticketList = await _dbHandler.GetTicketAsync(reference, system);

                if (ticketList == null)
                {
                    return(BadRequest(new GeneralMessage()
                    {
                        Message = "Ticket does not exist"
                    }));
                }

                var ticket = ticketList.FirstOrDefault(s => s.Status.Equals("Open"));
                if (ticket == null)
                {
                    return(BadRequest(new GeneralMessage()
                    {
                        Message = "Ticket does not exist"
                    }));
                }

                if (ticket.UserId != user.Id && user.Role != UserRole.Admin)
                {
                    return(BadRequest(new GeneralMessage()
                    {
                        Message = "Only Admin or ticket owner can close ticket"
                    }));
                }

                // Close the ticket
                var respond = await _dbHandler.CloseTicketAsync(ticket.Id, user);

                if (respond.MessageType == MessageType.NOK)
                {
                    return(BadRequest(new GeneralMessage()
                    {
                        Message = respond.MessageText
                    }));
                }

                return(Ok(new GeneralMessage()
                {
                    Message = respond.MessageText
                }));
            }
            else
            {
                return(BadRequest(new GeneralMessage()
                {
                    Message = "Invalid input parameter. Specify ID or Reference value and system name"
                }));
            }
        }