UNPKG

@tricoteuses/senat

Version:

Handle French Sénat's open data

142 lines (141 loc) 5.82 kB
import { sql } from "kysely"; import { dbSenat } from "../databases"; import { concat, rtrim, toDateString } from "./util"; import { jsonArrayFrom } from "kysely/helpers/postgres"; function stripTrailingHashes(expr) { return sql `regexp_replace(${expr}, '#+$', '')`; } function orderOrdreOrigineTexte(expr) { return sql `array_position(array['0','2','1'], ${expr})`; } function auteursRapport(rapportId) { return jsonArrayFrom(dbSenat .withSchema("dosleg") .selectFrom("dosleg.auteur") .leftJoin("dosleg.ecr", "dosleg.ecr.autcod", "dosleg.auteur.autcod") .leftJoin("dosleg.rolsig", "dosleg.rolsig.signataire", "dosleg.ecr.signataire") .where("dosleg.ecr.rapcod", "=", rapportId) .select([ "dosleg.auteur.prenom as prenom", "dosleg.auteur.nomuse as nom_usuel", "dosleg.auteur.autmat as matricule", "dosleg.ecr.ecrnumtri as ordre", "dosleg.rolsig.rolsiglib as role", "dosleg.ecr.ecrqua as qualite", ]) .orderBy("dosleg.ecr.ecrnumtri", "asc")); } function documentsAttaches(rapportId) { return jsonArrayFrom(dbSenat .withSchema("dosleg") .selectFrom("docatt") .leftJoin("typatt", "docatt.typattcod", "typatt.typattcod") .where("docatt.rapcod", "=", rapportId) .select(["docatt.docatturl as url", "typatt.typattlib as type_document"])); } function selectRapportAttributes({ eb, ref, val }) { return [ "rap.rapnum as numero", "raporg.orgcod as code_organisme", eb .case() .when("rap.rapurl", "is not", null) .then(stripTrailingHashes(sql `regexp_replace(trim(${ref("rap.rapurl")}), '^(.*/)?(.*?)(\\.html)?$', '\\2')`)) .else(null) .end() .as("id"), eb .case() .when("rap.typurl", "=", "I") .then(stripTrailingHashes(concat(val("https://www.senat.fr/rap/"), rtrim(ref("rap.rapurl"))))) .else(stripTrailingHashes(rtrim(ref("rap.rapurl")))) .end() .as("url"), rtrim(ref("denrap.libdenrap")).as("type"), rtrim(rtrim(ref("rap.raptil"))).as("titre"), rtrim(rtrim(ref("rap.rapsoustit"))).as("sous_titre"), toDateString(ref("rap.date_depot")).as("date"), "rap.sesann as session", auteursRapport(ref("rap.rapcod")).as("auteurs"), documentsAttaches(ref("rap.rapcod")).as("documents_annexes"), ]; } const baseQueryRapports = dbSenat .withSchema("dosleg") .selectFrom("rap") .leftJoin("raporg", "raporg.rapcod", "rap.rapcod") .leftJoin("denrap", "denrap.coddenrap", "rap.coddenrap") .leftJoin("lecassrap", "lecassrap.rapcod", "rap.rapcod"); const queryRapports = baseQueryRapports .leftJoin("lecass", "lecass.lecassidt", "lecassrap.lecassidt") .leftJoin("lecture", "lecture.lecidt", "lecass.lecidt") .leftJoin("loi", "loi.loicod", "lecture.loicod") .select((args) => ["loi.signet as signet_dossier", ...selectRapportAttributes(args)]); export function rapports(lectureAssembleeId) { return jsonArrayFrom(baseQueryRapports.select(selectRapportAttributes).where("lecassrap.lecassidt", "=", lectureAssembleeId)); } function auteursTexte(texteId) { return jsonArrayFrom(dbSenat .withSchema("dosleg") .selectFrom("auteur") .leftJoin("ecr", "ecr.autcod", "auteur.autcod") .leftJoin("rolsig", "rolsig.signataire", "ecr.signataire") .where("ecr.texcod", "=", texteId) .select([ "auteur.prenom as prenom", "auteur.nomuse as nom_usuel", "auteur.autmat as matricule", "ecr.ecrnumtri as ordre", "rolsig.rolsiglib as role", "ecr.ecrqua as qualite", ]) .orderBy("ecr.ecrnumtri", "asc")); } function selectTexteAttributes({ eb, ref, val }) { return [ "texte.texnum as numero", "texte.orgcod as code_organisme", eb .case() .when("texte.texurl", "is not", null) .then(stripTrailingHashes(sql `regexp_replace(trim(${ref("texte.texurl")}), '^(.*/)?(.*?)(\\.html)?$', '\\2')`)) .else(null) .end() .as("id"), eb .case() .when("texte.typurl", "=", "I") .then(stripTrailingHashes(concat(val("https://www.senat.fr/leg/"), rtrim(ref("texte.texurl"))))) .else(stripTrailingHashes(rtrim(ref("texte.texurl")))) .end() .as("url"), rtrim(ref("oritxt.oritxtlib")).as("origine"), "oritxt.oriordre as ordre_origine", "oritxt.oritxtado as code_adoption", "oritxt.oritxtmod as modification", rtrim(ref("typtxt.typtxtlib")).as("type"), toDateString(ref("texte.txtoritxtdat")).as("date"), "texte.sesann as session", auteursTexte(ref("texte.texcod")).as("auteurs"), ]; } const baseQueryTextes = dbSenat .withSchema("dosleg") .selectFrom("texte") .leftJoin("oritxt", "oritxt.oritxtcod", "texte.oritxtcod") .leftJoin("typtxt", "typtxt.typtxtcod", "texte.typtxtcod") .orderBy(({ ref }) => orderOrdreOrigineTexte(ref("oritxt.oriordre"))); const queryTextes = baseQueryTextes .leftJoin("lecass", "lecass.lecassidt", "texte.lecassidt") .leftJoin("lecture", "lecture.lecidt", "lecass.lecidt") .leftJoin("loi", "loi.loicod", "lecture.loicod") .select((args) => ["loi.signet as signet_dossier", ...selectTexteAttributes(args)]); export function textes(lectureAssembleeId) { return jsonArrayFrom(baseQueryTextes.select(selectTexteAttributes).where("texte.lecassidt", "=", lectureAssembleeId)); } export function findAllTextes() { return queryTextes.stream(); } export function findAllRapports() { return queryRapports.stream(); }