From dd26bfa09136ddbab8ff6f9be443032245367454 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Mon, 6 Nov 2023 09:47:19 +0100 Subject: [PATCH] feat(helloasso): basic list of users with custom fields --- .gitignore | 2 +- src/main.rs | 59 +++++++++++++++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 3d62c7b..86691aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /target .env - +log.log diff --git a/src/main.rs b/src/main.rs index a982629..cb6b30a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use serde::{Serialize, Deserialize}; use anyhow::{Context, Result, anyhow}; use chrono::prelude::{NaiveDate, DateTime, Utc}; use strum::{Display }; +use std::collections::HashSet; /// permanent config to store long-term config /// used to ingest env settings @@ -446,35 +447,40 @@ struct CustomFieldsMapping { } /// rust how to access inner enum value -#[derive(Debug, PartialEq)] -enum HelloAssoCustomFields { +#[derive(Debug, PartialEq, Clone, Copy)] +enum HelloAssoCustomFieldType { Address, PostalCode, City, Phone, Job, Skills, - Birthday + Birthday, + LinkedUserFirstName } -impl Into for HelloAssoCustomFields { - fn into(self) -> u64 { - match self { - HelloAssoCustomFields::Address => 12958695, - HelloAssoCustomFields::PostalCode => 12958717, - HelloAssoCustomFields::City => 12958722, - HelloAssoCustomFields::Phone => 13279172, - HelloAssoCustomFields::Job => 13279172, - HelloAssoCustomFields::Skills => 11231129, - HelloAssoCustomFields::Birthday => 12944367 +impl TryFrom<&str> for HelloAssoCustomFieldType { + type Error = (); + + fn try_from(subject: &str) -> Result { + match subject { + "Prénom conjoint" => Ok(HelloAssoCustomFieldType::LinkedUserFirstName), + "ADRESSE" => Ok(HelloAssoCustomFieldType::Address), + "CODE POSTAL" => Ok(HelloAssoCustomFieldType::PostalCode), + "VILLE" => Ok(HelloAssoCustomFieldType::City), + "PROFESSION" => Ok(HelloAssoCustomFieldType::Job), + "TÉLÉPHONE" => Ok(HelloAssoCustomFieldType::Phone), + "DATE DE NAISSANCE" => Ok(HelloAssoCustomFieldType::Birthday), + "CENTRE D'INTÉRÊTS / COMPÉTENCES" => Ok(HelloAssoCustomFieldType::Skills), + _ => Err(()) } } } -fn read_custom_field(form_answer: &FormAnswer, custom_field_id: HelloAssoCustomFields) -> Option { - let int_repr: u64 = custom_field_id.into(); +fn read_custom_field(form_answer: &FormAnswer, custom_field: HelloAssoCustomFieldType) -> Option { + // FIXME: compute the type directly at deserialization with serde form_answer.custom_fields.iter() - .find(|f| f.id == int_repr) + .find(|f| HelloAssoCustomFieldType::try_from(f.name.as_str()) == Ok(custom_field)) .and_then(|cf| Some(cf.answer.clone())) } @@ -507,24 +513,33 @@ async fn launch_adapter() -> Result<()> { let pk_memberships: Vec = vec![]; let mut pk_users: Vec = vec![]; + let mut count: u64 = 0; + let mut names: HashSet = HashSet::new(); for answer in answers { // TODO: parse birthday // NaiveDate::parse_from_str dbg!(&answer); + for custom_field in answer.custom_fields.iter() { + names.insert(custom_field.name.clone()); + count += 1; + } pk_users.push(PahekoUser { first_name: answer.user.first_name.clone(), last_name: answer.user.last_name.clone(), email: answer.user.email.clone(), - phone: read_custom_field(&answer, HelloAssoCustomFields::Phone), - skills: read_custom_field(&answer, HelloAssoCustomFields::Skills), - address: read_custom_field(&answer, HelloAssoCustomFields::Address).expect("to have address"), - postal_code: read_custom_field(&answer, HelloAssoCustomFields::PostalCode).expect("to have address"), - city: read_custom_field(&answer, HelloAssoCustomFields::City).expect("to have address"), - job: read_custom_field(&answer, HelloAssoCustomFields::Job), + phone: read_custom_field(&answer, HelloAssoCustomFieldType::Phone), + skills: read_custom_field(&answer, HelloAssoCustomFieldType::Skills), + address: read_custom_field(&answer, HelloAssoCustomFieldType::Address).expect("to have address"), + postal_code: read_custom_field(&answer, HelloAssoCustomFieldType::PostalCode).expect("to have postal code"), + city: read_custom_field(&answer, HelloAssoCustomFieldType::City).expect("to have city"), + job: read_custom_field(&answer, HelloAssoCustomFieldType::Job), birthday: None }); + // then create optional linked user } dbg!(pk_users); + dbg!(names); + dbg!(count); // then, request the current list of users // then, upload the PahekoMembership