Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply pending deposits #422

Merged
merged 15 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ newrelic==6.2.0.156
pandas==1.2.4
python-hosts==1.0.1
sentry-sdk==1.14.0
stpmex==3.13.1
stpmex==3.14.0
importlib-metadata==4.13.0
4 changes: 3 additions & 1 deletion speid/commands/spei.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ def reconciliate_deposits(
# STP por el webhook en `POST /ordenes`
stp_request = stp_model_to_dict(recibida)
click.echo(f'Depósito procesado: {recibida.claveRastreo}')
process_incoming_transaction(stp_request)
process_incoming_transaction(
stp_request, event_type=EventType.reconciled
)
else:
no_procesadas.append(recibida.claveRastreo)

Expand Down
8 changes: 6 additions & 2 deletions speid/helpers/transaction_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Dict
from typing import Dict, Optional

from mongoengine import NotUniqueError
from sentry_sdk import capture_exception, capture_message
Expand All @@ -12,7 +12,9 @@
CLABES_BLOCKED = os.getenv('CLABES_BLOCKED', '')


def process_incoming_transaction(incoming_transaction: dict) -> dict:
def process_incoming_transaction(
incoming_transaction: dict, event_type: Optional[EventType] = None
) -> dict:
transaction = Transaction()
try:
external_tx = StpTransaction(**incoming_transaction) # type: ignore
Expand All @@ -27,6 +29,8 @@ def process_incoming_transaction(incoming_transaction: dict) -> dict:
transaction.estado = Estado.succeeded
if not transaction.is_valid_account():
transaction.estado = Estado.rejected
if event_type:
transaction.events.append(Event(type=event_type))
transaction.confirm_callback_transaction()
transaction.save()
r = incoming_transaction
Expand Down
4 changes: 3 additions & 1 deletion speid/models/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class Transaction(Document, BaseModel):
events = ListField(ReferenceField(Event))

meta = {
'index_background': True,
'indexes': [
'+stp_id',
'+speid_id',
Expand All @@ -142,7 +143,8 @@ class Transaction(Document, BaseModel):
'fields': ['+stp_id', '+tipo'],
'partialFilterExpression': {'tipo': TipoTransaccion.retiro},
},
]
{'fields': ['+fecha_operacion', '+tipo']},
],
}

@property
Expand Down
65 changes: 49 additions & 16 deletions speid/tasks/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from mongoengine import DoesNotExist
from stpmex.business_days import (
current_cdmx_time_zone,
get_current_working_day,
get_next_business_day,
get_prior_business_day,
)
Expand Down Expand Up @@ -168,21 +169,53 @@ 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, event_type=EventType.reconciled)


@celery.task
def apply_missing_deposits_task() -> None:
"""Consulta los depositos de un día y aplica los no abonados"""
fecha_operacion = get_current_working_day()
apply_missing_deposits(fecha_operacion)

now = dt.datetime.utcnow()
if 0 <= now.hour < 1:
fecha_operacion = get_prior_business_day(fecha_operacion)
apply_missing_deposits(fecha_operacion)


def apply_missing_deposits(fecha_operacion: dt.date) -> List[str]:
"""Consulta los depositos de un día y aplica los no abonados"""
try:
stp_deposits = stpmex_client.conciliacion.consulta_recibidas(
fecha_operacion
)
except EmptyResultsError:
return []

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]
1 change: 1 addition & 0 deletions speid/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class EventType(Enum):
completed = 'COMPLETE' # The request was processed with no errors
error = 'ERROR' # Something happened when the response was obtained
received = 'RECEIVED' # When we get the response from a transaction
reconciled = 'RECONCILED' # When we use recon process to obtain the trx


class Estado(str, Enum):
Expand Down
Loading
Loading