From dae2a2e518a0a13228b00083837311fff0786301 Mon Sep 17 00:00:00 2001 From: Sayyid Hamid Mahdavi Date: Sat, 27 Dec 2025 14:35:26 +0330 Subject: [PATCH] add rollback to model --- apps/wallet/constans.py | 1 + apps/wallet/models.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/apps/wallet/constans.py b/apps/wallet/constans.py index 3531ce6..014b3e6 100644 --- a/apps/wallet/constans.py +++ b/apps/wallet/constans.py @@ -10,6 +10,7 @@ class StateChoices(IntegerChoices): SUCCESS = 5 , _('success') FAILED = 6 , _('failed') EXPECTED_FAILURE = 7 , _('expected_failure') # no exact data available but guessed to be failed + ROLLED_BACK = 8 , _('ROLLED_BACK') class TypeChoices(IntegerChoices): diff --git a/apps/wallet/models.py b/apps/wallet/models.py index f18d0f9..fadc523 100755 --- a/apps/wallet/models.py +++ b/apps/wallet/models.py @@ -187,6 +187,13 @@ class Transaction(BaseModel): raise UnprocessableEntity('not enough balance') + def rollback_withdraw_to_payer_balance(self): + logger.debug('start rollback deposit_to_payee_balance') + logger.info(f'payer_account balance {self.payer_account} -> {self.amount}') + self.payer_account.balance = F('balance') + self.amount + self.payer_account.save(update_fields=['balance']) + + def deposit_to_payee_balance(self): logger.debug('start deposit_to_payee_balance') logger.info(f'payee_account balance {self.payee_account} -> {self.payer_account.balance} + {self.amount}') @@ -288,6 +295,19 @@ class Transaction(BaseModel): ) return self + def rollback(self): + if self.state not in [StateChoices.PENDING, StateChoices.DELAYED, StateChoices.INCOMPLETE]: + raise APIException('this transaction is not pending') + + with transaction.atomic(): + self.rollback_withdraw_to_payer_balance() + self.change_state( + self.state, + StateChoices.SUCCESS, + error_message=_('TRANSACTION_STATE_NOT_SUITABLE_FOR_ROLLBACK') + ) + return self + def get_role(self, entity): if self.payer_account.owner_uuid == entity.uuid: return 1