From aa16958f4d0cd79096f405a2135e072e2717e080 Mon Sep 17 00:00:00 2001 From: Travis Semple Date: Fri, 18 Oct 2024 13:44:20 -0700 Subject: [PATCH] Small tweaks for EFT patch approve declined route + Add in get route for refund (#1785) --- pay-api/src/pay_api/dtos/eft_shortname.py | 2 +- pay-api/src/pay_api/models/base_model.py | 14 ++++++++- .../pay_api/resources/v1/eft_short_names.py | 29 +++++++++++++++++-- pay-api/src/pay_api/services/eft_refund.py | 6 ++-- .../tests/unit/api/test_eft_short_names.py | 22 ++++++++++++++ pay-api/tests/utilities/base_test.py | 2 ++ 6 files changed, 67 insertions(+), 8 deletions(-) diff --git a/pay-api/src/pay_api/dtos/eft_shortname.py b/pay-api/src/pay_api/dtos/eft_shortname.py index 7fb439d86..0924e8c4b 100644 --- a/pay-api/src/pay_api/dtos/eft_shortname.py +++ b/pay-api/src/pay_api/dtos/eft_shortname.py @@ -60,8 +60,8 @@ class EFTShortNameSummaryGetRequest(Serializable): class EFTShortNameRefundPatchRequest(Serializable): """EFT Short name refund DTO.""" - comment: str status: str + comment: str = None decline_reason: str = None diff --git a/pay-api/src/pay_api/models/base_model.py b/pay-api/src/pay_api/models/base_model.py index beb34054c..53886db85 100644 --- a/pay-api/src/pay_api/models/base_model.py +++ b/pay-api/src/pay_api/models/base_model.py @@ -14,6 +14,8 @@ """Super class to handle all operations related to base model.""" +from decimal import Decimal + from .db import db @@ -55,7 +57,17 @@ def delete(self): def to_dict(self): """Quick and easy way to convert to a dict.""" # We need a better way to do this in the future. - return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns if getattr(self, c.name) is not None} + result = {} + for column in self.__table__.columns: + value = getattr(self, column.name) + if value is not None: + if isinstance(value, int): + result[column.name] = value + elif isinstance(value, (Decimal, float)): + result[column.name] = float(value) + else: + result[column.name] = str(value) + return result @staticmethod def rollback(): diff --git a/pay-api/src/pay_api/resources/v1/eft_short_names.py b/pay-api/src/pay_api/resources/v1/eft_short_names.py index 70674bbe1..5eca5be9f 100644 --- a/pay-api/src/pay_api/resources/v1/eft_short_names.py +++ b/pay-api/src/pay_api/resources/v1/eft_short_names.py @@ -290,13 +290,36 @@ def post_shortname_refund(): return jsonify(response), status +@bp.route("/shortname-refund/", methods=["GET", "OPTIONS"]) +@cross_origin(origins="*", methods=["GET", "POST", "PATCH"]) +@_jwt.has_one_of_roles([Role.EFT_REFUND.value, Role.EFT_REFUND_APPROVER.value]) +def get_shortname_refund_by_id(eft_refund_id: int): + """Get EFT short name refund.""" + current_app.logger.info("get_shortname_refund_by_id") + return jsonify(response), status + + @bp.route("/shortname-refund/", methods=["PATCH"]) @cross_origin(origins="*") @_jwt.requires_auth @_jwt.has_one_of_roles([Role.EFT_REFUND_APPROVER.value]) def patch_shortname_refund(eft_refund_id: int): - """Patch EFT short name link.""" - current_app.logger.info("patch_eft_shortname_link") + current_app.logger.debug(">patch_eft_shortname_refund") return jsonify(response), status diff --git a/pay-api/src/pay_api/services/eft_refund.py b/pay-api/src/pay_api/services/eft_refund.py index 0ca26c0c2..50f41aa4f 100644 --- a/pay-api/src/pay_api/services/eft_refund.py +++ b/pay-api/src/pay_api/services/eft_refund.py @@ -200,9 +200,9 @@ def update_shortname_refund(refund_id: int, data: EFTShortNameRefundPatchRequest refund = EFTRefundModel.find_by_id(refund_id) if refund.status != EFTShortnameRefundStatus.PENDING_APPROVAL.value: raise BusinessException(Error.REFUND_ALREADY_FINALIZED) - refund.comment = data.comment + refund.comment = data.comment or refund.comment refund.status = data.status - refund.decline_reason = data.decline_reason + refund.decline_reason = data.decline_reason or refund.decline_reason refund.save_or_add(auto_save=False) short_name = EFTShortnamesModel.find_by_id(refund.short_name_id) match data.status: @@ -245,7 +245,7 @@ def update_shortname_refund(refund_id: int, data: EFTShortNameRefundPatchRequest client_body = content.render_body(is_for_client=True) send_email(client_recipients, subject, client_body) case _: - pass + raise NotImplementedError("Invalid status") return refund.to_dict() @staticmethod diff --git a/pay-api/tests/unit/api/test_eft_short_names.py b/pay-api/tests/unit/api/test_eft_short_names.py index 0590360d1..223eefe03 100755 --- a/pay-api/tests/unit/api/test_eft_short_names.py +++ b/pay-api/tests/unit/api/test_eft_short_names.py @@ -1226,3 +1226,25 @@ def test_patch_shortname(session, client, jwt): assert result is not None assert result["casSupplierNumber"] == data["casSupplierNumber"] assert result["email"] == data["email"] + + +def test_get_refund_by_id(session, client, jwt): + """Test get refund by id.""" + short_name = factory_eft_shortname("TEST_SHORTNAME").save() + refund = factory_eft_refund( + short_name_id=short_name.id, + refund_amount=10, + status=EFTShortnameRefundStatus.PENDING_APPROVAL.value, + decline_reason="sucks", + ) + token = jwt.create_jwt(get_claims(roles=[Role.EFT_REFUND_APPROVER.value]), token_header) + headers = {"Authorization": f"Bearer {token}", "content-type": "application/json"} + rv = client.get(f"/api/v1/eft-shortnames/shortname-refund/{refund.id}", headers=headers) + assert rv.status_code == 200 + assert rv.json["comment"] == refund.comment + assert rv.json["status"] == refund.status + assert rv.json["refundAmount"] == refund.refund_amount + assert rv.json["casSupplierNumber"] == refund.cas_supplier_number + assert rv.json["refundEmail"] == refund.refund_email + assert rv.json["shortNameId"] == refund.short_name_id + assert rv.json["declineReason"] == refund.decline_reason diff --git a/pay-api/tests/utilities/base_test.py b/pay-api/tests/utilities/base_test.py index d2fdaf5de..b672b3988 100644 --- a/pay-api/tests/utilities/base_test.py +++ b/pay-api/tests/utilities/base_test.py @@ -931,9 +931,11 @@ def factory_eft_refund( refund_email="test@test.com", comment="test comment", status="PENDING", + decline_reason=None, ): """Return an EFT Refund.""" return EFTRefund( + decline_reason=decline_reason, short_name_id=short_name_id, refund_amount=refund_amount, cas_supplier_number=cas_supplier_number,