Added a transaction for deleting all
This commit is contained in:
		@@ -79,7 +79,7 @@ impl Entity {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /// Deletes all the user's accounts from DB
 | 
					    /// Deletes all the user's accounts from DB
 | 
				
			||||||
    #[inline]
 | 
					    #[inline]
 | 
				
			||||||
    pub async fn delete_all(user_id: u64, db: &DatabaseConnection) -> crate::Result<()> {
 | 
					    pub async fn delete_all(user_id: u64, db: &impl ConnectionTrait) -> crate::Result<()> {
 | 
				
			||||||
        Self::delete_many()
 | 
					        Self::delete_many()
 | 
				
			||||||
            .filter(Column::UserId.eq(user_id))
 | 
					            .filter(Column::UserId.eq(user_id))
 | 
				
			||||||
            .exec(db)
 | 
					            .exec(db)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -33,7 +33,7 @@ impl Entity {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Removes a master password of the user from the database
 | 
					    /// Removes a master password of the user from the database
 | 
				
			||||||
    pub async fn remove(user_id: u64, db: &DatabaseConnection) -> Result<(), DbErr> {
 | 
					    pub async fn remove(user_id: u64, db: &impl ConnectionTrait) -> Result<(), DbErr> {
 | 
				
			||||||
        Self::delete_by_id(user_id).exec(db).await?;
 | 
					        Self::delete_by_id(user_id).exec(db).await?;
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,7 @@
 | 
				
			|||||||
use crate::prelude::*;
 | 
					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.
 | 
					/// 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
 | 
					/// 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<()> {
 | 
					) -> crate::Result<()> {
 | 
				
			||||||
    dialogue.exit().await?;
 | 
					    dialogue.exit().await?;
 | 
				
			||||||
    let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
 | 
					    let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
 | 
				
			||||||
    match join!(
 | 
					    let txn = db.begin().await?;
 | 
				
			||||||
        Account::delete_all(user_id, &db),
 | 
					    let result = try_join!(
 | 
				
			||||||
        MasterPass::remove(user_id, &db),
 | 
					        Account::delete_all(user_id, &txn),
 | 
				
			||||||
    ) {
 | 
					        MasterPass::remove(user_id, &txn),
 | 
				
			||||||
        (Ok(_), Ok(_)) => (),
 | 
					    );
 | 
				
			||||||
        (Err(err), _) | (Ok(_), Err(err)) => return Err(err.into()),
 | 
					    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())
 | 
					        .reply_markup(deletion_markup())
 | 
				
			||||||
        .await?;
 | 
					        .await?;
 | 
				
			||||||
    Ok(())
 | 
					    Ok(())
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user