Remove db call and unwrap by returning added Member

This commit is contained in:
Aadi Desai 2024-02-02 16:55:40 +00:00
parent e8502feb74
commit 2df0ba37d1
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM
3 changed files with 70 additions and 22 deletions

View file

@ -0,0 +1,50 @@
{
"db_name": "PostgreSQL",
"query": "insert into members values ($1, $2, $3, $4, $5) returning *",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "discord_id",
"type_info": "Int8"
},
{
"ordinal": 1,
"name": "shortcode",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "nickname",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "realname",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "fresher",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Int8",
"Varchar",
"Text",
"Text",
"Bool"
]
},
"nullable": [
false,
false,
false,
false,
false
]
},
"hash": "2a25f494fc244e877e4ade7b1d0c422cbea9263d74bae688d5dc9684bc855d4d"
}

View file

@ -131,7 +131,7 @@ pub(crate) async fn insert_member_from_pending(
id: i64, id: i64,
nickname: &str, nickname: &str,
fresher: bool, fresher: bool,
) -> Result<PendingMember, Error> { ) -> Result<Member, Error> {
let p = sqlx::query_as!( let p = sqlx::query_as!(
PendingMember, PendingMember,
"delete from pending where discord_id=$1 returning *", "delete from pending where discord_id=$1 returning *",
@ -139,40 +139,42 @@ pub(crate) async fn insert_member_from_pending(
) )
.fetch_one(pool) .fetch_one(pool)
.await?; .await?;
sqlx::query!( let m = sqlx::query_as!(
"insert into members values ($1, $2, $3, $4, $5)", Member,
"insert into members values ($1, $2, $3, $4, $5) returning *",
id, id,
p.shortcode, p.shortcode,
nickname, nickname,
p.realname, p.realname,
fresher fresher
) )
.execute(pool) .fetch_one(pool)
.await?; .await?;
Ok(p) Ok(m)
} }
/// Add member entry to members table from manual table /// Add member entry to members table from manual table
pub(crate) async fn insert_member_from_manual( pub(crate) async fn insert_member_from_manual(
pool: &sqlx::PgPool, pool: &sqlx::PgPool,
id: i64, id: i64,
) -> Result<ManualMember, Error> { ) -> Result<Member, Error> {
let m = sqlx::query_as!( let mm = sqlx::query_as!(
ManualMember, ManualMember,
"delete from manual where discord_id=$1 returning *", "delete from manual where discord_id=$1 returning *",
id id
) )
.fetch_one(pool) .fetch_one(pool)
.await?; .await?;
sqlx::query!( let m = sqlx::query_as!(
"insert into members values ($1, $2, $3, $4, $5)", Member,
"insert into members values ($1, $2, $3, $4, $5) returning *",
id, id,
m.shortcode, mm.shortcode,
m.nickname, mm.nickname,
m.realname, mm.realname,
m.fresher mm.fresher
) )
.execute(pool) .fetch_one(pool)
.await?; .await?;
Ok(m) Ok(m)
} }

View file

@ -225,18 +225,14 @@ pub(crate) async fn manual_4(
if verify { if verify {
match db::insert_member_from_manual(&data.db, user.id.into()).await { match db::insert_member_from_manual(&data.db, user.id.into()).await {
Ok(mm) => { Ok(mm) => {
let fresher = crate::db::get_member_by_id(&data.db, user.id.into())
.await?
.unwrap()
.fresher;
tracing::info!( tracing::info!(
"{} ({}) added via manual{}", "{} ({}) added via manual{}",
user.name, user.name,
user.id, user.id,
if fresher { " (fresher)" } else { "" } if mm.fresher { " (fresher)" } else { "" }
); );
if fresher {
verify::apply_role(ctx, &mut member, data.member).await?; verify::apply_role(ctx, &mut member, data.member).await?;
if mm.fresher {
verify::apply_role(ctx, &mut member, data.fresher).await?; verify::apply_role(ctx, &mut member, data.fresher).await?;
} }
m.create_response( m.create_response(
@ -249,7 +245,7 @@ pub(crate) async fn manual_4(
.thumbnail(user.face()) .thumbnail(user.face())
.title("Member verified via manual") .title("Member verified via manual")
.description(user.to_string()) .description(user.to_string())
.field("Fresher", fresher.to_string(), true) .field("Fresher", mm.fresher.to_string(), true)
.field("Nickname", mm.nickname, true) .field("Nickname", mm.nickname, true)
.field("Name", mm.realname, true) .field("Name", mm.realname, true)
.timestamp(serenity::Timestamp::now()), .timestamp(serenity::Timestamp::now()),
@ -261,7 +257,7 @@ pub(crate) async fn manual_4(
if member.roles.contains(&data.old_member) { if member.roles.contains(&data.old_member) {
verify::remove_role(ctx, &mut member, data.old_member).await?; verify::remove_role(ctx, &mut member, data.old_member).await?;
} else { } else {
verify::welcome_user(&ctx.http, &data.gn_ch_id, &user, fresher).await?; verify::welcome_user(&ctx.http, &data.gn_ch_id, &user, mm.fresher).await?;
} }
} }
Err(e) => { Err(e) => {