Commit dd2b22af authored by Emmanuel Raviart's avatar Emmanuel Raviart

Improve GraphQL links between objects.

parent 56ea5740
Pipeline #100431 passed with stage
in 2 minutes and 16 seconds
use acteurs::Acteur;
use chrono::{Date, DateTime, FixedOffset};
use commun::{Codier, EmptyChoice, InfoJo, Rapporteur};
use contexts::Context;
use mandats::Mandat;
use organes::Organe;
use serde_utils::{map_to_vec, str_to_vec};
use std::cmp;
use textes_legislatifs::Document;
......@@ -669,7 +672,7 @@ graphql_object!(ActesLegislatifs: Context |&self| {
}
});
#[derive(Clone, Debug, Deserialize, GraphQLObject)]
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ActeurInitiateur {
#[serde(rename = "acteurRef")]
......@@ -678,7 +681,37 @@ pub struct ActeurInitiateur {
pub mandat_ref: String,
}
#[derive(Clone, Debug, Deserialize, GraphQLObject)]
graphql_object!(ActeurInitiateur: Context |&self| {
field acteur(&executor) -> Option<&Acteur> {
let context = &executor.context();
context.get_acteur_at_uid(&self.acteur_ref)
}
field acteur_ref() -> &str {
&self.acteur_ref
}
field mandat(&executor) -> Option<&Mandat> {
let context = &executor.context();
match context.get_acteur_at_uid(&self.acteur_ref) {
None => None,
Some(ref acteur) => {
for mandat in &acteur.mandats.mandats {
if mandat.uid() == &self.mandat_ref {
return Some(mandat)
}
}
None
},
}
}
field mandat_ref() -> &str {
return &self.mandat_ref
}
});
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ActeursInitiateur {
#[serde(deserialize_with = "map_to_vec", rename = "acteur")]
......@@ -1786,13 +1819,49 @@ pub struct InfoJoce {
pub ref_joce: String,
}
#[derive(Clone, Debug, Deserialize, GraphQLObject)]
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Initiateur {
pub acteurs: Option<ActeursInitiateur>,
pub organes: Option<OrganesInitiateur>,
}
graphql_object!(Initiateur: Context |&self| {
field acteurs() -> Vec<&ActeurInitiateur> {
match &self.acteurs {
None => vec![],
Some(ref acteurs) =>
acteurs.acteurs
.iter()
.collect(),
}
}
field organes(&executor) -> Vec<&Organe> {
match &self.organes {
None => vec![],
Some(ref organes) => {
let context = &executor.context();
organes.organes
.iter()
.filter_map(|ref organe| context.get_organe_at_uid(&organe.organe_ref.uid))
.collect()
},
}
}
field organes_refs() -> Vec<&str> {
match &self.organes {
None => vec![],
Some(ref organes) =>
organes.organes
.iter()
.map(|ref organe| organe.organe_ref.uid.as_str())
.collect(),
}
}
});
#[derive(Clone, Debug, Deserialize, GraphQLObject)]
#[serde(deny_unknown_fields)]
pub struct InitiateursCreationOrganeTemporaire {
......@@ -1921,20 +1990,20 @@ graphql_object!(NominRapporteurs: Context |&self| {
}
});
#[derive(Clone, Debug, Deserialize, GraphQLObject)]
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OrganeInitiateur {
#[serde(rename = "organeRef")]
pub organe_ref: OrganeInitiateurRef,
}
#[derive(Clone, Debug, Deserialize, GraphQLObject)]
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OrganeInitiateurRef {
pub uid: String,
}
#[derive(Clone, Debug, Deserialize, GraphQLObject)]
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OrganesInitiateur {
#[serde(deserialize_with = "map_to_vec", rename = "organe")]
......
......@@ -130,11 +130,10 @@ graphql_object!(Amendement: Context |&self| {
&self.pointeur_fragment_texte
}
field representations(&executor) -> Vec<&Representation> {
field representations() -> Vec<&Representation> {
match self.representations {
None => vec![],
Some(ref representations) => {
let context = &executor.context();
representations.representations
.iter()
.collect()
......@@ -535,11 +534,10 @@ pub struct ListeProgrammes {
}
graphql_object!(ListeProgrammes: Context |&self| {
field liste_programmes(&executor) -> Vec<&Programme> {
field liste_programmes() -> Vec<&Programme> {
match self.liste_programmes {
None => vec![],
Some(ref liste_programmes) => {
let context = &executor.context();
liste_programmes.programmes
.iter()
.collect()
......@@ -642,11 +640,10 @@ graphql_object!(Programme: Context |&self| {
}
}
field lignes_credits(&executor) -> Vec<&LigneCredit> {
field lignes_credits() -> Vec<&LigneCredit> {
match self.lignes_credits {
None => vec![],
Some(ref lignes_credits) => {
let context = &executor.context();
lignes_credits.lignes_credits
.iter()
.collect()
......
......@@ -2,6 +2,7 @@ use acteurs::Acteur;
use chrono::{DateTime, FixedOffset};
use commun::{CodeEtLibelle, EmptyChoice, Indexation};
use contexts::Context;
use dossiers_legislatifs::DossierParlementaire;
use mandats::Mandat;
use organes::Organe;
use serde_utils::{map_to_vec, str_to_bool};
......@@ -76,6 +77,10 @@ graphql_object!(AccordInternational: Context |&self| {
&self.documents_indexes
}
field dossier(&executor) -> Option<&DossierParlementaire> {
executor.context().get_dossier_parlementaire_at_uid(&self.dossier_ref)
}
field dossier_ref() -> &str {
&self.dossier_ref
}
......@@ -174,7 +179,7 @@ graphql_object!(ActeurLeg: Context |&self| {
}
});
#[derive(Debug, Deserialize, GraphQLObject)]
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AmendementsCommission {
#[serde(deserialize_with = "map_to_vec", rename = "commission")]
......@@ -297,6 +302,10 @@ graphql_object!(AvisConseilEtat: Context |&self| {
&self.documents_indexes
}
field dossier(&executor) -> Option<&DossierParlementaire> {
executor.context().get_dossier_parlementaire_at_uid(&self.dossier_ref)
}
field dossier_ref() -> &str {
&self.dossier_ref
}
......@@ -361,7 +370,7 @@ pub struct Classification {
pub type_classification: CodeEtLibelle,
}
#[derive(Debug, Deserialize, GraphQLObject)]
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Commission {
#[serde(deserialize_with = "str_to_bool")]
......@@ -372,6 +381,21 @@ pub struct Commission {
pub organe_ref: String,
}
graphql_object!(Commission: Context |&self| {
field amendable() -> bool {
self.amendable
}
field organe(&executor) -> Option<&Organe> {
let context = &executor.context();
context.get_organe_at_uid(&self.organe_ref)
}
field organe_ref() -> &str {
&self.organe_ref
}
});
#[derive(Clone, Debug, Deserialize, GraphQLObject)]
#[serde(deny_unknown_fields)]
pub struct Correction {
......@@ -381,21 +405,59 @@ pub struct Correction {
pub type_correction: String,
}
#[derive(Debug, Deserialize, GraphQLObject)]
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Cosignataire {
pub acteur: Option<ActeurCosignataire>,
#[serde(deserialize_with = "str_to_bool")]
pub edite: bool,
#[serde(rename = "dateCosignature")]
// TODO: Date<FixedOffset> type is not able to deserialize dates with timezone (`YYYY-MM-DD±HH:MM`).
pub date_cosignature: String,
#[serde(rename = "dateRetraitCosignature")]
// TODO: Date<FixedOffset> type is not able to deserialize dates with timezone (`YYYY-MM-DD±HH:MM`).
pub date_retrait_cosignature: Option<String>,
#[serde(deserialize_with = "str_to_bool")]
pub edite: bool,
pub organe: Option<OrganeCosignataire>,
}
graphql_object!(Cosignataire: Context |&self| {
field acteur(&executor) -> Option<&Acteur> {
match &self.acteur {
None => None,
Some(ref acteur) => {
let context = &executor.context();
context.get_acteur_at_uid(&acteur.acteur_ref)
},
}
}
field acteur_ref() -> Option<&str> {
match &self.acteur {
None => None,
Some(ref acteur) => Some(acteur.acteur_ref.as_str()),
}
}
field date_cosignature() -> &str {
&self.date_cosignature
}
field date_retrait_cosignature() -> Option<&str> {
match &self.date_retrait_cosignature {
None => None,
Some(ref s) => Some(s.as_str()),
}
}
field edite() -> bool {
self.edite
}
field organe() -> &Option<OrganeCosignataire> {
&self.organe
}
});
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Cosignataires {
......@@ -409,7 +471,7 @@ pub struct CycleDeVieDocument {
pub chrono: ChronoDocument,
}
#[derive(Debug, Deserialize, GraphQLObject)]
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DepotAmendements {
#[serde(rename = "amendementsCommission")]
......@@ -418,6 +480,19 @@ pub struct DepotAmendements {
pub amendements_seance: AmendementsSeance,
}
graphql_object!(DepotAmendements: Context |&self| {
field amendements_commission() -> Vec<&Commission> {
match &self.amendements_commission {
None => vec![],
Some(ref amendements_commission) => amendements_commission.commissions.iter().collect(),
}
}
field amendements_seance() -> &AmendementsSeance {
&self.amendements_seance
}
});
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Divisions {
......@@ -526,15 +601,15 @@ impl Document {
// }
// }
pub fn divisions(&self) -> &Option<Divisions> {
match *self {
Document::AccordInternational(AccordInternational { ref divisions, .. })
| Document::AvisConseilEtat(AvisConseilEtat { ref divisions, .. })
| Document::DocumentEtudeImpact(DocumentEtudeImpact { ref divisions, .. })
| Document::RapportParlementaire(RapportParlementaire { ref divisions, .. })
| Document::TexteLoi(TexteLoi { ref divisions, .. }) => &divisions,
}
pub fn divisions(&self) -> &Option<Divisions> {
match *self {
Document::AccordInternational(AccordInternational { ref divisions, .. })
| Document::AvisConseilEtat(AvisConseilEtat { ref divisions, .. })
| Document::DocumentEtudeImpact(DocumentEtudeImpact { ref divisions, .. })
| Document::RapportParlementaire(RapportParlementaire { ref divisions, .. })
| Document::TexteLoi(TexteLoi { ref divisions, .. }) => &divisions,
}
}
// pub fn dossier_ref(&self) -> &str {
// match *self {
......@@ -581,11 +656,21 @@ impl Document {
let mut documents_indexes: Vec<i32> = vec![*index];
*index += 1;
match *self {
Document::AccordInternational(ref mut document) => document.index_divisions(index, &mut documents_indexes),
Document::AvisConseilEtat(ref mut document) => document.index_divisions(index, &mut documents_indexes),
Document::DocumentEtudeImpact(ref mut document) => document.index_divisions(index, &mut documents_indexes),
Document::RapportParlementaire(ref mut document) => document.index_divisions(index, &mut documents_indexes),
Document::TexteLoi(ref mut document) => document.index_divisions(index, &mut documents_indexes),
Document::AccordInternational(ref mut document) => {
document.index_divisions(index, &mut documents_indexes)
}
Document::AvisConseilEtat(ref mut document) => {
document.index_divisions(index, &mut documents_indexes)
}
Document::DocumentEtudeImpact(ref mut document) => {
document.index_divisions(index, &mut documents_indexes)
}
Document::RapportParlementaire(ref mut document) => {
document.index_divisions(index, &mut documents_indexes)
}
Document::TexteLoi(ref mut document) => {
document.index_divisions(index, &mut documents_indexes)
}
}
}
......@@ -693,6 +778,10 @@ graphql_union!(Document: Context |&self| {
// &self.documents_indexes
// }
// field dossier(&executor) -> Option<&DossierParlementaire> {
// executor.context().get_dossier_parlementaire_at_uid(&self.dossier_ref)
// }
// field dossier_ref() -> &str {
// self.dossier_ref()
// }
......@@ -819,6 +908,10 @@ graphql_object!(DocumentEtudeImpact: Context |&self| {
&self.documents_indexes
}
field dossier(&executor) -> Option<&DossierParlementaire> {
executor.context().get_dossier_parlementaire_at_uid(&self.dossier_ref)
}
field dossier_ref() -> &str {
&self.dossier_ref
}
......@@ -882,7 +975,7 @@ pub struct Notice {
pub num_notice: Option<String>,
}
#[derive(Debug, Deserialize, GraphQLObject)]
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OrganeCosignataire {
#[serde(deserialize_with = "str_to_bool", rename = "etApparentes")]
......@@ -891,6 +984,21 @@ pub struct OrganeCosignataire {
organe_ref: String,
}
graphql_object!(OrganeCosignataire: Context |&self| {
field et_apparentes() -> bool {
self.et_apparentes
}
field organe(&executor) -> Option<&Organe> {
let context = &executor.context();
context.get_organe_at_uid(&self.organe_ref)
}
field organe_ref() -> &str {
&self.organe_ref
}
});
#[derive(Clone, Debug, Deserialize, GraphQLObject)]
#[serde(deny_unknown_fields)]
pub struct OrganeLeg {
......@@ -968,6 +1076,10 @@ graphql_object!(RapportParlementaire: Context |&self| {
&self.documents_indexes
}
field dossier(&executor) -> Option<&DossierParlementaire> {
executor.context().get_dossier_parlementaire_at_uid(&self.dossier_ref)
}
field dossier_ref() -> &str {
&self.dossier_ref
}
......@@ -1085,6 +1197,10 @@ graphql_object!(TexteLoi: Context |&self| {
&self.denomination_structurelle
}
field depot_amendements() -> &Option<DepotAmendements> {
&self.depot_amendements
}
// Field `divisions` is not used, because document is flattened before being sent with GraphQL.
// field divisions() -> Vec<&Document> {
// match &self.divisions {
......@@ -1097,6 +1213,10 @@ graphql_object!(TexteLoi: Context |&self| {
&self.documents_indexes
}
field dossier(&executor) -> Option<&DossierParlementaire> {
executor.context().get_dossier_parlementaire_at_uid(&self.dossier_ref)
}
field dossier_ref() -> &str {
&self.dossier_ref
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment