public async Task <ResultModel> Run([ActivityTrigger] WeekModel weekModel, ILogger log) { log.LogWeek(weekModel); // initialise source service var credentials = await _secretsService.GetCredentialsAsync(weekModel.TeamId); _scheduleSourceService.SetCredentials(weekModel.TeamId, credentials); // get the current set of shifts from JDA var shifts = await _scheduleSourceService.ListWeekShiftsAsync(weekModel.TeamId, weekModel.StoreId, weekModel.StartDate, weekModel.TimeZoneInfoId); log.LogShifts(weekModel, shifts); // get the last saved set of shifts var savedShifts = await _scheduleCacheService.LoadScheduleAsync(weekModel.TeamId, weekModel.StartDate); // compute the delta var delta = _scheduleDeltaService.ComputeDelta(savedShifts.Tracked, shifts); log.LogFullDelta(weekModel, delta); if (delta.HasChanges) { delta.RemoveSkipped(savedShifts.Skipped); delta.ApplyMaximum(_options.MaximumDelta); log.LogPartialDelta(weekModel, delta); var allShifts = delta.All; // set teams employee var employeeLookup = BuildEmployeeLookup(savedShifts.Tracked); await SetTeamsEmployeeIds(allShifts.Where(s => string.IsNullOrEmpty(s.TeamsEmployeeId)), employeeLookup, weekModel, log); // set job & department name var jobLookup = BuildJobLookup(savedShifts.Tracked); await SetJobAndDepartmentName(allShifts, jobLookup, weekModel, log); // set teams schedule group (N.B this must be set after teams employee id and jobs) var groupLookup = BuildScheduleGroupLookup(savedShifts.Tracked); await SetTeamsSchedulingGroupId(allShifts.Where(s => string.IsNullOrEmpty(s.TeamsSchedulingGroupId)), groupLookup, weekModel, log); await AddEmployeesToSchedulingGroups(delta, groupLookup, weekModel, log); // update teams await ApplyDeltaAsync(weekModel, delta, log); log.LogAppliedDelta(weekModel, delta); // apply the final delta to the savedShifts delta.ApplyChanges(savedShifts.Tracked); delta.ApplySkipped(savedShifts.Skipped); await _scheduleCacheService.SaveScheduleAsync(weekModel.TeamId, weekModel.StartDate, savedShifts); } return(delta.AsResult()); }
private async Task <List <ShiftModel> > GetMissingShifts(ConnectionModel connection, DateTime weekStartDate, List <ShiftModel> cachedShifts) { // get the shifts for the week from JDA var jdaShifts = await _scheduleSourceService.ListWeekShiftsAsync(connection.TeamId, connection.StoreId, weekStartDate, connection.TimeZoneInfoId); var cachedShiftIds = cachedShifts.Select(s => s.JdaShiftId).ToList(); return(jdaShifts.Where(s => !cachedShiftIds.Contains(s.JdaShiftId)).ToList()); }