private async Task SetTimeOffReasonAsync(List <TimeOffModel> timeOffRecords, List <TimeOffModel> savedTimeOffRecords, TeamActivityModel activityModel, ILogger log) { var timeOffRecsToUpdate = timeOffRecords.Where(t => string.IsNullOrEmpty(t.TeamsTimeOffReasonId)); // use the saved time off records to get the teams time off reason foreach (var timeOffRecToUpdate in timeOffRecsToUpdate) { timeOffRecToUpdate.TeamsTimeOffReasonId = savedTimeOffRecords.FirstOrDefault(s => s.WfmTimeOffTypeId == timeOffRecToUpdate.WfmTimeOffTypeId)?.TeamsTimeOffReasonId; } // get the remaining unmapped time off records, if any timeOffRecsToUpdate = timeOffRecsToUpdate.Where(t => string.IsNullOrEmpty(t.TeamsTimeOffReasonId)).ToList(); if (!timeOffRecsToUpdate.Any()) { return; } // get the set of all time off reasons from Teams var timeOffReasons = await _teamsService.ListTimeOffReasonsAsync(activityModel.TeamId).ConfigureAwait(false); // process the distinct set of reasons from the remaining records to update foreach (var wfmTimeOff in timeOffRecsToUpdate.Distinct(t => t.WfmTimeOffTypeId)) { var timeOffReason = timeOffReasons.Find(t => t.Reason.Equals(wfmTimeOff.WfmTimeOffReason, StringComparison.OrdinalIgnoreCase)); if (timeOffReason == null) { // this reason does not exist in Teams, so create it try { timeOffReason = new TimeOffReasonModel { Reason = wfmTimeOff.WfmTimeOffReason }; timeOffReason = await _teamsService.CreateTimeOffReasonAsync(activityModel.TeamId, timeOffReason).ConfigureAwait(false); } catch (Exception ex) { // we failed to create this time off reason so skip this one and continue // with the next log.LogTimeOffReasonError(ex, activityModel, timeOffReason); continue; } } // update all time off records with this reason with the teams time off reason id foreach (var timeOffRec in timeOffRecsToUpdate.Where(t => t.WfmTimeOffTypeId == wfmTimeOff.WfmTimeOffTypeId)) { timeOffRec.TeamsTimeOffReasonId = timeOffReason.TeamsTimeOffReasonId; } } }
public static void LogTimeOffReasonError(this ILogger log, Exception ex, TeamActivityModel activityModel, TimeOffReasonModel timeOffReason) { log.LogError(EventIds.TimeOffReason, ex, "TimeOffReason: Status={status}, SourceId={sourceId}, Reason={timeOffReason}, TeamId={teamId}, Year={year}", Status.Failed, timeOffReason.WfmTimeOffReasonId, timeOffReason.Reason, activityModel.TeamId, activityModel.DateValue); }
public async Task <TimeOffReasonModel> CreateTimeOffReasonAsync(string teamId, TimeOffReasonModel timeOffType) { var client = _clientFactory.CreateClient(_options, teamId); var request = new TimeOffReasonRequest { DisplayName = timeOffType.Reason, IsActive = true }; var response = await client.CreateTimeOffReasonAsync(request, teamId).ConfigureAwait(false); timeOffType.TeamsTimeOffReasonId = response.Id; return(timeOffType); }