From 1b397bb80df0907b9e4027ab57b9ee0d73a02923 Mon Sep 17 00:00:00 2001 From: Pach Date: Fri, 5 Apr 2019 19:36:05 -0600 Subject: [PATCH] Reverse cambios change STP status lookup to a dict (#56) (#72) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Se envía Liquidación a toda respuesta hacia stp --- migrations/versions/4e043705435c_.py | 28 +++++++++++++++++++++++++++ speid/tables/transactions.py | 2 +- speid/tables/types.py | 22 +++++++++++---------- speid/views.py | 4 +++- test/cassettes/test_create_order.yaml | 2 +- test/test_receive_order.py | 28 +++++++++++++++++++++++++++ 6 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 migrations/versions/4e043705435c_.py diff --git a/migrations/versions/4e043705435c_.py b/migrations/versions/4e043705435c_.py new file mode 100644 index 00000000..df60ff4c --- /dev/null +++ b/migrations/versions/4e043705435c_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: 4e043705435c +Revises: 2773ca239148 +Create Date: 2019-04-06 01:00:59.333771 + +""" +import sqlalchemy as sa +from alembic import op + + +# revision identifiers, used by Alembic. +revision = '4e043705435c' +down_revision = '2773ca239148' +branch_labels = None +depends_on = None + + +def upgrade(): + + op.drop_constraint( + 'transactions_orden_id_key', 'transactions', type_='unique') + + +def downgrade(): + + op.create_unique_constraint( + 'transactions_orden_id_key', 'transactions', ['orden_id']) diff --git a/speid/tables/transactions.py b/speid/tables/transactions.py index 95d70060..38747247 100644 --- a/speid/tables/transactions.py +++ b/speid/tables/transactions.py @@ -7,7 +7,7 @@ transactions = db.Table( 'transactions', cols.id('tr'), cols.created_at(), cols.updated_at(), - Column('orden_id', Integer, unique=True), # STP Ordenes.clave + Column('orden_id', Integer, unique=False), # STP Ordenes.clave Column('fecha_operacion', Date, nullable=False), Column('institucion_ordenante', String, nullable=False), Column('institucion_beneficiaria', String, nullable=False), diff --git a/speid/tables/types.py b/speid/tables/types.py index 6bbc0f91..75e27f31 100644 --- a/speid/tables/types.py +++ b/speid/tables/types.py @@ -8,7 +8,7 @@ class HttpRequestMethod(Enum): class State(Enum): created = 'CREATE' # When a transaction has been received - retry = 'RETRY' # When the transaction has been retry + retry = 'RETRY' # When the transaction has been retry 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 made @@ -22,14 +22,16 @@ class Estado(Enum): @classmethod def get_state_from_stp(cls, stp_state): - status_from_stp = dict( - LIQUIDACION=cls.succeeded, - DEVOLUCION=cls.failed) - return status_from_stp.get(stp_state, cls.error) + if stp_state == 'LIQUIDACION': + return cls.succeeded + if stp_state == 'DEVOLUCION': + return cls.failed + return cls.error @classmethod - def convert_to_stp_state(cls, status): - status_to_stp = dict( - (cls.succeeded, 'LIQUIDACION'), - (cls.failed, 'DEVOLUCION')) - return status_to_stp.get(status, 'DEVOLUCION') + def convert_to_stp_state(cls, state): + if state == cls.succeeded: + return 'LIQUIDACION' + if state == cls.failed: + return 'DEVOLUCION' + return 'DEVOLUCION' diff --git a/speid/views.py b/speid/views.py index ec8ab171..77da4f54 100644 --- a/speid/views.py +++ b/speid/views.py @@ -77,7 +77,9 @@ def create_orden(): r = request.json r['estado'] = Estado.convert_to_stp_state(Estado(response['status'])) except Exception as exc: - r = dict(estado='DEVOLUCION') + r = dict(estado='LIQUIDACION') + transaction.type = State.error + db.session.commit() capture_exception(exc) return make_response(jsonify(r), 201) diff --git a/test/cassettes/test_create_order.yaml b/test/cassettes/test_create_order.yaml index ba9f7d2f..38b8364c 100644 --- a/test/cassettes/test_create_order.yaml +++ b/test/cassettes/test_create_order.yaml @@ -18,7 +18,7 @@ interactions: method: POST uri: https://testapi/set response: - body: {string: '{"status": "success"}'} + body: {string: '{"status": "succeeded"}'} headers: Connection: [keep-alive] Content-Encoding: [application/x-www-form-urlencoded] diff --git a/test/test_receive_order.py b/test/test_receive_order.py index 6fb2c511..1c34dddf 100644 --- a/test/test_receive_order.py +++ b/test/test_receive_order.py @@ -96,3 +96,31 @@ def test_create_order_without_ordenante(self, app): content_type='application/json') assert res.status_code == 201 + + def test_create_fail_orden(self, app): + thread = ConsumerThread() + thread.start() + data = dict( + Clave=123123233, + FechaOperacion=20190129, + InstitucionOrdenante=40102, + InstitucionBeneficiaria=90646, + ClaveRastreo='MANU-00000295251', + Monto=1000, + NombreOrdenante='null', + TipoCuentaOrdenante=0, + CuentaOrdenante='null', + RFCCurpOrdenante='null', + NombreBeneficiario='JESUS ADOLFO ORTEGA TURRUBIATES', + TipoCuentaBeneficiario=40, + CuentaBeneficiario='646180157020812599', + RFCCurpBeneficiario='ND', + ConceptoPago='FONDEO', + ReferenciaNumerica=1232134, + Empresa='TAMIZI' + ) + res = app.post('/ordenes', data=json.dumps(data), + content_type='application/json') + + assert res.status_code == 201 + assert res.json['estado'] == 'LIQUIDACION'