public static string GetSlaNotification(RestCommand command, int ticketID)
        {
            SlaNotification slaNotification = SlaNotifications.GetSlaNotification(command.LoginUser, ticketID);

            if (slaNotification.OrganizationID != command.Organization.OrganizationID)
            {
                throw new RestException(HttpStatusCode.Unauthorized);
            }
            return(slaNotification.GetXml("SlaNotification", true));
        }
Exemplo n.º 2
0
        private void NotifyViolation(int ticketID, bool useUser, bool useGroup, bool isWarning, SlaViolationType slaViolationType, SlaNotification notification, int?triggerId)
        {
            Users users = new Users(LoginUser);
            User  user  = null;

            Logs.WriteLine();
            Logs.WriteEvent("***** Processing TicketID: " + ticketID.ToString());
            TicketsViewItem ticket = TicketsView.GetTicketsViewItem(LoginUser, ticketID);

            if (ticket == null)
            {
                Logs.WriteEvent("Ticket is NULL, exiting");
                return;
            }

            //Since we are already re-loading the Ticket information we will check again if the SLAs still apply at this point
            string violationType      = "";
            bool   slaNotApplyAnymore = false;

            switch (slaViolationType)
            {
            case SlaViolationType.InitialResponse:
                violationType      = "Initial Response";
                slaNotApplyAnymore = ticket.SlaWarningInitialResponse == null;
                break;

            case SlaViolationType.LastAction:
                violationType      = "Last Action";
                slaNotApplyAnymore = ticket.SlaViolationLastAction == null;
                break;

            case SlaViolationType.TimeClosed:
                violationType      = "Time to Close";
                slaNotApplyAnymore = ticket.SlaViolationTimeClosed == null;
                break;

            default: break;
            }

            if (slaNotApplyAnymore)
            {
                Logs.WriteEvent($"The {((isWarning) ? "warning" : "violation")} for {violationType} does not apply anymore because the Ticket has been updated to satisfy this SLA while it was in process for the notification.");
            }
            else
            {
                if (!isWarning)
                {
                    SlaViolationHistoryItem history = (new SlaViolationHistory(LoginUser)).AddNewSlaViolationHistoryItem();
                    history.DateViolated  = DateTime.UtcNow;
                    history.GroupID       = ticket.GroupID;
                    history.UserID        = ticket.UserID;
                    history.ViolationType = slaViolationType;
                    history.TicketID      = ticket.TicketID;
                    history.SlaTriggerId  = triggerId;
                    history.Collection.Save();
                }

                Actions actions = new Actions(LoginUser);
                actions.LoadLatestByTicket(ticket.TicketID, false);

                ActionLogs.AddActionLog(LoginUser, ActionLogType.Update, ReferenceType.Tickets, ticket.TicketID, violationType + " SLA " + (isWarning ? "warning" : "violation") + " occurred");

                MailMessage message = EmailTemplates.GetSlaEmail(LoginUser, ticket, violationType, isWarning);


                if (ticket.GroupID != null && useGroup)
                {
                    users.LoadByGroupID((int)ticket.GroupID);
                }

                if (ticket.UserID != null && useUser && users.FindByUserID((int)ticket.UserID) == null)
                {
                    user = Users.GetUser(LoginUser, (int)ticket.UserID);
                }

                foreach (User item in users)
                {
                    if (Emails.IsEmailDisabled(LoginUser, item.UserID, "SLA"))
                    {
                        continue;
                    }
                    message.To.Add(new MailAddress(item.Email, item.FirstLastName));
                }

                if (user != null)
                {
                    if (!Emails.IsEmailDisabled(LoginUser, user.UserID, "SLA"))
                    {
                        message.To.Add(new MailAddress(user.Email, user.FirstLastName));
                        Logs.WriteEvent(string.Format("Adding Main User, Name:{0}  Email:{1}  UserID:{2}", user.FirstLastName, user.Email, user.UserID.ToString()));
                    }
                }

                if (message.To.Count > 0)
                {
                    Email email = Emails.AddEmail(LoginUser, ticket.OrganizationID, null, "Sla Message", message);
                    Logs.WriteEvent("Email Added (EmailID: " + email.EmailID.ToString() + ")", true);
                }

                try
                {
                    TimeZoneInfo tz  = null;
                    Organization org = Organizations.GetOrganization(LoginUser, ticket.OrganizationID);
                    if (org.TimeZoneID != null)
                    {
                        tz = System.TimeZoneInfo.FindSystemTimeZoneById(org.TimeZoneID);
                    }
                    if (tz == null)
                    {
                        Logs.WriteEvent("Timezone is null, using system's");
                        tz = System.TimeZoneInfo.Local;
                    }
                    Logs.WriteEvent(tz.DisplayName);
                    Logs.WriteEvent("Supports DLS: " + tz.SupportsDaylightSavingTime.ToString());
                    Logs.WriteEvent("Is DLS: " + tz.IsDaylightSavingTime(DateTime.UtcNow).ToString());
                    Logs.WriteEvent("UTC: " + DateTime.UtcNow.ToString());
                    Logs.WriteEvent(string.Format("NOTIFYING TicketID:{0}  TicketNumber:{1}  OrganizationID:{2} ", ticket.TicketID.ToString(), ticket.TicketNumber.ToString(), ticket.OrganizationID.ToString()));
                    Logs.WriteEvent(string.Format("User:{1}  Group:{2}  IsWarning:{3}  NoficationType:{4}", ticketID.ToString(), useUser.ToString(), useGroup.ToString(), isWarning.ToString(), slaViolationType));
                    Logs.WriteEvent("Ticket Data:");
                    Logs.WriteData(ticket.Row);
                    Logs.WriteEvent("Notification Data:");
                    Logs.WriteData(notification.Row);
                    Logs.WriteEvent("Organization Data:");
                    Logs.WriteData(org.Row);
                    Organizations customers = new Organizations(LoginUser);
                    customers.LoadByTicketID(ticket.TicketID);

                    foreach (Organization customer in customers)
                    {
                        Logs.WriteEvent("-- Customer: " + customer.Name);
                        if (customer.SlaLevelID != null)
                        {
                            SlaLevel level = SlaLevels.GetSlaLevel(LoginUser, (int)customer.SlaLevelID);
                            Logs.WriteEvent("SLA Level: " + level.Name);
                            SlaTriggers triggers = new SlaTriggers(LoginUser);
                            triggers.LoadByTicketTypeAndSeverity(level.SlaLevelID, ticket.TicketTypeID, ticket.TicketSeverityID);

                            foreach (SlaTrigger trigger in triggers)
                            {
                                Logs.WriteData(trigger.Row);
                            }
                        }
                        else
                        {
                            Logs.WriteEvent("No SLA Level Assigned to " + customer.Name);
                        }
                    }

                    if (org.InternalSlaLevelID != null)
                    {
                        Logs.WriteEvent("Internal SLA:");

                        SlaTriggers triggers = new SlaTriggers(LoginUser);
                        triggers.LoadByTicketTypeAndSeverity((int)org.InternalSlaLevelID, ticket.TicketTypeID, ticket.TicketSeverityID);
                        foreach (SlaTrigger trigger in triggers)
                        {
                            Logs.WriteData(trigger.Row);
                        }
                    }
                    else
                    {
                        Logs.WriteEvent("No Internal SLA");
                    }
                }
                catch (Exception ex)
                {
                    Logs.WriteEvent("Logging Exception:");
                    Logs.WriteException(ex);
                }
            }
        }
Exemplo n.º 3
0
        private void ProcessTicket(TicketSlaInfo ticket)
        {
            UpdateHealth();

            bool isPaused     = false;
            bool isPending    = false;
            int? slaTriggerId = null;

            Logs.WriteEvent("Getting SlaTicket record");
            SlaTicket slaTicket = SlaTickets.GetSlaTicket(LoginUser, ticket.TicketId);

            if (slaTicket != null)
            {
                isPaused     = ticket.IsSlaPaused(slaTicket.SlaTriggerId, ticket.OrganizationId);
                isPending    = slaTicket.IsPending;
                slaTriggerId = slaTicket.SlaTriggerId;
            }

            Logs.WriteEventFormat("IsPaused: {0}; IsPending: {1}", isPaused.ToString(), isPending.ToString());

            if (!isPaused && !isPending)
            {
                SlaTriggersView triggers = new SlaTriggersView(LoginUser);
                triggers.LoadByTicketId(ticket.TicketId);
                bool warnGroup = false;
                bool warnUser  = false;
                bool vioGroup  = false;
                bool vioUser   = false;

                foreach (SlaTriggersViewItem item in triggers)
                {
                    warnGroup = item.NotifyGroupOnWarning || warnGroup;
                    warnUser  = item.NotifyUserOnWarning || warnUser;
                    vioGroup  = item.NotifyGroupOnViolation || vioGroup;
                    vioUser   = item.NotifyUserOnViolation || vioUser;
                }

                SlaNotification notification = SlaNotifications.GetSlaNotification(LoginUser, ticket.TicketId);
                if (notification == null)
                {
                    notification          = (new SlaNotifications(LoginUser)).AddNewSlaNotification();
                    notification.TicketID = ticket.TicketId;
                }

                DateTime notifyTime;

                if (ticket.SlaViolationInitialResponse != null && ticket.SlaViolationInitialResponse <= DateTime.UtcNow)
                {
                    notifyTime = (DateTime)ticket.SlaViolationInitialResponse;
                    if (!IsTooOld(notifyTime))
                    {
                        if (notification.InitialResponseViolationDate == null || Math.Abs(((DateTime)notification.InitialResponseViolationDateUtc - notifyTime).TotalMinutes) > 5)
                        {
                            NotifyViolation(ticket.TicketId, vioUser, vioGroup, false, SlaViolationType.InitialResponse, notification, slaTriggerId);
                            notification.InitialResponseViolationDate = notifyTime;
                        }
                    }
                }
                else if (ticket.SlaWarningInitialResponse != null && ticket.SlaWarningInitialResponse <= DateTime.UtcNow)
                {
                    notifyTime = (DateTime)ticket.SlaWarningInitialResponse;

                    if (!IsTooOld(notifyTime))
                    {
                        if (notification.InitialResponseWarningDate == null || Math.Abs(((DateTime)notification.InitialResponseWarningDateUtc - notifyTime).TotalMinutes) > 5)
                        {
                            NotifyViolation(ticket.TicketId, warnUser, warnGroup, true, SlaViolationType.InitialResponse, notification, slaTriggerId);
                            notification.InitialResponseWarningDate = notifyTime;
                        }
                    }
                }


                if (ticket.SlaViolationLastAction != null && ticket.SlaViolationLastAction <= DateTime.UtcNow)
                {
                    notifyTime = (DateTime)ticket.SlaViolationLastAction;

                    if (!IsTooOld(notifyTime))
                    {
                        if (notification.LastActionViolationDate == null || Math.Abs(((DateTime)notification.LastActionViolationDateUtc - notifyTime).TotalMinutes) > 5)
                        {
                            NotifyViolation(ticket.TicketId, vioUser, vioGroup, false, SlaViolationType.LastAction, notification, slaTriggerId);
                            notification.LastActionViolationDate = notifyTime;
                        }
                    }
                }
                else if (ticket.SlaWarningLastAction != null && ticket.SlaWarningLastAction <= DateTime.UtcNow)
                {
                    notifyTime = (DateTime)ticket.SlaWarningLastAction;

                    if (!IsTooOld(notifyTime))
                    {
                        if (notification.LastActionWarningDate == null || Math.Abs(((DateTime)notification.LastActionWarningDateUtc - notifyTime).TotalMinutes) > 5)
                        {
                            NotifyViolation(ticket.TicketId, warnUser, warnGroup, true, SlaViolationType.LastAction, notification, slaTriggerId);
                            notification.LastActionWarningDate = notifyTime;
                        }
                    }
                }


                if (ticket.SlaViolationTimeClosed != null && ticket.SlaViolationTimeClosed <= DateTime.UtcNow)
                {
                    notifyTime = (DateTime)ticket.SlaViolationTimeClosed;

                    if (!IsTooOld(notifyTime))
                    {
                        if (notification.TimeClosedViolationDate == null || Math.Abs(((DateTime)notification.TimeClosedViolationDateUtc - notifyTime).TotalMinutes) > 5)
                        {
                            NotifyViolation(ticket.TicketId, vioUser, vioGroup, false, SlaViolationType.TimeClosed, notification, slaTriggerId);
                            notification.TimeClosedViolationDate = notifyTime;
                        }
                    }
                }
                else if (ticket.SlaWarningTimeClosed != null && ticket.SlaWarningTimeClosed <= DateTime.UtcNow)
                {
                    notifyTime = (DateTime)ticket.SlaWarningTimeClosed;

                    if (!IsTooOld(notifyTime))
                    {
                        if (notification.TimeClosedWarningDate == null || Math.Abs(((DateTime)notification.TimeClosedWarningDateUtc - notifyTime).TotalMinutes) > 5)
                        {
                            NotifyViolation(ticket.TicketId, warnUser, warnGroup, true, SlaViolationType.TimeClosed, notification, slaTriggerId);
                            notification.TimeClosedWarningDate = notifyTime;
                        }
                    }
                }

                notification.Collection.Save();
            }
        }