Skip to content

Commit

Permalink
feat: origination migration
Browse files Browse the repository at this point in the history
  • Loading branch information
trickypr committed Feb 16, 2025
1 parent baff1a4 commit 2cc90ac
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ fn main() -> Result<()> {
let mut crobot_updates = Vec::new();

let mut imported = 0;
let mut originated = 0;

for qpay_user in members {
match qpay_user.in_membership_db(&mut pg, &table) {
postgres::InDb::Empty => {
qpay_user
.create_membership(&mut pg, &table)
.with_context(|| "Importing member")
.with_context(|| format!("{:#?}", qpay_user))?;

crobot_updates.push(
qpay_user
.add_username(&mut pg, &table)
.with_context(|| "Importing discord")
.with_context(|| format!("{:#?}", qpay_user))?,
);

Expand All @@ -43,17 +47,26 @@ fn main() -> Result<()> {
postgres::InDb::NeedsDiscord => crobot_updates.push(
qpay_user
.add_username(&mut pg, &table)
.with_context(|| "Importing discord")
.with_context(|| format!("{:#?}", qpay_user))?,
),
_ => (),
postgres::InDb::NeedsOrigination => {
qpay_user
.add_origination(&mut pg, &table)
.with_context(|| "Importing origination")
.with_context(|| format!("{:#?}", qpay_user))?;
originated += 1;
()
}
postgres::InDb::Full => (),
}
}

let discord_imported = crobot::send_webhook(crobot_updates)?;

println!(
"Imported {}, {} discord usernames",
imported, discord_imported
"Imported {}, {} discord usernames, {} originated",
imported, discord_imported, originated
);

Ok(())
Expand Down
34 changes: 27 additions & 7 deletions src/postgres.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
use std::env::var;

use crate::crobot::CrobotWebook;
use crate::qpay::QPayMember;
use color_eyre::eyre::OptionExt as _;
use color_eyre::Result;
use eyre::Context as _;
use itertools::Itertools;
use postgres::Row;

pub enum InDb {
Full,
NeedsOrigination,
NeedsDiscord,
Empty,
}

impl QPayMember {
pub fn in_membership_db(&self, db: &mut postgres::Client, table: &str) -> InDb {
let row_missing = |row: &Row, query| row.get::<_, Option<&str>>(query).is_none();

match db.query_one(
&format!("SELECT discord_username FROM {table} WHERE email = $1"),
&format!("SELECT discord_username, origination FROM {table} WHERE email = $1"),
&[&self.email],
) {
Ok(row) => match row.get::<_, Option<&str>>("discord_username") {
Some(_) => InDb::Full,
None => InDb::NeedsDiscord,
},
Ok(row) if row_missing(&row, "discord_username") => InDb::NeedsDiscord,
Ok(row) if self.origination().is_some() && row_missing(&row, "origination") => {
InDb::NeedsOrigination
}

Ok(_) => InDb::Full,
Err(_) => InDb::Empty,
}
}
Expand Down Expand Up @@ -108,4 +112,20 @@ impl QPayMember {

Ok(())
}

pub fn add_origination(&self, db: &mut postgres::Client, table: &str) -> Result<()> {
let origination = self.origination();

let Some(origination) = origination else {
return Ok(());
};

let query = format!("UPDATE {table} SET origination = $1 WHERE email = $2",);

let _result = db
.query(&query, &[&origination, &self.email])
.with_context(|| "Updating")?;

Ok(())
}
}
9 changes: 7 additions & 2 deletions src/qpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@ pub fn qpay_request() -> Result<QPayResponse> {
.with_context(|| res)
}

#[cfg(test)]
mod test {}
impl QPayMember {
pub fn origination(&self) -> Option<&'_ str> {
self.responses
.get("Are you a domestic or international student?")
.map(|s| s.as_str())
}
}

0 comments on commit 2cc90ac

Please sign in to comment.