52 lines
No EOL
1.3 KiB
TypeScript
52 lines
No EOL
1.3 KiB
TypeScript
require('dotenv').config();
|
|
const env = process.env;
|
|
import { google } from 'googleapis';
|
|
|
|
export default class GoogleCalendar {
|
|
connected: boolean;
|
|
calendar: any;
|
|
googlePrivateKey = env.GOOGLE_PRIVATE_KEY
|
|
googleClientEmail = env.GOOGLE_CLIENT_EMAIL
|
|
googleProjectNumber = env.GOOGLE_PROJECT_NUMBER
|
|
googleCalendarId = env.GOOGLE_CALENDAR_ID
|
|
projectScope = 'https://www.googleapis.com/auth/calendar.readonly'
|
|
connection: any;
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
const SCOPES = this.projectScope;
|
|
const jwtClient = new google.auth.JWT(
|
|
this.googleClientEmail,
|
|
null,
|
|
this.googlePrivateKey,
|
|
SCOPES
|
|
);
|
|
this.connection = jwtClient;
|
|
if(this.connection) {
|
|
this.connected = true;
|
|
} else {
|
|
this.connected = false;
|
|
}
|
|
}
|
|
|
|
insert_new_event_in_calender(event: any) {
|
|
const calendar = google.calendar({ version: 'v3', auth: this.connection });
|
|
const options = {
|
|
auth: this.connection,
|
|
calendarId: this.googleCalendarId,
|
|
date: event.date,
|
|
// resource: event,
|
|
};
|
|
|
|
const callback = (err: any, res: any) => {
|
|
if (err) {
|
|
console.log(err);
|
|
}
|
|
console.log(res);
|
|
}
|
|
calendar.events.insert(options, callback);
|
|
console.log("Successfully Inserted Event To Google Calendar")
|
|
}
|
|
} |