From 275911b27cd437c3bfafb6ac13a4c3c1db96ba28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Arod?= Date: Tue, 17 Sep 2024 16:01:16 +0200 Subject: [PATCH] feat: header d'info sur chaque page --- src/index.ts | 9 ++ .../v2/createStatGroupListItemBlock.ts | 72 ++++++++++++++++ src/notion/publish/v2/publishStatsToPage.ts | 86 ++++--------------- src/notion/publish/v2/updatePageContent.ts | 23 +++++ 4 files changed, 120 insertions(+), 70 deletions(-) create mode 100644 src/notion/publish/v2/createStatGroupListItemBlock.ts create mode 100644 src/notion/publish/v2/updatePageContent.ts diff --git a/src/index.ts b/src/index.ts index 812d0c1..bc150b8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,7 @@ import { statsSocialesDesc } from "./statistiques/v2/sociales/StatsSociales"; import { checkDbSchemas } from "./notion/fetch/schemaCheck/checkDbSchemas"; import { computeSequencEvtPenalSankeyData } from "./statistiques/v2/penales/computeSankeyData"; import { sankeyDataToMermaidDiagram } from "./statistiques/v2/sankey/sankeyDataToMermaidDiagram"; +import { formatDate } from "date-fns"; type ProcessOptions = { dryRun: boolean; @@ -111,21 +112,29 @@ function buildProcessOptions(): ProcessOptions { console.log("Publishing statistics..."); await publishStatisticsToNotion(elStats, currentDate, notionClient); + const header = ` + Note: Sauf mention contraire les statistiques sont basées sur les familles résistantes & ex-résistantes. + Dernière mise à jour le ${formatDate(currentDate, "dd/MM/yyyy à HH:mm")} + + `; await publishStatsToPage( notionClient, "313751fb-daed-4b33-992f-c86d7ac2de37", + header, statsGeneralesDesc, statsGenerales ); await publishStatsToPage( notionClient, "969eac5c-a4eb-49d4-b4ad-c341c9c4c785", + header, statsPenalesDesc, statsPenales ); await publishStatsToPage( notionClient, "68a8c03a-a4a2-4ff3-b044-7c8dbbdc28ce", + header, statsSocialesDesc, statsSociales ); diff --git a/src/notion/publish/v2/createStatGroupListItemBlock.ts b/src/notion/publish/v2/createStatGroupListItemBlock.ts new file mode 100644 index 0000000..2d8aaff --- /dev/null +++ b/src/notion/publish/v2/createStatGroupListItemBlock.ts @@ -0,0 +1,72 @@ +import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints"; +import { + isStatGroupDesc, + StatDesc, + StatGroupDesc, + StatsType, +} from "../../../statistiques/v2/desc/StatsDesc"; +import { formatValue } from "../../../format/formatValue"; + +export function createStatGroupListItemBlock( + descriptor: D, + stats: StatsType +): BulletedListItemBlockObjectRequest { + return { + bulleted_list_item: { + rich_text: [ + { + text: { + content: descriptor.label, + }, + }, + ], + children: createStatGroupChildrenListItemBlock( + descriptor, + stats + ) as BulletedListItemChildren, + }, + }; +} + +export type BulletedListItemBlockObjectRequest = Extract< + BlockObjectRequest, + { bulleted_list_item: object } +>; + +export type BulletedListItemChildren = + BulletedListItemBlockObjectRequest["bulleted_list_item"]["children"]; + +export function createStatGroupChildrenListItemBlock( + descriptor: D, + stats: StatsType +): BulletedListItemBlockObjectRequest[] { + return Object.keys(descriptor.stats).map((statName) => { + const childStatDesc = descriptor.stats[statName]; + const childStatValue = stats[statName]; + + return isStatGroupDesc(childStatDesc) + ? createStatGroupListItemBlock( + childStatDesc, + childStatValue as StatsType + ) + : createStatListItemBlock(childStatDesc, childStatValue as number); + }); +} + +function createStatListItemBlock( + descriptor: StatDesc, + statValue: number +): BulletedListItemBlockObjectRequest { + return { + bulleted_list_item: { + rich_text: [ + { + text: { + content: + descriptor.label + ": " + formatValue(statValue, descriptor), + }, + }, + ], + }, + }; +} diff --git a/src/notion/publish/v2/publishStatsToPage.ts b/src/notion/publish/v2/publishStatsToPage.ts index 8e3e2c3..5f25d89 100644 --- a/src/notion/publish/v2/publishStatsToPage.ts +++ b/src/notion/publish/v2/publishStatsToPage.ts @@ -1,94 +1,40 @@ -import { Client, isFullBlock } from "@notionhq/client"; -import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints"; -import { formatValue } from "../../../format/formatValue"; +import { Client } from "@notionhq/client"; import { - isStatGroupDesc, - StatDesc, StatGroupDesc, StatsType, } from "../../../statistiques/v2/desc/StatsDesc"; -import { listAllChildrenBlocks } from "../../utils/listAllChildrenBlocks"; -import { removeBlocks } from "../../utils/removeBlocks"; +import { createStatGroupChildrenListItemBlock } from "./createStatGroupListItemBlock"; +import { updatePageContent } from "./updatePageContent"; +import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints"; export async function publishStatsToPage( notionClient: Client, statsPageId: string, + pageHeader: string, descriptor: D, stats: StatsType ) { - const childrenBlocks = ( - await listAllChildrenBlocks(notionClient, { - block_id: statsPageId, - }) - ).filter(isFullBlock); - const blocksIdsToRemove = childrenBlocks.map((b) => b.id); - await removeBlocks(notionClient, blocksIdsToRemove); + const statsBlocks = createStatGroupChildrenListItemBlock(descriptor, stats); - const newBlocks = createStatGroupChildrenListItemBlock(descriptor, stats); - - await notionClient.blocks.children.append({ - block_id: statsPageId, - children: [...newBlocks], - }); + const textBlock = createParagraphBlock(pageHeader); + await updatePageContent(notionClient, statsPageId, [ + textBlock, + ...statsBlocks, + ]); } -function createStatGroupListItemBlock( - descriptor: D, - stats: StatsType -): BulletedListItemBlockObjectRequest { - return { - bulleted_list_item: { - rich_text: [ - { - text: { - content: descriptor.label, - }, - }, - ], - children: createStatGroupChildrenListItemBlock( - descriptor, - stats - ) as BulletedListItemChildren, - }, - }; -} - -type BulletedListItemBlockObjectRequest = Extract< +type ParagraphBlockObjectRequest = Extract< BlockObjectRequest, - { bulleted_list_item: object } + { paragraph: unknown } >; -type BulletedListItemChildren = - BulletedListItemBlockObjectRequest["bulleted_list_item"]["children"]; - -function createStatGroupChildrenListItemBlock( - descriptor: D, - stats: StatsType -): BulletedListItemBlockObjectRequest[] { - return Object.keys(descriptor.stats).map((statName) => { - const childStatDesc = descriptor.stats[statName]; - const childStatValue = stats[statName]; - - return isStatGroupDesc(childStatDesc) - ? createStatGroupListItemBlock( - childStatDesc, - childStatValue as StatsType - ) - : createStatListItemBlock(childStatDesc, childStatValue as number); - }); -} - -function createStatListItemBlock( - descriptor: StatDesc, - statValue: number -): BulletedListItemBlockObjectRequest { +function createParagraphBlock(content: string): ParagraphBlockObjectRequest { return { - bulleted_list_item: { + paragraph: { rich_text: [ { text: { - content: - descriptor.label + ": " + formatValue(statValue, descriptor), + content: content, }, }, ], diff --git a/src/notion/publish/v2/updatePageContent.ts b/src/notion/publish/v2/updatePageContent.ts new file mode 100644 index 0000000..d295cc8 --- /dev/null +++ b/src/notion/publish/v2/updatePageContent.ts @@ -0,0 +1,23 @@ +import { Client, isFullBlock } from "@notionhq/client"; +import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints"; +import { listAllChildrenBlocks } from "../../utils/listAllChildrenBlocks"; +import { removeBlocks } from "../../utils/removeBlocks"; + +export async function updatePageContent( + notionClient: Client, + notionPageId: string, + newBlocks: BlockObjectRequest[] +) { + const childrenBlocks = ( + await listAllChildrenBlocks(notionClient, { + block_id: notionPageId, + }) + ).filter(isFullBlock); + const blocksIdsToRemove = childrenBlocks.map((b) => b.id); + await removeBlocks(notionClient, blocksIdsToRemove); + + await notionClient.blocks.children.append({ + block_id: notionPageId, + children: [...newBlocks], + }); +}