public static DateTime NextRun(TaskSchedule ts, Usertask ut, User u) { //this could be used to populate the next run of a grouptaskschedule //Get last run time, will need to add to correct data later var lastRunTime = DateTime.Now; if (ut.SendNow) { lastRunTime = ts.TimeOverride ? ts.Time : u.ContactTime; } else { lastRunTime = ut.SendTime; } //need to figure out the next x of when it should send based off of taskschedule or somehow from the last scheduled switch (ts.Frequency) { case Enums.Frequency.Daily: return(lastRunTime.AddDays(1)); case Enums.Frequency.Weekly: return(lastRunTime.AddDays(7)); case Enums.Frequency.Monthly: return(lastRunTime.AddMonths(1)); default: break; } return(DateTime.Now); }
/// <summary> /// Sends a message to a user /// </summary> /// <param name="msg">Message information</param> /// <param name="cType">Contact type, Email or Phone</param> public async Task SendMessage(MessageData msg, Enums.ContactType cType, Usertask userTask) { //don't need to send a message while testing if (AppSettings.AppSetting["testmode"] == "true") { Console.WriteLine("Done"); } else { if (userTask.SendTime < DateTime.Now.AddHours(23) && !userTask.MessageSent) { switch (cType) { case Enums.ContactType.Email: var emailSuccess = await EmailAgent.SendMail(msg); break; case Enums.ContactType.Phone: var smsSuccess = SmsAgent.TwiSend(msg); break; default: break; } } } //need to decipher success rresponses to determine userTask.MessageSent = true; //this is basically what admin auth does, why not change it/remove it? var dict = new Dictionary <string, string>() { { "uid", userTask.UidToken }, { "token", null } }; var json = JsonConvert.SerializeObject(userTask); //update this message to being sent //this also removes the messageid which should be fine await _ds.AddData(dict, Enums.DataType.UserTasks, json, true, true); _logger.LogInformation("{0} sent for Usertask {1}", cType.ToString(), userTask.Id); //omitting this for now since it was causing issues /* * if (userTask.LastScheduled) * { * //Clear all other lastScheduled flags in this group * await CheckLastMessage(userTask); * //increment the grouptask run * _logger.LogInformation("Usertask {0} was run to repopulate GroupTaskId {1} for GroupTaskRun {2}", * userTask.Id, userTask.GroupTaskId, userTask.GroupTaskRun); * //Schedule the next event(s) * await OnScheduledEvent(userTask.GroupTaskId); * _logger.LogInformation("After repopulated"); * } */ }
//TODO put this in the message scheduler /// <summary> /// Schedules the notification to be sent /// </summary> /// <param name="userTaskList"></param> /// <param name="dict"></param> /// <returns>nothing</returns> public async Task <string> ScheduleNotification(Usertask userTask, Dictionary <string, string> dict, TaskSchedule taskSchedule = null) { //get admin info var adminUserObj = await GetAdmin(dict); var adminContact = new SimpleUser() { name = adminUserObj.name, email = adminUserObj.ReplyToEmail }; //get contact info var user = await _data.GetOrSetCachedData(dict, Enums.DataType.Users, userTask.UserIdAssigned); var userObj = user.First().ToObject <User>(); //get task var task = await _data.GetOrSetCachedData(dict, Enums.DataType.Tasks, userTask.TaskId); var taskObj = task.First().ToObject <TaskItem>(); //Make message return(_ms.ScheduleMessage(adminContact, userObj, taskObj, userTask, taskSchedule)); }
public async Task CheckLastMessage(Usertask userTask) { //use admin auth since we've already gotten to this point var dict = await AuthController.GetAdminAuth(userTask.GroupTaskId); //get usertasks to update var uts = await _ds.GetData(dict, Enums.DataType.UserTasks, true); var userTasks = new List <Usertask>(); foreach (var ut in uts) { userTasks.Add(ut.ToObject <Usertask>()); } //iterate over userTasks to find the potentially matching ones foreach (var u in userTasks) { if (u.LastScheduled == true && u.GroupTaskRun == userTask.GroupTaskRun && u.GroupTaskId == userTask.GroupTaskId) { //change lastscheduled to false and update it! u.LastScheduled = false; var jData = JsonConvert.SerializeObject(u); await _ds.AddData(dict, Enums.DataType.UserTasks, jData, true, true); } } //increment grouptaskrun var gts = await _ds.GetData(dict, Enums.DataType.GroupSchedule, true, "/" + userTask.GroupTaskId); var groupSchedule = gts.FirstOrDefault().ToObject <GroupTaskSchedule>(); groupSchedule.GroupTaskRun = userTask.GroupTaskRun + 1; var jsData = JsonConvert.SerializeObject(groupSchedule); await _ds.AddData(dict, Enums.DataType.GroupSchedule, jsData, true, true); }
public string ScheduleMessage(SimpleUser adminContact, User userObj, TaskItem taskObj, Usertask userTask, TaskSchedule taskSchedule, int offset = 0) { //This will create an html email that looks nice var email = new EmailData(taskObj, userObj); var msg = new MessageData() { MessageId = Guid.NewGuid().ToString(), tags = new[] { taskObj.Title }, sender = adminContact, to = new[] { new SimpleUser() { name = userObj.name, email = userTask.ContactMethod == Enums.ContactType.Email ? userObj.email : userObj.Phone } }, htmlContent = email.BodyTemplate, textContent = taskObj.TaskDetails, subject = taskObj.Title, replyTo = adminContact, SendTime = taskSchedule?.Time ?? userTask.SendTime }; if (taskSchedule == null) { //Immediately send message if (userTask.SendNow) { return(BackgroundJob.Enqueue(() => SendMessage(msg, userTask.ContactMethod, userTask))); } //wait until you say so else { var offSet = msg.SendTime - DateTime.Now; return(BackgroundJob.Schedule(() => SendMessage(msg, userTask.ContactMethod, userTask), offSet)); } } else { var freq = GetCronString(taskSchedule); //TODO finish this, figure out logic RecurringJob.AddOrUpdate(taskSchedule.Id, () => SendMessage(msg, userTask.ContactMethod, userTask), freq, TimeZoneInfo.Local); return(taskSchedule.Id); //or do we schedule one at a time? //need to figure out how this will work with send once things //return BackgroundJob.Schedule(() => SendMessage(msg, userTask.ContactMethod, userTask), NextRun(taskSchedule, userTask, userObj)); //ScheduleNextMessage } }