public LateTimesheetContactor(ILateTimesheetRepository timesheetRepo, IEmailSender emailSender, DateTime emailWeek, DateTime bulkUpdateWeek, string reportCreationTime, string locationToSaveTeamsMessages, string senderName) { _repository = timesheetRepo; _emailSender = emailSender; _weekToEmail = emailWeek; _weekToBulkUpdate = bulkUpdateWeek; _reportCreationTimeString = reportCreationTime; _teamsMessagesSaveFile = locationToSaveTeamsMessages; _senderName = senderName; _emailClosingString = null; _emailOpeningString = null; }
public void SendEmailsAndAlertUser(object sender, RoutedEventArgs e) { bool allFieldsValid = true; string timeOfReportCreation = this.ReportCreationTimeTextBox.Text; allFieldsValid = (allFieldsValid && !String.IsNullOrWhiteSpace(timeOfReportCreation)); string timeSheetFile = this.SelectedFileTextBlock.Text; allFieldsValid = (allFieldsValid && !String.IsNullOrWhiteSpace(timeSheetFile)); ILateTimesheetRepository timesheetRepository = null; if (System.IO.Path.GetExtension(timeSheetFile) == ".txt") { timesheetRepository = new LateTimesheetsTsvRepository(timeSheetFile); } if (timesheetRepository == null) { allFieldsValid = false; } string attachmentFile = this.AttachmentFileTextBlock.Text; allFieldsValid = (allFieldsValid && !String.IsNullOrWhiteSpace(attachmentFile)); DateTime?weekToStartEmailing = this.DateToSendIndividualEmails.SelectedDate; allFieldsValid = (allFieldsValid && weekToStartEmailing.HasValue); DateTime?weekToBulkEmail = this.DateToSendMassEmail.SelectedDate; if (allFieldsValid) { allFieldsValid = (allFieldsValid && weekToBulkEmail.HasValue && weekToBulkEmail.Value > weekToStartEmailing.Value); } string teamsMessagesSaveFile = TeamsMessagesFileNameTextBlock.Text; allFieldsValid = (allFieldsValid && !String.IsNullOrWhiteSpace(teamsMessagesSaveFile)); string emailSender = EmailSenderTextBox.Text; allFieldsValid = (allFieldsValid && !String.IsNullOrWhiteSpace(emailSender)); IEmailSender senderToUse = new EmailSenderOutlook(attachmentFile); if (allFieldsValid) { try { LateTimesheetContactor contactor = new LateTimesheetContactor(timesheetRepository, senderToUse, weekToStartEmailing.Value, weekToBulkEmail.Value, timeOfReportCreation, teamsMessagesSaveFile, emailSender); contactor.ContactLateTimesheetEmployees(); string message = $"The emails have been sent and the teams messages to send have been saved to {teamsMessagesSaveFile}"; string title = "Success"; MessageBox.Show(message, title); } catch (Exception ex) { string message = "Something went wrong. Please check your Outlook sent emails to see if any emails were sent before retrying to avoid repeat emails."; string title = "Error"; MessageBox.Show(ex.Message, title); } App.ParentWindowRef.Close(); } else { string message = "Please ensure all fields are filled out correctly."; string title = "More Information Needed"; MessageBox.Show(message, title); } }