@@ -6,48 +6,60 @@ use eyre::Context as _;
66use itertools:: Itertools ;
77use 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
1617impl 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