diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2025-10-07 13:04:03 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2025-10-07 16:03:28 +0300 |
| commit | b0db0e55c0c400bc948a4934b0e4fddfbfdcca51 (patch) | |
| tree | 0d29e8015178a0eaa52e9f821ad243c5435da99f /src/index.ts | |
| parent | fe57e4996ec79267dffd77e503ef8164eceea229 (diff) | |
Rewrite to run as a traditional script, update deps
Diffstat (limited to 'src/index.ts')
| -rw-r--r-- | src/index.ts | 99 |
1 files changed, 40 insertions, 59 deletions
diff --git a/src/index.ts b/src/index.ts index b675584..c6d92eb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,70 +1,51 @@ -import { App } from "@tinyhttp/app"; -import { logger } from "@tinyhttp/logger"; -import bodyParser from "body-parser"; -import config from "./config"; import { buildSheetsClient } from "./sheets"; -import { bot, broadcastMessage } from "./tg"; -import { httpHeaderAuth } from "./middleware"; +import { broadcastMessage } from "./tg"; -const main = async () => { - const sheets = await buildSheetsClient(); - - const app = new App({ - onError: (err, _req, res) => { - console.log(err); - res.status(500).send("Something bad happened"); - }, - }); +const main = async (): Promise<void> => { + const startTime = Date.now(); + console.log("[run] Starting recurring tasks notification script"); - app - .use(logger({ - timestamp: { - format: "YYYY-MM-DDTHH:mm:ssZ[Z]", - }, - })) - .use(bot.webhookCallback("/webhook/telegram")) - .use(bodyParser.json()) - .get("/", async (_, res) => { - res.send({ service: "hommabot2 API" }); - }) - .post("/scheduler/trigger", httpHeaderAuth, async (_req, res) => { - try { - const sheetData = await sheets.fetchSheetData(); - const entries = sheets.processRows(sheetData); - const thisWeeksEntries = sheets.filterOnlyThisWeek(entries); + const sheets = await buildSheetsClient(); - if (thisWeeksEntries.length > 0) { - const tasks = thisWeeksEntries.map(e => `${e.name} (${e.interval})`); - const taskText = tasks.join("\n"); - const msg = `Tällä viikolla tehtävät hommat:\n${taskText}`; - await broadcastMessage(msg); - console.log("Broadcasted message:", msg); - } else { - const msg = "Tällä viikolla ei toistuvia hommia! 🎉"; - await broadcastMessage(msg); - console.log("Broadcasted message:", msg); - } + // 1. Fetch raw sheet data + const sheetData = await sheets.fetchSheetData(); + console.log(`[run] Retrieved ${sheetData.length} raw rows from Sheets`); - const newDateStamps = sheets.updatedLastDoneDateStamps(entries); - await sheets.updateSheetLastDoneColumn(newDateStamps); + // 2. Process rows (compute next/last dates) + const entries = sheets.processRows(sheetData); + console.log(`[run] Processed ${entries.length} entries`); - res.sendStatus(200); - } catch (err) { - console.error(err); - res.sendStatus(500); - } - }); + // 3. Filter only tasks due this week + const thisWeeksEntries = sheets.filterOnlyThisWeek(entries); + console.log(`[run] Found ${thisWeeksEntries.length} entries due this week`); - console.log(`Setting up webhook on ${config.tgWebhookUrl}`); - bot.telegram.setWebhook(config.tgWebhookUrl); + // 4. Compose & broadcast Telegram message + if (thisWeeksEntries.length > 0) { + const tasks = thisWeeksEntries.map((e) => `${e.name} (${e.interval})`); + const taskText = tasks.join("\n"); + const msg = `Tällä viikolla tehtävät hommat:\n${taskText}`; + await broadcastMessage(msg); + console.log("[run] Broadcasted weekly task message"); + } else { + const msg = "Tällä viikolla ei toistuvia hommia! 🎉"; + await broadcastMessage(msg); + console.log("[run] Broadcasted empty-week message"); + } - console.log(`Serving on http://localhost:${config.port}`); - app.listen(config.port); + // 5. Update "last done" column with new date stamps (only tasks done this week are advanced) + const newDateStamps = sheets.updatedLastDoneDateStamps(entries); + await sheets.updateSheetLastDoneColumn(newDateStamps); + console.log("[run] Updated last done column in sheet"); + const durationMs = Date.now() - startTime; + console.log(`[run] Completed successfully in ${durationMs}ms`); }; -main(); - -// Enable graceful stop -process.once("SIGINT", () => bot.stop("SIGINT")); -process.once("SIGTERM", () => bot.stop("SIGTERM")); +main() + .then(() => { + process.exit(0); + }) + .catch((err) => { + console.error("[run] Fatal error:", err); + process.exit(1); + }); |
