public int CreateDeliveryFailureAlert(int tenant, string user, int mailboxId, string subject, string from,
                                              int messageId, int mailDaemonMessageid)
        {
            var data = new DeliveryFailure
            {
                @from      = from,
                message_id = messageId,
                subject    = subject,
                failure_id = mailDaemonMessageid
            };

            var jsonData = MailUtil.GetJsonString(data);

            var alert = new Alert
            {
                Tenant    = tenant,
                User      = user,
                MailboxId = mailboxId,
                Type      = MailAlertTypes.DeliveryFailure,
                Data      = jsonData
            };

            using (var dao = new DaoFactory())
            {
                var result = dao.CreateAlertDao(tenant, user).SaveAlert(alert);

                if (result <= 0)
                {
                    throw new Exception("Save alert failed");
                }

                return(result);
            }
        }
        public int CreateUploadToDocumentsFailureAlert(int tenant, string user, int mailboxId, UploadToDocumentsErrorType errorType)
        {
            var data = new UploadToDocumentsFailure
            {
                error_type = (int)errorType
            };

            var jsonData = MailUtil.GetJsonString(data);

            var alert = new Alert
            {
                Tenant    = tenant,
                User      = user,
                MailboxId = mailboxId,
                Type      = MailAlertTypes.UploadFailure,
                Data      = jsonData
            };

            using (var dao = new DaoFactory())
            {
                var result = dao.CreateAlertDao(tenant, user).SaveAlert(alert);

                if (result <= 0)
                {
                    throw new Exception("Save alert failed");
                }

                return(result);
            }
        }
 public List <MailAlertData> GetAlerts(int mailboxId = -1, MailAlertTypes type = MailAlertTypes.Empty)
 {
     using (var dao = new DaoFactory())
     {
         return(dao.CreateAlertDao(Tenant, User)
                .GetAlerts(mailboxId, type)
                .ConvertAll(ToMailAlert));
     }
 }
        public bool DeleteAlert(long id)
        {
            using (var dao = new DaoFactory())
            {
                var result = dao.CreateAlertDao(Tenant, User)
                             .DeleteAlert(id);

                if (result <= 0)
                {
                    return(false);
                }
            }

            return(true);
        }
        public bool DeleteAlert(MailAlertTypes type)
        {
            using (var daoFactory = new DaoFactory())
            {
                var dao = daoFactory.CreateAlertDao(Tenant, User);

                var quotaAlerts = dao.GetAlerts(-1, type);

                if (!quotaAlerts.Any())
                {
                    return(true);
                }

                var result = dao.DeleteAlerts(quotaAlerts.Select(a => a.Id).ToList());

                if (result <= 0)
                {
                    throw new Exception("Delete old alerts failed");
                }
            }

            return(true);
        }
        public int CreateQuotaErrorWarningAlert(int tenant, string user)
        {
            var alert = new Alert
            {
                Tenant    = tenant,
                User      = user,
                MailboxId = -1,
                Type      = MailAlertTypes.QuotaError,
                Data      = null
            };

            using (var dao = new DaoFactory())
            {
                var result = dao.CreateAlertDao(tenant, user).SaveAlert(alert, true);

                if (result <= 0)
                {
                    throw new Exception("Save alert failed");
                }

                return(result);
            }
        }
        public int CreateAuthErrorDisableAlert(int tenant, string user, int mailboxId)
        {
            using (var daoFactory = new DaoFactory())
            {
                var dao = daoFactory.CreateAlertDao(tenant, user);

                var alerts = dao.GetAlerts(mailboxId, MailAlertTypes.AuthConnectFailure);

                if (alerts.Any())
                {
                    var r = dao.DeleteAlerts(alerts.Select(a => a.Id).ToList());

                    if (r <= 0)
                    {
                        throw new Exception("Delete alerts failed");
                    }
                }

                var alert = new Alert
                {
                    Tenant    = tenant,
                    User      = user,
                    MailboxId = mailboxId,
                    Type      = MailAlertTypes.TooManyAuthError,
                    Data      = null
                };

                var result = dao.SaveAlert(alert, true);

                if (result <= 0)
                {
                    throw new Exception("Save alert failed");
                }

                return(result);
            }
        }
        public int CreateDisableAllMailboxesAlert(int tenant, List <string> users)
        {
            var result = 0;

            if (!users.Any())
            {
                return(result);
            }

            using (var daoFactory = new DaoFactory())
            {
                var dao = daoFactory.CreateAlertDao(tenant);

                foreach (var user in users)
                {
                    var alert = new Alert
                    {
                        Tenant    = tenant,
                        User      = user,
                        MailboxId = -1,
                        Type      = MailAlertTypes.DisableAllMailboxes,
                        Data      = null
                    };

                    var r = dao.SaveAlert(alert);

                    if (r <= 0)
                    {
                        throw new Exception("Save alert failed");
                    }

                    result += r;
                }
            }

            return(result);
        }