From af8b93ab91ff565aafee592187f24e823c0d5583 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Thu, 21 Dec 2023 17:31:21 +0100 Subject: [PATCH] refactor: split paheko source code --- src/main.rs | 94 +++++++++++---------------------------------------- src/paheko.rs | 62 +++++++++++++++++++++++++++++++++ src/utils.rs | 21 ++++++++++++ 3 files changed, 102 insertions(+), 75 deletions(-) create mode 100644 src/paheko.rs create mode 100644 src/utils.rs diff --git a/src/main.rs b/src/main.rs index 78708fe..abb430f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod utils; mod paheko; mod helloasso; @@ -7,27 +8,8 @@ use chrono::prelude::{NaiveDate, DateTime, Utc, Datelike}; use strum::Display; use serde::{Serialize, Deserialize}; use std::collections::HashSet; -use rand::{thread_rng, Rng}; use phonenumber; - -/// ID -#[derive(Clone, Deserialize, Serialize, Debug, PartialEq, Eq, Hash)] -pub struct Id(pub u64); -impl Id { - pub fn to_string(&self) -> String { - format!("{:x}", self.0) - } -} -impl Into for Id { - fn into(self) -> String { - format!("{:x}", self.0) - } -} - -pub fn generate_id() -> Id { - Id(thread_rng().gen()) -} - +use utils::generate_id; /// permanent config to store long-term config /// used to ingest env settings @@ -142,48 +124,6 @@ async fn get_auth_client_from_cache( } -#[derive(Debug, Serialize, Deserialize, Clone)] -struct HelloassoReferences { - answer_id: u64, - order_id: u64 - // payment_id: u64, -} - -#[derive(Debug, Serialize, Deserialize, Clone)] -struct ExternalReferences { - helloasso_ref: HelloassoReferences -} - -/// for now we include the custom fields into the paheko user -/// we don't have time to implement user settings to change the custom fields mapping -/// for now, manual mapping -#[derive(Debug, Serialize, Deserialize, Clone)] -struct PahekoUser { - id: Id, - first_name: String, - last_name: String, - email: Option, - - phone: Option, - address: String, - city: String, - postal_code: String, - country: String, - skills: Option, - job: Option, - birthday: Option, -} - -#[derive(Debug, Serialize, Deserialize, Clone)] -struct PahekoMembership { - id: Id, - users: Vec, - campaign: String, - mode: helloasso::MembershipMode, - inception_datum: DateTime, - external_references: ExternalReferences -} - /// rust how to access inner enum value #[derive(Debug, PartialEq, Clone, Copy)] enum HelloassoCustomFieldType { @@ -247,6 +187,15 @@ fn parse_and_get_birthday_year(raw_date: String) -> Option { Some(d.year().try_into().ok()?) } +fn helloasso_to_paheko_membership(helloasso_membership: &helloasso::MembershipMode) -> paheko::MembershipMode { + match helloasso_membership { + helloasso::MembershipMode::Couple => paheko::MembershipMode::Couple, + helloasso::MembershipMode::Individual => paheko::MembershipMode::Individual, + helloasso::MembershipMode::BenefactorCouple => paheko::MembershipMode::BenefactorCouple, + helloasso::MembershipMode::BenefactorIndividual => paheko::MembershipMode::BenefactorIndividual + } +} + async fn launch_adapter() -> Result<()> { dotenvy::dotenv()?; @@ -276,16 +225,15 @@ async fn launch_adapter() -> Result<()> { // get the list of payments associated // first step: output a list of PahekoUser with PahekoMembership - let pk_memberships: Vec = vec![]; - let mut pk_users: Vec = vec![]; - let mut pk_memberships: Vec = vec![]; + let pk_memberships: Vec = vec![]; + let mut pk_users: Vec = vec![]; + let mut pk_memberships: Vec = vec![]; let mut count: u64 = 0; let mut names: HashSet = HashSet::new(); // read_custom_field(&answer, HelloAssoCustomFieldType::Email).or(Some(answer.payer_user.email.clone())), use email_address::*; - use std::str::FromStr; fn choose_email(answer: &helloasso::FormAnswer) -> Option { read_custom_field(&answer, HelloassoCustomFieldType::Email) .and_then(|x| { @@ -305,7 +253,7 @@ async fn launch_adapter() -> Result<()> { names.insert(custom_field.name.clone()); count += 1; } - let paheko_user = PahekoUser { + let paheko_user = paheko::User { id: generate_id(), first_name: answer.user.first_name.clone(), last_name: answer.user.last_name.clone(), @@ -321,14 +269,14 @@ async fn launch_adapter() -> Result<()> { // FIXME: the reference will be in the data of the paheko activity, and will only // reference the answer id }; - let mut pk_membership = PahekoMembership { + let mut pk_membership = paheko::Membership { id: generate_id(), campaign: "".to_string(), inception_datum: Utc::now(), - mode: answer.mode.clone(), + mode: helloasso_to_paheko_membership(&answer.mode), users: vec![paheko_user.id.clone()], - external_references: ExternalReferences { - helloasso_ref: HelloassoReferences { + external_references: paheko::ExternalReferences { + helloasso_ref: paheko::HelloassoReferences { answer_id: answer.id, order_id: answer.order.id } @@ -368,7 +316,6 @@ async fn launch_adapter() -> Result<()> { dbg!(&pk_users.len()); dbg!(&pk_memberships.len()); - // println!("{:?}", &pk_users.iter().map(|user| format!("{:?}", user.email)).collect::>()); for u in pk_users.iter() { @@ -388,9 +335,6 @@ async fn launch_adapter() -> Result<()> { // id, // for each uses we extracted // we check if there is an existing user by checking for the ha forn answer id - - - Ok(()) } diff --git a/src/paheko.rs b/src/paheko.rs new file mode 100644 index 0000000..55b8bf6 --- /dev/null +++ b/src/paheko.rs @@ -0,0 +1,62 @@ +use anyhow::{Context, Result, anyhow}; +use url::Url; +use serde::{Serialize, Deserialize}; +use fully_pub::fully_pub; +use crate::utils::Id; +use chrono::prelude::{NaiveDate, DateTime, Utc, Datelike}; + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[fully_pub] +struct HelloassoReferences { + answer_id: u64, + order_id: u64 + // payment_id: u64, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[fully_pub] +struct ExternalReferences { + helloasso_ref: HelloassoReferences +} + +/// for now we include the custom fields into the paheko user +/// we don't have time to implement user settings to change the custom fields mapping +/// for now, manual mapping +#[derive(Debug, Serialize, Deserialize, Clone)] +#[fully_pub] +struct User { + id: Id, + first_name: String, + last_name: String, + email: Option, + + phone: Option, + address: String, + city: String, + postal_code: String, + country: String, + skills: Option, + job: Option, + birthday: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[fully_pub] +enum MembershipMode { + Individual, + Couple, + BenefactorIndividual, + BenefactorCouple, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[fully_pub] +struct Membership { + id: Id, + users: Vec, + campaign: String, + mode: MembershipMode, + inception_datum: DateTime, + external_references: ExternalReferences +} + diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..59b2d28 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,21 @@ +use serde::{Serialize, Deserialize}; +use rand::{thread_rng, Rng}; + +/// ID +#[derive(Clone, Deserialize, Serialize, Debug, PartialEq, Eq, Hash)] +pub struct Id(pub u64); +impl Id { + pub fn to_string(&self) -> String { + format!("{:x}", self.0) + } +} +impl Into for Id { + fn into(self) -> String { + format!("{:x}", self.0) + } +} + +pub fn generate_id() -> Id { + Id(thread_rng().gen()) +} +