Files
77th_eventcalenderntfy/src/app.ts

117 lines
4.4 KiB
TypeScript

import { db } from "./sql";
import { Event, type TEventEntityNew, type TGetEventsOptions } from "./component/event/events";
import { createPlaceholders, getTsNow } from "./util";
import { sendNotification } from "./sendNotification";
import minimist from "minimist";
const argv = minimist(process.argv.slice(2))
console.log("App started");
console.dir({argv})
const TODAY = getTsNow();
console.dir({TODAY});
async function events_update_db() {
const events_fetched_currentMonth = await Event.fetch_events( TODAY.year, TODAY.month , -120 );
console.log("events_fetched_currentMonth.length: " + events_fetched_currentMonth.length );
const events_fetched_nextMonth = await Event.fetch_events( TODAY.year, TODAY.month + 1 , -120 );
console.log("events_fetched_nextMonth.length: " + events_fetched_nextMonth.length );
const events_fetched = [...events_fetched_currentMonth, ...events_fetched_nextMonth];
console.log("events_fetched.length: " + events_fetched.length );
const events_fetched_list_of_uids = events_fetched.map( event => { return event.uid; });
console.dir( {events_fetched_list_of_uids} );
const events_db_currentMonth = Event.get_events({month: {year: TODAY.year, month: TODAY.month}}, db);
const events_db_nextMonth = Event.get_events({month: {year: TODAY.year, month: (TODAY.month + 1)}}, db);
const events_db = [... events_db_currentMonth, ... events_db_nextMonth];
const events_removed: Event[] = events_db.filter( (ev) => {
return ! events_fetched_list_of_uids.includes(ev.uid);
});
console.dir({events_removed});
events_removed.forEach( ev => {
ev.set_notification("removed", db);
});
const placeholders = createPlaceholders( events_fetched_list_of_uids );
const getAllRelevantEventsQuery = db.query(
`SELECT * FROM events WHERE uid IN (${placeholders}) AND deleteDate IS NULL;`
).as(Event );
const AllRelevantEvents = getAllRelevantEventsQuery.all(...events_fetched_list_of_uids);
console.log("AllRelevantEvents.length: " + AllRelevantEvents.length );
const eventsToInsert: TEventEntityNew[] = [];
for ( const ev of events_fetched ) {
console.log("loop ev " + ev.uid + " : " + [ ev.title, ev.date_at ].join( ", " ) );
const found = AllRelevantEvents.find(event => event.uid === ev.uid);
if ( found ) {
console.log("loop ev " + ev.uid + " f: " + [ found.title, found.date_at ].join( ", " ) );
if (
found.title != ev.title ||
found.description != ev.description ||
found.date_at != ev.date_at ||
found.time_start != ev.time_start ||
found.time_end != ev.time_end ||
found.posted_by != ev.posted_by ||
found.location != ev.location ||
found.event_type != ev.event_type ||
found.timezone != ev.timezone ||
found.link != ev.link
) {
console.log("loop ev " + ev.uid + " c: " + [ ev.title, ev.date_at ].join( ", " ) );
const newEventToInsert: TEventEntityNew = {... ev, notification: "changed"};
eventsToInsert.push( newEventToInsert );
}
} else {
console.log("loop ev " + ev.uid + " n: " + [ ev.title, ev.date_at ].join( ", " ) );
const newEventToInsert: TEventEntityNew = {... ev, notification: "new"};
eventsToInsert.push( newEventToInsert );
}
}
console.dir({eventsToInsert})
Event.insert( eventsToInsert, db);
}
async function events_check_for_notification() {
const where: TGetEventsOptions = {
notification: ["new", "changed", "removed"],
deleted: false
}
if ( argv.today ) {
where.date = {
year: TODAY.year,
month: TODAY.month,
day: TODAY.day
}
}
const list_of_events = Event.get_events( where, db );
console.dir({
list_of_events,
where
});
for ( const ev of list_of_events ) {
console.log("loop list_of_events - ev: " + [ ev.uid, ev.title, ev.date_at, "notification: " + ev.notification ].join( ", " ) );
console.log("loop list_of_events - ev 'title': " + ev.get_title() );
const notificationOptions = {
ntfy: null,
discord: {
avatar_url: ( process.env.dc_avatar_url as string),
botname: ( process.env.dc_botname as string)
}
};
await sendNotification( ev.get_title(), ev.get_body(), notificationOptions );
if ( ev.notification == "removed" ) {
ev.set_deleted( db );
} else {
ev.set_notification("done", db);
}
}
}
async function main ( ) {
console.log("Excecuting main()");
await events_update_db();
await events_check_for_notification();
};
main();