Skip to content

Commit

Permalink
IDs duplicados (#363)
Browse files Browse the repository at this point in the history
* tipo transaccion

* set tipo_transaccion

* set tipo transaccion in validators

* remove comments

* asserts
  • Loading branch information
felipao-mx authored Aug 16, 2022
1 parent b2be006 commit 617bcd5
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 6 deletions.
7 changes: 6 additions & 1 deletion speid/models/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from speid.exc import MalformedOrderException
from speid.helpers import callback_helper
from speid.processors import stpmex_client
from speid.types import Estado, EventType
from speid.types import Estado, EventType, TipoTransaccion

from .account import Account
from .base import BaseModel
Expand Down Expand Up @@ -57,6 +57,7 @@ class Transaction(Document, BaseModel):
created_at = date_now()
updated_at = DateTimeField()
stp_id = IntField()
tipo: TipoTransaccion = EnumField(TipoTransaccion)
fecha_operacion = DateTimeField()
institucion_ordenante = StringField()
institucion_beneficiaria = StringField()
Expand Down Expand Up @@ -109,6 +110,10 @@ class Transaction(Document, BaseModel):
# The Unique-Sparse index skips over any document that is missing
# the indexed field (null values)
{'fields': ['+compound_key'], 'unique': True, 'sparse': True},
{
'fields': ['+stp_id', '+tipo'],
'partialFilterExpression': {'tipo': TipoTransaccion.retiro},
},
]
}

Expand Down
5 changes: 5 additions & 0 deletions speid/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ def convert_to_stp_state(cls, status: Enum) -> str:
cls.rejected: 'DEVOLUCION',
}
return status_to_stp.get(status, 'DEVOLUCION') # type: ignore


class TipoTransaccion(str, Enum):
retiro = 'retiro'
deposito = 'deposito'
5 changes: 4 additions & 1 deletion speid/validations/speid_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from stpmex.types import TipoCuenta

from speid.models import Transaction
from speid.types import TipoTransaccion


@dataclass
Expand Down Expand Up @@ -59,5 +60,7 @@ def __post_init__(self):
raise ValueError(f'{cuenta_len} is not a valid cuenta length')

def transform(self) -> Transaction:
transaction = Transaction(**self.to_dict())
transaction_dict = self.to_dict()
transaction_dict['tipo'] = TipoTransaccion.retiro
transaction = Transaction(**transaction_dict)
return transaction
2 changes: 2 additions & 0 deletions speid/validations/stp_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from speid.models import Transaction
from speid.models.helpers import base62_uuid, camel_to_snake
from speid.types import TipoTransaccion

regex = re.compile(r'^[A-Z]{4}[0-9]{6}[A-Z]{6}[A-Z|0-9][0-9]$')

Expand Down Expand Up @@ -39,6 +40,7 @@ def transform(self) -> Transaction:
}
trans_dict['stp_id'] = trans_dict.pop('clave', None)
trans_dict['monto'] = round(trans_dict['monto'] * 100)
trans_dict['tipo'] = TipoTransaccion.deposito
transaction = Transaction(**trans_dict)
transaction.speid_id = base62_uuid('SR')()
transaction.fecha_operacion = datetime.strptime(
Expand Down
6 changes: 4 additions & 2 deletions speid/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from speid import app
from speid.helpers.transaction_helper import process_incoming_transaction
from speid.models import Transaction
from speid.types import Estado
from speid.types import Estado, TipoTransaccion
from speid.utils import post

logging.basicConfig(level=logging.INFO, format='SPEID: %(message)s')
Expand All @@ -22,7 +22,9 @@ def health_check():
@app.route('/orden_events', methods=['POST'])
def create_orden_events():
try:
transaction = Transaction.objects.get(stp_id=request.json['id'])
transaction = Transaction.objects.get(
stp_id=request.json['id'], tipo=TipoTransaccion.retiro
)
state = Estado.get_state_from_stp(request.json['Estado'])
transaction.detalle = str(request.json.get('Detalle', ''))

Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from celery import Celery

from speid.models import Transaction
from speid.types import TipoTransaccion

SEND_TRANSACTION_TASK = os.environ['SEND_TRANSACTION_TASK']
SEND_STATUS_TRANSACTION_TASK = os.environ['SEND_STATUS_TRANSACTION_TASK']
Expand Down Expand Up @@ -41,6 +42,7 @@ def outcome_transaction() -> Generator[Transaction, None, None]:
rfc_curp_ordenante='ND',
speid_id='go' + dt.datetime.now().strftime('%m%d%H%M%S'),
version=1,
tipo=TipoTransaccion.retiro,
)
transaction.save()
yield transaction
Expand Down
1 change: 1 addition & 0 deletions tests/helpers/test_callback_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_send_transaction(mock_send_transaction: MagicMock):
concepto_pago="PRUEBA",
referencia_numerica=2423,
empresa="TAMIZI",
tipo='deposito',
)

transaction = Transaction(**params)
Expand Down
4 changes: 3 additions & 1 deletion tests/tasks/test_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from speid.models import Transaction
from speid.tasks.orders import execute, retry_timeout, send_order
from speid.types import Estado, EventType
from speid.types import Estado, EventType, TipoTransaccion


@pytest.mark.parametrize(
Expand Down Expand Up @@ -94,6 +94,7 @@ def test_create_order_debit_card(physical_account):
transaction = Transaction.objects.order_by('-created_at').first()
assert transaction.estado is Estado.submitted
assert transaction.events[-1].type is EventType.completed
assert transaction.tipo is TipoTransaccion.retiro
transaction.delete()


Expand All @@ -116,6 +117,7 @@ def test_worker_with_version_2(physical_account):
transaction = Transaction.objects.order_by('-created_at').first()
assert transaction.estado is Estado.submitted
assert transaction.events[-1].type is EventType.completed
assert transaction.tipo is TipoTransaccion.retiro
transaction.delete()


Expand Down
3 changes: 2 additions & 1 deletion tests/views/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from celery import Celery

from speid.models import Transaction
from speid.types import Estado
from speid.types import Estado, TipoTransaccion


def test_ping(client):
Expand Down Expand Up @@ -120,6 +120,7 @@ def test_create_orden(client, default_income_transaction):
resp = client.post('/ordenes', json=default_income_transaction)
transaction = Transaction.objects.order_by('-created_at').first()
assert transaction.estado is Estado.succeeded
assert transaction.tipo is TipoTransaccion.deposito
assert resp.status_code == 201
assert resp.json['estado'] == 'LIQUIDACION'
transaction.delete()
Expand Down

0 comments on commit 617bcd5

Please sign in to comment.