Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update service free allowance when organisation type change #4371

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function only keeps the manual free allowances if they're 0, but not if they're any other value - it sounds like this isn't what we want to happen?



@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