Skip to content

Commit

Permalink
Add api to suspend a callback (#2275)
Browse files Browse the repository at this point in the history
* Add api to suspend a callback

* fix

* fix

* fix
  • Loading branch information
jzbahrai authored Sep 3, 2024
1 parent 9a68cac commit 84c3ecb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
9 changes: 7 additions & 2 deletions app/dao/service_callback_api_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),)
Expand Down
17 changes: 17 additions & 0 deletions app/service/callback_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions tests/app/dao/test_service_callback_api_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/app/service/test_callback_rest.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

0 comments on commit 84c3ecb

Please sign in to comment.