This commit is contained in:
Kfir Dayan 2023-08-06 00:35:07 +03:00
parent 92a256c784
commit 3aeb79f054
3 changed files with 37 additions and 46 deletions

View file

@ -1,28 +1,14 @@
FROM node:16
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
RUN npm install -g typescript
# If you are building your code for production
# RUN npm ci --omit=dev
# Bundle app source
COPY . .
EXPOSE ${PORT}
# run npm build to create the dist folder
RUN tsc
# run npm start to start the server
CMD [ "npm", "start" ]

View file

View file

@ -1,8 +1,8 @@
import { JWT } from 'google-auth-library';
import { google } from 'googleapis';
import { GoogleCalendarEvent } from './types/index';
import { JWT } from "google-auth-library";
import { google } from "googleapis";
import { GoogleCalendarEvent } from "./types/index";
require('dotenv').config();
require("dotenv").config();
const env = process.env;
export default class GoogleCalendar {
@ -12,31 +12,29 @@ export default class GoogleCalendar {
calenderId: string = env.GOOGLE_CALENDAR_ID;
calendar: any;
clientEmail: string = env.GOOGLE_CLIENT_EMAIL;
googlePrivateKey: string = env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n');
googlePrivateKey: string = env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n");
token: any;
JWT_client: JWT;
async init() {
console.log("INIT GOOGLE CALENDAR")
console.log("INIT GOOGLE CALENDAR");
const jwtClient = await this.authorize();
this.calendar = google.calendar({ version: 'v3', auth: jwtClient });
this.calendar = google.calendar({ version: "v3", auth: jwtClient });
}
async authorize() {
console.log("AUTHORIZE GOOGLE CALENDAR")
console.log("AUTHORIZE GOOGLE CALENDAR");
this.JWT_client = new JWT({
email: this.clientEmail,
key: this.googlePrivateKey,
scopes: [
'https://www.googleapis.com/auth/calendar'
]
scopes: ["https://www.googleapis.com/auth/calendar"],
});
const { access_token } = await this.JWT_client.authorize();
this.token = access_token;
if (!this.token) {
throw new Error('Failed to connect to google calendar');
throw new Error("Failed to connect to google calendar");
}
console.log("GOOGLE CALENDAR AUTHORIZED SUCCESSFULLY")
console.log("GOOGLE CALENDAR AUTHORIZED SUCCESSFULLY");
return this.JWT_client;
}
@ -44,7 +42,7 @@ export default class GoogleCalendar {
// console.log(upcomingEvents)
setTimeout(async () => {
upcomingEvents.forEach(async (event: GoogleCalendarEvent) => {
console.log("UPDATE NEW EVENT", upcomingEvents)
console.log("UPDATE NEW EVENT", upcomingEvents);
const options = {
auth: this.JWT_client,
calendarId: this.calenderId,
@ -52,30 +50,37 @@ export default class GoogleCalendar {
summary: event.summary,
location: event.location,
description: event.description,
start: { dateTime: event.start.dateTime, timeZone: 'Asia/Jerusalem' },
end: { dateTime: event.end.dateTime, timeZone: 'Asia/Jerusalem' }
start: {
dateTime: event.start.dateTime,
timeZone: "Asia/Jerusalem",
},
end: { dateTime: event.end.dateTime, timeZone: "Asia/Jerusalem" },
sendNotifications: true,
},
}
await this.calendar.events.insert(options, function (err: any, event: any) {
if (err) {
console.log('There was an error contacting the Calendar service: ' + err);
return;
};
await this.calendar.events.insert(
options,
function (err: any, event: any) {
if (err) {
console.log(
"There was an error contacting the Calendar service: " + err
);
return;
}
console.log(event.description + " created");
}
console.log(event.description + ' created');
});
})
}, 3000)
);
});
}, 3000);
}
async isDuplicateEvent(startTime: string, endTime: string, title: string) {
if(this.gamesMap[startTime]) {
console.log("duplicate event")
if (this.gamesMap[startTime]) {
console.log("duplicate event");
return true;
}
this.gamesMap[startTime] = true;
console.log("checking for duplicate event")
console.log("checking for duplicate event");
try {
const response = await this.calendar.events.list({
calendarId: this.calenderId,