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

View file

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