/// <summary> /// Gets the shifts for a given Kronos user id. /// </summary> /// <param name="kronosUserId">A Kronos user id.</param> /// <param name="queryStartDate">The query start date.</param> /// <param name="queryEndDate">The query end date.</param> /// <returns>The schedule response.</returns> public async Task <App.KronosWfc.Models.ResponseEntities.Shifts.UpcomingShifts.Response> GetShiftsForUser(string kronosUserId, string queryStartDate, string queryEndDate) { App.KronosWfc.Models.ResponseEntities.Shifts.UpcomingShifts.Response shiftsResponse = null; this.utility.SetQuerySpan(Convert.ToBoolean(false, CultureInfo.InvariantCulture), out string shiftStartDate, out string shiftEndDate); var allRequiredConfigurations = await this.utility.GetAllConfigurationsAsync().ConfigureAwait(false); // Check whether date range are in correct format. var isCorrectDateRange = Utility.CheckDates(shiftStartDate, shiftEndDate); if (!isCorrectDateRange) { throw new Exception($"{Resource.SyncShiftsFromKronos} - Query date was invalid."); } if ((bool)allRequiredConfigurations?.IsAllSetUpExists) { var user = new List <ResponseHyperFindResult>() { new ResponseHyperFindResult { PersonNumber = kronosUserId }, }; // Get shift response for a batch of users. shiftsResponse = await this.shiftsActivity.ShowUpcomingShiftsInBatchAsync( new Uri(allRequiredConfigurations.WfmEndPoint), allRequiredConfigurations.KronosSession, queryStartDate, queryEndDate, user).ConfigureAwait(false); } return(shiftsResponse); }
/// <summary> /// Method to process the shift entities in a batch manner. /// </summary> /// <param name="accessToken">The MS Graph Access token.</param> /// <param name="lookUpEntriesFoundList">The lookUp entries that have been found.</param> /// <param name="shiftsNotFoundList">The shifts that have not been found.</param> /// <param name="userModelList">The users list.</param> /// <param name="userModelNotFoundList">The list of users that have not been found.</param> /// <param name="lookUpData">The look up data from the Shift Entity Mapping table.</param> /// <param name="processKronosUsersQueueInBatch">The Kronos users in the queue.</param> /// <param name="shiftsResponse">The Shifts Response from MS Graph.</param> /// <param name="monthPartitionKey">The monthwise partition.</param> /// <returns>A unit of execution.</returns> private async Task ProcessShiftEntitiesBatchAsync( string accessToken, List<TeamsShiftMappingEntity> lookUpEntriesFoundList, List<Shift> shiftsNotFoundList, List<UserDetailsModel> userModelList, List<UserDetailsModel> userModelNotFoundList, List<TeamsShiftMappingEntity> lookUpData, IEnumerable<UserDetailsModel> processKronosUsersQueueInBatch, App.KronosWfc.Models.ResponseEntities.Shifts.UpcomingShifts.Response shiftsResponse, string monthPartitionKey) { this.telemetryClient.TrackTrace($"ShiftController - ProcessShiftEntitiesBatchAsync started at: {DateTime.UtcNow.ToString("O", CultureInfo.InvariantCulture)}"); var shiftDisplayName = string.Empty; var shiftNotes = string.Empty; var shiftTheme = this.appSettings.ShiftTheme; // This foreach loop processes each user in the batch. foreach (var user in processKronosUsersQueueInBatch) { // This foreach loop will process the shift(s) that belong to each user. foreach (var kronosShift in shiftsResponse?.Schedule?.ScheduleItems?.ScheduleShift) { if (user.KronosPersonNumber == kronosShift.Employee.FirstOrDefault().PersonNumber) { this.telemetryClient.TrackTrace($"ShiftController - Processing the shifts for user: {user.KronosPersonNumber}"); List<ShiftActivity> shiftActivity = new List<ShiftActivity>(); // This foreach loop will create the necessary Shift Activities // based on the Shift Segments that are retrieved from Kronos. foreach (var activity in kronosShift?.ShiftSegments) { shiftActivity.Add(new ShiftActivity { IsPaid = true, StartDateTime = this.utility.CalculateStartDateTime(activity), EndDateTime = this.utility.CalculateEndDateTime(activity), Code = string.Empty, DisplayName = activity.SegmentTypeName, }); } var shift = new Shift { UserId = user.ShiftUserId, SchedulingGroupId = user.ShiftScheduleGroupId, SharedShift = new SharedShift { DisplayName = shiftDisplayName, Notes = this.utility.GetShiftNotes(kronosShift), StartDateTime = shiftActivity[0].StartDateTime, EndDateTime = shiftActivity[shiftActivity.Count - 1].EndDateTime, Theme = shiftTheme, Activities = shiftActivity, }, }; shift.KronosUniqueId = this.utility.CreateUniqueId(shift); this.telemetryClient.TrackTrace($"ShiftController-KronosHash: {shift.KronosUniqueId}"); userModelList.Add(user); if (lookUpData.Count == 0) { shiftsNotFoundList.Add(shift); userModelNotFoundList.Add(user); } else { var kronosUniqueIdExists = lookUpData.Where(c => c.KronosUniqueId == shift.KronosUniqueId); if (kronosUniqueIdExists.Any() && (kronosUniqueIdExists != default(List<TeamsShiftMappingEntity>))) { lookUpEntriesFoundList.Add(kronosUniqueIdExists.FirstOrDefault()); } else { shiftsNotFoundList.Add(shift); userModelNotFoundList.Add(user); } } } } } if (lookUpData.Except(lookUpEntriesFoundList).Any()) { await this.DeleteOrphanDataShiftsEntityMappingAsync(accessToken, lookUpEntriesFoundList, userModelList, lookUpData).ConfigureAwait(false); } await this.CreateEntryShiftsEntityMappingAsync(accessToken, userModelNotFoundList, shiftsNotFoundList, monthPartitionKey).ConfigureAwait(false); this.telemetryClient.TrackTrace($"ShiftController - ProcessShiftEntitiesBatchAsync ended at: {DateTime.UtcNow.ToString("O", CultureInfo.InvariantCulture)}"); }