From d213191fc3775363be1808af512d2b3a7c490dcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Arod?= Date: Tue, 17 Dec 2024 22:44:27 +0100 Subject: [PATCH] feat: support arbitrary block depth in updatePageContent --- src/notion/publish/v2/updatePageContent.ts | 76 ++++++++++++++++++- .../v2/penales/computeStatsPenales.ts | 19 ++--- 2 files changed, 80 insertions(+), 15 deletions(-) diff --git a/src/notion/publish/v2/updatePageContent.ts b/src/notion/publish/v2/updatePageContent.ts index d295cc8..fbd2a47 100644 --- a/src/notion/publish/v2/updatePageContent.ts +++ b/src/notion/publish/v2/updatePageContent.ts @@ -16,8 +16,78 @@ export async function updatePageContent( const blocksIdsToRemove = childrenBlocks.map((b) => b.id); await removeBlocks(notionClient, blocksIdsToRemove); - await notionClient.blocks.children.append({ - block_id: notionPageId, - children: [...newBlocks], + const useLayeredAppend = true; + if (useLayeredAppend) { + await appendBlocksByDepths(notionClient, notionPageId, newBlocks); + } else { + await notionClient.blocks.children.append({ + block_id: notionPageId, + children: [...newBlocks], + }); + } +} + +/** + * This approach works around the depth limit of Notion API by appending children as a second step. + * @param notionClient + * @param parentBlockId + * @param blocksToAppend + */ +async function appendBlocksByDepths(notionClient: Client, + parentBlockId: string, + blocksToAppend: BlockObjectRequest[] + ) { + const { blocksWithoutChildren, extractedChildren } = extractChildren(blocksToAppend); + + const appendResponse = await notionClient.blocks.children.append({ + block_id: parentBlockId, + children: [...blocksWithoutChildren], + }); + + appendResponse.results.forEach((b, index) => { + if (extractedChildren[index].length !== 0) { + appendBlocksByDepths(notionClient, b.id, extractedChildren[index]); + } }); } + +function extractChildren(blocks: BlockObjectRequest[]): { + blocksWithoutChildren: BlockObjectRequest[], + extractedChildren: BlockObjectRequest[][] +} { + const extractedChildren: BlockObjectRequest[][] = []; + const blocksWithoutChildren = blocks.map(b => { + if ("bulleted_list_item" in b) { + extractedChildren.push(b.bulleted_list_item.children || []); + return { + ...b, + bulleted_list_item: { + ...b.bulleted_list_item, + children: [] + }}; + } else if ("toggle" in b) { + extractedChildren.push(b.toggle.children|| []); + return { + ...b, + toggle: { + ...b.toggle, + children: [] + }}; + } else if ("paragraph" in b) { + extractedChildren.push(b.paragraph.children || []); + return { + ...b, + paragraph: { + ...b.paragraph, + children: [] + }}; + } else { + extractedChildren.push([]); + return b; + } + }); + return { + blocksWithoutChildren, + extractedChildren: extractedChildren + }; +} diff --git a/src/statistiques/v2/penales/computeStatsPenales.ts b/src/statistiques/v2/penales/computeStatsPenales.ts index 06194ed..368de04 100644 --- a/src/statistiques/v2/penales/computeStatsPenales.ts +++ b/src/statistiques/v2/penales/computeStatsPenales.ts @@ -15,7 +15,6 @@ import { filterFamillesWithOneOfEvenements } from "../filterFamillesWithOneOfEve import { filterFamillesWithOneOfEvenementsOfType } from "../filterFamillesWithOneOfEvenementsOfType"; import { StatsPenales } from "./StatsPenales"; import { nbFamillesAvecPagesLiees } from "./nbFamillesAvecPagesLiees"; -import { StatsData } from "../desc/StatsDesc"; type FamilleAvecInfoTribunaux = Famille & { evtTribunal1?: EvenementFamille; @@ -148,9 +147,9 @@ function computeCrpc( ); return { - nbFamilles: nbFamilles(famillesConcernees), - acceptees: nbFamilles(acceptees), - refusees: nbFamilles(refusees), + nbFamilles: nbFamillesAvecPagesLiees(famillesConcernees), + acceptees: nbFamillesAvecPagesLiees(acceptees), + refusees: nbFamillesAvecPagesLiees(refusees), }; } @@ -171,9 +170,9 @@ function computeCompositionPenales( ); return { - nbFamilles: nbFamilles(famillesConcernees), - acceptees: nbFamilles(acceptees), - refusees: nbFamilles(refusees), + nbFamilles: nbFamillesAvecPagesLiees(famillesConcernees), + acceptees: nbFamillesAvecPagesLiees(acceptees), + refusees: nbFamillesAvecPagesLiees(refusees), }; } @@ -284,8 +283,4 @@ function computeIntervalProcureurTribunalCorrectionnel( function isTribunalCorrectionnel(e: EvenementFamille): boolean { return e.Type === "Tribunal correctionnel"; -} - -export function nbFamilles(familles: Famille[]): StatsData { - return familles.length; -} +} \ No newline at end of file