haifa-reminder/src/index.ts

65 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-07-26 10:17:32 +00:00
import GameSource from "./GameSource";
import GoogleCalendar from "./GoogleCalendar";
import env from "dotenv";
2024-02-04 09:45:59 +00:00
import cron from 'node-cron';
env.config();
class App {
gameSource: GameSource;
googleToken: string;
2023-07-26 10:17:32 +00:00
googleCalendar: GoogleCalendar;
constructor() {
this.gameSource = new GameSource();
2023-07-26 10:17:32 +00:00
this.googleCalendar = new GoogleCalendar();
}
2023-04-04 09:31:20 +00:00
async startCronJob() {
2024-02-04 09:45:59 +00:00
console.log("START Haifa Reminder");
2023-08-06 07:48:24 +00:00
const newGamesAdded = [];
2023-07-26 10:17:32 +00:00
await this.googleCalendar.init();
2023-08-06 07:48:24 +00:00
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)
2023-08-06 07:48:24 +00:00
if (!isDuplicateEvent) {
newGamesAdded.push(game);
console.log("Event does not exist");
await this.googleCalendar.updateNewEvent([game]);
} else {
console.log("Event already exists");
2023-07-26 10:17:32 +00:00
}
}
2023-08-06 07:48:24 +00:00
if(newGamesAdded.length > 0) {
console.log("New games added:", newGamesAdded);
} else {
console.log("No new games was Added!");
2023-08-06 07:48:24 +00:00
}
} catch (error) {
console.error("Error in cron job:", error.message);
}
2023-04-04 09:31:20 +00:00
}
}
const app = new App();
2023-04-04 09:31:20 +00:00
2024-02-04 09:45:59 +00:00
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"
2023-07-26 11:31:24 +00:00
});