private async Task <int> AddClientEmailReport(Email.ClientEmailReport emailReport) { try { using (Connection = connection.returnConnection()) { command.Parameters.Clear(); command.Parameters.Add(new SqlParameter("@AgencyID", emailReport.staffDetails.AgencyId)); command.Parameters.Add(new SqlParameter("@RoleID", emailReport.staffDetails.RoleId)); command.Parameters.Add(new SqlParameter("@UserID", emailReport.staffDetails.UserId)); command.Parameters.Add(new SqlParameter("@ClientID", emailReport.ClientID)); command.Parameters.Add(new SqlParameter("@ParentID", emailReport.ParentID)); command.Parameters.Add(new SqlParameter("@ReferenceID", emailReport.ReferenceID)); command.Parameters.Add(new SqlParameter("@EmailType", (int)emailReport.EmailType)); command.Parameters.Add(new SqlParameter("@EmailStatus", (int)emailReport.EmailStatus)); command.CommandText = "USP_AddClientEmailReport"; command.CommandType = CommandType.StoredProcedure; command.Connection = Connection; Connection.Open(); await Task.Run(() => command.ExecuteNonQuery()); return((int)emailReport.EmailStatus); } } catch (Exception ex) { clsError.WriteException(ex); return(0); } finally { Connection.Dispose(); command.Dispose(); } }
protected override async Task <int> SendEmailUnscheduledSchoolDay(params object[] list) { try { DataSet dataset = new DataSet(); bool isStaff; StreamReader reader = null; UnscheduledSchoolDay unscheduledSchoolDay = new UnscheduledSchoolDay(); string emailMessage = ""; StaffDetails staff = null; // StaffDetails staffThread = await Task.Factory.StartNew(() => StaffDetails.GetThreadedInstance(context)); FingerprintsModel.Enums.EmailStatus emailStatusEnum = FingerprintsModel.Enums.EmailStatus.All; string serer = "".Replace(" ", "").Trim(); foreach (var item in list) { if (item.GetType() == typeof(DataSet)) { dataset = (DataSet)item; } else if (item.GetType() == typeof(bool)) { isStaff = (bool)item; } else if (item.GetType() == typeof(UnscheduledSchoolDay)) { } else if (item.GetType() == typeof(StreamReader)) { reader = (StreamReader)item; } else if (item.GetType() == typeof(StaffDetails)) { staff = (StaffDetails)item; } else if (item.GetType() == typeof(FingerprintsModel.Enums.EmailStatus)) { emailStatusEnum = (FingerprintsModel.Enums.EmailStatus)item; } } if (dataset.Tables.Count > 0) { switch (emailStatusEnum) { case FingerprintsModel.Enums.EmailStatus.BouncedEmails: break; } string reason = string.Empty; string classDaste = string.Empty; long unscheduledSchoolDayID = 0; string staffRole = string.Empty; string agencyName = string.Empty; if (dataset.Tables.Count > 2 && dataset.Tables[2] != null && dataset.Tables[2].Rows.Count > 0) { reason = (Convert.ToString(dataset.Tables[2].Rows[0]["Reason"])); classDaste = (Convert.ToString(dataset.Tables[2].Rows[0]["ClassDate"])); unscheduledSchoolDayID = Convert.ToInt64(dataset.Tables[2].Rows[0]["UnscheduledSchoolDayID"]); staffRole = Convert.ToString(dataset.Tables[2].Rows[0]["StaffRole"]); agencyName = Convert.ToString(dataset.Tables[2].Rows[0]["AgencyName"]); } Email.TotalEmails = dataset.Tables[0].AsEnumerable().Where(x => x.Field <int>("EmailStatus") == (int)emailStatusEnum || emailStatusEnum == FingerprintsModel.Enums.EmailStatus.All).Count(); foreach (DataRow dr in dataset.Tables[0].Rows) { string centerName = string.Empty; string classroomName = string.Empty; string parentName = string.Empty; string childName = string.Empty; string parentEmail = string.Empty; string subject = string.Empty; long clientID = 0; long parentID = 0; bool emailStatus = false; int emailstatusType = 0; emailMessage = reader.ReadToEnd(); parentName = Convert.ToString(dr["ParentName"]); childName = Convert.ToString(dr["ChildName"]); parentEmail = Convert.ToString(dr["Email"]); centerName = Convert.ToString(dr["CenterName"]); classroomName = Convert.ToString(dr["ClassroomName"]); clientID = Convert.ToInt64(dr["ClientID"]); parentID = Convert.ToInt64(dr["ParentID"]); emailstatusType = Convert.ToInt32(dr["EmailStatus"]); // either the email needs to be sent for all or it should be sent to only for unsent or bounced emails // if (emailStatusEnum == FingerprintsModel.Enums.EmailStatus.All || (int)emailStatusEnum == emailstatusType) { string day = DateTime.Parse(classDaste, new CultureInfo("en-US", true)).ToString("dddd"); emailMessage = emailMessage.Replace("{Name}", parentName) .Replace("{reason}", reason) .Replace("{reasonBody}", "School has been scheduled on " + classDaste + " (" + day + "). Your child (" + childName + ") will be expected to attend school as usual.") .Replace("{reportbody}", "<p style='color:#0e2965;'>Regards,<br>" + staff.FullName + "<br>" + staffRole + "<br>" + centerName + "</p>") .Replace("{copyright}", "<p style='text-align:center'>Copyright © " + DateTime.Now.Year + " All Rights Reserved</p><p style='text-align:center;'>" + agencyName + "</p>"); subject = "School has been scheduled on " + classDaste + ""; if (await Task.Run(() => SendMail.SendEmailWithTask(staff.EmailID, parentEmail, emailMessage, subject))) { emailStatus = true; } else { emailStatus = false; } Email.ClientEmailReport emailReport = new Email.ClientEmailReport { ClientID = clientID, ParentID = parentID, EmailType = FingerprintsModel.Enums.EmailType.UnscheduledSchoolDay, EmailStatus = (emailStatus)? FingerprintsModel.Enums.EmailStatus.SentEmails:FingerprintsModel.Enums.EmailStatus.BouncedEmails, staffDetails = staff, ReferenceID = unscheduledSchoolDayID }; await Task.Run(() => this.AddClientEmailReport(emailReport)).ContinueWith(x => { //if(x.IsCompleted) //Email.conque.Enqueue((int)x.Result); }); } } } return(1); } catch (Exception ex) { clsError.WriteException(ex); } return(0); }