mirror of
https://framagit.org/enfance-libre/statistiques
synced 2025-12-07 13:53:45 +00:00
feat: fetch Amendes
This commit is contained in:
parent
cbed51883c
commit
beb69eacac
6 changed files with 188 additions and 47 deletions
6
src/data/Amende.ts
Normal file
6
src/data/Amende.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
export type Amende = Readonly<{
|
||||||
|
notionId: string;
|
||||||
|
notionIdEvenement: string;
|
||||||
|
Montant: number;
|
||||||
|
Type: "ferme" | "avec sursis";
|
||||||
|
}>;
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { Period } from "../period/Period";
|
import { Period } from "../period/Period";
|
||||||
import { isPeriodContaining } from "../period/isPeriodContaining";
|
import { isPeriodContaining } from "../period/isPeriodContaining";
|
||||||
|
import { Amende } from "./Amende";
|
||||||
import { TypeEvenement } from "./TypeEvenement";
|
import { TypeEvenement } from "./TypeEvenement";
|
||||||
import {
|
import {
|
||||||
isEvtTypeProcedurePenale,
|
isEvtTypeProcedurePenale,
|
||||||
|
|
@ -14,6 +15,7 @@ export type EvenementFamille = Readonly<{
|
||||||
Évènement: string;
|
Évènement: string;
|
||||||
Date: Date | null;
|
Date: Date | null;
|
||||||
Type: TypeEvenement;
|
Type: TypeEvenement;
|
||||||
|
Amendes: Amende[];
|
||||||
"Enfants concernés": string;
|
"Enfants concernés": string;
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,3 +3,4 @@ export const departementsDbId: string = "787779be614544069f609bf9d2a7cd15";
|
||||||
export const contactsDbId: string = "be112427dd314c14b93dfd027323c908";
|
export const contactsDbId: string = "be112427dd314c14b93dfd027323c908";
|
||||||
|
|
||||||
export const missionsDbId: string = "fa2ab021a5dc4e239b7e7201ae579162";
|
export const missionsDbId: string = "fa2ab021a5dc4e239b7e7201ae579162";
|
||||||
|
export const amendesDbId: string = "1fd168be9f198019bb38c6ed4a90d00b";
|
||||||
|
|
|
||||||
39
src/notion/fetch/fetchAmendes.ts
Normal file
39
src/notion/fetch/fetchAmendes.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
import { Client, isFullPage } from "@notionhq/client";
|
||||||
|
import { Amende } from "../../data/Amende";
|
||||||
|
import { relationPropertyToPageId } from "../utils/properties/relationPropertyToPageId";
|
||||||
|
import { selectPropertyToText } from "../utils/properties/selectPropertyToText";
|
||||||
|
import { titlePropertyToText } from "../utils/properties/titlePropertyToText";
|
||||||
|
import { queryAllDbResults } from "../utils/queryAllDbResults";
|
||||||
|
import { amendesDbId } from "./dbIds";
|
||||||
|
|
||||||
|
export async function fetchAmendes(
|
||||||
|
notionClient: Client,
|
||||||
|
cacheConfig: boolean | { ttl: number }
|
||||||
|
): Promise<Amende[]> {
|
||||||
|
const amendesPages = (
|
||||||
|
await queryAllDbResults(
|
||||||
|
notionClient,
|
||||||
|
{
|
||||||
|
database_id: amendesDbId,
|
||||||
|
},
|
||||||
|
{ cache: cacheConfig }
|
||||||
|
)
|
||||||
|
).filter(isFullPage);
|
||||||
|
|
||||||
|
const amendes: Amende[] = amendesPages.map((page) => ({
|
||||||
|
notionId: page.id,
|
||||||
|
notionIdEvenement: relationPropertyToPageId(page.properties, "Évènements")!,
|
||||||
|
Montant: amendeStringToNumber(
|
||||||
|
titlePropertyToText(page.properties, "Montant/parent")
|
||||||
|
)!,
|
||||||
|
Type: selectPropertyToText(page.properties, "Amendes")! as Amende["Type"],
|
||||||
|
}));
|
||||||
|
return amendes;
|
||||||
|
}
|
||||||
|
|
||||||
|
function amendeStringToNumber(value: string | null): number {
|
||||||
|
if (value === null) {
|
||||||
|
return NaN; // or throw an error if you prefer
|
||||||
|
}
|
||||||
|
return parseFloat(value);
|
||||||
|
}
|
||||||
|
|
@ -31,6 +31,8 @@ import { Contact } from "../../data/Contact";
|
||||||
import { Mission } from "../../data/Mission";
|
import { Mission } from "../../data/Mission";
|
||||||
import { relationPropertyToPageIds } from "../utils/properties/relationPropertyToPageIds";
|
import { relationPropertyToPageIds } from "../utils/properties/relationPropertyToPageIds";
|
||||||
import { checkboxPropertyToBoolean } from "../utils/properties/checkboxPropertyToBoolean";
|
import { checkboxPropertyToBoolean } from "../utils/properties/checkboxPropertyToBoolean";
|
||||||
|
import { Amende } from "../../data/Amende";
|
||||||
|
import { fetchAmendes } from "./fetchAmendes";
|
||||||
|
|
||||||
type Departement = {
|
type Departement = {
|
||||||
notionId: string;
|
notionId: string;
|
||||||
|
|
@ -40,6 +42,76 @@ export async function fetchFamiliesWithEventsFromNotion(
|
||||||
notionClient: Client,
|
notionClient: Client,
|
||||||
cacheConfig: CacheConfig = { ttl: 3600 * 1000 }
|
cacheConfig: CacheConfig = { ttl: 3600 * 1000 }
|
||||||
): Promise<Famille[]> {
|
): Promise<Famille[]> {
|
||||||
|
const departements: Departement[] = await fetchDepartements(
|
||||||
|
notionClient,
|
||||||
|
cacheConfig
|
||||||
|
);
|
||||||
|
const missions: Mission[] = await fetchMissions(notionClient, cacheConfig);
|
||||||
|
const contacts: Contact[] = await fetchContacts(
|
||||||
|
notionClient,
|
||||||
|
cacheConfig,
|
||||||
|
missions
|
||||||
|
);
|
||||||
|
const amendes: Amende[] = await fetchAmendes(notionClient, cacheConfig);
|
||||||
|
const evenements = await fetchEvenements(notionClient, cacheConfig, amendes);
|
||||||
|
const familles: Famille[] = await fetchFamilles(
|
||||||
|
notionClient,
|
||||||
|
cacheConfig,
|
||||||
|
evenements,
|
||||||
|
departements,
|
||||||
|
contacts
|
||||||
|
);
|
||||||
|
return familles;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchFamilles(
|
||||||
|
notionClient: Client,
|
||||||
|
cacheConfig: boolean | { ttl: number },
|
||||||
|
familyEvents: Readonly<{
|
||||||
|
notionId: string;
|
||||||
|
notionIdFamille: string;
|
||||||
|
Évènement: string;
|
||||||
|
Date: Date | null;
|
||||||
|
Type: TypeEvenement;
|
||||||
|
Amendes: Amende[];
|
||||||
|
"Enfants concernés": string;
|
||||||
|
}>[],
|
||||||
|
departements: Departement[],
|
||||||
|
contacts: Readonly<{
|
||||||
|
notionId: string;
|
||||||
|
notionIdFamille: string;
|
||||||
|
Nom: string;
|
||||||
|
Missions: Mission[];
|
||||||
|
AExercéUneMission: boolean;
|
||||||
|
}>[]
|
||||||
|
) {
|
||||||
|
const familyPages = (
|
||||||
|
await queryAllDbResults(
|
||||||
|
notionClient,
|
||||||
|
{
|
||||||
|
database_id: familiesDbId,
|
||||||
|
},
|
||||||
|
{ cache: cacheConfig }
|
||||||
|
)
|
||||||
|
).filter(isFullPage);
|
||||||
|
const familles: Famille[] = await Promise.all(
|
||||||
|
familyPages.map((pageObjectResponse) => {
|
||||||
|
return buildFamily(
|
||||||
|
pageObjectResponse,
|
||||||
|
familyEvents,
|
||||||
|
departements,
|
||||||
|
contacts
|
||||||
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
return familles;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchEvenements(
|
||||||
|
notionClient: Client,
|
||||||
|
cacheConfig: boolean | { ttl: number },
|
||||||
|
amendes: Amende[]
|
||||||
|
) {
|
||||||
const eventPages = (
|
const eventPages = (
|
||||||
await queryAllDbResults(
|
await queryAllDbResults(
|
||||||
notionClient,
|
notionClient,
|
||||||
|
|
@ -50,16 +122,22 @@ export async function fetchFamiliesWithEventsFromNotion(
|
||||||
{ cache: cacheConfig }
|
{ cache: cacheConfig }
|
||||||
)
|
)
|
||||||
).filter(isFullPage);
|
).filter(isFullPage);
|
||||||
const familyPages = (
|
const familyEvents = eventPages.map((pageObjectResponse) => {
|
||||||
await queryAllDbResults(
|
return buildFamilyEvent(pageObjectResponse, amendes);
|
||||||
notionClient,
|
});
|
||||||
{
|
return familyEvents;
|
||||||
database_id: familiesDbId,
|
}
|
||||||
},
|
|
||||||
{ cache: cacheConfig }
|
|
||||||
)
|
|
||||||
).filter(isFullPage);
|
|
||||||
|
|
||||||
|
async function fetchContacts(
|
||||||
|
notionClient: Client,
|
||||||
|
cacheConfig: boolean | { ttl: number },
|
||||||
|
missions: Readonly<{
|
||||||
|
notionId: string;
|
||||||
|
Nom: string;
|
||||||
|
Equipe: string;
|
||||||
|
ContactsNotionIds: string[];
|
||||||
|
}>[]
|
||||||
|
) {
|
||||||
const contactPages = (
|
const contactPages = (
|
||||||
await queryAllDbResults(
|
await queryAllDbResults(
|
||||||
notionClient,
|
notionClient,
|
||||||
|
|
@ -70,6 +148,23 @@ export async function fetchFamiliesWithEventsFromNotion(
|
||||||
)
|
)
|
||||||
).filter(isFullPage);
|
).filter(isFullPage);
|
||||||
|
|
||||||
|
const contacts: Contact[] = contactPages.map((page) => ({
|
||||||
|
notionId: page.id,
|
||||||
|
notionIdFamille: relationPropertyToPageId(page.properties, "Famille")!,
|
||||||
|
Nom: titlePropertyToText(page.properties, "Nom"),
|
||||||
|
AExercéUneMission: checkboxPropertyToBoolean(
|
||||||
|
page.properties,
|
||||||
|
"A exercé une mission"
|
||||||
|
),
|
||||||
|
Missions: missions.filter((m) => m.ContactsNotionIds.includes(page.id)),
|
||||||
|
}));
|
||||||
|
return contacts;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchMissions(
|
||||||
|
notionClient: Client,
|
||||||
|
cacheConfig: boolean | { ttl: number }
|
||||||
|
) {
|
||||||
const missionsPages = (
|
const missionsPages = (
|
||||||
await queryAllDbResults(
|
await queryAllDbResults(
|
||||||
notionClient,
|
notionClient,
|
||||||
|
|
@ -79,7 +174,22 @@ export async function fetchFamiliesWithEventsFromNotion(
|
||||||
{ cache: cacheConfig }
|
{ cache: cacheConfig }
|
||||||
)
|
)
|
||||||
).filter(isFullPage);
|
).filter(isFullPage);
|
||||||
|
const missions: Mission[] = missionsPages.map((page) => ({
|
||||||
|
notionId: page.id,
|
||||||
|
Nom: titlePropertyToText(page.properties, "Nom"),
|
||||||
|
Equipe: selectPropertyToText(page.properties, "Équipe")!,
|
||||||
|
ContactsNotionIds: relationPropertyToPageIds(
|
||||||
|
page.properties,
|
||||||
|
"📔 Contacts"
|
||||||
|
),
|
||||||
|
}));
|
||||||
|
return missions;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchDepartements(
|
||||||
|
notionClient: Client,
|
||||||
|
cacheConfig: boolean | { ttl: number }
|
||||||
|
) {
|
||||||
const departementPages = (
|
const departementPages = (
|
||||||
await queryAllDbResults(
|
await queryAllDbResults(
|
||||||
notionClient,
|
notionClient,
|
||||||
|
|
@ -94,46 +204,13 @@ export async function fetchFamiliesWithEventsFromNotion(
|
||||||
notionId: page.id,
|
notionId: page.id,
|
||||||
name: titlePropertyToText(page.properties, "Nom"),
|
name: titlePropertyToText(page.properties, "Nom"),
|
||||||
}));
|
}));
|
||||||
|
return departements;
|
||||||
const missions: Mission[] = missionsPages.map((page) => ({
|
|
||||||
notionId: page.id,
|
|
||||||
Nom: titlePropertyToText(page.properties, "Nom"),
|
|
||||||
Equipe: selectPropertyToText(page.properties, "Équipe")!,
|
|
||||||
ContactsNotionIds: relationPropertyToPageIds(
|
|
||||||
page.properties,
|
|
||||||
"📔 Contacts"
|
|
||||||
),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const contacts: Contact[] = contactPages.map((page) => ({
|
|
||||||
notionId: page.id,
|
|
||||||
notionIdFamille: relationPropertyToPageId(page.properties, "Famille")!,
|
|
||||||
Nom: titlePropertyToText(page.properties, "Nom"),
|
|
||||||
AExercéUneMission: checkboxPropertyToBoolean(
|
|
||||||
page.properties,
|
|
||||||
"A exercé une mission"
|
|
||||||
),
|
|
||||||
Missions: missions.filter((m) => m.ContactsNotionIds.includes(page.id)),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const familyEvents = eventPages.map((pageObjectResponse) => {
|
|
||||||
return buildFamilyEvent(pageObjectResponse);
|
|
||||||
});
|
|
||||||
|
|
||||||
const familles: Famille[] = await Promise.all(
|
|
||||||
familyPages.map((pageObjectResponse) => {
|
|
||||||
return buildFamily(
|
|
||||||
pageObjectResponse,
|
|
||||||
familyEvents,
|
|
||||||
departements,
|
|
||||||
contacts
|
|
||||||
);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
return familles;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildFamilyEvent(page: PageObjectResponse): EvenementFamille {
|
function buildFamilyEvent(
|
||||||
|
page: PageObjectResponse,
|
||||||
|
amendes: Amende[]
|
||||||
|
): EvenementFamille {
|
||||||
const pageProperties = page.properties;
|
const pageProperties = page.properties;
|
||||||
|
|
||||||
const familyEvent: EvenementFamille = {
|
const familyEvent: EvenementFamille = {
|
||||||
|
|
@ -146,6 +223,7 @@ function buildFamilyEvent(page: PageObjectResponse): EvenementFamille {
|
||||||
),
|
),
|
||||||
Date: datePropertyToDate(pageProperties, "Date"),
|
Date: datePropertyToDate(pageProperties, "Date"),
|
||||||
notionIdFamille: relationPropertyToPageId(pageProperties, "Famille")!,
|
notionIdFamille: relationPropertyToPageId(pageProperties, "Famille")!,
|
||||||
|
Amendes: amendes.filter((a) => a.notionIdEvenement === page.id),
|
||||||
};
|
};
|
||||||
return familyEvent;
|
return familyEvent;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
15
src/notion/utils/properties/numberPropertyToNumber.ts
Normal file
15
src/notion/utils/properties/numberPropertyToNumber.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
import { PageProperties } from "../types/PageProperties";
|
||||||
|
import { extractPagePropertyValue } from "./extractPagePropertyValue";
|
||||||
|
|
||||||
|
export function numberPropertyToNumber(
|
||||||
|
pageProperties: PageProperties,
|
||||||
|
propName: string
|
||||||
|
): number | null {
|
||||||
|
const propValue = extractPagePropertyValue(pageProperties, propName);
|
||||||
|
if (propValue.type !== "number") {
|
||||||
|
throw new Error(
|
||||||
|
`Property ${propName} was expected to have type "number" but got "${propValue.type}".`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return propValue.number;
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue