From bbcad60d0ab43558b8fd76b76f2f832e9b7aa28d Mon Sep 17 00:00:00 2001 From: spatel033 <177532680+spatel033@users.noreply.github.com> Date: Tue, 4 Feb 2025 11:13:54 +0000 Subject: [PATCH] Update service free allowance when organisation type change Update the free allowance when updating organisation type. If the service free allowance has been updated manually then it should keep the manual allowance. --- app/dao/organisation_dao.py | 7 +++++ tests/app/dao/test_organisation_dao.py | 12 +++++++- tests/app/organisation/test_rest.py | 38 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/app/dao/organisation_dao.py b/app/dao/organisation_dao.py index 225299b6b2..5b3c28cf36 100644 --- a/app/dao/organisation_dao.py +++ b/app/dao/organisation_dao.py @@ -4,6 +4,7 @@ from app import db from app.constants import NHS_ORGANISATION_TYPES +from app.dao.annual_billing_dao import set_default_free_allowance_for_service from app.dao.dao_utils import VersionOptions, autocommit, version_class from app.dao.email_branding_dao import dao_get_email_branding_by_id from app.dao.letter_branding_dao import dao_get_letter_branding_by_id @@ -117,6 +118,7 @@ def dao_update_organisation(organisation_id, **kwargs): if "organisation_type" in kwargs: _update_organisation_services(organisation, "organisation_type", only_where_none=False) + _update_organisation_services_free_allowance(organisation) if "crown" in kwargs: _update_organisation_services(organisation, "crown", only_where_none=False) @@ -158,6 +160,11 @@ def _update_organisation_services(organisation, attribute, only_where_none=True) db.session.add(service) +def _update_organisation_services_free_allowance(organisation): + for service in organisation.services: + set_default_free_allowance_for_service(service, year_start=None) + + @autocommit def dao_archive_organisation(organisation_id): organisation = dao_get_organisation_by_id(organisation_id) diff --git a/tests/app/dao/test_organisation_dao.py b/tests/app/dao/test_organisation_dao.py index 7ee8654eee..4fdb7aaa2c 100644 --- a/tests/app/dao/test_organisation_dao.py +++ b/tests/app/dao/test_organisation_dao.py @@ -7,6 +7,7 @@ from sqlalchemy.exc import IntegrityError, SQLAlchemyError from app import db +from app.dao.annual_billing_dao import set_default_free_allowance_for_service from app.dao.organisation_dao import ( dao_add_email_branding_list_to_organisation_pool, dao_add_email_branding_to_organisation_pool, @@ -28,7 +29,7 @@ dao_update_organisation, ) from app.errors import InvalidRequest -from app.models import Organisation, Service +from app.models import AnnualBilling, Organisation, Service from tests.app.db import ( create_annual_billing, create_domain, @@ -165,6 +166,11 @@ def test_update_organisation_updates_the_service_org_type_if_org_type_is_provide ): sample_service.organisation_type = "local" sample_organisation.organisation_type = "local" + set_default_free_allowance_for_service(service=sample_service, year_start=None) + annual_billing = AnnualBilling.query.all() + assert len(annual_billing) == 1 + assert annual_billing[0].service_id == sample_service.id + assert annual_billing[0].free_sms_fragment_limit == 10000 sample_organisation.services.append(sample_service) db.session.commit() @@ -177,6 +183,10 @@ def test_update_organisation_updates_the_service_org_type_if_org_type_is_provide Service.get_history_model().query.filter_by(id=sample_service.id, version=2).one().organisation_type == "central" ) + annual_billing = AnnualBilling.query.all() + assert len(annual_billing) == 1 + assert annual_billing[0].service_id == sample_service.id + assert annual_billing[0].free_sms_fragment_limit == 30000 def test_update_organisation_updates_the_service_branding_if_branding_is_provided( diff --git a/tests/app/organisation/test_rest.py b/tests/app/organisation/test_rest.py index 9c952be339..ddd476fe54 100644 --- a/tests/app/organisation/test_rest.py +++ b/tests/app/organisation/test_rest.py @@ -9,6 +9,7 @@ from sqlalchemy.exc import SQLAlchemyError from app.constants import INVITE_ACCEPTED, INVITE_CANCELLED +from app.dao.annual_billing_dao import set_default_free_allowance_for_service from app.dao.email_branding_dao import dao_get_email_branding_by_id from app.dao.letter_branding_dao import dao_get_letter_branding_by_id from app.dao.organisation_dao import ( @@ -354,6 +355,43 @@ def test_post_update_organisation_updates_fields( assert organisation[0].can_approve_own_go_live_requests == can_approve_own_go_live_requests +def test_post_update_organisation_updates_fields_rollback_if_update_service_free_allows_fails( + admin_request, + notify_db_session, + sample_service, + mocker, +): + org = create_organisation(organisation_type="local") + data = { + "organisation_type": "central", + } + + organisation = Organisation.query.all() + assert len(organisation) == 1 + assert organisation[0].id == org.id + assert organisation[0].organisation_type == "local" + + set_default_free_allowance_for_service(service=sample_service, year_start=None) + annual_billing = AnnualBilling.query.all() + assert len(annual_billing) == 1 + assert annual_billing[0].service_id == sample_service.id + assert annual_billing[0].free_sms_fragment_limit == 5000 + + mocker.patch("app.dao.organisation_dao._update_organisation_services_free_allowance", side_effect=SQLAlchemyError) + with pytest.raises(expected_exception=SQLAlchemyError): + admin_request.post("organisation.update_organisation", _data=data, organisation_id=org.id) + + organisation = Organisation.query.all() + assert len(organisation) == 1 + assert organisation[0].id == org.id + assert organisation[0].organisation_type == "local" + + annual_billing = AnnualBilling.query.all() + assert len(annual_billing) == 1 + assert annual_billing[0].service_id == sample_service.id + assert annual_billing[0].free_sms_fragment_limit == 5000 + + @pytest.mark.parametrize( "domain_list", (