Ejemplo n.º 1
0
        public static string GetTicketsByCustomerID(RestCommand command, int customerID, bool orderByDateCreated = false)
        {
            TicketsView tickets = new TicketsView(command.LoginUser);
            string      orderBy = orderByDateCreated ? "ot.DateCreated DESC" : "TicketNumber";
            string      xml     = "";

            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(customerID, filters, (int)command.PageNumber, (int)command.PageSize, orderBy);
                    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. GetTicketsByCustomerID(). 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(customerID, orderBy);
                xml = tickets.GetXml("Tickets", "Ticket", true, command.Filters, command.IsPaging);
                xml = AddTagsToTickets(xml, command);
            }

            return(xml);
        }
Ejemplo n.º 2
0
        public static string GetZapierTickets(RestCommand command, int limitNumber)
        {
            string xml = "";

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

            tickets.LoadByOrganizationIDOrderByNumberDESC(command.Organization.OrganizationID, limitNumber);
            foreach (DataRow row in tickets.Table.Rows)
            {
                tickets.WriteXml(writer, row, "Ticket", true, command.Filters);
            }

            xml = Tickets.EndXmlWrite(writer);

            //return tickets.GetXml("Tickets", "Ticket", command.Filters["TicketTypeID"] != null, command.Filters);
            return(xml);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 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);
        }