mirror of
https://framagit.org/enfance-libre/statistiques
synced 2025-12-07 13:53:45 +00:00
81 lines
3 KiB
TypeScript
81 lines
3 KiB
TypeScript
import { Client, isFullPage } from "@notionhq/client";
|
||
import { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints";
|
||
import { ContexteEntreeDC } from "../../data/ContexteEntreeDC";
|
||
import { EvenementFamille } from "../../data/EvenementFamille";
|
||
import { Famille } from "../../data/Famille";
|
||
import { StatutFamille } from "../../data/StatutFamille";
|
||
import { TypeEvenement } from "../../data/TypeEvenement";
|
||
import { datePropertyToDate } from "../utils/properties/datePropertyToDate";
|
||
import { relationPropertyToPageId } from "../utils/properties/relationPropertyToPageId";
|
||
import { selectPropertyToText } from "../utils/properties/selectPropertyToText";
|
||
import { statusPropertyToText } from "../utils/properties/statusPropertyToText";
|
||
import { titlePropertyToText } from "../utils/properties/titlePropertyToText";
|
||
import { queryAllDbResults } from "../utils/queryAllDbResults";
|
||
import { richTextPropertyToPlainText } from "../utils/text/richTextPropertyToPlainText";
|
||
|
||
export async function fetchFamiliesWithEventsFromNotion(
|
||
notionClient: Client
|
||
): Promise<Famille[]> {
|
||
const familiesDbId: string = "5b69e02b296d4a578f8c8ab7fe8b05da";
|
||
const familEventsDbId: string = "c4d434b4603c4481a4d445618ecdf999";
|
||
|
||
const eventPages = (
|
||
await queryAllDbResults(notionClient, {
|
||
database_id: familEventsDbId,
|
||
})
|
||
).filter(isFullPage);
|
||
const familyPages = (
|
||
await queryAllDbResults(notionClient, {
|
||
database_id: familiesDbId,
|
||
})
|
||
).filter(isFullPage);
|
||
|
||
const familyEvents = eventPages.map((pageObjectResponse) => {
|
||
return buildFamilyEvent(pageObjectResponse);
|
||
});
|
||
|
||
const families: Famille[] = await Promise.all(
|
||
familyPages.map((pageObjectResponse) => {
|
||
return buildFamily(pageObjectResponse, familyEvents);
|
||
})
|
||
);
|
||
return families;
|
||
}
|
||
|
||
function buildFamilyEvent(page: PageObjectResponse): EvenementFamille {
|
||
const pageProperties = page.properties;
|
||
|
||
const familyEvent: EvenementFamille = {
|
||
notionId: page.id,
|
||
Évènement: titlePropertyToText(pageProperties, "Évènement"),
|
||
Type: selectPropertyToText(pageProperties, "Type")! as TypeEvenement,
|
||
"Enfants concernés": richTextPropertyToPlainText(
|
||
pageProperties,
|
||
"Enfants concernés"
|
||
),
|
||
Date: datePropertyToDate(pageProperties, "Date"),
|
||
notionIdFamille: relationPropertyToPageId(pageProperties, "Famille")!,
|
||
};
|
||
return familyEvent;
|
||
}
|
||
|
||
function buildFamily(
|
||
page: PageObjectResponse,
|
||
familyEvents: EvenementFamille[]
|
||
): Famille {
|
||
const pageProperties = page.properties;
|
||
|
||
const family: Famille = {
|
||
notionId: page.id,
|
||
Titre: titlePropertyToText(pageProperties, ""),
|
||
Statut: statusPropertyToText(pageProperties, "Statut") as StatutFamille,
|
||
ContexteEntree: selectPropertyToText(
|
||
pageProperties,
|
||
"Contexte d’entrée DC"
|
||
) as ContexteEntreeDC,
|
||
Integration: datePropertyToDate(pageProperties, "Intégration"),
|
||
Sortie: datePropertyToDate(pageProperties, "Sortie"),
|
||
Evenements: familyEvents.filter((fe) => fe.notionIdFamille === page.id),
|
||
};
|
||
return family;
|
||
}
|