refactor: split paheko source code

This commit is contained in:
Matthieu Bessat 2023-12-21 17:31:21 +01:00
parent a604d63622
commit af8b93ab91
3 changed files with 102 additions and 75 deletions

View file

@ -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<String> 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<String>,
phone: Option<String>,
address: String,
city: String,
postal_code: String,
country: String,
skills: Option<String>,
job: Option<String>,
birthday: Option<u32>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
struct PahekoMembership {
id: Id,
users: Vec<Id>,
campaign: String,
mode: helloasso::MembershipMode,
inception_datum: DateTime<Utc>,
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<u32> {
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<PahekoMembership> = vec![];
let mut pk_users: Vec<PahekoUser> = vec![];
let mut pk_memberships: Vec<PahekoMembership> = vec![];
let pk_memberships: Vec<paheko::Membership> = vec![];
let mut pk_users: Vec<paheko::User> = vec![];
let mut pk_memberships: Vec<paheko::Membership> = vec![];
let mut count: u64 = 0;
let mut names: HashSet<String> = 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<String> {
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::<Vec<String>>());
for u in pk_users.iter() {
@ -389,9 +336,6 @@ async fn launch_adapter() -> Result<()> {
// for each uses we extracted
// we check if there is an existing user by checking for the ha forn answer id
Ok(())
}

62
src/paheko.rs Normal file
View file

@ -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<String>,
phone: Option<String>,
address: String,
city: String,
postal_code: String,
country: String,
skills: Option<String>,
job: Option<String>,
birthday: Option<u32>,
}
#[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<Id>,
campaign: String,
mode: MembershipMode,
inception_datum: DateTime<Utc>,
external_references: ExternalReferences
}

21
src/utils.rs Normal file
View file

@ -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<String> for Id {
fn into(self) -> String {
format!("{:x}", self.0)
}
}
pub fn generate_id() -> Id {
Id(thread_rng().gen())
}