Skip to content

Commit

Permalink
Small tweaks for EFT patch approve declined route + Add in get route …
Browse files Browse the repository at this point in the history
…for refund (#1785)
  • Loading branch information
seeker25 authored Oct 18, 2024
1 parent 0723ede commit aa16958
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pay-api/src/pay_api/dtos/eft_shortname.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
14 changes: 13 additions & 1 deletion pay-api/src/pay_api/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"""Super class to handle all operations related to base model."""


from decimal import Decimal

from .db import db


Expand Down Expand Up @@ -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():
Expand Down
29 changes: 26 additions & 3 deletions pay-api/src/pay_api/resources/v1/eft_short_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,36 @@ def post_shortname_refund():
return jsonify(response), status


@bp.route("/shortname-refund/<int:eft_refund_id>", 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_eft_shortname_refund_by_id")

try:
refund = EFTRefundService.find_refund_by_id(eft_refund_id)
if refund:
response, status = (
EFTRefundService.find_refund_by_id(eft_refund_id).to_dict(),
HTTPStatus.OK,
)
else:
response, status = {}, HTTPStatus.NOT_FOUND
except BusinessException as exception:
return exception.response()

current_app.logger.debug(">get_shortname_refund_by_id")
return jsonify(response), status


@bp.route("/shortname-refund/<int:eft_refund_id>", 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")
"""Patch EFT short name refund."""
current_app.logger.info("<patch_eft_shortname_refund")
request_json = request.get_json()
short_name_refund = EFTShortNameRefundPatchRequest.from_dict(request_json)
try:
Expand All @@ -310,5 +333,5 @@ def patch_shortname_refund(eft_refund_id: int):
except BusinessException as exception:
return exception.response()

current_app.logger.debug(">patch_eft_shortname_link")
current_app.logger.debug(">patch_eft_shortname_refund")
return jsonify(response), status
6 changes: 3 additions & 3 deletions pay-api/src/pay_api/services/eft_refund.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions pay-api/tests/unit/api/test_eft_short_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions pay-api/tests/utilities/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,9 +931,11 @@ def factory_eft_refund(
refund_email="[email protected]",
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,
Expand Down

0 comments on commit aa16958

Please sign in to comment.