add rollback to model
This commit is contained in:
parent
9183b1aa78
commit
dae2a2e518
2 changed files with 21 additions and 0 deletions
|
|
@ -10,6 +10,7 @@ class StateChoices(IntegerChoices):
|
||||||
SUCCESS = 5 , _('success')
|
SUCCESS = 5 , _('success')
|
||||||
FAILED = 6 , _('failed')
|
FAILED = 6 , _('failed')
|
||||||
EXPECTED_FAILURE = 7 , _('expected_failure') # no exact data available but guessed to be failed
|
EXPECTED_FAILURE = 7 , _('expected_failure') # no exact data available but guessed to be failed
|
||||||
|
ROLLED_BACK = 8 , _('ROLLED_BACK')
|
||||||
|
|
||||||
|
|
||||||
class TypeChoices(IntegerChoices):
|
class TypeChoices(IntegerChoices):
|
||||||
|
|
|
||||||
|
|
@ -187,6 +187,13 @@ class Transaction(BaseModel):
|
||||||
raise UnprocessableEntity('not enough balance')
|
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):
|
def deposit_to_payee_balance(self):
|
||||||
logger.debug('start deposit_to_payee_balance')
|
logger.debug('start deposit_to_payee_balance')
|
||||||
logger.info(f'payee_account balance {self.payee_account} -> {self.payer_account.balance} + {self.amount}')
|
logger.info(f'payee_account balance {self.payee_account} -> {self.payer_account.balance} + {self.amount}')
|
||||||
|
|
@ -288,6 +295,19 @@ class Transaction(BaseModel):
|
||||||
)
|
)
|
||||||
return self
|
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):
|
def get_role(self, entity):
|
||||||
if self.payer_account.owner_uuid == entity.uuid:
|
if self.payer_account.owner_uuid == entity.uuid:
|
||||||
return 1
|
return 1
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue