/// <summary> /// update transactions states /// </summary> public static void UpdateTransactionStateByEmailState(int emailId, int emailStateId) { try { using (var db = new FCCEmailAgentEntities()) { EmailTransactions transaction = db.EmailTransactions.FirstOrDefault(x => x.Id == emailId); if (transaction != null) { switch (emailStateId) { case 2: transaction.State = 3; break; case 3: transaction.State = 4; break; } transaction.ExecutionDate = DateTime.Now; db.SaveChanges(); } } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); } }
/// <summary> /// generate email body /// </summary> public static string GenerateEmailBody(int typeId, List <EmailContentElementModel> contentElements) { try { string result = ""; using (var db = new FCCEmailAgentEntities()) { string templateName = db.EmailTypes.Where(x => x.Id == typeId).Select(x => x.TemplateName).FirstOrDefault(); string templatePath = Path.Combine(Program.Agent.SystemSettings.TemplatesPath, SettingsHelper.GetSettingValueByName(templateName)); string template = IOHelper.ReadFile(templatePath); foreach (var contentElement in contentElements) { template = template.Replace(contentElement.Name, contentElement.Value); } result = template; } return(result); } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); return(""); } }
/// <summary> /// add task to db /// </summary> public static int AddTask(DateTime dateTime) { try { using (var db = new FCCEmailAgentEntities()) { Tasks task = new Tasks() { DayNumber = null, ExecutionTime = dateTime, Interval = null, TaskTypeId = 1, TaskStateId = 1 }; db.Tasks.Add(task); db.SaveChanges(); return(task.Id); } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); return(-1); } }
/// <summary> /// add email to database /// </summary> /// <returns></returns> public static int AddEmailToDbByEmailModel(int taskId, EmailModel emailModel) { try { using (var db = new FCCEmailAgentEntities()) { EmailContentModel content = new EmailContentModel(emailModel.EmailContentLine); var attachmentsContentModel = content.Elements.Find(x => x.Name == "#attachmentslist#"); Emails email = new Emails(); email.Subject = emailModel.Subject; email.FromEmail = emailModel.FromEmail; email.ReceiverUserId = UsersHelper.CheckExistsUserByEmail(emailModel.ToEmails); email.StateId = 1; email.TaskId = taskId; email.CcResponseTo = string.IsNullOrWhiteSpace(emailModel.CcResponseTo)?"": emailModel.CcResponseTo; email.TypeId = emailModel.TypeId; email.Host = Program.Agent.ServiceSettings.SMTPSettings.Server; email.Port = Program.Agent.ServiceSettings.SMTPSettings.Port; email.Body = EmailGeneratorHelper.GenerateEmailBody(emailModel.TypeId, content.Elements); if (attachmentsContentModel != null) { email.Attachments = EmailGeneratorHelper.GenerateStringAttachments(attachmentsContentModel.Value); } else { email.Attachments = EmailGeneratorHelper.GenerateStringAttachments(""); } db.Emails.Add(email); db.SaveChanges(); EmailTransactionsHelper.AddEmailTransaction(email.Id); return(email.Id); } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); return(-1); } }
/// <summary> /// get to user inforation by id /// </summary> /// <param name="userId"></param> /// <returns></returns> public static Users GetToUserInfo(int userId) { try { using (var db = new FCCEmailAgentEntities()) { return(db.Users.FirstOrDefault(x => x.Id == userId)); } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); return(null); } }
/// <summary> /// select all not executed emails /// </summary> /// <param name="taskId"></param> /// <returns></returns> public static List <Emails> GetToNotExecutedEmailsByTaskId(int taskId) { try { using (var db = new FCCEmailAgentEntities()) { return(db.Emails.Where(x => x.TaskId == taskId && x.StateId == 1).ToList()); } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); return(null); } }
/// <summary> /// get to all not overdue tasks /// </summary> /// <returns></returns> public static List <Tasks> GetToOverdueTasks() { try { using (var db = new FCCEmailAgentEntities()) { return(db.Tasks.Where(x => x.TaskStateId == 1 && x.ExecutionTime < DateTime.Now).ToList()); } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); return(null); } }
/// <summary> /// get to setting value by name /// </summary> /// <returns></returns> public static string GetSettingValueByName(string settingName) { try { using (var db = new FCCEmailAgentEntities()) { var setting = db.Settings.FirstOrDefault(x => x.SettingName.ToLower().Equals(settingName.ToLower())); return(setting != null ? setting.SettingValue : ""); } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); return(""); } }
/// <summary> /// add email transaction /// </summary> public static void AddEmailTransaction(int emailId) { try { using (var db = new FCCEmailAgentEntities()) { db.EmailTransactions.Add(new EmailTransactions() { EmailId = emailId, State = 2, ExecutionDate = DateTime.Now }); db.SaveChanges(); } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); } }
/// <summary> /// add record to log /// </summary> public static void AddLog(string message) { try { using (var db = new FCCEmailAgentEntities()) { if (db.Log.Count() > 1000) { db.Log.RemoveRange(db.Log.Select(x => x)); } db.Log.Add(new Log() { DateTime = DateTime.Now, Message = message }); db.SaveChanges(); } } catch (Exception) { } }
/// <summary> /// update task state /// </summary> public static void UpdateTaskState(int taskId, int stateId) { try { using (var db = new FCCEmailAgentEntities()) { Tasks tasks = db.Tasks.FirstOrDefault(x => x.Id == taskId); if (tasks != null) { tasks.TaskStateId = stateId; db.SaveChanges(); } } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); } }
/// <summary> /// update email state /// </summary> public static void UpdateEmailState(int emailId, int stateId) { try { using (var db = new FCCEmailAgentEntities()) { Emails email = db.Emails.FirstOrDefault(x => x.Id == emailId); if (email != null) { email.StateId = stateId; db.SaveChanges(); EmailTransactionsHelper.UpdateTransactionStateByEmailState(emailId, stateId); } } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); } }
/// <summary> /// add queue to database /// </summary> public static void AddQueueToDb(QueueModel model) { try { using (var db = new FCCEmailAgentEntities()) { Queue queue = new Queue() { EmailContent = model.EmailContent, StateId = 1 }; db.Queue.Add(queue); db.SaveChanges(); } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); } }
/// <summary> /// check user in db and add if not exists /// </summary> /// <returns></returns> public static int CheckExistsUserByEmail(string email) { try { using (var db = new FCCEmailAgentEntities()) { email = email.ToLower(); email = email.Replace(" ", string.Empty); var user = db.Users.FirstOrDefault(x => x.UserEmail == email); if (user == null) { Users users = new Users() { UserEmail = email }; db.Users.Add(users); db.SaveChanges(); return(users.Id); } else { return(user.Id); } } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); return(-1); } }
/// <summary> /// add user to database /// </summary> public static int AddUser(string userEmail, string userName) { try { using (FCCEmailAgentEntities db = new FCCEmailAgentEntities()) { Users user = new Users() { UserEmail = userEmail, UserName = userName }; db.Users.Add(user); db.SaveChanges(); return(user.Id); } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); return(-1); } }