aboutsummaryrefslogtreecommitdiffstats
path: root/src/index.ts
blob: c6d92eb93c8f51f595327d47ce39b14d0b57be41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { buildSheetsClient } from "./sheets";
import { broadcastMessage } from "./tg";

const main = async (): Promise<void> => {
  const startTime = Date.now();
  console.log("[run] Starting recurring tasks notification script");

  const sheets = await buildSheetsClient();

  // 1. Fetch raw sheet data
  const sheetData = await sheets.fetchSheetData();
  console.log(`[run] Retrieved ${sheetData.length} raw rows from Sheets`);

  // 2. Process rows (compute next/last dates)
  const entries = sheets.processRows(sheetData);
  console.log(`[run] Processed ${entries.length} entries`);

  // 3. Filter only tasks due this week
  const thisWeeksEntries = sheets.filterOnlyThisWeek(entries);
  console.log(`[run] Found ${thisWeeksEntries.length} entries due this week`);

  // 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");
  }

  // 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()
  .then(() => {
    process.exit(0);
  })
  .catch((err) => {
    console.error("[run] Fatal error:", err);
    process.exit(1);
  });