|
1 |
| -from typing import List |
| 1 | +import datetime as dt |
| 2 | +from typing import Dict, List |
2 | 3 |
|
3 | 4 | import cep
|
4 | 5 | import pytz
|
5 | 6 | from celery.exceptions import MaxRetriesExceededError
|
6 | 7 | from cep.exc import CepError, MaxRequestError
|
7 | 8 | from mongoengine import DoesNotExist
|
8 |
| -from stpmex.business_days import current_cdmx_time_zone |
| 9 | +from pydantic import ValidationError |
| 10 | +from stpmex.business_days import ( |
| 11 | + current_cdmx_time_zone, |
| 12 | + get_next_business_day, |
| 13 | + get_prior_business_day, |
| 14 | +) |
| 15 | +from stpmex.exc import EmptyResultsError |
9 | 16 |
|
10 | 17 | from speid.helpers import callback_helper
|
| 18 | +from speid.helpers.transaction_helper import ( |
| 19 | + process_incoming_transaction, |
| 20 | + stp_model_to_dict, |
| 21 | +) |
11 | 22 | from speid.models import Account, Event, Transaction
|
| 23 | +from speid.models.transaction import ( |
| 24 | + REFUNDS_PAYMENTS_TYPES, |
| 25 | + STP_VALID_DEPOSITS_STATUSES, |
| 26 | +) |
| 27 | +from speid.processors import stpmex_client |
12 | 28 | from speid.tasks import celery
|
13 | 29 | from speid.types import Estado, EventType, TipoTransaccion
|
| 30 | +from speid.validations.queries import DepositStatusQuery |
14 | 31 |
|
15 | 32 | CURP_LENGTH = 18
|
16 | 33 | RFC_LENGTH = 13
|
@@ -127,25 +144,51 @@ def send_transaction_status(self, transaction_id: str, state: str) -> None:
|
127 | 144 | )
|
128 | 145 |
|
129 | 146 |
|
130 |
| -@celery.task(max_retries=GET_RFC_TASK_MAX_RETRIES) |
131 |
| -def check_transfer_status(cuenta_ordenante: str, clave_rastreo: str) -> None: |
| 147 | +@celery.task |
| 148 | +def check_deposits_status(deposit: Dict) -> None: |
132 | 149 | try:
|
133 |
| - transaction = Transaction.objects.get( |
134 |
| - clave_rastreo=clave_rastreo, cuenta_ordenante=cuenta_ordenante |
135 |
| - ) |
136 |
| - except DoesNotExist: |
| 150 | + req = DepositStatusQuery(**deposit) |
| 151 | + except ValidationError: |
137 | 152 | return
|
138 | 153 |
|
139 |
| - if transaction.tipo is not TipoTransaccion.retiro: |
140 |
| - return |
141 |
| - |
142 |
| - if transaction.estado in [Estado.succeeded, Estado.failed]: |
143 |
| - send_transaction_status.apply_async( |
144 |
| - [transaction.id, transaction.estado] |
| 154 | + try: |
| 155 | + transaction = Transaction.objects.get( |
| 156 | + clave_rastreo=req.clave_rastreo, |
| 157 | + cuenta_beneficiario=req.cuenta_beneficiario, |
| 158 | + tipo=TipoTransaccion.deposito, |
145 | 159 | )
|
146 |
| - elif transaction.estado is Estado.error: |
147 |
| - send_transaction_status.apply_async([transaction.id, Estado.failed]) |
148 |
| - elif transaction.estado is Estado.created: |
149 |
| - ... |
150 |
| - elif transaction.estado is Estado.submitted: |
| 160 | + except DoesNotExist: |
151 | 161 | ...
|
| 162 | + else: |
| 163 | + retry_incoming_transactions.apply_async(([transaction.speid_id],)) |
| 164 | + |
| 165 | + # Si no existe en los registros se obtiene de STP y se intenta con 3 fechas |
| 166 | + # operativas próximas a la fecha que el cliente nos proporcionó |
| 167 | + fechas_operacion = [ |
| 168 | + get_next_business_day(req.fecha_deposito), |
| 169 | + get_prior_business_day(req.fecha_deposito), |
| 170 | + get_next_business_day(req.fecha_deposito + dt.timedelta(days=1)), |
| 171 | + ] |
| 172 | + |
| 173 | + for fecha_operacion in fechas_operacion: |
| 174 | + try: |
| 175 | + recibida = ( |
| 176 | + stpmex_client.ordenes_v2.consulta_clave_rastreo_recibida( |
| 177 | + clave_rastreo=req.clave_rastreo, |
| 178 | + fecha_operacion=fecha_operacion |
| 179 | + if Transaction.current_fecha_operacion() > fecha_operacion |
| 180 | + else None, |
| 181 | + ) |
| 182 | + ) |
| 183 | + except EmptyResultsError: |
| 184 | + ... |
| 185 | + else: |
| 186 | + if ( |
| 187 | + recibida.tipoPago in REFUNDS_PAYMENTS_TYPES |
| 188 | + or recibida.estado not in STP_VALID_DEPOSITS_STATUSES |
| 189 | + ): |
| 190 | + return |
| 191 | + |
| 192 | + stp_request = stp_model_to_dict(recibida) |
| 193 | + process_incoming_transaction(stp_request) |
| 194 | + return |
0 commit comments