added --today parameter to send a nofitication for todays events else its change or new events. also fetches current and next month now.
This commit is contained in:
69
app/app.ts
69
app/app.ts
@@ -1,20 +1,13 @@
|
||||
import { TEventType } from "./component/event/event.types";
|
||||
import { db } from "./sql";
|
||||
import { Event, type TEventEntityNew } from "./component/event/events";
|
||||
import { Event, type TEventEntityNew, type TGetEventsOptions } from "./component/event/events";
|
||||
import { sendNotification } from "./sendNotification";
|
||||
import { createPlaceholders } from "./util";
|
||||
import { createPlaceholders, pad_l2 } from "./util";
|
||||
|
||||
const argv = require('minimist')(process.argv.slice(2));
|
||||
console.dir(argv)
|
||||
|
||||
const TS_TODAY = new Date();
|
||||
|
||||
function pad_l2 ( _thing: string | number ): string {
|
||||
if ( typeof _thing == "number" ) {
|
||||
_thing = JSON.stringify(_thing);
|
||||
};
|
||||
return _thing.padStart(2, "0");
|
||||
}
|
||||
// const TS_TODAY = new Date();
|
||||
|
||||
function getTsNow() {
|
||||
const now = new Date();
|
||||
@@ -29,8 +22,10 @@ function getTsNow() {
|
||||
}
|
||||
|
||||
async function main( ) {
|
||||
const events = await Event.fetch_events( TS_TODAY.getFullYear(), TS_TODAY.getMonth() + 1 , -120 );
|
||||
|
||||
const TODAY = getTsNow();
|
||||
const events_currentMonth = await Event.fetch_events( TODAY.year, TODAY.month , -120 );
|
||||
const events_nextMonth = await Event.fetch_events( TODAY.year, TODAY.month + 1 , -120 );
|
||||
const events = [...events_currentMonth, ...events_nextMonth];
|
||||
// Write to JSON File Section START
|
||||
// const data = JSON.stringify(events, null, 2);
|
||||
// const TS = `${TS_TODAY.getFullYear()}-${TS_TODAY.getMonth() + 1}-${TS_TODAY.getDate()}_${TS_TODAY.getHours()}-${TS_TODAY.getMinutes()}-${TS_TODAY.getSeconds()}`;
|
||||
@@ -70,7 +65,18 @@ async function main( ) {
|
||||
}
|
||||
|
||||
Event.insert( eventsToInsert, db);
|
||||
const list_of_events = Event.get_events(["new", "changed"], db);
|
||||
const options: TGetEventsOptions = {
|
||||
}
|
||||
if (argv.today) {
|
||||
options.date = {
|
||||
year: TODAY.year,
|
||||
month: TODAY.month,
|
||||
day: TODAY.day
|
||||
}
|
||||
} else {
|
||||
options.notification = ["new", "changed"]
|
||||
}
|
||||
const list_of_events = Event.get_events( options, db );
|
||||
for ( const ev of list_of_events ) {
|
||||
const body = [
|
||||
`Title: ${ev.title}`,
|
||||
@@ -108,41 +114,10 @@ async function main( ) {
|
||||
})( ev );
|
||||
sendNotification(
|
||||
`${today_prefix ? "TODAY " : ""}${notification_prefix ? notification_prefix + ": " : ""} ${ev.title} (${ TEventType[ ev.event_type ] })`,
|
||||
`${body}`,
|
||||
`${ev.link || "https://77th-jsoc.com/#/events"}`
|
||||
`${body}`
|
||||
// `${ev.link || "https://77th-jsoc.com/#/events"}`
|
||||
);
|
||||
ev.set_notification("done", db);
|
||||
}
|
||||
// events.forEach( event => {
|
||||
// const now = getTsNow();
|
||||
// const [year, month, day] = event.date_at.split("-")
|
||||
// if (
|
||||
// year == String(now.year) &&
|
||||
// month == pad_l2( String(now.month) ) &&
|
||||
// day == pad_l2( String( now.day ) )
|
||||
// ) {
|
||||
// // console.dir( event );
|
||||
// const body = [
|
||||
// `Title: ${event.title}`,
|
||||
// `Location: ${event.location}`,
|
||||
// `Type: ${ TEventType[ event.event_type ] }`,
|
||||
// `Date: ${event.date_at}`,
|
||||
// `Time: ${event.time_start}`,
|
||||
// `By: ${event.posted_by}`,
|
||||
// `Link: ${event.link}`,
|
||||
// ].join("\n");
|
||||
|
||||
// sendNotification(
|
||||
// `TODAY ${ TEventType[ event.event_type ] } - ${event.title}`,
|
||||
// `${body}`,
|
||||
// `${event.link || "https://77th-jsoc.com/#/events"}`
|
||||
// );
|
||||
// }
|
||||
// });
|
||||
};
|
||||
main();
|
||||
// do {
|
||||
// await getEvents(TS_TODAY.getFullYear(), TS_TODAY.getMonth() + 1 , -120);
|
||||
// await Bun.sleep(1000 * 60 * 60 * 24);
|
||||
// }
|
||||
// while( true )
|
||||
main();
|
||||
Reference in New Issue
Block a user