Beispiel #1
0
        public TeamHoursRepository(
            ILogger <TeamHoursRepository> logger,
            IOptions <TimeTrackerOptions> timeTrackerOptions,
            IUserContext userContext,
            GraphAppUserService graphUserService,
            GraphAppSharePointService graphSharePointService,
            GraphAppCalendarService graphCalendarService,
            GraphAppTasksService graphTasksService,
            GraphAppMailService graphMailService,
            IWorkflowService <WorkHours> workHoursWorkflowService,
            ICacheService <ListCollectionPage <TeamHours> > teamHoursCache,
            ICacheService <ListCollectionPage <GraphResultItem> > directReportsCache)
        {
            _logger                   = logger ?? throw new ArgumentNullException(nameof(logger));
            _timeTrackerOptions       = timeTrackerOptions.Value ?? throw new ArgumentNullException(nameof(timeTrackerOptions));
            _userContext              = userContext ?? throw new ArgumentNullException(nameof(userContext));
            _graphUserService         = graphUserService ?? throw new ArgumentNullException(nameof(graphUserService));
            _graphSharePointService   = graphSharePointService ?? throw new ArgumentNullException(nameof(graphSharePointService));
            _graphCalendarService     = graphCalendarService ?? throw new ArgumentNullException(nameof(graphCalendarService));
            _graphTasksService        = graphTasksService ?? throw new ArgumentNullException(nameof(graphTasksService));
            _graphMailService         = graphMailService ?? throw new ArgumentNullException(nameof(graphMailService));
            _workHoursWorkflowService = workHoursWorkflowService ?? throw new ArgumentNullException(nameof(workHoursWorkflowService));
            _teamHoursCache           = teamHoursCache ?? throw new ArgumentNullException(nameof(teamHoursCache));
            _directReportsCache       = directReportsCache ?? throw new ArgumentNullException(nameof(directReportsCache));

            _teamHoursDataList = new List <TeamHours>();
        }
 public TeamHoursRepository(
     TimeTrackerOptions timeTrackerOptions,
     IUserContext userContext,
     GraphAppUserService graphUserService,
     GraphAppSharePointService graphSharePointService,
     GraphAppCalendarService graphCalendarService,
     GraphAppTasksService graphTasksService,
     GraphAppMailService graphMailService,
     String objectIdentifier
     )
 {
     _timeTrackerOptions     = timeTrackerOptions ?? throw new ArgumentNullException(nameof(timeTrackerOptions));
     _userContext            = userContext ?? throw new ArgumentNullException(nameof(userContext));
     _graphUserService       = graphUserService ?? throw new ArgumentNullException(nameof(graphUserService));
     _graphSharePointService = graphSharePointService ?? throw new ArgumentNullException(nameof(graphSharePointService));
     _graphCalendarService   = graphCalendarService ?? throw new ArgumentNullException(nameof(graphCalendarService));
     _graphTasksService      = graphTasksService ?? throw new ArgumentNullException(nameof(graphTasksService));
     _graphMailService       = graphMailService ?? throw new ArgumentNullException(nameof(graphMailService));
     _teamHoursDataList      = new List <TeamHours>();
     _objectIdentifier       = objectIdentifier;//?? throw new ArgumentNullException(nameof(userContext));
 }
 public WorkHoursRepository(
     // IOptions<TimeTrackerOptions> timeTrackerOptions,
     TimeTrackerOptions timeTrackerOptions,
     IUserContext userContext,
     GraphAppUserService graphUserService,
     GraphAppSharePointService graphSharePointService,
     GraphAppCalendarService graphCalendarService,
     GraphAppTasksService graphTasksService,
     GraphAppMailService graphMailService,
     TimezoneHelper timezoneHelper,
     String objectIdentifier
     )
 {
     _timeTrackerOptions     = timeTrackerOptions ?? throw new ArgumentNullException(nameof(timeTrackerOptions));
     _userContext            = userContext ?? throw new ArgumentNullException(nameof(userContext));
     _graphUserService       = graphUserService ?? throw new ArgumentNullException(nameof(graphUserService));
     _graphSharePointService = graphSharePointService ?? throw new ArgumentNullException(nameof(graphSharePointService));
     _graphCalendarService   = graphCalendarService ?? throw new ArgumentNullException(nameof(graphCalendarService));
     _graphTasksService      = graphTasksService ?? throw new ArgumentNullException(nameof(graphTasksService));
     _graphMailService       = graphMailService ?? throw new ArgumentNullException(nameof(graphMailService));
     this.timezoneHelper     = timezoneHelper ?? throw new ArgumentNullException(nameof(timezoneHelper));
     _objectIdentifier       = objectIdentifier;      //?? throw new ArgumentNullException(nameof(userContext));
 }
        public async Task <IEnumerable <WorkHours> > ComputeHours(
            DateTime date, IEnumerable <GraphResultItem> results,
            SiteList siteList,
            string userObjectIdentifier,
            TimeTrackerOptions timeTrackerOptions,
            GraphAppSharePointService graphSharePointService,
            GraphAppCalendarService graphCalendarService,
            GraphAppTasksService graphTasksService,
            GraphAppMailService graphMailService,
            TimezoneHelper timezoneHelper,
            List <string> categoryFilter,
            List <string> showAsFilter,
            int allDayCountHours)
        {
            try
            {
                var workHoursList = new List <WorkHours>();

                // Create a variable to track last day retrieved from SharePoint
                var lastDay = new DateTime(date.Year, date.Month, 1);

                foreach (var item in results)
                {
                    var workHoursFields = ConvertToWorkHours(item, userObjectIdentifier);

                    lastDay = DateTime.ParseExact(workHoursFields.Date, "yyyyMMdd", CultureInfo.InvariantCulture);

                    workHoursList.Add(new WorkHours
                    {
                        Id     = item.Id,
                        Fields = workHoursFields
                    });
                }

                // WorkHours Compute Logic
                //
                // New Logic - retrieve 1 week at a time then pause & repeat.
                // Create Arrays[] to hold partial results.
                // Calculate:   1. BeginDate = beginning of Month
                //              2. EndOfWeek - BeginDate +7 days
                //                  (if EndOfWeek => Now, EndOfWeek = Now, finishWeeks = true)
                //              3. BeginDate = EndOfWeek date +1
                //              4. if !(finishWeeks), pause - to avoid throttling
                //              5. Repeat 2-4.

                var  fullCalendar  = new List <GraphResultItem>();
                var  fullMail      = new List <GraphResultItem>();
                var  fullTaskItems = new List <JToken>();
                bool finishWeeks   = false;

                // Add remaining days till today for calendar
                var endOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));

                if (lastDay.Date != endOfMonth.Date)
                {
                    // Ensure we only calculate until today
                    if (DateTime.Now.Month == date.Month && endOfMonth > DateTime.Now)
                    {
                        endOfMonth = DateTime.Now;
                    }
                    endOfMonth = new DateTime(endOfMonth.Year, endOfMonth.Month, endOfMonth.Day, 23, 59, 59, 999);

                    // Skip lastDay TODO: test when today is the first
                    if (endOfMonth.Date > lastDay.Date && lastDay.Day != 1)
                    {
                        lastDay = lastDay.AddDays(1);
                    }
                    lastDay = new DateTime(lastDay.Year, lastDay.Month, lastDay.Day, 0, 0, 0, 0);

                    // Get calendar items
                    //var calendarResults = await graphCalendarService.GetCalendarEventsAsync(lastDay, endOfMonth);
                    var taskCalendar     = Task.Run(() => graphCalendarService.GetCalendarEventsAsync(lastDay, endOfMonth));
                    var weeklyCalResults = await taskCalendar;
                    if (weeklyCalResults != null)
                    {
                        foreach (var item in weeklyCalResults)
                        {
                            fullCalendar.Add(item);
                        }
                    }

                    // Get Emails in inbox for count
                    //var emailResults = await graphMailService.GetMailItemsAsync(lastDay, endOfMonth);
                    var taskMail          = Task.Run(() => graphMailService.GetMailItemsAsync(lastDay, endOfMonth));
                    var weeklyMailResults = await taskMail;
                    if (weeklyMailResults != null)
                    {
                        foreach (var item in weeklyMailResults)
                        {
                            fullMail.Add(item);
                        }
                    }

                    // Get task items for count
                    //var tasksResults = await graphTasksService.GetUserTasksAsync(lastDay, endOfMonth);

                    // TODO:  Varma:  uncomment the following after ensuring app context is supported
                    //var taskTasksItems = Task.Run(() => graphTasksService.GetUserTasksAsync(lastDay, endOfMonth));
                    //var weeklyTaskItemsResults = await taskTasksItems;
                    //if (weeklyTaskItemsResults != null)
                    //{
                    //    foreach (var item in weeklyTaskItemsResults)
                    //    {
                    //        fullTaskItems.Add(item);
                    //    }
                    //}

                    // *** Loop Back and do more weeks *** //


                    // To track the day that is being calculated in the while
                    var dateToCalculate = lastDay;

                    while (dateToCalculate.Date <= endOfMonth.Date)
                    {
                        var workHoursFields = new WorkHoursFields
                        {
                            Date                   = dateToCalculate.ToString("yyyyMMdd"),
                            ObjectIdentifier       = userObjectIdentifier,
                            TeamHoursItemState     = ItemState.NotSubmitted,
                            ItemState              = ItemState.NotSubmitted,
                            AdjustedHoursReason    = "",
                            TeamHoursSubmittedDate = new DateTime(),
                            SubmittedDate          = new DateTime(),
                            MeetingHours           = 0,
                            MeetingMinutes         = 0,
                            EmailHours             = 0,
                            EmailMinutes           = 0,
                            OtherHours             = 0,
                            OtherMinutes           = 0,
                            OtherTimerHours        = 0,
                            OtherTimerMinutes      = 0,
                            EmailTimerHours        = 0,
                            EmailTimerMinutes      = 0,
                            MeetingTimerHours      = 0,
                            MeetingTimerMinutes    = 0
                        };

                        #region calendar items
                        // var calendarResults = await taskCalendar;

                        // Variables needed for calculating sum for calendar events
                        double totalTimeSpan = 0;
                        var    span          = TimeSpan.FromHours(0);

                        // Calculate time spent in calendar events ////////
                        if (fullCalendar != null)
                        {
                            // TODO: can refactor into a select type statement and remove foreach? var calendarEventsToSum = calendarResults.Select<>
                            foreach (var item in fullCalendar)
                            {
                                // Filter out rules
                                var includeItem = true;
                                var categories  = (List <string>)item.Properties["Categories"];
                                foreach (var value in categories)
                                {
                                    if (categoryFilter.Contains(value))
                                    {
                                        includeItem = false;
                                    }
                                }

                                if (showAsFilter.Contains(item.Properties["ShowAs"].ToString()))
                                {
                                    includeItem = false;
                                }

                                if (includeItem && !Convert.ToBoolean(item.Properties["IsCancelled"]))
                                {
                                    if (Convert.ToBoolean(item.Properties["IsAllDay"]))
                                    {
                                        if (allDayCountHours > 0)
                                        {
                                            totalTimeSpan = totalTimeSpan + allDayCountHours;
                                        }
                                    }
                                    else
                                    {
                                        var startTime = Convert.ToDateTime(item.Properties["Start"]);
                                        var endTime   = Convert.ToDateTime(item.Properties["End"]);

                                        if (startTime.Date == dateToCalculate.Date)
                                        {
                                            span          = endTime.Subtract(startTime);
                                            totalTimeSpan = totalTimeSpan + span.TotalHours;
                                        }
                                    }
                                }
                            }

                            span = TimeSpan.FromHours(totalTimeSpan);

                            workHoursFields.MeetingHours   = Convert.ToInt16(span.Hours);
                            workHoursFields.MeetingMinutes = Convert.ToInt16(span.Minutes);
                        }

                        #endregion

                        #region mail items
                        // var emailResults = await taskMail;

                        // Variables needed for calculating counts of emails
                        var sentEmailCount     = 0;
                        var receivedEmailCount = 0;

                        // Calculate time spent in email //////////////
                        if (fullMail != null)
                        {
                            foreach (var item in fullMail)
                            {
                                // Filter out rules
                                var includeItem = true;
                                var categories  = (List <string>)item.Properties["Categories"];
                                foreach (var category in categories)
                                {
                                    if (categoryFilter.Contains(category))
                                    {
                                        includeItem = false;
                                    }
                                }

                                if (includeItem)
                                {
                                    if (Convert.ToDateTime(item.Properties["DateTime"]).Date == dateToCalculate.Date)
                                    {
                                        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;
                                        }
                                    }
                                }
                            }

                            // Calculate total time in minutes
                            span = TimeSpan.FromMinutes((sentEmailCount * timeTrackerOptions.SentEmailTime) + (receivedEmailCount * timeTrackerOptions.ReceivedEmailTime));
                            workHoursFields.EmailHours   = Convert.ToInt16(span.Hours);
                            workHoursFields.EmailMinutes = Convert.ToInt16(span.Minutes);
                        }

                        #endregion

                        #region task items
                        // var tasksResults = await taskTasksItems;

                        // Variables needed for calculating counts of tasks
                        var tasksCount = 0;

                        if (fullTaskItems?.Count > 0)
                        {
                            // Initialize the Time Zone based on appSettings value.
                            var definedZone = timezoneHelper.timeZoneInfo;
                            // TODO: can refactor into a select type statement and remove foreach? var calendarEventsToSum = calendarResults.Select<>
                            foreach (var item in fullTaskItems)
                            {
                                var dateTime = DateTime.Parse(item["startDateTime"]["dateTime"].ToString());
                                // Adjust for TimeZone stored in appSettings.
                                var adjustedDateTime = TimeZoneInfo.ConvertTime(dateTime, definedZone);
                                if (adjustedDateTime.Date == dateToCalculate.Date)
                                {
                                    tasksCount = tasksCount + 1;
                                }
                            }

                            // Calculate total time in minutes
                            span = TimeSpan.FromMinutes(tasksCount * timeTrackerOptions.TaskTime);
                            workHoursFields.OtherHours   = Convert.ToInt16(span.Hours);
                            workHoursFields.OtherMinutes = Convert.ToInt16(span.Minutes);
                        }

                        #endregion

                        var workHours = new WorkHours
                        {
                            Id     = "",
                            Fields = workHoursFields
                        };

                        // Persist to SharePoint
                        if (dateToCalculate.Date < DateTime.Now.Date)
                        {
                            // Create JSON object
                            dynamic fieldsObject = new JObject();
                            fieldsObject.ObjectIdentifier       = workHoursFields.ObjectIdentifier;
                            fieldsObject.Date                   = workHoursFields.Date;
                            fieldsObject.MeetingHours           = workHoursFields.MeetingHours;
                            fieldsObject.MeetingMinutes         = workHoursFields.MeetingMinutes;
                            fieldsObject.MeetingAdjustedHours   = workHoursFields.MeetingAdjustedHours;
                            fieldsObject.MeetingAdjustedMinutes = workHoursFields.MeetingAdjustedMinutes;
                            fieldsObject.EmailHours             = workHoursFields.EmailHours;
                            fieldsObject.EmailMinutes           = workHoursFields.EmailMinutes;
                            fieldsObject.EmailAdjustedHours     = workHoursFields.EmailAdjustedHours;
                            fieldsObject.EmailAdjustedMinutes   = workHoursFields.EmailAdjustedMinutes;
                            fieldsObject.OtherHours             = workHoursFields.OtherHours;
                            fieldsObject.OtherMinutes           = workHoursFields.OtherMinutes;
                            fieldsObject.OtherAdjustedHours     = workHoursFields.OtherAdjustedHours;
                            fieldsObject.OtherAdjustedMinutes   = workHoursFields.OtherAdjustedMinutes;
                            fieldsObject.AdjustedHoursReason    = workHoursFields.AdjustedHoursReason;
                            fieldsObject.TeamHoursItemState     = workHoursFields.TeamHoursItemState.ToString();
                            fieldsObject.ItemState              = workHoursFields.ItemState.ToString();
                            fieldsObject.MeetingTimerHours      = workHoursFields.MeetingTimerHours.ToString();
                            fieldsObject.MeetingTimerMinutes    = workHoursFields.MeetingTimerMinutes.ToString();
                            fieldsObject.EmailTimerHours        = workHoursFields.EmailTimerHours.ToString();
                            fieldsObject.EmailTimerMinutes      = workHoursFields.EmailTimerMinutes.ToString();
                            fieldsObject.OtherTimerHours        = workHoursFields.OtherTimerHours.ToString();
                            fieldsObject.OtherTimerMinutes      = workHoursFields.OtherTimerMinutes.ToString();


                            dynamic jsonObject = new JObject();
                            jsonObject.fields = fieldsObject;

                            // Call graph to create the item in the SHarePoint List
                            var saveResults = await graphSharePointService.CreateSiteListItemAsync(siteList, jsonObject.ToString());

                            workHours.Id = saveResults;
                            workHoursList.Add(workHours);
                        }


                        dateToCalculate = dateToCalculate.AddDays(1);
                    }
                }

                return(workHoursList);
            }
            catch (Exception ex)
            {
                throw;
            }
        }