2024-06-02 21:00:42 +02:00
|
|
|
|
import { Client, isFullPage } from "@notionhq/client";
|
|
|
|
|
|
import { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints";
|
2024-06-03 21:56:35 +02:00
|
|
|
|
import {
|
2024-06-04 09:29:33 +02:00
|
|
|
|
ContexteEntreeDC,
|
2024-06-03 21:56:35 +02:00
|
|
|
|
EvenementFamille,
|
|
|
|
|
|
Famille,
|
|
|
|
|
|
StatutFamille,
|
|
|
|
|
|
TypeEvenement,
|
|
|
|
|
|
} from "../../data/Famille";
|
2024-06-02 14:34:11 +02:00
|
|
|
|
import { datePropertyToDate } from "../utils/properties/datePropertyToDate";
|
2024-06-03 10:27:30 +02:00
|
|
|
|
import { relationPropertyToPageId } from "../utils/properties/relationPropertyToPageId";
|
2024-06-02 21:00:42 +02:00
|
|
|
|
import { selectPropertyToText } from "../utils/properties/selectPropertyToText";
|
2024-06-02 14:34:11 +02:00
|
|
|
|
import { statusPropertyToText } from "../utils/properties/statusPropertyToText";
|
2024-06-02 21:00:42 +02:00
|
|
|
|
import { titlePropertyToText } from "../utils/properties/titlePropertyToText";
|
2024-06-02 14:34:11 +02:00
|
|
|
|
import { queryAllDbResults } from "../utils/queryAllDbResults";
|
2024-06-02 21:00:42 +02:00
|
|
|
|
import { richTextPropertyToPlainText } from "../utils/text/richTextPropertyToPlainText";
|
2024-06-02 14:34:11 +02:00
|
|
|
|
|
|
|
|
|
|
export async function fetchFamiliesWithEventsFromNotion(
|
|
|
|
|
|
notionClient: Client
|
2024-06-03 21:56:35 +02:00
|
|
|
|
): Promise<Famille[]> {
|
2024-06-02 14:34:11 +02:00
|
|
|
|
const familiesDbId: string = "5b69e02b296d4a578f8c8ab7fe8b05da";
|
2024-06-02 21:00:42 +02:00
|
|
|
|
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);
|
|
|
|
|
|
});
|
2024-06-02 14:34:11 +02:00
|
|
|
|
|
2024-06-03 21:56:35 +02:00
|
|
|
|
const families: Famille[] = await Promise.all(
|
2024-06-02 21:00:42 +02:00
|
|
|
|
familyPages.map((pageObjectResponse) => {
|
|
|
|
|
|
return buildFamily(pageObjectResponse, familyEvents);
|
2024-06-02 14:34:11 +02:00
|
|
|
|
})
|
|
|
|
|
|
);
|
|
|
|
|
|
return families;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-03 21:56:35 +02:00
|
|
|
|
function buildFamilyEvent(page: PageObjectResponse): EvenementFamille {
|
2024-06-02 21:00:42 +02:00
|
|
|
|
const pageProperties = page.properties;
|
|
|
|
|
|
|
2024-06-03 21:56:35 +02:00
|
|
|
|
const familyEvent: EvenementFamille = {
|
2024-06-02 21:00:42 +02:00
|
|
|
|
notionId: page.id,
|
|
|
|
|
|
Évènement: titlePropertyToText(pageProperties, "Évènement"),
|
2024-06-03 21:56:35 +02:00
|
|
|
|
Type: selectPropertyToText(pageProperties, "Type")! as TypeEvenement,
|
2024-06-02 21:00:42 +02:00
|
|
|
|
"Enfants concernés": richTextPropertyToPlainText(
|
|
|
|
|
|
pageProperties,
|
|
|
|
|
|
"Enfants concernés"
|
|
|
|
|
|
),
|
|
|
|
|
|
Date: datePropertyToDate(pageProperties, "Date"),
|
|
|
|
|
|
notionIdFamille: relationPropertyToPageId(pageProperties, "Famille")!,
|
|
|
|
|
|
};
|
|
|
|
|
|
return familyEvent;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function buildFamily(
|
|
|
|
|
|
page: PageObjectResponse,
|
2024-06-03 21:56:35 +02:00
|
|
|
|
familyEvents: EvenementFamille[]
|
|
|
|
|
|
): Famille {
|
2024-06-02 14:34:11 +02:00
|
|
|
|
const pageProperties = page.properties;
|
|
|
|
|
|
|
2024-06-03 21:56:35 +02:00
|
|
|
|
const family: Famille = {
|
2024-06-02 14:34:11 +02:00
|
|
|
|
notionId: page.id,
|
2024-06-02 21:00:42 +02:00
|
|
|
|
Titre: titlePropertyToText(pageProperties, ""),
|
|
|
|
|
|
Statut: statusPropertyToText(pageProperties, "Statut") as StatutFamille,
|
2024-06-04 09:29:33 +02:00
|
|
|
|
ContexteEntree: selectPropertyToText(
|
|
|
|
|
|
pageProperties,
|
|
|
|
|
|
"Contexte d’entrée DC"
|
|
|
|
|
|
) as ContexteEntreeDC,
|
2024-06-02 21:00:42 +02:00
|
|
|
|
Integration: datePropertyToDate(pageProperties, "Intégration"),
|
|
|
|
|
|
Sortie: datePropertyToDate(pageProperties, "Sortie"),
|
|
|
|
|
|
Evenements: familyEvents.filter((fe) => fe.notionIdFamille === page.id),
|
2024-06-02 14:34:11 +02:00
|
|
|
|
};
|
|
|
|
|
|
return family;
|
|
|
|
|
|
}
|