Skip to content

Commit 2cc90ac

Browse files
committed
feat: origination migration
1 parent baff1a4 commit 2cc90ac

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

src/main.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,20 @@ fn main() -> Result<()> {
2525
let mut crobot_updates = Vec::new();
2626

2727
let mut imported = 0;
28+
let mut originated = 0;
29+
2830
for qpay_user in members {
2931
match qpay_user.in_membership_db(&mut pg, &table) {
3032
postgres::InDb::Empty => {
3133
qpay_user
3234
.create_membership(&mut pg, &table)
35+
.with_context(|| "Importing member")
3336
.with_context(|| format!("{:#?}", qpay_user))?;
3437

3538
crobot_updates.push(
3639
qpay_user
3740
.add_username(&mut pg, &table)
41+
.with_context(|| "Importing discord")
3842
.with_context(|| format!("{:#?}", qpay_user))?,
3943
);
4044

@@ -43,17 +47,26 @@ fn main() -> Result<()> {
4347
postgres::InDb::NeedsDiscord => crobot_updates.push(
4448
qpay_user
4549
.add_username(&mut pg, &table)
50+
.with_context(|| "Importing discord")
4651
.with_context(|| format!("{:#?}", qpay_user))?,
4752
),
48-
_ => (),
53+
postgres::InDb::NeedsOrigination => {
54+
qpay_user
55+
.add_origination(&mut pg, &table)
56+
.with_context(|| "Importing origination")
57+
.with_context(|| format!("{:#?}", qpay_user))?;
58+
originated += 1;
59+
()
60+
}
61+
postgres::InDb::Full => (),
4962
}
5063
}
5164

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

5467
println!(
55-
"Imported {}, {} discord usernames",
56-
imported, discord_imported
68+
"Imported {}, {} discord usernames, {} originated",
69+
imported, discord_imported, originated
5770
);
5871

5972
Ok(())

src/postgres.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
use std::env::var;
2-
31
use crate::crobot::CrobotWebook;
42
use crate::qpay::QPayMember;
53
use color_eyre::eyre::OptionExt as _;
64
use color_eyre::Result;
75
use eyre::Context as _;
86
use itertools::Itertools;
7+
use postgres::Row;
98

109
pub enum InDb {
1110
Full,
11+
NeedsOrigination,
1212
NeedsDiscord,
1313
Empty,
1414
}
1515

1616
impl QPayMember {
1717
pub fn in_membership_db(&self, db: &mut postgres::Client, table: &str) -> InDb {
18+
let row_missing = |row: &Row, query| row.get::<_, Option<&str>>(query).is_none();
19+
1820
match db.query_one(
19-
&format!("SELECT discord_username FROM {table} WHERE email = $1"),
21+
&format!("SELECT discord_username, origination FROM {table} WHERE email = $1"),
2022
&[&self.email],
2123
) {
22-
Ok(row) => match row.get::<_, Option<&str>>("discord_username") {
23-
Some(_) => InDb::Full,
24-
None => InDb::NeedsDiscord,
25-
},
24+
Ok(row) if row_missing(&row, "discord_username") => InDb::NeedsDiscord,
25+
Ok(row) if self.origination().is_some() && row_missing(&row, "origination") => {
26+
InDb::NeedsOrigination
27+
}
28+
29+
Ok(_) => InDb::Full,
2630
Err(_) => InDb::Empty,
2731
}
2832
}
@@ -108,4 +112,20 @@ impl QPayMember {
108112

109113
Ok(())
110114
}
115+
116+
pub fn add_origination(&self, db: &mut postgres::Client, table: &str) -> Result<()> {
117+
let origination = self.origination();
118+
119+
let Some(origination) = origination else {
120+
return Ok(());
121+
};
122+
123+
let query = format!("UPDATE {table} SET origination = $1 WHERE email = $2",);
124+
125+
let _result = db
126+
.query(&query, &[&origination, &self.email])
127+
.with_context(|| "Updating")?;
128+
129+
Ok(())
130+
}
111131
}

src/qpay.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,10 @@ pub fn qpay_request() -> Result<QPayResponse> {
5454
.with_context(|| res)
5555
}
5656

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

0 commit comments

Comments
 (0)