diff --git a/src/index.ts b/src/index.ts index e7d0880..39b3736 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,9 +27,11 @@ import { computeELStats } from "./statistiques/computeELStats"; console.log(consistencyIssues); } + const currentDate = new Date(Date.now()); + console.log("Building statistics..."); const resistantCountStats = computeELStats(families); console.log("Publishing statistics..."); - publishStatisticsToNotion(resistantCountStats, notionClient); + publishStatisticsToNotion(resistantCountStats, currentDate, notionClient); })(); diff --git a/src/notion/publish/publishStatisticsToNotion.ts b/src/notion/publish/publishStatisticsToNotion.ts index c7991c9..45120dc 100644 --- a/src/notion/publish/publishStatisticsToNotion.ts +++ b/src/notion/publish/publishStatisticsToNotion.ts @@ -1,5 +1,7 @@ -import { Client } from "@notionhq/client"; +import { Client, isFullBlock } from "@notionhq/client"; import { ELStats } from "../../statistiques/ELStats"; +import { listAllChildrenBlocks } from "../utils/listAllChildrenBlocks"; +import { richTextToPlainText } from "../utils/text/richTextToPlainText"; import { publishPeriodStats } from "./publishPeriodStats"; import { publishStatsActuelles } from "./publishStatsActuelles"; @@ -10,11 +12,52 @@ const yearStatsDb = "4b19a72aa07840eab948525ea41878ee"; const monthStatsDb = "8418a8a4a7544f6a8c54e6003be7efe5"; export async function publishStatisticsToNotion( stats: ELStats, + currentDate: Date, notionClient: Client ) { + await updateUpdateDate(notionClient, currentDate); await publishStatsActuelles(notionClient, stats.actuelles); await publishPeriodStats(notionClient, yearStatsDb, stats.annees); await publishPeriodStats(notionClient, monthStatsDb, stats.mois); } + +async function updateUpdateDate(notionClient: Client, updateDate: Date) { + const childrenBlocks = ( + await listAllChildrenBlocks(notionClient, { + block_id: statsPageId, + }) + ).filter(isFullBlock); + + const blockTextPrefix = "Dernière mise à jour des statistiques : "; + const block = childrenBlocks.find( + (b) => + b.type === "paragraph" && + richTextToPlainText(b.paragraph.rich_text).startsWith(blockTextPrefix) + ); + if (!block) { + console.log(`Could not find block starting with "${blockTextPrefix}"`); + return; + } + + await notionClient.blocks.update({ + block_id: block?.id, + paragraph: { + rich_text: [ + { + text: { + content: blockTextPrefix, + }, + }, + { + mention: { + date: { + start: updateDate.toISOString(), + }, + }, + }, + ], + }, + }); +}