private async Task <IList <ReportHours> > AddItemsToDataListAsync(IList <GraphResultItem> graphResults, string dateQuery) { try { var reportHoursList = new List <ReportHours>(); foreach (var item in graphResults) { object objectValue; if (item.Properties.TryGetValue("ObjectIdentifier", out objectValue)) { // Get the site list var workHoursSiteList = await _graphSharePointService.GetSiteListAsync(item.Properties["ObjectIdentifier"].ToString(), ListSchema.WorkHoursListSchema); var workHoursResults = await _graphSharePointService.GetSiteListItemsAsync(workHoursSiteList, dateQuery); var userBasicInfo = await _graphUserService.GetUserBasicAsync(item.Properties["ObjectIdentifier"].ToString()); var managerBasicInfo = new GraphResultItem { Id = "", DisplayName = "" }; if (item.Properties.TryGetValue("ManagerObjectIdentifier", out objectValue)) { managerBasicInfo = await _graphUserService.GetUserBasicAsync(objectValue.ToString()); } if (workHoursResults?.Count > 0) { foreach (var workHoursItem in workHoursResults) { var reportHours = new ReportHours { Id = workHoursItem.Id, ListId = workHoursSiteList.ListId, Fields = ConvertToReportHours(workHoursItem, managerBasicInfo, userBasicInfo) }; reportHoursList.Add(reportHours); } } } } return(reportHoursList); } catch (Exception ex) { _logger.LogError("Error getting report hour items in repository AddItemsToDataListAsync: " + ex.Message); throw; } }
public async Task <ListCollectionPage <TeamHours> > GetNextItemsAsync(ListCollectionPage <TeamHours> listCollectionPage) { try { // Try fetching the data from cache listCollectionPage.CurrentPageIndex++; var cacheKey = listCollectionPage.CurrentPageIndex.ToString() + "_" + listCollectionPage.ObjectIdentifier + listCollectionPage.QueryDate; var cacheEntry = await _teamHoursCache.TryRetrieveFromCacheAsync(listCollectionPage, cacheKey); if (cacheEntry != null) { return(cacheEntry); } if (String.IsNullOrEmpty(listCollectionPage.SkipToken)) { return(null); // No more entries TODO: process not submitted } // TeamHours entry not in cache listCollectionPage.DataList = new List <TeamHours>(); // Get all Team hours for the date if not in memory if (_teamHoursDataList?.Count == 0) { var teamHoursResults = await _graphSharePointService.GetSiteListItemsAsync(listCollectionPage.SiteList, listCollectionPage.QueryDate); foreach (var teamHoursItem in teamHoursResults) { var teamHoursFields = ConvertToTeamHours(teamHoursItem); var userInfo = await _graphUserService.GetUserBasicAsync(teamHoursFields.ObjectIdentifier); teamHoursFields.DisplayName = userInfo.DisplayName ?? ""; _teamHoursDataList.Add(new TeamHours { Id = teamHoursItem.Id, ListId = listCollectionPage.SiteList.ListId, Fields = teamHoursFields }); } } // Call graph for direct reports next page var directReports = await _graphUserService.GetUserDirectReportsAsync(listCollectionPage.ObjectIdentifier, listCollectionPage.SkipToken, listCollectionPage.PageSize); listCollectionPage.SkipToken = directReports.SkipToken; var teamHoursDataList = new List <TeamHours>(); foreach (var item in directReports.DataList) { var teamHoursId = String.Empty; var teamHoursFields = new TeamHoursFields { ObjectIdentifier = item.Id, Date = listCollectionPage.QueryDate, ItemState = ItemState.NotSubmitted, TeamHoursItemState = ItemState.NotSubmitted }; foreach (var teamHoursItem in _teamHoursDataList) { if (teamHoursItem.Fields.ObjectIdentifier == item.Id) { teamHoursId = teamHoursItem.Id; teamHoursFields = teamHoursItem.Fields; } } if (String.IsNullOrEmpty(teamHoursFields.DisplayName)) { var userInfo = await _graphUserService.GetUserBasicAsync(teamHoursFields.ObjectIdentifier); teamHoursFields.DisplayName = userInfo.DisplayName ?? ""; } teamHoursDataList.Add(new TeamHours { Id = teamHoursId, ListId = listCollectionPage.SiteList.ListId, Fields = teamHoursFields }); } listCollectionPage.DataList = teamHoursDataList; // Save to cache await _teamHoursCache.SaveToCacheAsync(listCollectionPage, cacheKey); return(listCollectionPage); } catch (Exception ex) { _logger.LogError("Error getting team hour items in repository: " + ex.Message); throw; } }
public async Task RequestHoursReviewAsync(IEnumerable <TeamHours> teamHoursToReview, string messageBody = "") { try { // Get the SpSiteList, if the list does not exists, it will create one var userProfile = await _userProfileRepository.GetItemAsync(); var managerObjectIdentifier = userProfile.Id; //var teamHoursSiteList = await _graphSharePointService.GetSiteListAsync(managerObjectIdentifier, ListSchema.TeamHoursListSchema); var workHoursDate = DateTime.Now; var teamHoursToReviewList = teamHoursToReview.ToList(); //var dateKey = teamHoursToReviewList.First().Fields.Date.Remove(6).ToString(); //var cacheKey = managerObjectIdentifier + dateKey; //await _cacheService.ClearCacheAsync(cacheKey); //// Persist to SharePoint foreach (var item in teamHoursToReview) { // Update the workHoursDate which is used to set the month in the message body //workHoursDate = DateTime.ParseExact(item.Fields.Date, "yyyyMMdd", CultureInfo.InvariantCulture); //// Create JSON object to update Team Hours in SharePoint //dynamic teamHoursFieldsObject = new JObject(); //teamHoursFieldsObject.ItemState = ItemState.RequiresRevision.ToString(); //dynamic teamHoursRootObject = new JObject(); //teamHoursRootObject.fields = teamHoursFieldsObject; //// Update List Item in TeamHours List //var teamHoursItemId = _graphSharePointService.UpdateSiteListItemAsync(teamHoursSiteList, item.Id, teamHoursRootObject.ToString()); //// Update work hours in sharepoint with request revision var userObjectIdentifier = item.Fields.ObjectIdentifier; //var workHoursSiteList = await _graphSharePointService.GetSiteListAsync(userObjectIdentifier, ListSchema.WorkHoursListSchema); //var dateFix = item.Fields.Date.Remove(6).ToString(); //var workHoursToUpdate = await _graphSharePointService.GetSiteListItemsAsync(workHoursSiteList, dateFix); //foreach (var workHoursItem in workHoursToUpdate) //{ // // Create JSON object to update Work Hours in SharePoint // dynamic workHoursFieldsObject = new JObject(); // workHoursFieldsObject.ItemState = ItemState.RequiresRevision.ToString(); // dynamic workHoursRootObject = new JObject(); // workHoursRootObject.fields = workHoursFieldsObject; // // Update List Item in WorkHours List // var workHoursItemId = _graphSharePointService.UpdateSiteListItemAsync(workHoursSiteList, workHoursItem.Id, workHoursRootObject.ToString()); //} // Create notification and send email var sendToObject = await _graphUserService.GetUserBasicAsync(userObjectIdentifier); messageBody = _workflowServiceHelper.ComposeMessageBody(NotificationType.RequestWorkHoursRevision, workHoursDate, messageBody); await _workflowServiceHelper.SendNotificationAsync(sendToObject, NotificationType.RequestWorkHoursRevision, messageBody); } } catch (Exception ex) { _logger.LogError("Error saving team hours in submit: " + ex.Message); throw ex; } }
// Private methods private async Task <Notifications> AddFromGraphResultsItem(GraphResultItem graphResultsItem, string listId) { // TODO: See if we can refactor into an extension try { if (graphResultsItem == null) { throw new ArgumentNullException(nameof(graphResultsItem)); } object objectValue; var notificationsFields = new NotificationsFields(); var notification = new Notifications { Id = graphResultsItem.Id, ListId = listId }; if (graphResultsItem.Properties.TryGetValue("SentToMail", out objectValue)) { notificationsFields.SentToMail = objectValue.ToString(); } if (graphResultsItem.Properties.TryGetValue("SentToName", out objectValue)) { notificationsFields.SentToName = objectValue.ToString(); } if (graphResultsItem.Properties.TryGetValue("SentFromMail", out objectValue)) { notificationsFields.SentFromMail = objectValue.ToString(); } if (graphResultsItem.Properties.TryGetValue("SentFromName", out objectValue)) { notificationsFields.SentFromName = objectValue.ToString(); } if (graphResultsItem.Properties.TryGetValue("MessageBody", out objectValue)) { notificationsFields.MessageBody = objectValue.ToString(); } if (graphResultsItem.Properties.TryGetValue("ReadDate", out objectValue)) { notificationsFields.ReadDate = DateTime.Parse(objectValue.ToString()); } if (graphResultsItem.Properties.TryGetValue("SentDate", out objectValue)) { notificationsFields.SentDate = DateTime.Parse(objectValue.ToString()); } if (String.IsNullOrEmpty(notificationsFields.SentToName) && !String.IsNullOrEmpty(notificationsFields.SentToMail)) { if (notificationsFields.SentToMail != _timeTrackerOptions.HrNotificationMail) { var toName = await _graphUserService.GetUserBasicAsync(notificationsFields.SentToMail); notificationsFields.SentToName = toName.DisplayName; } else { notificationsFields.SentToName = _timeTrackerOptions.HrNotificationMail; } } if (String.IsNullOrEmpty(notificationsFields.SentFromName) && !String.IsNullOrEmpty(notificationsFields.SentFromMail)) { var toName = await _graphUserService.GetUserBasicAsync(notificationsFields.SentFromMail); notificationsFields.SentFromName = toName.DisplayName; } notification.Fields = notificationsFields; return(notification); } catch (Exception ex) { _logger.LogError("Error mapping a notifiction item from repository Message: " + ex.Message); throw; } }