Skip to content

Commit

Permalink
Agrega funcionalidad para dar de baja una cuenta en stp (#280)
Browse files Browse the repository at this point in the history
* Agrega funcionalidad para dar de baja una cuenta en stp #279

* cov

* Cambios requeridos
  • Loading branch information
pachCode authored Mar 4, 2021
1 parent 5220540 commit 76f431c
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 4 deletions.
27 changes: 23 additions & 4 deletions speid/tasks/accounts.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
from mongoengine import DoesNotExist
from pydantic import ValidationError
from sentry_sdk import capture_exception
from stpmex.exc import InvalidRfcOrCurp
from stpmex.exc import InvalidRfcOrCurp, StpmexException
from stpmex.resources.cuentas import Cuenta

from speid.models import Account, Event
from speid.tasks import celery
from speid.types import Estado, EventType
from speid.validations import Account as AccountValidation

COUNTDOWN = 600

@celery.task(bind=True, max_retries=60)

@celery.task(bind=True, max_retries=10)
def create_account(self, account_dict: dict) -> None:
try:
execute_create_account(account_dict)
except (InvalidRfcOrCurp, ValidationError) as exc:
capture_exception(exc)
except Exception as exc:
capture_exception(exc)
self.retry(countdown=600, exc=exc) # Reintenta en 10 minutos
self.retry(countdown=COUNTDOWN, exc=exc) # Reintenta en 10 minutos


def execute_create_account(account_dict: dict):
Expand Down Expand Up @@ -51,4 +54,20 @@ def update_account(self, account_dict: dict) -> None:
create_account.apply((account_dict,))
except Exception as exc:
capture_exception(exc)
self.retry(countdown=600, exc=exc)
self.retry(countdown=COUNTDOWN, exc=exc)


@celery.task(bind=True, max_retries=5)
def deactivate_account(self, cuenta: str) -> None:
account = Account.objects.get(cuenta=cuenta)
stp_cuenta = Cuenta( # type: ignore
rfcCurp=account.rfc_curp,
cuenta=account.cuenta,
)
try:
stp_cuenta.baja(stp_cuenta._base_endpoint + '/fisica')
except StpmexException as exc:
self.retry(countdown=COUNTDOWN, exc=exc)
else:
account.estado = Estado.deactivated
account.save()
1 change: 1 addition & 0 deletions speid/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Estado(Enum):
succeeded = 'succeeded' # LIQUIDACION from STP
failed = 'failed' # DEVOLUCION from STP
error = 'error' # Malformed order
deactivated = 'deactivated'

@classmethod
def get_state_from_stp(cls, stp_state: str) -> Enum:
Expand Down
97 changes: 97 additions & 0 deletions tests/tasks/cassettes/test_deactivate_account.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
interactions:
- request:
body: '{"cuenta": "646180157069665325", "rfcCurp": "SACR891125HDFGHI01", "nombre":
"RICARDO", "apellidoPaterno": "SANCHEZ", "paisNacimiento": 187, "fechaNacimiento":
"19940419", "telefono": "5567980796", "firma": "mD4Sh4bcILdhQ0h2LDrMI11nbkKHjSoLplQYw12QWXEQKDHwCfuRYA/O20DgGJ7uq2xTq3jr2cP0wDllw923YekI7yt1xBOwJMqCLv4kgNmREuq6EFZ+FZkNGyrC8RSZFUlv+DrmePazNbL4X7qLknMaj7grX3eCLd8wRe41XuA=",
"empresa": "TAMIZI"}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '402'
Content-Type:
- application/json
User-Agent:
- stpmex-python/3.7.2
method: PUT
uri: https://demo.stpmex.com:7024/speiws/rest/cuentaModule/fisica
response:
body:
string: '{"resultado":{"id":7564945}}'
headers:
Content-Length:
- '67'
Content-Type:
- application/json
Date:
- Mon, 18 Jan 2021 19:06:52 GMT
status:
code: 200
message: OK
- request:
body: '{"cuenta": "646180157069665325", "empresa": "TAMIZI", "rfcCurp": "SACR891125HDFGHI01",
"firma": "mD4Sh4bcILdhQ0h2LDrMI11nbkKHjSoLplQYw12QWXEQKDHwCfuRYA/O20DgGJ7uq2xTq3jr2cP0wDllw923YekI7yt1xBOwJMqCLv4kgNmREuq6EFZ+FZkNGyrC8RSZFUlv+DrmePazNbL4X7qLknMaj7grX3eCLd8wRe41XuA="}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '271'
Content-Type:
- application/json
User-Agent:
- stpmex-python/3.7.2
method: DELETE
uri: https://demo.stpmex.com:7024/speiws/rest/cuentaModule/fisica
response:
body:
string: '{"resultado":{"id":7564945}}'
headers:
Content-Length:
- '67'
Content-Type:
- application/json
Date:
- Mon, 18 Jan 2021 19:18:03 GMT
status:
code: 200
message: OK
- request:
body: '{"cuenta": "646180157069665325", "empresa": "TAMIZI", "rfcCurp": "SACR891125HDFGHI01",
"firma": "mD4Sh4bcILdhQ0h2LDrMI11nbkKHjSoLplQYw12QWXEQKDHwCfuRYA/O20DgGJ7uq2xTq3jr2cP0wDllw923YekI7yt1xBOwJMqCLv4kgNmREuq6EFZ+FZkNGyrC8RSZFUlv+DrmePazNbL4X7qLknMaj7grX3eCLd8wRe41XuA="}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '271'
Content-Type:
- application/json
User-Agent:
- stpmex-python/3.7.2
method: DELETE
uri: https://demo.stpmex.com:7024/speiws/rest/cuentaModule/fisica
response:
body:
string: '{"descripcion":"rfc/curp invalido","id":1}'
headers:
Content-Length:
- '67'
Content-Type:
- application/json
Date:
- Mon, 18 Jan 2021 19:18:03 GMT
status:
code: 200
message: OK
version: 1
27 changes: 27 additions & 0 deletions tests/tasks/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from speid.models import Account
from speid.tasks.accounts import (
create_account,
deactivate_account,
execute_create_account,
update_account,
)
Expand Down Expand Up @@ -272,3 +273,29 @@ def test_update_account_retries_on_unexpected_exception(

mock_capture_exception.assert_called_once()
mock_retry.assert_called_once()


@pytest.mark.vcr
@patch('speid.tasks.accounts.deactivate_account.retry')
def test_deactivate_account(
mock_retry: MagicMock,
):
account_dict = dict(
nombre='Ricardo',
apellido_paterno='Sánchez',
cuenta='646180157069665325',
rfc_curp='SACR891125HDFGHI01',
telefono='5567980796',
fecha_nacimiento='1994-04-19T00:00:00',
pais_nacimiento='MX',
)
# Crea la cuenta
execute_create_account(account_dict)
account = Account.objects.get(cuenta='646180157069665325')
assert account.estado == Estado.succeeded
# Elimina la cuenta
deactivate_account(account.cuenta)
account = Account.objects.get(cuenta=account.cuenta)
assert account.estado == Estado.deactivated
deactivate_account(account.cuenta)
mock_retry.assert_called_once()

0 comments on commit 76f431c

Please sign in to comment.