diff --git a/app/dao/service_callback_api_dao.py b/app/dao/service_callback_api_dao.py index 5e0d497d61..d8bff5eb56 100644 --- a/app/dao/service_callback_api_dao.py +++ b/app/dao/service_callback_api_dao.py @@ -73,6 +73,11 @@ def reset_service_callback_api(service_callback_api, updated_by_id, url=None, be db.session.add(service_callback_api) +def get_service_callback_api_with_service_id(service_id): + # There is ONLY one callback configured per service + return ServiceCallbackApi.query.filter_by(service_id=service_id).all() + + def get_service_callback_api(service_callback_api_id, service_id): return ServiceCallbackApi.query.filter_by(id=service_callback_api_id, service_id=service_id).first() @@ -92,8 +97,8 @@ def delete_service_callback_api(service_callback_api): @transactional @version_class(ServiceCallbackApi) -def suspend_service_callback_api(service_callback_api, updated_by_id): - service_callback_api.is_suspended = True +def suspend_unsuspend_service_callback_api(service_callback_api, updated_by_id, suspend=False): + service_callback_api.is_suspended = suspend service_callback_api.suspended_at = datetime.now(timezone.utc) service_callback_api.updated_by_id = updated_by_id service_callback_api.updated_at = datetime.now(timezone.utc) diff --git a/app/models.py b/app/models.py index 8e832d0997..e0cba92019 100644 --- a/app/models.py +++ b/app/models.py @@ -872,6 +872,7 @@ class ServiceCallbackApi(BaseModel, Versioned): updated_by = db.relationship("User") updated_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey("users.id"), index=True, nullable=False) is_suspended = db.Column(db.Boolean, nullable=True, default=False) + # If is_suspended is False and suspended_at is not None, then the callback was suspended and then unsuspended suspended_at = db.Column(db.DateTime, nullable=True) __table_args__ = (UniqueConstraint("service_id", "callback_type", name="uix_service_callback_type"),) diff --git a/app/service/callback_rest.py b/app/service/callback_rest.py index 15b243ec60..83c8126e8a 100644 --- a/app/service/callback_rest.py +++ b/app/service/callback_rest.py @@ -4,8 +4,10 @@ from app.dao.service_callback_api_dao import ( delete_service_callback_api, get_service_callback_api, + get_service_callback_api_with_service_id, reset_service_callback_api, save_service_callback_api, + suspend_unsuspend_service_callback_api, ) from app.dao.service_inbound_api_dao import ( delete_service_inbound_api, @@ -129,6 +131,21 @@ def remove_service_callback_api(service_id, callback_api_id): return "", 204 +@service_callback_blueprint.route("/delivery-receipt-api/suspend-callback", methods=["POST"]) +def suspend_callback_api(service_id): + data = request.get_json() + callback_api = get_service_callback_api_with_service_id(service_id) + if not callback_api: + error = "Service delivery receipt callback API not found" + raise InvalidRequest(error, status_code=404) + + updated_by_id = data["updated_by_id"] + suspend_unsuspend = data["suspend_unsuspend"] + + suspend_unsuspend_service_callback_api(callback_api[0], updated_by_id, suspend_unsuspend) + return jsonify(data=callback_api[0].serialize()), 200 + + def handle_sql_error(e, table_name): if ( hasattr(e, "orig") diff --git a/tests/app/dao/test_service_callback_api_dao.py b/tests/app/dao/test_service_callback_api_dao.py index 9bb357511e..28d17a48c8 100644 --- a/tests/app/dao/test_service_callback_api_dao.py +++ b/tests/app/dao/test_service_callback_api_dao.py @@ -11,7 +11,7 @@ reset_service_callback_api, resign_service_callbacks, save_service_callback_api, - suspend_service_callback_api, + suspend_unsuspend_service_callback_api, ) from app.models import ServiceCallbackApi from tests.app.db import create_service_callback_api @@ -238,9 +238,10 @@ def test_update_service_callback_api(self, sample_service): assert len(results) == 1 saved_callback_api = results[0] - suspend_service_callback_api( + suspend_unsuspend_service_callback_api( saved_callback_api, updated_by_id=sample_service.users[0].id, + suspend=True, ) updated_results = ServiceCallbackApi.query.all() assert len(updated_results) == 1 diff --git a/tests/app/service/test_callback_rest.py b/tests/app/service/test_callback_rest.py index d3e016e9dc..dbacaa72c1 100644 --- a/tests/app/service/test_callback_rest.py +++ b/tests/app/service/test_callback_rest.py @@ -1,5 +1,7 @@ import uuid +import pytest + from app.models import ServiceCallbackApi, ServiceInboundApi from tests.app.db import create_service_callback_api, create_service_inbound_api @@ -196,3 +198,22 @@ def test_delete_service_callback_api(admin_request, sample_service): assert response is None assert ServiceCallbackApi.query.count() == 0 + + +class TestSuspendCallbackApi: + @pytest.mark.parametrize("suspend_unsuspend", [True, False]) + def test_suspend_callback_api(self, admin_request, sample_service, suspend_unsuspend): + service_callback_api = create_service_callback_api(sample_service) + + data = { + "suspend_unsuspend": suspend_unsuspend, + "updated_by_id": str(sample_service.users[0].id), + } + admin_request.post( + "service_callback.suspend_callback_api", + service_id=sample_service.id, + _data=data, + ) + callback = ServiceCallbackApi.query.get(service_callback_api.id) + assert callback.is_suspended is suspend_unsuspend + assert callback.updated_by_id == sample_service.users[0].id