public async Task DoWork() // Add new gmail account and sync with db { var service = new GoogleCalendarService(); var range = (int)AppDomain.CurrentDomain.GetData("EventViewableYearRange"); var timeMax = new DateTime(DateTime.Today.Year + range - 1, 12, 31, 23, 59, 59); var accounts = _dbs.GetAvailableAccounts(); try { var events = await service.GetEvents(timeMax); if (events != null && !events.Items.IsNullOrEmpty()) { var email = events.Items[0].Creator.Email; var foundAcc = accounts.Find(a => a.Email == email); List <GXEvent> newEvents = null; int targetAccountId = 0; // Email exists within db, add new events from last sync timestamp if (foundAcc != null) { targetAccountId = foundAcc.AccId; var fromLastSync = events.Items.Where(e => e.Updated >= foundAcc.LastSync); newEvents = fromLastSync.ToGXEvents(targetAccountId); } // New email else { targetAccountId = accounts.Max(a => a.AccId) + 1; await _dbs.AddAccount(email); newEvents = events.Items.ToGXEvents(targetAccountId); } // Add new events to db await _dbs.AddEvents(newEvents, targetAccountId); await _dbs.UpdateLastSync(targetAccountId); } } catch (Newtonsoft.Json.JsonException) { throw new FormatException("Incorrect oauth token file format. Restore or delete it and try again"); } catch (DataException) { throw new DataException("Failed to modify database. Verify the connection and try again"); } finally { if (Events.Count < viewableEventsNum) { RefreshEvents(viewableEventsNum); } } }
public static async Task <System.Collections.Generic.List <Models.CalendarEntry> > GetCalendars(TokenEntity msaToken, TokenEntity googleToken, ILogger logger) { var start = DateTime.Now.Date.AddDays(-7); var end = DateTime.Now.Date.AddDays(Constants.CalendarWeeks * 7); var events = new System.Collections.Generic.List <CalendarEntry>(); try { var holidays = new System.Collections.Generic.List <CalendarEntry>(); // combine public and school holidays var publicHolidaysService = new PublicHolidaysService(); var schoolHolidaysService = new SchoolHolidaysService(); holidays.AddRange(await publicHolidaysService.GetEvents(start, end)); holidays.AddRange(await schoolHolidaysService.GetEvents(start, end, logger)); var deduplicatedHolidays = holidays.GroupBy(x => x.Date).Select(y => y.First()).ToList <CalendarEntry>(); events.AddRange(deduplicatedHolidays); var googleCalendarService = new GoogleCalendarService( googleToken, calendarId: Util.GetEnvironmentVariable("GOOGLE_CALENDAR_ID"), timeZone: Util.GetEnvironmentVariable("CALENDAR_TIMEZONE")); var googleEvents = await googleCalendarService.GetEvents(start, end, isPrimary : true); events.AddRange(googleEvents); var outlookCalendarService = new OutlookCalendarService(msaToken, timeZone: Util.GetEnvironmentVariable("CALENDAR_TIMEZONE")); var outlookEvents = await outlookCalendarService.GetEvents(start, end, isSecondary : true); events.AddRange(outlookEvents); } catch (Exception ex) { throw ex; } return(events); }
public async Task TestGoogleCalendarService1Week() { // arrange var service = new GoogleCalendarService( googleToken: new TokenEntity() { TokenType = "Bearer", AccessToken = TestContext.Parameters["GOOGLE_TEST_TOKEN"] }, calendarId: TestContext.Parameters["GOOGLE_CALENDAR_ID"] ); var start = DateTime.Now.Date; var end = DateTime.Now.Date.AddDays(7); // act var result = await service.GetEvents(start, end); // assert Assert.IsNotNull(result); Assert.Greater(result.Count, 0); }