diff --git a/functions/src/__tests__/calendar.spec.ts b/functions/src/__tests__/calendar.spec.ts index 1c9ac96..e1fd9e0 100644 --- a/functions/src/__tests__/calendar.spec.ts +++ b/functions/src/__tests__/calendar.spec.ts @@ -1,51 +1,30 @@ import {auth} from 'google-auth-library'; import { atob } from 'atob'; +import Calendar from '../calendar' +import { gcal as gcalConfig } from '../config' +import Repository from "../repository"; +let calendar describe('cal access', () => { - it('atob test', () => { + beforeEach(() => { + calendar = new Calendar({ + repository: new Repository('testProject'), + gcalConfig: gcalConfig, + }) + }) + xit('atob test', () => { expect(atob('aGVsbG8=')).toEqual('hello') }); - it('try to access', async (done) => { - - - /** - iss The email address of the service account. - scope A space-delimited list of the permissions that the application requests. - aud A descriptor of the intended target of the assertion. When making an access token request this value is always https://www.googleapis.com/oauth2/v4/token. - exp The expiration time of the assertion, specified as seconds since 00:00:00 UTC, January 1, 1970. This value has a maximum of 1 hour after the issued time. - iat The time the assertion was issued, specified as seconds since 00:00:00 UTC, January 1, 1970. - */ - - // const client: any = auth.fromJSON(keys); - // client.scopes = ['https://www.googleapis.com/auth/calendar']; - // await client.authorize(); - - // const url = `https://www.googleapis.com/calendar/v3/users/me/calendarList`; - // const res = await client.request({ - // url, - // }); - // console.log(JSON.stringify(res.data.items[0], null, '\t')); - - - // const calendarId = 'tved0d7d5s4pk9qebub2pgpkks@group.calendar.google.com' - // - // const calendar = await client.request({ - // method: 'get', - // url: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`, - // }) - - - // console.log('calendar', calendar.data) - - // client.request({ - // method: 'post', - // url: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`, - // }) - - + it('read events', async (done) => { + const events = await calendar.listEvents() + console.log('events', events) + done() + }) + xit('try to access', async (done) => { + const cals = await calendar.listCalendars() + console.log('cals', cals) done(); - }) }) \ No newline at end of file diff --git a/functions/src/calendar.ts b/functions/src/calendar.ts index 42658e9..a46e4bd 100644 --- a/functions/src/calendar.ts +++ b/functions/src/calendar.ts @@ -33,6 +33,27 @@ export default class Calendar { return client as OAuth2Client; } + createPrimaryCalendar = async ( ) => { + const client = await this.getClient() + const res = await client.request({ + method: 'post', + url: 'https://www.googleapis.com/calendar/v3/calendars', + data: { + summary: 'primary events calendar' + } + }); + console.log('res', res.data) + return res.data + } + + listCalendars = async () => { + const client = await this.getClient() + const res = await client.request({ + url: 'https://www.googleapis.com/calendar/v3/users/me/calendarList' + }); + return (res.data as any).items; + } + listEvents = async () => { const client = await this.getClient() let eventsResponse