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()); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Admin, "get", Route = "teamHealth/{teamId}")] HttpRequest req, [OrchestrationClient] DurableOrchestrationClient starter, string teamId, ILogger log) { try { var connection = await _scheduleConnectorService.GetConnectionAsync(teamId); var credentials = await _secretsService.GetCredentialsAsync(connection.TeamId); _scheduleSourceService.SetCredentials(connection.TeamId, credentials); var date = DateTime.Today; if (req.Query.ContainsKey("date")) { DateTime.TryParse(req.Query["date"], out date); } var weekStartDate = date.StartOfWeek(_options.StartDayOfWeek); var jdaEmployees = await _scheduleSourceService.GetEmployeesAsync(connection.TeamId, connection.StoreId, weekStartDate); await UpdateEmployeesWithTeamsData(connection.TeamId, jdaEmployees); var cachedShifts = await GetCachedShiftsAsync(connection, weekStartDate); var jobIds = cachedShifts.SelectMany(s => s.Jobs).Select(j => j.JdaJobId).Distinct().ToList(); var jobs = await GetJobsAsync(connection, jobIds); ExpandIds(cachedShifts, jdaEmployees, jobs); var missingUsers = jdaEmployees.Where(e => string.IsNullOrEmpty(e.DestinationId)).ToList(); var missingShifts = await GetMissingShifts(connection, weekStartDate, cachedShifts); jobIds = missingShifts.SelectMany(s => s.Jobs).Select(j => j.JdaJobId).Distinct().ToList(); jobs = await GetJobsAsync(connection, jobIds); ExpandIds(missingShifts, missingUsers, jobs); var teamHealthResponseModel = new TeamHealthResponseModel { TeamId = connection.TeamId, WeekStartDate = weekStartDate, TeamOrchestratorStatus = await starter.GetStatusAsync(connection.TeamId), MissingUsers = missingUsers, MissingShifts = missingShifts, CachedShifts = cachedShifts }; return(new JsonResult(teamHealthResponseModel, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })); } catch (Exception ex) { log.LogError(ex, "Team health failed!"); return(new ContentResult { Content = $"Unexpected exception: {ex.Message}", StatusCode = StatusCodes.Status500InternalServerError }); } }