From 87d9d62b474ff08aa116ee7d52652ff6f6b9a81c Mon Sep 17 00:00:00 2001 From: rogeliolopez Date: Fri, 3 Nov 2023 19:10:41 -0600 Subject: [PATCH] first approach --- speid/models/transaction.py | 1 + speid/tasks/transactions.py | 54 ++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/speid/models/transaction.py b/speid/models/transaction.py index 3efef571..d101d092 100644 --- a/speid/models/transaction.py +++ b/speid/models/transaction.py @@ -135,6 +135,7 @@ class Transaction(Document, BaseModel): '+stp_id', '+speid_id', '+clave_rastreo', + ['+fecha_operacion', '+tipo'], # The Unique-Sparse index skips over any document that is missing # the indexed field (null values) {'fields': ['+compound_key'], 'unique': True, 'sparse': True}, diff --git a/speid/tasks/transactions.py b/speid/tasks/transactions.py index c9af929c..ed87cfcd 100644 --- a/speid/tasks/transactions.py +++ b/speid/tasks/transactions.py @@ -12,6 +12,7 @@ get_prior_business_day, ) from stpmex.exc import EmptyResultsError, InvalidFutureDateError +from stpmex.resources import Orden from speid.helpers import callback_helper from speid.helpers.transaction_helper import ( @@ -168,21 +169,42 @@ def check_deposits_status(deposit: Dict) -> None: for fecha_operacion in fechas_operacion: try: - recibida = ( - stpmex_client.ordenes_v2.consulta_clave_rastreo_recibida( - clave_rastreo=req.clave_rastreo, - fecha_operacion=fecha_operacion, - ) - ) + apply_stp_deposit(req.clave_rastreo, fecha_operacion) except (InvalidFutureDateError, EmptyResultsError): continue - else: - if ( - recibida.tipoPago in REFUNDS_PAYMENTS_TYPES - or recibida.estado not in STP_VALID_DEPOSITS_STATUSES - ): - return - - stp_request = stp_model_to_dict(recibida) - process_incoming_transaction(stp_request) - return + return + + +def apply_stp_deposit(clave_rastreo, fecha_operacion) -> None: + """Busca una transaccion en el API de STP y la aplica si no existe""" + recibida = stpmex_client.ordenes_v2.consulta_clave_rastreo_recibida( + clave_rastreo=clave_rastreo, + fecha_operacion=fecha_operacion, + ) + if ( + recibida.tipoPago in REFUNDS_PAYMENTS_TYPES + or recibida.estado not in STP_VALID_DEPOSITS_STATUSES + ): + return + stp_request = stp_model_to_dict(recibida) + process_incoming_transaction(stp_request) + + +def get_deposits(fecha_operacion) -> List[Orden]: + # Todo: leer de stp la lista de depositos de hoy + ... + + +@celery.task +def apply_missing_deposits() -> List[str]: + """Consulta los depositos de un día y aplica los no abonados""" + fecha_operacion = dt.date.today() # Todo: Usar la correcta + stp_deposits = get_deposits(fecha_operacion) + transactions = Transaction.objects( + tipo=TipoTransaccion.deposito, fecha_operacion=fecha_operacion + ) + claves = [t.clave_rastreo for t in transactions] + missing = [d for d in stp_deposits if d.claveRastreo not in claves] + for orden in missing: + apply_stp_deposit(orden.claveRastreo, fecha_operacion) + return [m.claveRastreo for m in missing]