haifa-reminder/src/index.ts
2024-02-04 11:45:59 +02:00

65 lines
1.6 KiB
TypeScript

import GameSource from "./GameSource";
import GoogleCalendar from "./GoogleCalendar";
import env from "dotenv";
import cron from 'node-cron';
env.config();
class App {
gameSource: GameSource;
googleToken: string;
googleCalendar: GoogleCalendar;
constructor() {
this.gameSource = new GameSource();
this.googleCalendar = new GoogleCalendar();
}
async startCronJob() {
console.log("START Haifa Reminder");
const newGamesAdded = [];
await this.googleCalendar.init();
try {
const games = await app.gameSource.getGamesFromHaifa();
for (const game of games) {
const isDuplicateEvent = await this.googleCalendar.isDuplicateEvent(
game.start.dateTime,
game.end.dateTime,
game.summary
);
console.log(game)
if (!isDuplicateEvent) {
newGamesAdded.push(game);
console.log("Event does not exist");
await this.googleCalendar.updateNewEvent([game]);
} else {
console.log("Event already exists");
}
}
if(newGamesAdded.length > 0) {
console.log("New games added:", newGamesAdded);
} else {
console.log("No new games was Added!");
}
} catch (error) {
console.error("Error in cron job:", error.message);
}
}
}
const app = new App();
cron.schedule('* * * * *', () => {
console.log('Running startCronJob at 7:00 AM Jerusalem time');
console.log("START CRON JOB");
app.startCronJob().catch((error) => {
console.error("Error in scheduled cron job:", error.message);
});
}, {
scheduled: true,
timezone: "Asia/Jerusalem"
});