Added a transaction for deleting all
This commit is contained in:
		@@ -1,5 +1,7 @@
 | 
			
		||||
use crate::prelude::*;
 | 
			
		||||
use tokio::join;
 | 
			
		||||
use log::error;
 | 
			
		||||
use sea_orm::TransactionTrait;
 | 
			
		||||
use tokio::try_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
 | 
			
		||||
@@ -12,14 +14,23 @@ async fn get_master_pass(
 | 
			
		||||
) -> crate::Result<()> {
 | 
			
		||||
    dialogue.exit().await?;
 | 
			
		||||
    let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
 | 
			
		||||
    match join!(
 | 
			
		||||
        Account::delete_all(user_id, &db),
 | 
			
		||||
        MasterPass::remove(user_id, &db),
 | 
			
		||||
    ) {
 | 
			
		||||
        (Ok(_), Ok(_)) => (),
 | 
			
		||||
        (Err(err), _) | (Ok(_), Err(err)) => return Err(err.into()),
 | 
			
		||||
    let txn = db.begin().await?;
 | 
			
		||||
    let result = try_join!(
 | 
			
		||||
        Account::delete_all(user_id, &txn),
 | 
			
		||||
        MasterPass::remove(user_id, &txn),
 | 
			
		||||
    );
 | 
			
		||||
    let text = match result {
 | 
			
		||||
        Ok(_) => {
 | 
			
		||||
            txn.commit().await?;
 | 
			
		||||
            "Everything was deleted"
 | 
			
		||||
        }
 | 
			
		||||
        Err(err) => {
 | 
			
		||||
            error!("{}", crate::Error::from(err));
 | 
			
		||||
            txn.rollback().await?;
 | 
			
		||||
            "Something went wrong. Try again later"
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
    bot.send_message(msg.chat.id, "Everything was deleted")
 | 
			
		||||
    bot.send_message(msg.chat.id, text)
 | 
			
		||||
        .reply_markup(deletion_markup())
 | 
			
		||||
        .await?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user