Beispiel #1
0
        public static string GetCustomerTickets(RestCommand command)
        {
            TicketsView tickets = new TicketsView(command.LoginUser);
            string      xml     = "";

            if (command.Filters["TicketTypeID"] != null)
            {
                try
                {
                    int        ticketTypeID = int.Parse(command.Filters["TicketTypeID"]);
                    TicketType ticketType   = TicketTypes.GetTicketType(command.LoginUser, ticketTypeID);
                    if (ticketType.OrganizationID != command.Organization.ParentID)
                    {
                        throw new Exception();
                    }
                    tickets.LoadByCustomerTicketTypeID(command.Organization.OrganizationID, ticketTypeID);
                }
                catch (Exception ex)
                {
                    throw new RestException(HttpStatusCode.NotAcceptable, ex.Message);
                    throw new RestException(HttpStatusCode.NotAcceptable, "Invalid TicketTypeID to filter.", ex);
                }
            }
            else
            {
                if (command.IsPaging)
                {
                    try
                    {
                        //remove Paging parameters
                        NameValueCollection filters = new NameValueCollection();

                        foreach (string key in command.Filters.AllKeys)
                        {
                            if (key.ToLower() != "pagenumber" && key.ToLower() != "pagesize")
                            {
                                filters.Add(key, command.Filters[key]);
                            }
                        }

                        tickets.LoadByCustomerID(command.Organization.OrganizationID, command.Filters, (int)command.PageNumber, (int)command.PageSize);

                        XmlTextWriter writer = Tickets.BeginXmlWrite("Tickets");

                        foreach (DataRow row in tickets.Table.Rows)
                        {
                            int  ticketId = (int)row["TicketID"];
                            Tags tags     = new Tags(command.LoginUser);
                            tags.LoadByReference(ReferenceType.Tickets, ticketId);
                            tags = tags ?? new Tags(command.LoginUser);
                            tickets.WriteXml(writer, row, "Ticket", true, new NameValueCollection(), tags);
                        }

                        int totalRecords = 0;

                        if (tickets.Count > 0)
                        {
                            totalRecords = tickets[0].TotalRecords;
                        }

                        writer.WriteElementString("TotalRecords", totalRecords.ToString());
                        xml = Tickets.EndXmlWrite(writer);
                    }
                    catch (Exception ex)
                    {
                        ExceptionLogs.LogException(command.LoginUser, ex, "API", "RestTickets. GetCustomerTickets(). Paging. SQL filtering generation failed.");
                        throw new RestException(HttpStatusCode.InternalServerError, "There was an error processing your request. Please contact TeamSupport.com", ex);
                    }
                }
                else
                {
                    tickets.LoadByCustomerID(command.Organization.OrganizationID);
                    xml = tickets.GetXml("Tickets", "Ticket", true, command.Filters, command.IsPaging);
                    xml = AddTagsToTickets(xml, command, true);
                }
            }

            return(xml);
        }
Beispiel #2
0
        /// <summary>
        /// Update the Ticket related fields that live in their own table.
        /// </summary>
        /// <param name="command">Command received in the request to read and process the data in the request body.</param>
        /// <param name="ticketId">TicketId to update its record.</param>
        private static void UpdateFieldsOfSeparateTable(RestCommand command, Ticket ticket, bool isCustomerTicket = false)
        {
            try
            {
                //Add as necessary to the list and then to the switch-case below for the work to update it.
                List <string> fields = new List <string>()
                {
                    "jirakey", "tags"
                };

                foreach (string field in fields.Select(p => p.ToLower()).ToList())
                {
                    XmlNode node = GetNode(command, field);

                    if (node != null)
                    {
                        switch (field)
                        {
                        case "jirakey":
                            string           jiraKey          = node.InnerText;
                            TicketLinkToJira ticketLinkToJira = new TicketLinkToJira(command.LoginUser);
                            ticketLinkToJira.LoadByTicketID(ticket.TicketID);
                            int?crmLinkId = null;

                            //Next line and 2 If statements are the same as in \webapp\app_code\ticketservice.cs SetSyncWithJira(). Might need to consider making a common funcion for both
                            crmLinkId = CRMLinkTable.GetIdBy(ticket.OrganizationID, IntegrationType.Jira.ToString().ToLower(), ticket.ProductID, command.LoginUser);

                            //If product is not associated to an instance then get the 'default' instance
                            if (crmLinkId == null || crmLinkId <= 0)
                            {
                                CRMLinkTable crmlink = new CRMLinkTable(command.LoginUser);
                                crmlink.LoadByOrganizationID(ticket.OrganizationID);

                                crmLinkId = crmlink.Where(p => p.InstanceName == "Default" &&
                                                          p.CRMType.ToLower() == IntegrationType.Jira.ToString().ToLower())
                                            .Select(p => p.CRMLinkID).FirstOrDefault();
                            }

                            if (ticketLinkToJira != null && ticketLinkToJira.Any())
                            {
                                string oldJiraKey = ticketLinkToJira[0].JiraKey;
                                ticketLinkToJira[0].JiraKey   = jiraKey.ToLower() == "newjiraissue" ? null : jiraKey;
                                ticketLinkToJira[0].CrmLinkID = crmLinkId;
                                ticketLinkToJira.Save();
                                ActionLogs.AddActionLog(command.LoginUser, ActionLogType.Update, ReferenceType.Tickets, ticket.TicketID, string.Format("Changed JiraKey from '{0}' to '{1}'.", oldJiraKey, jiraKey));
                            }
                            else
                            {
                                TicketLinkToJiraItem newJiraLink = ticketLinkToJira.AddNewTicketLinkToJiraItem();
                                newJiraLink.TicketID     = ticket.TicketID;
                                newJiraLink.SyncWithJira = true;
                                newJiraLink.JiraID       = null;
                                newJiraLink.JiraKey      = jiraKey.ToLower() == "newjiraissue" ? null : jiraKey;
                                newJiraLink.JiraLinkURL  = null;
                                newJiraLink.JiraStatus   = null;
                                newJiraLink.CreatorID    = command.LoginUser.UserID;
                                newJiraLink.CrmLinkID    = crmLinkId;

                                if (newJiraLink.CrmLinkID != null && newJiraLink.CrmLinkID > 0)
                                {
                                    newJiraLink.Collection.Save();
                                    ActionLogs.AddActionLog(command.LoginUser, ActionLogType.Update, ReferenceType.Tickets, ticket.TicketID, string.Format("Linked to JiraKey '{0}'.", jiraKey));
                                }
                            }
                            break;

                        case "tags":
                            TagLinks currentTagLinks = new TagLinks(command.LoginUser);
                            currentTagLinks.LoadByReference(ReferenceType.Tickets, ticket.TicketID);
                            XmlNodeList nodeList = node.ChildNodes;
                            List <int>  newTags  = new List <int>();

                            foreach (XmlNode tagNode in nodeList)
                            {
                                string tagValue = tagNode["Value"].InnerText;

                                Tag newTag = Tags.GetTag(command.LoginUser, tagValue);

                                if (newTag == null)
                                {
                                    Tags tag = new Tags(command.LoginUser);
                                    newTag = tag.AddNewTag();
                                    newTag.OrganizationID = isCustomerTicket ? command.Organization.ParentID ?? command.Organization.OrganizationID : command.Organization.OrganizationID;
                                    newTag.Value          = tagValue;
                                    tag.Save();
                                }

                                newTags.Add(newTag.TagID);
                            }

                            foreach (int tag in newTags)
                            {
                                TagLink newTagLink = currentTagLinks.Where(p => p.TagID == tag && p.RefID == ticket.TicketID).SingleOrDefault();
                                if (newTagLink == null)
                                {
                                    TagLinks tagLink = new TagLinks(command.LoginUser);
                                    newTagLink         = tagLink.AddNewTagLink();
                                    newTagLink.TagID   = tag;
                                    newTagLink.RefType = ReferenceType.Tickets;
                                    newTagLink.RefID   = ticket.TicketID;
                                    tagLink.Save();
                                }
                            }

                            List <TagLink> deleteTagLinks = currentTagLinks.Where(p => !newTags.Contains(p.TagID)).ToList();

                            foreach (TagLink deleteTagLink in deleteTagLinks)
                            {
                                deleteTagLink.Delete();
                                deleteTagLink.Collection.Save();
                            }

                            break;

                        default:
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionLogs.LogException(command.LoginUser, ex, "API", string.Format("OrgID: {0}{1}Verb: {2}{1}Url: {3}{1}Body: {4}", command.Organization.OrganizationID, Environment.NewLine, command.Method, command.Method, command.Data));
            }
        }
Beispiel #3
0
        public static string GetTicketAssignments(RestCommand command, bool isPost = false)
        {
            string  ticketIdFilter     = "ticketids";
            string  ticketNumberFilter = "ticketnumbers";
            string  xml    = "";
            bool    byId   = false;
            dynamic errors = new System.Dynamic.ExpandoObject();

            if (!isPost && !command.Filters.AllKeys.Where(p => p.ToLower() == "ticketids").Any() && !command.Filters.AllKeys.Where(p => p.ToLower() == "ticketnumbers").Any())
            {
                errors.Error = "This call requires a filter. TicketIds or TicketNumbers should be used to filter the results.";
                throw new RestException(HttpStatusCode.BadRequest, JsonConvert.SerializeObject(errors));
            }
            else if (isPost && string.IsNullOrEmpty(command.Data))
            {
                errors.Error = "This call requires a filter in the POST body. TicketIds or TicketNumbers should be used to filter the results.";
                throw new RestException(HttpStatusCode.BadRequest, JsonConvert.SerializeObject(errors));
            }

            string tickets = string.Empty;

            if (isPost)
            {
                System.Xml.Linq.XDocument xmlDoc = System.Xml.Linq.XDocument.Parse(command.Data.ToLower());
                string  jsonText = JsonConvert.SerializeXNode(xmlDoc);
                dynamic filters  = JsonConvert.DeserializeObject <System.Dynamic.ExpandoObject>(jsonText);

                if (((IDictionary <String, object>)filters).ContainsKey(ticketIdFilter))
                {
                    tickets = filters.ticketids;
                    byId    = true;
                }
                else if (((IDictionary <String, object>)filters).ContainsKey(ticketNumberFilter))
                {
                    tickets = filters.ticketnumbers;
                }
            }
            else
            {
                foreach (string key in command.Filters)
                {
                    if (key.ToLower().Trim() == ticketIdFilter)
                    {
                        tickets = command.Filters[key.ToLower().Trim()];
                        byId    = true;
                        break;
                    }

                    if (key.ToLower().Trim() == ticketNumberFilter)
                    {
                        tickets = command.Filters[key.ToLower().Trim()];
                        break;
                    }
                }
            }

            TicketUserAssignment userTicketAssignments = new TicketUserAssignment(command.LoginUser);

            userTicketAssignments.LoadByTicketList(command.Organization.OrganizationID, tickets, byId);

            int pageNumber = command.IsPaging && command.PageNumber != null ? (int)--command.PageNumber : 0;
            int pageSize   = command.IsPaging && command.PageSize != null ? (int)command.PageSize : userTicketAssignments.History.Select(p => p.TicketID).Distinct().Count();

            TicketUserAssignment.TicketUserAssignmentHistory assignments = userTicketAssignments.GetTicketUserAssignmentHistory(pageNumber, pageSize);

            string jsonResult = JsonConvert.SerializeObject(assignments);
            var    doc        = JsonConvert.DeserializeXNode(jsonResult, "TicketAssignments");

            xml = doc.ToString();

            return(xml);
        }
Beispiel #4
0
        public static string GetTickets(RestCommand command)
        {
            string xml             = "";
            bool   hasBeenFiltered = false;
            int    totalRecords    = 0;

            if (command.IsPaging)
            {
                try
                {
                    TicketsView tickets = new TicketsView(command.LoginUser);
                    tickets.LoadAllTicketIds(command.Organization.OrganizationID, command.Filters, command.PageNumber, command.PageSize);
                    hasBeenFiltered = true;
                    XmlTextWriter writer = Tickets.BeginXmlWrite("Tickets");

                    foreach (int ticketTypeId in tickets.GroupBy(g => g.TicketTypeID).Select(p => p.Key).ToList())
                    {
                        try
                        {
                            TicketsView ticketsResult = new TicketsView(command.LoginUser);
                            ticketsResult.LoadByTicketIDList(command.Organization.OrganizationID, ticketTypeId, tickets.Where(w => w.TicketTypeID == ticketTypeId).Select(p => p.TicketID).ToList());

                            foreach (DataRow row in ticketsResult.Table.Rows)
                            {
                                int  ticketId = (int)row["TicketID"];
                                Tags tags     = new Tags(command.LoginUser);
                                tags.LoadByReference(ReferenceType.Tickets, ticketId);
                                tags = tags ?? new Tags(command.LoginUser);
                                ticketsResult.WriteXml(writer, row, "Ticket", true, !hasBeenFiltered ? command.Filters : new System.Collections.Specialized.NameValueCollection(), tags);
                            }
                        }
                        catch (Exception ex)
                        {
                            ExceptionLogs.LogException(command.LoginUser, ex, "API", "RestTickets. GetTickets(). Paging.");
                        }
                    }

                    if (tickets.Count > 0)
                    {
                        totalRecords = tickets[0].TotalRecords;
                    }

                    writer.WriteElementString("TotalRecords", totalRecords.ToString());
                    xml = Tickets.EndXmlWrite(writer);
                }
                catch (Exception ex)
                {
                    ExceptionLogs.LogException(command.LoginUser, ex, "API", "RestTickets. GetTickets(). Paging. SQL filtering generation failed.");
                    throw new RestException(HttpStatusCode.InternalServerError, "There was an error processing your request. Please contact TeamSupport.com", ex);
                }
            }
            else
            {
                //No Paging
                if (command.Filters["TicketTypeID"] != null)
                {
                    try
                    {
                        TicketsView tickets      = new TicketsView(command.LoginUser);
                        int         ticketTypeID = int.Parse(command.Filters["TicketTypeID"]);
                        TicketType  ticketType   = TicketTypes.GetTicketType(command.LoginUser, ticketTypeID);
                        if (ticketType.OrganizationID != command.Organization.OrganizationID)
                        {
                            throw new Exception();
                        }

                        try
                        {
                            tickets.LoadByTicketTypeID(ticketTypeID, command.Organization.OrganizationID, command.Filters);
                        }
                        catch (Exception ex)
                        {
                            //if something fails use the old method
                            tickets.LoadByTicketTypeID(ticketTypeID);
                            ExceptionLogs.LogException(command.LoginUser, ex, "API", "RestTickets. GetTickets(). No Paging. SQL filtering generation failed, fell into old method.");
                        }

                        xml = tickets.GetXml("Tickets", "Ticket", true, command.Filters);
                        xml = AddTagsToTickets(xml, command);
                    }
                    catch (Exception ex)
                    {
                        throw new RestException(HttpStatusCode.NotAcceptable, "Invalid TicketTypeID to filter.", ex);
                    }
                }
                else
                {
                    TicketTypes ticketTypes = new TicketTypes(command.LoginUser);
                    ticketTypes.LoadByOrganizationID(command.Organization.OrganizationID);

                    TicketsView   tickets = new TicketsView(command.LoginUser);
                    XmlTextWriter writer  = Tickets.BeginXmlWrite("Tickets");

                    foreach (TicketType ticketType in ticketTypes)
                    {
                        try
                        {
                            tickets.LoadByTicketTypeID(ticketType.TicketTypeID, command.Organization.OrganizationID, command.Filters);
                        }
                        catch (Exception ex)
                        {
                            if (ex is System.Data.SqlClient.SqlException && ex.Message.ToLower().Contains("variable names must be unique within a query batch or stored procedure"))
                            {
                                throw ex;
                            }
                            else
                            {
                                //if something fails use the old method
                                tickets.LoadByTicketTypeID(ticketType.TicketTypeID);
                                ExceptionLogs.LogException(command.LoginUser, ex, "API", "RestTickets. GetTickets(). No Paging. No TicketTypeId filter. SQL filtering generation failed, fell into old method.");
                            }
                        }

                        foreach (DataRow row in tickets.Table.Rows)
                        {
                            int  ticketId = (int)row["TicketID"];
                            Tags tags     = new Tags(command.LoginUser);
                            tags.LoadByReference(ReferenceType.Tickets, ticketId);
                            tags = tags ?? new Tags(command.LoginUser);
                            tickets.WriteXml(writer, row, "Ticket", true, command.Filters, tags);
                        }
                    }

                    xml = Tickets.EndXmlWrite(writer);
                }
            }

            return(xml);
        }
 public RestTicketsView(RestCommand command, RestTicketType restTicketType)
 {
     _restTicketType = restTicketType;
     _command        = command;
 }
Beispiel #6
0
 public RestProcessor(RestCommand command)
 {
     _command = command;
 }
 public RestTicketsViewItem(RestCommand command, string ticketID)
 {
     _ticketID = int.Parse(ticketID);
     _command  = command;
 }
        public static void WriteTicketsViewItemXml(RestCommand command, XmlWriter writer, int ticketID, CustomFields customFields)
        {
            TicketsViewItem ticket = TicketsView.GetTicketsViewItem(command.LoginUser, ticketID);

            WriteTicketsViewItemXml(command, writer, ticket, customFields);
        }
 // add a layer to get at Attachments.GetXML
 public static string GetAttachmentsAsXML(RestCommand _command, int attachmentID)
 {
     return(TeamSupport.Data.Quarantine.RestAttachmentsQ.GetAttachmentsAsXML(_command, attachmentID));
 }
 public static string DeleteAttachment(RestCommand command, int assetID, int attachmentID)
 {
     return(TeamSupport.Data.Quarantine.RestAttachmentsQ.DeleteAttachment(command, assetID, attachmentID));
 }
 public static string GetAttachmentsByAssetID(RestCommand command, int assetID, bool orderByDateCreated = false)
 {
     return(TeamSupport.Data.Quarantine.RestAttachmentsQ.GetAttachmentsByAssetID(command, assetID, orderByDateCreated));
 }
 public static string CreateAttachment(RestCommand command, int ticketIDOrNumber, int actionID)
 {
     return(TeamSupport.Data.Quarantine.RestAttachmentsQ.CreateAttachment(command, ticketIDOrNumber, actionID));
 }
Beispiel #13
0
 public static string GetKBStat(RestCommand command, int kBViewID)
 {
   KBStat kBStat = KBStats.GetKBStat(command.LoginUser, kBViewID);
   if (kBStat.OrganizationID != command.Organization.OrganizationID) throw new RestException(HttpStatusCode.Unauthorized);
   return kBStat.GetXml("KBStat", true);
 }