public async Task CreateShift_ReportsErrorAndReturns_IfIncompleteShift() { var calendarService = GetCalendarService(); var newShiftNoPos = new ShiftReadEditDto() { StartTime = DateTime.Now.AddHours(6), EndTime = DateTime.Now.AddHours(12) }; var newShiftNoStart = new ShiftReadEditDto() { StartTime = DateTime.Now.AddHours(6), PositionId = 3 }; var newShiftNoEnd = new ShiftReadEditDto() { EndTime = DateTime.Now.AddHours(12), PositionId = 3 }; var insertParams = new Dictionary <string, object>(); string resultNoPos = await calendarService.CreateShift(newShiftNoPos, insertParams); string resultNoStart = await calendarService.CreateShift(newShiftNoStart, insertParams); string resultNoEnd = await calendarService.CreateShift(newShiftNoEnd, insertParams); int dbShiftsAmount = (await calendarService.GetShifts()).Count; Assert.Equal("The provided data for the new shift was missing either a StartTime, Endtime or Position", resultNoPos); Assert.Equal("The provided data for the new shift was missing either a StartTime, Endtime or Position", resultNoStart); Assert.Equal("The provided data for the new shift was missing either a StartTime, Endtime or Position", resultNoEnd); Assert.Equal(0, dbShiftsAmount); }
public async Task <string> UpdateShift(ShiftReadEditDto newShiftDto) { var shiftBeingUpdated = _context.Shifts.FirstOrDefault(c => c.Id == Convert.ToInt32(newShiftDto.Id)); if (shiftBeingUpdated == null) { return("The shift to be updated could not be found."); } // this method cancels the reminder for the shift being removed as a side effect. // The side effect is necessary to prevent having to calculate the exDate list more than once bool?shiftIsBeingRemovedFromRecurringSet = await CheckIfShiftIsBeingRemovedFromRecurringSet(newShiftDto, shiftBeingUpdated); if (shiftIsBeingRemovedFromRecurringSet == null) { return("The shift to be updated could not be found."); } // this runs in the standard case, a shift model's data is simply being changed if (shiftIsBeingRemovedFromRecurringSet == false) { // cancel old reminder if old version of shift had a volunteer if (shiftBeingUpdated.VolunteerProfileId != null) { _reminderManager.CancelReminder(shiftBeingUpdated); } bool initialUpdateSuccessful = UpdateShiftProperties(newShiftDto, shiftBeingUpdated); if (!initialUpdateSuccessful) { return("The data provided to update the shift with was incomplete: either the Startime or EndTime was null."); } var chosenPosition = _context.Positions.FirstOrDefault(p => p.Id == shiftBeingUpdated.PositionId); var chosenVolunteer = shiftBeingUpdated.VolunteerProfileId != null?_context.VolunteerProfiles .FirstOrDefault(p => p.Id == shiftBeingUpdated.VolunteerProfileId) : null; shiftBeingUpdated.Subject = shiftBeingUpdated.VolunteerProfileId == null ? $"Open - {chosenPosition.Name}" : $"{chosenVolunteer.FirstName} {chosenVolunteer.LastName} - {chosenPosition.Name}"; await _context.SaveChangesAsync(); //create new reminder if new version of shift has volunteer and if this isn't a request to delete a child shift from its recurring set if (shiftBeingUpdated.VolunteerProfileId != null || shiftBeingUpdated.VolunteerProfileId > 0) { bool reminderScheduledSuccessfully = _reminderManager.ScheduleReminder(shiftBeingUpdated); if (!reminderScheduledSuccessfully) { return("Something went wrong when trying to schedule the reminder for the updated shift."); } } return("The selected shift was successfully updated."); } return("The shift was successfully removed from the recurring set."); }
public static ShiftReadEditDto GenerateShiftDtoForUpdate(MainContext context) { var dto = new ShiftReadEditDto() { Id = context.Shifts.FirstOrDefault().Id, PositionId = context.Positions.FirstOrDefault().Id, StartTime = new DateTime(1, 1, 1, 5, 0, 0), EndTime = new DateTime(1, 1, 1, 15, 0, 0), Subject = "Updated Test Shift", }; return(dto); }
private void UpdateShftProperties(Shift shift, ShiftReadEditDto dto) { shift.StartTime = dto.StartTime; shift.EndTime = dto.EndTime; shift.MemberProfileId = dto.MemberProfileId; shift.PositionId = dto.PositionId; shift.IsRecurrence = dto.IsRecurrence; shift.RecurrenceID = dto.RecurrenceID; shift.RecurrenceException = dto.RecurrenceException; shift.RecurrenceRule = dto.RecurrenceRule; shift.Subject = dto.Subject; shift.Description = dto.Description; shift.IsAllDay = dto.IsAllDay; shift.IsBlock = dto.IsBlock; }
private bool UpdateShiftProperties(ShiftReadEditDto newShiftDto, Shift shiftBeingUpdated) { if (newShiftDto.StartTime == null || newShiftDto.EndTime == null) { return(false); } shiftBeingUpdated.StartTime = newShiftDto.StartTime.AddHours(-6); shiftBeingUpdated.EndTime = newShiftDto.EndTime.AddHours(-6); shiftBeingUpdated.VolunteerProfileId = newShiftDto.VolunteerProfileId; shiftBeingUpdated.PositionId = newShiftDto.PositionId; shiftBeingUpdated.IsAllDay = newShiftDto.IsAllDay; shiftBeingUpdated.RecurrenceRule = newShiftDto.RecurrenceRule; shiftBeingUpdated.RecurrenceID = newShiftDto.RecurrenceID; shiftBeingUpdated.RecurrenceException = newShiftDto.RecurrenceException; shiftBeingUpdated.IsRecurrence = !string.IsNullOrWhiteSpace(shiftBeingUpdated.RecurrenceRule); return(true); }
public async Task <string> CreateShift(ShiftReadEditDto newShiftDto, IDictionary <string, object> insertParams) { bool incompleteShift = newShiftDto.StartTime.Year == 1 || newShiftDto.EndTime.Year == 1 || ((newShiftDto.PositionId == null || newShiftDto.PositionId == 0) && newShiftDto.PositionWorked == null); if (incompleteShift) { _logger.LogError("The provided data for the new shift was missing either a StartTime, Endtime or Position"); return("The provided data for the new shift was missing either a StartTime, Endtime or Position"); } // this must be set manually to prevent errors when adding it to the context newShiftDto.Id = 0; // the datetime data from syncfusion's calendar is consistently 6 hours behind, // so it is corrected here newShiftDto.StartTime = newShiftDto.StartTime.AddHours(-6); newShiftDto.EndTime = newShiftDto.EndTime.AddHours(-6); var newShift = _mapper.Map <Shift>(newShiftDto); // check if the the request requires multiple shifts to be created bool multipleShifts = false; if (insertParams.ContainsKey("multiShifts")) { multipleShifts = Convert.ToBoolean(insertParams["multiShifts"]); } if (multipleShifts) { await CreateMultipleShifts(newShift, insertParams); return("A new shift has successfully been created"); } else { await CreateSingleShift(newShift); return("Multiple shifts has successfully been created."); } }
// private methods exclusive to this class // this method cancels the reminder for the shift being removed as a side effect private async Task <bool?> CheckIfShiftIsBeingRemovedFromRecurringSet(ShiftReadEditDto newShiftDto, Shift shiftBeingUpdated) { // determine if the shift selected for change is a shift that's being removed from its recurring set. // this is done by comparing the amount of exdates in the old recshift to the amount in the new recshift. // if the new shift has more exdates than the old shift, a child shift is being removed var oldExDates = shiftBeingUpdated.RecurrenceException == null ? new List <DateTime>() : RecurrenceHelper.ConvertExDateStringToDateTimes(shiftBeingUpdated.RecurrenceException); var newExDates = newShiftDto.RecurrenceException == null ? new List <DateTime>() : RecurrenceHelper.ConvertExDateStringToDateTimes(newShiftDto.RecurrenceException); bool shiftIsBeingRemovedFromRecurringSet = newExDates.Count() > oldExDates.Count(); // if shift is being removed, pass in the date of the last shift excluded when cancelling reminder and update exdate list try { if (shiftIsBeingRemovedFromRecurringSet) { var newExDate = newExDates.Last(); _reminderManager.CancelReminder(shiftBeingUpdated, newExDate); shiftBeingUpdated.RecurrenceException = newShiftDto.RecurrenceException; await _context.SaveChangesAsync(); return(true); } else { return(false); } } catch (Exception ex) { _logger.LogError($"Something went wrong when trying to cancel a reminder for a shift being removed from its recurring set. \nError Message:\n{ex.Message}"); return(null); } }
public async Task CreateShift_ReportsSuccessAndPopulatesDB_IfSingleShiftCreationWasSuccessful() { var calendarService = GetCalendarService(); var insertParams = new Dictionary <string, object>(); var newShift = new ShiftReadEditDto() { StartTime = DateTime.Now.AddHours(6), EndTime = DateTime.Now.AddHours(12), PositionId = 3 }; string result = await calendarService.CreateShift(newShift, insertParams); var shifts = (await calendarService.GetShifts()); Assert.Equal(DateTime.Now.AddHours(6), shifts[0].StartTime); Assert.Equal(DateTime.Now.AddHours(12), shifts[0].EndTime); Assert.Equal(3, shifts[0].PositionId); Assert.Equal("A new shift has successfully been created", result); var testServiceProvider = new TestServiceProvider(); var config = testServiceProvider.GetConfig(); var contextOptionsBuilder = new DbContextOptionsBuilder <FoodBankContext>() .UseMySql(config.GetConnectionString("MainDevConnection")); var context = new FoodBankContext(contextOptionsBuilder.Options); using (context) { foreach (var shift in shifts) { var currentShift = await context.Shifts.FirstOrDefaultAsync(s => s.Id == shift.Id); context.Remove(currentShift); } await context.SaveChangesAsync(); } }