From 39eb9b08b44d74831edce8887407a7d22e77852c Mon Sep 17 00:00:00 2001 From: "sebastien.arod@gmail.com" Date: Tue, 4 Jun 2024 14:58:54 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20ajoute=20la=20date=20de=20mise=20=C3=A0?= =?UTF-8?q?=20jour=20des=20stats?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 4 +- .../publish/publishStatisticsToNotion.ts | 45 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) 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(), + }, + }, + }, + ], + }, + }); +}