diff --git a/speid/exc.py b/speid/exc.py index eb6aa23..740896d 100644 --- a/speid/exc.py +++ b/speid/exc.py @@ -31,7 +31,7 @@ class TransactionNeedManualReviewError(Exception): error: str -class DuplicatedTransaction(Exception): +class DuplicatedTransactionError(Exception): """ Duplicated transactions are not allowed """ diff --git a/speid/tasks/orders.py b/speid/tasks/orders.py index e762b77..a436b1d 100644 --- a/speid/tasks/orders.py +++ b/speid/tasks/orders.py @@ -3,7 +3,7 @@ import clabe import luhnmod10 -from mongoengine import DoesNotExist +from mongoengine import DoesNotExist, NotUniqueError from pydantic import ValidationError from sentry_sdk import capture_exception from stpmex.exc import ( @@ -17,7 +17,7 @@ ) from speid.exc import ( - DuplicatedTransaction, + DuplicatedTransactionError, MalformedOrderException, ResendSuccessOrderException, ScheduleError, @@ -58,7 +58,7 @@ def send_order(self, order_val: dict): MalformedOrderException, ResendSuccessOrderException, TransactionNeedManualReviewError, - DuplicatedTransaction, + DuplicatedTransactionError, ) as exc: capture_exception(exc) except ScheduleError: @@ -97,8 +97,10 @@ def execute(order_val: dict): transaction.events.append(Event(type=EventType.retry)) except DoesNotExist: transaction.events.append(Event(type=EventType.created)) - transaction.save() - + try: + transaction.save() + except NotUniqueError: + raise DuplicatedTransactionError except AssertionError: # Se hace un reenvĂ­o del estado de la transferencia # transaction.set_state(Estado.succeeded) diff --git a/tests/tasks/test_orders.py b/tests/tasks/test_orders.py index 172d443..e2ea680 100644 --- a/tests/tasks/test_orders.py +++ b/tests/tasks/test_orders.py @@ -5,7 +5,7 @@ import pytest from freezegun import freeze_time -from mongoengine import DoesNotExist, NotUniqueError +from mongoengine import DoesNotExist from stpmex.exc import ( AccountDoesNotExist, BankCodeClabeMismatch, @@ -18,6 +18,7 @@ ) from speid.exc import ( + DuplicatedTransactionError, MalformedOrderException, ResendSuccessOrderException, ScheduleError, @@ -95,7 +96,7 @@ def test_not_unique_speid_id_are_not_allowed( clave_rastreo=order['clave_rastreo'] ) - with pytest.raises(NotUniqueError), patch( + with pytest.raises(DuplicatedTransactionError), patch( 'speid.tasks.orders.Transaction.create_order' ) as mock_create_order, patch( # Simulate that in a parallel task execution the order doesn't exist