UNPKG

@tricoteuses/senat

Version:

Handle French Sénat's open data

96 lines (95 loc) 4.16 kB
import { jsonArrayFrom, jsonBuildObject } from "kysely/helpers/postgres"; import { dbSenat } from "../databases"; import { ID_DATE_FORMAT } from "../scripts/datautil"; import { toDateString } from "./util"; function sectionsLegislatives(dateSeance) { return jsonArrayFrom(dbSenat .selectFrom("debats.secdis") .leftJoin("debats.typsec", "debats.secdis.typseccod", "debats.typsec.typseccod") .where("debats.secdis.datsea", "=", dateSeance) .select(({ ref }) => [ "debats.secdis.secdisordid as id", "debats.secdis.secdisnum as numero", "debats.secdis.secdisobj as objet", "debats.secdis.secdisurl as url", "debats.typsec.typseclib as type", "debats.typsec.typseccat as categorie", interventionsLegislatives(ref("debats.secdis.secdiscle")).as("interventions"), "debats.secdis.lecassidt as lecture_id", ]) .orderBy("debats.secdis.secdisordid", "asc")); } function interventionsLegislatives(sectionId) { return jsonArrayFrom(dbSenat .selectFrom("debats.intpjl") .leftJoin("dosleg.auteur", "debats.intpjl.autcod", "dosleg.auteur.autcod") .where("debats.intpjl.secdiscle", "=", sectionId) .select(({ ref, val, fn }) => [ "debats.intpjl.intordid as id", "debats.intpjl.autcod as auteur_code", "debats.intpjl.intfon as fonction_intervenant", "debats.intpjl.inturl as url", "debats.intpjl.intana as analyse", jsonBuildObject({ code: ref("dosleg.auteur.autcod"), nom: ref("dosleg.auteur.nomuse"), prenom: ref("dosleg.auteur.prenom"), matricule: ref("dosleg.auteur.autmat"), }).as("auteur"), ]) .orderBy("debats.intpjl.intordid", "asc")); } function sectionsNonLegislatives(dateSeance) { return jsonArrayFrom(dbSenat .selectFrom("debats.secdivers") .leftJoin("debats.typsec", "debats.secdivers.typseccod", "debats.typsec.typseccod") .where("debats.secdivers.datsea", "=", dateSeance) .select(({ ref }) => [ "debats.secdivers.secdiverslibelle as libelle", "debats.secdivers.secdiversobj as objet", "debats.typsec.typseclib as type", "debats.typsec.typseccat as categorie", interventionsNonLegislatives(ref("debats.secdivers.secdiverscle")).as("interventions"), ])); } function interventionsNonLegislatives(sectionId) { return jsonArrayFrom(dbSenat .selectFrom("debats.intdivers") .leftJoin("dosleg.auteur", "debats.intdivers.autcod", "dosleg.auteur.autcod") .where("debats.intdivers.intdiverscle", "=", sectionId) .select(({ ref, val }) => [ "debats.intdivers.intdiversordid as id", "debats.intdivers.autcod as auteur_code", "debats.intdivers.intfon as fonction_intervenant", "debats.intdivers.inturl as url", "debats.intdivers.intana as analyse", jsonBuildObject({ code: ref("dosleg.auteur.autcod"), nom: ref("dosleg.auteur.nomuse"), prenom: ref("dosleg.auteur.prenom"), matricule: ref("dosleg.auteur.autmat"), }).as("auteur"), ]) .orderBy("debats.intdivers.intdiversordid", "asc")); } function lecturesAssemblee(dateSeance) { return jsonArrayFrom(dbSenat .selectFrom("debats.lecassdeb") .where("debats.lecassdeb.datsea", "=", dateSeance) .select("debats.lecassdeb.lecassidt as id")); } const findAllQuery = dbSenat .selectFrom("debats.debats") .select(({ ref, val }) => [ toDateString(ref("debats.debats.datsea"), val(ID_DATE_FORMAT)).as("id"), toDateString(ref("debats.debats.datsea")).as("date_seance"), "debats.debats.numero as numero", "debats.debats.deburl as url", "debats.debats.debsyn as etat_synchronisation", sectionsLegislatives(ref("debats.debats.datsea")).as("sections"), sectionsNonLegislatives(ref("debats.debats.datsea")).as("sections_divers"), lecturesAssemblee(ref("debats.debats.datsea")).as("lectures"), ]); export function findAll(fromSession) { return findAllQuery.stream(); }