From 3aeb79f054be3144b2c566aa172de9bee2688984 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sun, 6 Aug 2023 00:35:07 +0300 Subject: [PATCH] clean up --- Dockerfile | 16 +---------- calendar.ics | 0 src/GoogleCalendar.ts | 67 +++++++++++++++++++++++-------------------- 3 files changed, 37 insertions(+), 46 deletions(-) delete mode 100644 calendar.ics diff --git a/Dockerfile b/Dockerfile index 1d09dc4..4ecd879 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" ] diff --git a/calendar.ics b/calendar.ics deleted file mode 100644 index e69de29..0000000 diff --git a/src/GoogleCalendar.ts b/src/GoogleCalendar.ts index 6eadbd7..8c92ff8 100644 --- a/src/GoogleCalendar.ts +++ b/src/GoogleCalendar.ts @@ -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,35 +50,42 @@ 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, timeMin: startTime, - timeMax: endTime, + timeMax: endTime, q: title, // Search for events with the same title }); return response.data.items.length > 0;