UNPKG

@tricoteuses/senat

Version:

Handle French Sénat's open data

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