private async void updateStatus(string status, string id) { dynamic workHoursFieldsObject = new JObject(); switch (status) { case "scheduled": workHoursFieldsObject.DailyUpdateStatus = "scheduled"; break; case "updated": workHoursFieldsObject.DailyUpdateStatus = "scheduled"; workHoursFieldsObject.UpdateDate = DateTime.Now; break; case "inprogress": workHoursFieldsObject.DailyUpdateStatus = "inprogress"; break; } dynamic workHoursRootObject = new JObject(); workHoursRootObject.fields = workHoursFieldsObject; // Update List Item in WorkHours List await _graphSharePointService.UpdateSiteListItemAsync(_usersSiteList, id, workHoursRootObject.ToString()); }
private async Task updateStatus(string status, string id) { dynamic workHoursFieldsObject = new JObject(); switch (status) { case "updated": workHoursFieldsObject.AutoSubmitStatus = "Complete"; workHoursFieldsObject.SubmitDate = DateTime.Now; break; case "inprogress": workHoursFieldsObject.AutoSubmitStatus = "Inprogress"; break; } dynamic workHoursRootObject = new JObject(); workHoursRootObject.fields = workHoursFieldsObject; // Update List Item in WorkHours List await _graphSharePointService.UpdateSiteListItemAsync(_usersSiteList, id, workHoursRootObject.ToString()); }
public async Task SubmitHoursAsync(IEnumerable <TeamHours> teamHoursToSubmit, string messageBody = "") { try { if (teamHoursToSubmit == null) { throw new ArgumentNullException(nameof(teamHoursToSubmit)); } var hoursToSubmit = teamHoursToSubmit.ToList(); if (hoursToSubmit?.Count == 0) { return; // Nothing to do } // Get the Team hours site list for current user, 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 reportHoursSiteList = await _graphSharePointService.GetSiteListAsync(_reportHoursListIdentifier, ListSchema.ReportHoursListSchema); // Try fetching the data from cache var dateKey = hoursToSubmit[0].Fields.Date.ToString(); var cacheKey = managerObjectIdentifier + dateKey; var teamHoursSubmitList = teamHoursToSubmit.ToList(); var submittedDate = DateTime.Now; var teamHoursDate = DateTime.Now; foreach (var item in teamHoursToSubmit) { // Hours Submitted, but NO HR Report. //if (item.Fields.TeamHoursItemState == ItemState.NotSubmitted || item.Fields.TeamHoursItemState == ItemState.RequiresRevision) if (item.Fields.TeamHoursItemState == ItemState.NotSubmitted && item.Fields.ItemState == ItemState.Submitted) { // Update teamHoursDate with last date value which is used in the notificationn message body // Date contains a string! yyyyMMdd teamHoursDate = DateTime.ParseExact(item.Fields.Date, "yyyyMMdd", CultureInfo.InvariantCulture); // Create JSON object to update Team Hours in SharePoint dynamic teamHoursFieldsObject = new JObject(); teamHoursFieldsObject.TeamHoursItemState = ItemState.Submitted.ToString(); teamHoursFieldsObject.TeamHoursSubmittedDate = submittedDate; dynamic teamHoursRootObject = new JObject(); teamHoursRootObject.fields = teamHoursFieldsObject; // Create JSON object to create entries in Report Hours in SharePoint dynamic reportHoursFieldsObject = new JObject(); reportHoursFieldsObject.ObjectIdentifier = item.Fields.ObjectIdentifier; reportHoursFieldsObject.ManagerObjectIdentifier = managerObjectIdentifier; reportHoursFieldsObject.Date = item.Fields.Date; reportHoursFieldsObject.TeamHoursItemState = ItemState.Submitted.ToString(); reportHoursFieldsObject.TeamHoursSubmittedDate = submittedDate; reportHoursFieldsObject.ItemState = item.Fields.ItemState.ToString(); reportHoursFieldsObject.SubmittedDate = item.Fields.SubmittedDate; dynamic reportHoursRootObject = new JObject(); reportHoursRootObject.fields = reportHoursFieldsObject; // Update the team hours entry in SharePoint await _graphSharePointService.UpdateSiteListItemAsync(teamHoursSiteList, item.Id, teamHoursRootObject.ToString()); // Create List Item in ReportHours List var createResult = await _graphSharePointService.CreateSiteListItemAsync(reportHoursSiteList, reportHoursRootObject.ToString()); // Get work hours list for this teamHours entry and update the TeamHoursItemState var workHoursSiteList = await _graphSharePointService.GetSiteListAsync(item.Fields.ObjectIdentifier, ListSchema.WorkHoursListSchema); // Works for longer yyyyMMdd date. var dateQuery = item.Fields.Date.Remove(6); var workHoursResult = await _graphSharePointService.GetSiteListItemsAsync(workHoursSiteList, dateQuery); if (workHoursResult?.Count == 0) { throw new ServiceException(new Error { Code = ErrorConstants.Codes.InvalidRequest, Message = "Can't retrieve work hours for a team hours entry" }); } foreach (var workHoursItem in workHoursResult) { // Create JSON object to update Work Hours list in SharePoint dynamic workHoursFieldsObject = new JObject(); workHoursFieldsObject.TeamHoursItemState = ItemState.Submitted.ToString(); workHoursFieldsObject.TeamHoursSubmittedDate = submittedDate; dynamic workHoursRootObject = new JObject(); workHoursRootObject.fields = workHoursFieldsObject; // Update List Item in WorkHours List // var updateResult = await _graphSharePointService.UpdateSiteListItemAsync(workHoursSiteList, workHoursItem.Id, workHoursRootObject.ToString()); await _graphSharePointService.UpdateSiteListItemAsync(workHoursSiteList, workHoursItem.Id, workHoursRootObject.ToString()); } } } // Create notification and send email var sendToObject = new GraphResultItem { Id = _hrListIdentifier, Properties = new Dictionary <string, object> { { "Mail", _timeTrackerOptions.HrNotificationMail }, } }; // Send notification //var messageBody = _workflowServiceHelper.ComposeMessageBody(NotificationType.SubmitTeamHours, teamHoursDate); await _workflowServiceHelper.SendNotificationAsync(sendToObject, NotificationType.SubmitTeamHours, messageBody); } catch (Exception ex) { throw ex; } }
public async Task SubmitHoursAsync(IEnumerable <WorkHours> workHoursToSubmit, string userObjectIdentifier, GraphResultItem managerOfUser, string messageBody = "") { try { if (workHoursToSubmit == null) { throw new ArgumentNullException(nameof(workHoursToSubmit)); } if (((List <WorkHours>)workHoursToSubmit)?.Count == 0) { return; // Nothing to do } if (String.IsNullOrEmpty(userObjectIdentifier)) { return; // Nothing to do } // Try to get the work hours from cache var workHoursListCollectionPage = new ListCollectionPage <WorkHours>(); var dateQuery = workHoursToSubmit.ToList(); // Works for longer Date yyyyMMdd var cacheKey = userObjectIdentifier + dateQuery[0].Fields.Date.Remove(6); //var workHoursCacheEntry = await _cacheService.TryRetrieveFromCacheAsync(workHoursListCollectionPage, cacheKey); //if (workHoursCacheEntry != null) //{ // workHoursListCollectionPage = workHoursCacheEntry; //} var managerObjectIdentifier = managerOfUser.Id ?? ""; var teamHoursSiteList = new SiteList(); if (String.IsNullOrEmpty(managerObjectIdentifier)) { // Skip manager by passing report hours identifier as manager identifier and handling that when building the models for the report by setting the manager display name to blank managerObjectIdentifier = ""; managerOfUser.DisplayName = ""; teamHoursSiteList = await _graphSharePointService.GetSiteListAsync("entries", ListSchema.ReportHoursListSchema); } // Get the SpSiteList, if the list does not exists, it will create one var workHoursSiteList = workHoursListCollectionPage.SiteList; if (String.IsNullOrEmpty(workHoursSiteList.ListId)) { workHoursSiteList = await _graphSharePointService.GetSiteListAsync(userObjectIdentifier, ListSchema.WorkHoursListSchema); } if (!String.IsNullOrEmpty(managerObjectIdentifier)) { teamHoursSiteList = await _graphSharePointService.GetSiteListAsync(managerObjectIdentifier, ListSchema.TeamHoursListSchema); } var teamHoursRow = new TeamHours(); var submittedDate = DateTime.Now; var hasRequestedRevision = false; var tasksList = new List <Task>(); var workHoursDate = DateTime.Now; foreach (var workHours in workHoursToSubmit) { // Only count not submitted and requieres revision if (workHours.Fields.ItemState == ItemState.NotSubmitted || workHours.Fields.ItemState == ItemState.RequiresRevision) { //Update the workHoursDate so we can assamble the message of the notification workHoursDate = DateTime.ParseExact(workHours.Fields.Date, "yyyyMMdd", CultureInfo.InvariantCulture); // To track if there is an entry that has a requiered revision if (!hasRequestedRevision && workHours.Fields.ItemState == ItemState.RequiresRevision) { hasRequestedRevision = true; } teamHoursRow.Fields.Date = workHours.Fields.Date; teamHoursRow.Fields.ObjectIdentifier = workHours.Fields.ObjectIdentifier; teamHoursRow.Fields.MeetingHours += workHours.Fields.MeetingHours; teamHoursRow.Fields.MeetingMinutes += workHours.Fields.MeetingMinutes; if (workHours.Fields.MeetingAdjustedHours != 0 || workHours.Fields.MeetingAdjustedMinutes != 0) { teamHoursRow.Fields.MeetingAdjustedHours += workHours.Fields.MeetingAdjustedHours; teamHoursRow.Fields.MeetingAdjustedMinutes += workHours.Fields.MeetingAdjustedMinutes; } else { teamHoursRow.Fields.MeetingAdjustedHours += workHours.Fields.MeetingHours; teamHoursRow.Fields.MeetingAdjustedMinutes += workHours.Fields.MeetingMinutes; } teamHoursRow.Fields.EmailHours += workHours.Fields.EmailHours; teamHoursRow.Fields.EmailMinutes += workHours.Fields.EmailMinutes; if (workHours.Fields.EmailAdjustedHours != 0 || workHours.Fields.EmailAdjustedMinutes != 0) { teamHoursRow.Fields.EmailAdjustedHours += workHours.Fields.EmailAdjustedHours; teamHoursRow.Fields.EmailAdjustedMinutes += workHours.Fields.EmailAdjustedMinutes; } else { teamHoursRow.Fields.EmailAdjustedHours += workHours.Fields.EmailHours; teamHoursRow.Fields.EmailAdjustedMinutes += workHours.Fields.EmailMinutes; } teamHoursRow.Fields.OtherHours += workHours.Fields.OtherHours; teamHoursRow.Fields.OtherMinutes += workHours.Fields.OtherMinutes; if (workHours.Fields.OtherAdjustedHours != 0 || workHours.Fields.OtherAdjustedMinutes != 0) { teamHoursRow.Fields.OtherAdjustedHours += workHours.Fields.OtherAdjustedHours; teamHoursRow.Fields.OtherAdjustedMinutes += workHours.Fields.OtherAdjustedMinutes; } else { teamHoursRow.Fields.OtherAdjustedHours += workHours.Fields.OtherHours; teamHoursRow.Fields.OtherAdjustedMinutes += workHours.Fields.OtherMinutes; } teamHoursRow.Fields.TeamHoursItemState = ItemState.NotSubmitted; teamHoursRow.Fields.ItemState = ItemState.Submitted; teamHoursRow.Fields.SubmittedDate = submittedDate; // Create JSON object to update WORK HOURS (aka employee entries) in SharePoint dynamic workHoursFieldsObject = new JObject(); if (String.IsNullOrEmpty(managerObjectIdentifier)) { // Skip submit to manager and send to HR since user does not has manager workHoursFieldsObject.TeamHoursItemState = ItemState.Submitted.ToString(); workHoursFieldsObject.TeamHoursSubmittedDate = submittedDate; } workHoursFieldsObject.ItemState = ItemState.Submitted.ToString(); workHoursFieldsObject.SubmittedDate = submittedDate; dynamic workHoursRootObject = new JObject(); workHoursRootObject.fields = workHoursFieldsObject; // Update List Item in WorkHours List await _graphSharePointService.UpdateSiteListItemAsync(workHoursSiteList, workHours.Id, workHoursRootObject.ToString()); } } // Adjust minutes to hours due the sum (above) var timeSpan = new TimeSpan(teamHoursRow.Fields.MeetingHours, teamHoursRow.Fields.MeetingMinutes, 0); teamHoursRow.Fields.MeetingHours = Convert.ToInt16(timeSpan.Hours + (timeSpan.Days * 24)); teamHoursRow.Fields.MeetingMinutes = Convert.ToInt16(timeSpan.Minutes); timeSpan = new TimeSpan(teamHoursRow.Fields.MeetingAdjustedHours, teamHoursRow.Fields.MeetingAdjustedMinutes, 0); teamHoursRow.Fields.MeetingAdjustedHours = Convert.ToInt16(timeSpan.Hours + (timeSpan.Days * 24)); teamHoursRow.Fields.MeetingAdjustedMinutes = Convert.ToInt16(timeSpan.Minutes); timeSpan = new TimeSpan(teamHoursRow.Fields.EmailHours, teamHoursRow.Fields.EmailMinutes, 0); teamHoursRow.Fields.EmailHours = Convert.ToInt16(timeSpan.Hours + (timeSpan.Days * 24)); teamHoursRow.Fields.EmailMinutes = Convert.ToInt16(timeSpan.Minutes); timeSpan = new TimeSpan(teamHoursRow.Fields.EmailAdjustedHours, teamHoursRow.Fields.EmailAdjustedMinutes, 0); teamHoursRow.Fields.EmailAdjustedHours = Convert.ToInt16(timeSpan.Hours + (timeSpan.Days * 24)); teamHoursRow.Fields.EmailAdjustedMinutes = Convert.ToInt16(timeSpan.Minutes); timeSpan = new TimeSpan(teamHoursRow.Fields.OtherHours, teamHoursRow.Fields.OtherMinutes, 0); teamHoursRow.Fields.OtherHours = Convert.ToInt16(timeSpan.Hours + (timeSpan.Days * 24)); teamHoursRow.Fields.OtherMinutes = Convert.ToInt16(timeSpan.Minutes); timeSpan = new TimeSpan(teamHoursRow.Fields.OtherAdjustedHours, teamHoursRow.Fields.OtherAdjustedMinutes, 0); teamHoursRow.Fields.OtherAdjustedHours = Convert.ToInt16(timeSpan.Hours + (timeSpan.Days * 24)); teamHoursRow.Fields.OtherAdjustedMinutes = Convert.ToInt16(timeSpan.Minutes); // Create JSON object to add a new list item in team Hours list in SharePoint dynamic teamHoursFieldsObject = new JObject(); teamHoursFieldsObject.ObjectIdentifier = teamHoursRow.Fields.ObjectIdentifier; teamHoursFieldsObject.Date = teamHoursRow.Fields.Date; teamHoursFieldsObject.MeetingHours = teamHoursRow.Fields.MeetingHours; teamHoursFieldsObject.MeetingMinutes = teamHoursRow.Fields.MeetingMinutes; teamHoursFieldsObject.MeetingAdjustedHours = teamHoursRow.Fields.MeetingAdjustedHours; teamHoursFieldsObject.MeetingAdjustedMinutes = teamHoursRow.Fields.MeetingAdjustedMinutes; teamHoursFieldsObject.EmailHours = teamHoursRow.Fields.EmailHours; teamHoursFieldsObject.EmailMinutes = teamHoursRow.Fields.EmailMinutes; teamHoursFieldsObject.EmailAdjustedHours = teamHoursRow.Fields.EmailAdjustedHours; teamHoursFieldsObject.EmailAdjustedMinutes = teamHoursRow.Fields.EmailAdjustedMinutes; teamHoursFieldsObject.OtherHours = teamHoursRow.Fields.OtherHours; teamHoursFieldsObject.OtherMinutes = teamHoursRow.Fields.OtherMinutes; teamHoursFieldsObject.OtherAdjustedHours = teamHoursRow.Fields.OtherAdjustedHours; teamHoursFieldsObject.OtherAdjustedMinutes = teamHoursRow.Fields.OtherAdjustedMinutes; teamHoursFieldsObject.AdjustedHoursReason = "" + teamHoursRow.Fields.AdjustedHoursReason; if (String.IsNullOrEmpty(managerObjectIdentifier)) { // Skip submit to manager and send to HR since user does not has manager teamHoursFieldsObject.TeamHoursItemState = ItemState.Submitted.ToString(); teamHoursFieldsObject.TeamHoursSubmittedDate = submittedDate; } else { teamHoursFieldsObject.TeamHoursItemState = teamHoursRow.Fields.TeamHoursItemState.ToString(); } teamHoursFieldsObject.ItemState = teamHoursRow.Fields.ItemState.ToString(); teamHoursFieldsObject.SubmittedDate = teamHoursRow.Fields.SubmittedDate; dynamic teamHoursRootObject = new JObject(); teamHoursRootObject.fields = teamHoursFieldsObject; // If submit is not due to a request revision, create team hours entry otherwise we only update the existing one if (hasRequestedRevision) { // Update existing team hours entry (this is the case for requesting revision) List <QueryOption> options = new List <QueryOption>(); options.Add(new QueryOption("filter", @"startswith(fields/Date,'" + teamHoursRow.Fields.Date + "') and startswith(fields/ObjectIdentifier,'" + teamHoursRow.Fields.ObjectIdentifier + "')")); var teamHoursResults = await _graphSharePointService.GetSiteListItemsAsync(teamHoursSiteList, options); var updateResults = await _graphSharePointService.UpdateSiteListItemAsync(teamHoursSiteList, teamHoursResults[0].Id, teamHoursRootObject.ToString()); } else { // Create List Item in TeamHours list var createResults = await _graphSharePointService.CreateSiteListItemAsync(teamHoursSiteList, teamHoursRootObject.ToString()); } // Create notification and send email //var messageBody = _workflowServiceHelper.ComposeMessageBody(NotificationType.SubmitWorkHours, workHoursDate); await _workflowServiceHelper.SendNotificationAsync(managerOfUser, NotificationType.SubmitWorkHours, messageBody); // Clear te cache //await _cacheService.ClearCacheAsync(cacheKey); } catch (Exception ex) { } }
public async Task UpdateTimerHours(string id, DateTime date, string userObjectIdentifier) { try { //get timer hours list. var timerHoursList = new SiteList { SiteId = _timeTrackerOptions.SharePointSiteId, ListId = _timeTrackerOptions.TimerHoursListPrefix + userObjectIdentifier }; //get all timer entries from userTimerHoursList var queryOptions = new List <QueryOption>(); queryOptions.Add(new QueryOption("filter", @"startswith(fields/Date, '" + date.ToString("MM/dd/yyyy") + "')")); var timerHours = await _graphSharePointService.GetSiteListItemsAsync(timerHoursList, queryOptions); HelperMethods helperMethods = new HelperMethods(_context); //get email timer var emailHours = await helperMethods.GetGraphAppMailService(userObjectIdentifier).GetMailItemsAsync(date, date.AddDays(1).AddSeconds(-1)); //get calender hours var calenderHours = await helperMethods.GetGraphAppCalendarService(userObjectIdentifier).GetCalendarEventsAsync(date, date.AddDays(1).AddSeconds(-1)); //variables declared to keep track of the total timer hours info. int calendarTimerHours = 0; int calendarTimerMinutes = 0; int emailTimerHours = 0; int emailTimerMinutes = 0; int otherTimerHours = 0; int otherTimerMinutes = 0; TimeSpan totalTimerHours = new TimeSpan(); string stopTime = string.Empty; // Calculate timer hours if (timerHours != null) { foreach (var timerSet in timerHours) { //handling the case when the user doent click the stop button. if (timerSet.Properties["StopTime"].ToString() == "InProgress") { stopTime = "11:59 PM"; //update stopTime in the list dynamic stopTimeField = new JObject(); stopTimeField.StopTime = stopTime; dynamic stopTimeRootObj = new JObject(); stopTimeRootObj.fields = stopTimeField; await _graphSharePointService.UpdateSiteListItemAsync(timerHoursList, timerSet.Id, stopTimeRootObj.ToString()); } else { stopTime = timerSet.Properties["StopTime"].ToString(); } //get how long the start button was clicked before the user clicked stop. totalTimerHours = totalTimerHours + Convert.ToDateTime(stopTime).TimeOfDay.Subtract(Convert.ToDateTime(timerSet.Properties["StartTime"]).TimeOfDay); //get the calendar hours between the start and the stop click. var calendarTimerSec = from x in calenderHours where Convert.ToDateTime(x.Properties["Start"]).TimeOfDay >= Convert.ToDateTime(timerSet.Properties["StartTime"]).TimeOfDay && Convert.ToDateTime(x.Properties["End"]).TimeOfDay <= Convert.ToDateTime(stopTime).TimeOfDay select x; //get the email hours between the start and the stop click. var emailTimerSec = from x in emailHours where Convert.ToDateTime(x.Properties["DateTime"]).TimeOfDay >= Convert.ToDateTime(timerSet.Properties["StartTime"]).TimeOfDay && Convert.ToDateTime(x.Properties["DateTime"]).TimeOfDay <= Convert.ToDateTime(stopTime).TimeOfDay select x; //Calculate the calendar hours. foreach (var item in calendarTimerSec) { var startTime = Convert.ToDateTime(item.Properties["Start"]); var endTime = Convert.ToDateTime(item.Properties["End"]); var span = endTime.Subtract(startTime); calendarTimerHours = calendarTimerHours + Convert.ToInt16(span.Hours); calendarTimerMinutes = calendarTimerMinutes + Convert.ToInt16(span.Minutes); } //calculate the email hours. int receivedEmailCount = 0; int sentEmailCount = 0; foreach (var item in emailTimerSec) { if (item.Properties["EmailType"].ToString() == "received") { if (Convert.ToBoolean(item.Properties["IsRead"])) { receivedEmailCount = receivedEmailCount + 1; } } else if (item.Properties["EmailType"].ToString() == "sent") { sentEmailCount = sentEmailCount + 1; } } TimeSpan emailspan = TimeSpan.FromMinutes((sentEmailCount * _timeTrackerOptions.SentEmailTime) + (receivedEmailCount * _timeTrackerOptions.ReceivedEmailTime)); emailTimerHours = emailTimerHours + Convert.ToInt16(emailspan.Hours); emailTimerMinutes = emailTimerMinutes + Convert.ToInt16(emailspan.Minutes); } //calculate other hours based on the total email and calendar hours otherTimerHours = totalTimerHours.Hours - (emailTimerHours + calendarTimerHours); if (totalTimerHours.Minutes < (emailTimerMinutes + calendarTimerMinutes)) { if (((emailTimerMinutes + calendarTimerMinutes) - totalTimerHours.Minutes) < 60) { otherTimerMinutes = 60 - ((emailTimerMinutes + calendarTimerMinutes) - totalTimerHours.Minutes); otherTimerHours = otherTimerHours - 1; } else { otherTimerMinutes = ((emailTimerMinutes + calendarTimerMinutes) - totalTimerHours.Minutes) - 60; otherTimerHours = otherTimerHours - 2; } } else { otherTimerMinutes = totalTimerHours.Minutes - (emailTimerMinutes + calendarTimerMinutes); } //get work hours list. var workHoursList = new SiteList { SiteId = _timeTrackerOptions.SharePointSiteId, ListId = _timeTrackerOptions.WorkHoursListPrefix + userObjectIdentifier }; //update workhours list date entry with timer hours. dynamic fieldsObject = new JObject(); fieldsObject.MeetingTimerHours = calendarTimerHours.ToString(); fieldsObject.MeetingTimerMinutes = calendarTimerMinutes.ToString(); fieldsObject.EmailTimerHours = emailTimerHours.ToString(); fieldsObject.EmailTimerMinutes = emailTimerMinutes.ToString(); fieldsObject.OtherTimerHours = otherTimerHours.ToString(); fieldsObject.OtherTimerMinutes = otherTimerMinutes.ToString(); dynamic timerHoursRootObject = new JObject(); timerHoursRootObject.fields = fieldsObject; // Update List Item in WorkHours List await _graphSharePointService.UpdateSiteListItemAsync(workHoursList, id, timerHoursRootObject.ToString()); } } catch (Exception ex) { throw; } }