// in the instance that a single shift from a recurring set needs to have its reminder scheduled // (e.g. when a shift is having its original properties restored), // the datetime of the selected shift will be passed in public bool ScheduleReminder(Shift shift) { try { //schedule email notification for shift if shift is after todays date if it's not an open shift if (shift.VolunteerProfileId != null || shift.VolunteerProfileId > 0) { // if shift is recurring, check each child shift to see if the recurrence period requires reminders if (shift.IsRecurrence) { var recurrenceDates = RecurrenceHelper.GetRecurrenceDateTimeCollection(shift.RecurrenceRule, shift.StartTime); if (recurrenceDates.Any(d => d > DateTime.Now.AddDays(1).Date)) { var volunteerAccount = _context.Users.FirstOrDefault(u => u.VolunteerProfile.Id == shift.VolunteerProfileId); ScheduleReminderForRecurringShifts(volunteerAccount, shift); } } // if it isn't recurring then simply schedule a reminder else if (shift.StartTime > DateTime.Now.AddDays(1).Date) { var volunteerAccount = _context.Users.FirstOrDefault(u => u.VolunteerProfile.Id == shift.VolunteerProfileId); ScheduleReminderForSingleShift(volunteerAccount, shift); } } _logger.LogInformation($"A new reminder was successfully scheduled for shift {shift.Id} on {shift.StartTime.Date.AddHours(-6)}"); return(true); } catch (Exception ex) { _logger.LogError($"Failed to schedule a new reminder for shift {shift.Id} on {shift.StartTime.Date.AddHours(-6)} \n Error Message: {ex.Message}"); return(true); } }
private async Task ScheduleReminderForRecurringShifts(AppUser volunteer, Shift shift) { // then schedule a reminder for each child shift that needs one var childShiftDates = RecurrenceHelper.GetRecurrenceDateTimeCollection(shift.RecurrenceRule, shift.StartTime); foreach (var date in childShiftDates) { // only schedule a reminder if the date is the next day or after, and if there isn't already a scheduled reminder // there might already be a scheduled reminder since the caller might removing a shift from a recurring set bool isFutureShift = date > DateTime.Now.AddDays(1).Date; bool noScheduledReminder = !_context.Reminders.Any(r => r.ShiftId == shift.Id && r.ShiftDate == date); if (isFutureShift && noScheduledReminder) { string idForRecurring = ""; if (!_isTesting) { idForRecurring = BackgroundJob.Schedule(() => _emailSender.SendEmailAsync(volunteer.Email, "Volunteering Reminder - MHFB", CreateEmail(volunteer, shift)), date.AddHours(-6)); } var reminders = await _context.Reminders.ToListAsync(); idForRecurring = (reminders.Max(p => p.Id) + 1).ToString(); _context.Add(new Reminder() { ShiftId = shift.Id, ShiftDate = date, HangfireJobId = idForRecurring }); } } _context.SaveChanges(); }
public JsonResult getDates(string dates) { var recurrenceRule = JsonConvert.DeserializeObject <string>(dates); var dateCollection = RecurrenceHelper.GetRecurrenceDateTimeCollection(recurrenceRule, DateTime.Now); return(Json(dateCollection, JsonRequestBehavior.AllowGet)); }
public async Task <string> SaveScheduleValidation(InspectionScheduleViewModel data) { var testInspectionSchedule = new InspectionSchedule { InspectionScheduleId = data.Id, ProcessId = data.ProcessId, StartDate = data.StartTime, Timeslot = (await _prepareService.GetTimeslots(data.TimeslotId)).FirstOrDefault(), RecurrenceRule = data.RecurrenceRule, RecurrenceException = data.RecurrenceException }; var schedules = (await _prepareService.GetInspectionSchedulesNonFilter()).Where(_ => _.ProcessId == data.ProcessId); if (data.Id > 0) { schedules = schedules.Where(s => s.InspectionScheduleId != data.Id); } //Check validation if schedule data have same inspection in the same timeslot in the same day + recurrence var dataDates = RecurrenceHelper.GetRecurrenceDateTimeCollection(testInspectionSchedule); foreach (var schedule in schedules) { var dates = RecurrenceHelper.GetRecurrenceDateTimeCollection(schedule); var intersect = dataDates.Intersect(dates); if (intersect.Any()) { return("L'horaire pour l'inspection sélectionnée, le créneau horaire et la date existe déjà"); } } return(string.Empty); }
public List <Shift> GetWorkablShiftsForVolunteer(VolunteerProfile volunteer) { // find all nonrecurring shifts that agree with the given availabilites if (_context.Shifts.Any()) { _context.Entry(volunteer).Collection(v => v.Availabilities).Load(); _context.Entry(volunteer).Reference(v => v.Positions).Load(); List <Shift> availableShifts = new List <Shift>(); foreach (var shift in _context.Shifts.Where(s => string.IsNullOrEmpty(s.RecurrenceRule))) { if (shift.Volunteer == null && shift.StartTime > DateTime.Now && volunteer.Availabilities .Any(a => shift.StartTime.TimeOfDay >= a.StartTime && shift.EndTime.TimeOfDay <= a.EndTime && Enum.GetName(typeof(DayOfWeek), shift.StartTime.DayOfWeek).ToLower() == a.AvailableDay) && volunteer.Positions.Any(p => p.Id == shift.PositionId)) { availableShifts.Add(shift); } } // find all recurring shifts that agree with the given availabilities foreach (var recurringShift in _context.Shifts.Where(s => !string.IsNullOrWhiteSpace(s.RecurrenceRule))) { var childShiftDates = RecurrenceHelper.GetRecurrenceDateTimeCollection(recurringShift.RecurrenceRule, recurringShift.StartTime); foreach (var date in childShiftDates) { bool dateIsAvailable = recurringShift.Volunteer == null && date > DateTime.Now.Date.AddDays(1) && volunteer.Availabilities .Any(a => recurringShift.StartTime.TimeOfDay >= a.StartTime && recurringShift.EndTime.TimeOfDay <= a.EndTime) && volunteer.Positions.Any(p => p.Id == recurringShift.PositionId); if (dateIsAvailable) { var availableShift = new Shift() { StartTime = date + recurringShift.StartTime.TimeOfDay, EndTime = date + recurringShift.EndTime.TimeOfDay, Position = recurringShift.Position }; availableShifts.Add(availableShift); } } } // order shifts by ascending date return(availableShifts.OrderBy(s => s.StartTime).ToList()); } return(null); }
public IActionResult Put(ScheduleModel model) { model.UserProfileId = User.Identity.GetUserProfileId() ?? default(long); if (ModelState.IsValid) { var dates = model.RecurrenceRule != null?RecurrenceHelper.GetRecurrenceDateTimeCollection(model.RecurrenceRule, model.StartDate) : null; var result = _scheduleService.EditSchedule(model, dates); return(Json(result)); } return(PartialView("_EditPartial", model)); }
public async Task <IActionResult> GetSalary(Guid userId, DateTime from, DateTime to) { var result = (await _schedulerAppointmentService.GetAllAsync(userId)) .Select((x) => new SchedulerAppointmentEntity { TaskName = x.task_name, Price = x.price, Start = x.start, End = x.end, RecurrenceId = x.recurrence_id, RecurrenceRule = x.recurrence_rule, RecurrenceException = x.recurrence_exception, IsAllDay = x.is_all_day, IsOff = x.is_off, Notes = x.notes }) .Where(x => x.Start.Date >= from.Date && x.Start.Date <= to.Date) .ToList(); var eventItems = result .Where(x => string.IsNullOrEmpty(x.RecurrenceRule)) .Select(x => new SalaryItem { TaskName = x.TaskName, Price = x.Price, Start = x.Start, End = x.End, IsAllDay = x.IsAllDay, IsOff = x.IsOff, Notes = x.Notes }) .ToList(); var recurrenceEvents = result.Where(x => !string.IsNullOrEmpty(x.RecurrenceRule)); foreach (var item in recurrenceEvents) { IEnumerable <DateTime> fromCollection = null; var itemCount = 0; var loopAttempt = 0; do { itemCount += 100; if (!string.IsNullOrEmpty(item.RecurrenceException)) { fromCollection = RecurrenceHelper.GetRecurrenceDateTimeCollection(item.RecurrenceRule, item.Start, item.RecurrenceException, itemCount) .Where(x => x.Date <= to.Date); } else { fromCollection = RecurrenceHelper.GetRecurrenceDateTimeCollection(item.RecurrenceRule, item.Start, itemCount) .Where(x => x.Date <= to.Date); } loopAttempt++; } while (loopAttempt < 2 && fromCollection.Count() == 0); var recurrenceItems = fromCollection.Select(x => new SalaryItem { TaskName = item.TaskName, Price = item.Price, Start = new DateTime(x.Date.Year, x.Date.Month, x.Date.Day, item.Start.Hour, item.Start.Minute, item.Start.Second), End = new DateTime(x.Date.Year, x.Date.Month, x.Date.Day, item.End.Hour, item.End.Minute, item.End.Second), IsAllDay = item.IsAllDay, IsOff = item.IsOff, Notes = item.Notes }) .ToList(); eventItems.AddRange(recurrenceItems); } return(Ok(eventItems.OrderByDescending(x => x.Start))); }