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 FROM node:16
# Create app directory
WORKDIR /usr/src/app 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 ./ COPY package*.json ./
RUN npm install RUN npm install
RUN npm install -g typescript RUN npm install -g typescript
# If you are building your code for production
# RUN npm ci --omit=dev
# Bundle app source
COPY . . COPY . .
EXPOSE ${PORT} EXPOSE ${PORT}
# run npm build to create the dist folder
RUN tsc RUN tsc
# run npm start to start the server
CMD [ "npm", "start" ] CMD [ "npm", "start" ]

View file

View file

@ -1,8 +1,8 @@
import { JWT } from 'google-auth-library'; import { JWT } from "google-auth-library";
import { google } from 'googleapis'; import { google } from "googleapis";
import { GoogleCalendarEvent } from './types/index'; import { GoogleCalendarEvent } from "./types/index";
require('dotenv').config(); require("dotenv").config();
const env = process.env; const env = process.env;
export default class GoogleCalendar { export default class GoogleCalendar {
@ -12,31 +12,29 @@ export default class GoogleCalendar {
calenderId: string = env.GOOGLE_CALENDAR_ID; calenderId: string = env.GOOGLE_CALENDAR_ID;
calendar: any; calendar: any;
clientEmail: string = env.GOOGLE_CLIENT_EMAIL; 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; token: any;
JWT_client: JWT; JWT_client: JWT;
async init() { async init() {
console.log("INIT GOOGLE CALENDAR") console.log("INIT GOOGLE CALENDAR");
const jwtClient = await this.authorize(); const jwtClient = await this.authorize();
this.calendar = google.calendar({ version: 'v3', auth: jwtClient }); this.calendar = google.calendar({ version: "v3", auth: jwtClient });
} }
async authorize() { async authorize() {
console.log("AUTHORIZE GOOGLE CALENDAR") console.log("AUTHORIZE GOOGLE CALENDAR");
this.JWT_client = new JWT({ this.JWT_client = new JWT({
email: this.clientEmail, email: this.clientEmail,
key: this.googlePrivateKey, key: this.googlePrivateKey,
scopes: [ scopes: ["https://www.googleapis.com/auth/calendar"],
'https://www.googleapis.com/auth/calendar'
]
}); });
const { access_token } = await this.JWT_client.authorize(); const { access_token } = await this.JWT_client.authorize();
this.token = access_token; this.token = access_token;
if (!this.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; return this.JWT_client;
} }
@ -44,7 +42,7 @@ export default class GoogleCalendar {
// console.log(upcomingEvents) // console.log(upcomingEvents)
setTimeout(async () => { setTimeout(async () => {
upcomingEvents.forEach(async (event: GoogleCalendarEvent) => { upcomingEvents.forEach(async (event: GoogleCalendarEvent) => {
console.log("UPDATE NEW EVENT", upcomingEvents) console.log("UPDATE NEW EVENT", upcomingEvents);
const options = { const options = {
auth: this.JWT_client, auth: this.JWT_client,
calendarId: this.calenderId, calendarId: this.calenderId,
@ -52,35 +50,42 @@ export default class GoogleCalendar {
summary: event.summary, summary: event.summary,
location: event.location, location: event.location,
description: event.description, description: event.description,
start: { dateTime: event.start.dateTime, timeZone: 'Asia/Jerusalem' }, start: {
end: { dateTime: event.end.dateTime, timeZone: 'Asia/Jerusalem' } 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) { await this.calendar.events.insert(
if (err) { options,
console.log('There was an error contacting the Calendar service: ' + err); function (err: any, event: any) {
return; 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) { async isDuplicateEvent(startTime: string, endTime: string, title: string) {
if(this.gamesMap[startTime]) { if (this.gamesMap[startTime]) {
console.log("duplicate event") console.log("duplicate event");
return true; return true;
} }
this.gamesMap[startTime] = true; this.gamesMap[startTime] = true;
console.log("checking for duplicate event") console.log("checking for duplicate event");
try { try {
const response = await this.calendar.events.list({ const response = await this.calendar.events.list({
calendarId: this.calenderId, calendarId: this.calenderId,
timeMin: startTime, timeMin: startTime,
timeMax: endTime, timeMax: endTime,
q: title, // Search for events with the same title q: title, // Search for events with the same title
}); });
return response.data.items.length > 0; return response.data.items.length > 0;