Skip to content

Commit 2a37674

Browse files
committed
fix: filter and remove empty / no / na usernames
1 parent 633fb0b commit 2a37674

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ fn main() -> Result<()> {
3737

3838
crobot_updates.push(
3939
qpay_user
40-
.add_username(&mut pg, &table)
40+
.set_username(&mut pg, &table, qpay_user.discord())
4141
.with_context(|| "Importing discord")
4242
.with_context(|| format!("{:#?}", qpay_user))?,
4343
);
4444

4545
imported += 1;
4646
}
47-
postgres::InDb::NeedsDiscord => crobot_updates.push(
47+
postgres::InDb::NeedsDiscord(username) => crobot_updates.push(
4848
qpay_user
49-
.add_username(&mut pg, &table)
49+
.set_username(&mut pg, &table, username)
5050
.with_context(|| "Importing discord")
5151
.with_context(|| format!("{:#?}", qpay_user))?,
5252
),
@@ -58,6 +58,12 @@ fn main() -> Result<()> {
5858
originated += 1;
5959
()
6060
}
61+
postgres::InDb::NeedsDiscordRemoval => {
62+
qpay_user
63+
.set_username(&mut pg, &table, None)
64+
.with_context(|| "Importing discord")
65+
.with_context(|| format!("{:#?}", qpay_user))?;
66+
}
6167
postgres::InDb::Full => (),
6268
}
6369
}

src/postgres.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,60 @@ use eyre::Context as _;
66
use itertools::Itertools;
77
use postgres::Row;
88

9-
pub enum InDb {
9+
pub enum InDb<'a> {
1010
Full,
11+
NeedsDiscordRemoval,
1112
NeedsOrigination,
12-
NeedsDiscord,
13+
NeedsDiscord(Option<&'a str>),
1314
Empty,
1415
}
1516

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

21+
let discord = self.discord();
22+
let discord_exists = discord
23+
.map(|u| {
24+
let simple = u.trim().to_lowercase();
25+
!simple.is_empty() && simple != "no" && simple != "n/a"
26+
})
27+
.unwrap_or(false);
28+
2029
match db.query_one(
2130
&format!("SELECT discord_username, origination FROM {table} WHERE email = $1"),
2231
&[&self.email],
2332
) {
24-
Ok(row) if self.discord().is_some() && row_missing(&row, "discord_username") => {
25-
InDb::NeedsDiscord
33+
Ok(row) if discord_exists && row_missing(&row, "discord_username") => {
34+
InDb::NeedsDiscord(discord)
2635
}
2736
Ok(row) if self.origination().is_some() && row_missing(&row, "origination") => {
2837
InDb::NeedsOrigination
2938
}
39+
Ok(row) if !discord_exists && !row_missing(&row, "discord_username") => {
40+
InDb::NeedsDiscordRemoval
41+
}
3042

3143
Ok(_) => InDb::Full,
3244
Err(_) => InDb::Empty,
3345
}
3446
}
3547

36-
pub fn add_username(
48+
pub fn set_username(
3749
&self,
3850
db: &mut postgres::Client,
3951
table: &str,
52+
username: Option<&str>,
4053
) -> Result<Option<CrobotWebook>> {
41-
let Some(username) = self.discord() else {
42-
return Ok(None);
43-
};
44-
4554
let query = format!("UPDATE {table} SET discord_username = $1 WHERE email = $2",);
4655

4756
let _result = db
4857
.query(&query, &[&username, &self.email])
4958
.with_context(|| "Updating")?;
5059

60+
let Some(username) = username else {
61+
return Ok(None);
62+
};
5163
Ok(Some(CrobotWebook::new(username.to_owned())))
5264
}
5365

0 commit comments

Comments
 (0)