Skip to content

Commit

Permalink
Update service free allowance when organisation type change
Browse files Browse the repository at this point in the history
Update the free allowance when updating organisation type. If the service free allowance has been updated manually then it should keep the manual allowance.
  • Loading branch information
spatel033 committed Feb 4, 2025
1 parent cbc24b6 commit bbcad60
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/dao/organisation_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion tests/app/dao/test_organisation_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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(
Expand Down
38 changes: 38 additions & 0 deletions tests/app/organisation/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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",
(
Expand Down

0 comments on commit bbcad60

Please sign in to comment.