public void ConvertToRRULE_WhenRecurrenceTypeByDayOfWeek_ProducesValidRRULE(DaysOfWeek daysOfWeek, string expectedResult)
        {
            // Arrange
            var model = new ScheduleRuleModel()
            {
                ScheduleRuleType = ScheduleRuleType.ByDayOfWeek,
                DaysOfWeek       = daysOfWeek
            };

            // Act
            var rrule = ScheduleRepresentationHelper.GenerateRrule(ScheduleRuleType.ByDayOfWeek, );

            // Assert
            Assert.Equal(expectedResult, rrule);
        }
Esempio n. 2
0
        public async Task CreateCalendarForScheduleAsync(Guid scheduleId, CancellationToken cancellationToken = default(CancellationToken))
        {
            var schedule = await _appDbContext.Schedules
                           .Include(s => s.ScheduleRules)
                           .FirstOrDefaultAsync(s => s.ScheduleId == scheduleId, cancellationToken).ConfigureAwait(false);

            if (schedule == null)
            {
                throw new Exception("Unable to find schedule with given ID");
            }

            var token = await flow.LoadTokenAsync(_httpContextAccessor.HttpContext.User.Identity.Name, cancellationToken).ConfigureAwait(false);

            var userCredentials = new UserCredential(flow, _httpContextAccessor.HttpContext.User.Identity.Name, token);

            var service = new CalendarService(new BaseClientService.Initializer
            {
                HttpClientInitializer = userCredentials
            });

            var calendars = await service.CalendarList.List().ExecuteAsync(cancellationToken).ConfigureAwait(false);

            string userCalendarId;

            if (calendars.Items.Count == 0 || !calendars.Items.Any(i => i.Description == schedule.ScheduleId.ToString()))
            {
                var cal = await service.Calendars.Insert(new Google.Apis.Calendar.v3.Data.Calendar
                {
                    Summary     = $"FWTBT - {schedule.ScheduleName}",
                    Description = schedule.ScheduleId.ToString()
                }).ExecuteAsync().ConfigureAwait(false);

                userCalendarId = cal.Id;
            }
            else
            {
                var userCalendar = calendars.Items.First(i => i.Description == schedule.ScheduleId.ToString());
                userCalendarId = userCalendar.Id;

                // Clear existing events
                var result = await service.Calendars.Clear(userCalendarId).ExecuteAsync(cancellationToken).ConfigureAwait(false);
            }

            schedule.GoogleCalendarId = userCalendarId;

            foreach (var rule in schedule.ScheduleRules)
            {
                var rrule = ScheduleRepresentationHelper.GenerateRrule(rule.ScheduleRuleType, rule.DaysOfWeek, rule.EndDate.Date + rule.EndTime);

                await service.Events.Insert(new Event
                {
                    Summary  = rule.Name,
                    Location = rule.Url,
                    Start    = new EventDateTime {
                        DateTime = rule.StartDate.Date + rule.StartTime,
                        TimeZone = TZConvert.WindowsToIana(TimeZoneInfo.Local.StandardName)
                    },
                    End = new EventDateTime
                    {
                        DateTime = rule.StartDate.Date + rule.EndTime,
                        TimeZone = TZConvert.WindowsToIana(TimeZoneInfo.Local.StandardName)
                    },
                    Recurrence = new List <string>
                    {
                        rrule
                    }
                }, userCalendarId).ExecuteAsync(cancellationToken).ConfigureAwait(false);
            }

            await _appDbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
        }