Updated delete_all to delete the accounts and the master password concurrently

This commit is contained in:
2023-05-26 13:57:33 +03:00
parent 084b72b896
commit 1fb004f949
3 changed files with 37 additions and 25 deletions

View File

@@ -72,4 +72,10 @@ impl Entity {
.await?;
Ok(id.is_some())
}
/// Removes a master password of the user from the database
pub async fn remove(user_id: u64, db: &DatabaseConnection) -> crate::Result<()> {
Self::delete_by_id(user_id).exec(db)?;
Ok(())
}
}

View File

@@ -5,6 +5,7 @@ use crate::{
};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::join;
/// Gets the master password, deletes the accounts and the master password from DB.
/// Although it doesn't use the master password, we get it to be sure that it's the user who used that command
@@ -13,12 +14,17 @@ async fn get_master_pass(
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
_master_pass: String,
_: String,
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
Account::delete_all(user_id, &db).await?;
MasterPass::delete_by_id(user_id).exec(&db).await?;
match join!(
Account::delete_all(user_id, &db),
MasterPass::remove(user_id, &db),
) {
(Ok(_), Ok(_)) => (),
(Err(err), _) | (Ok(_), Err(err)) => return Err(err),
};
bot.send_message(msg.chat.id, "Everything was deleted")
.reply_markup(deletion_markup())
.await?;