From 12f305bf86a582f8dd18fc663d2a8eab7471e9ff Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Mon, 12 Aug 2019 15:59:27 +0100 Subject: [PATCH] - Migration to add Service.organisation_id - Add oranisation_id to Service data model. - Update methods to create service and associate service to organisation to set the organisation_id on the Service. - Create the missing test, if the service user email matches a domain for an organisation then associate the service to the organisation and inherit crown and organisation_type from the organisation. --- app/dao/organisation_dao.py | 1 + app/dao/services_dao.py | 19 ++++++++--------- app/models.py | 8 +++++-- tests/app/dao/test_organisation_dao.py | 1 + tests/app/dao/test_services_dao.py | 29 ++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/app/dao/organisation_dao.py b/app/dao/organisation_dao.py index 13a60b6f2..d1354a4d9 100644 --- a/app/dao/organisation_dao.py +++ b/app/dao/organisation_dao.py @@ -100,6 +100,7 @@ def dao_add_service_to_organisation(service, organisation_id): ).one() organisation.services.append(service) + service.organisation_id = organisation_id service.organisation_type = organisation.organisation_type service.crown = organisation.crown diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index f901fce0a..1e1a9b9df 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -284,13 +284,6 @@ def dao_create_service( service.id = service_id or uuid.uuid4() # must be set now so version history model can use same id service.active = True service.research_mode = False - if organisation: - service.crown = organisation.crown - elif service.organisation_type in CROWN_ORGANISATION_TYPES: - service.crown = True - elif service.organisation_type in NON_CROWN_ORGANISATION_TYPES: - service.crown = False - service.count_as_live = not user.platform_admin for permission in service_permissions: service_permission = ServicePermission(service_id=service.id, permission=permission) @@ -300,9 +293,9 @@ def dao_create_service( insert_service_sms_sender(service, current_app.config['FROM_NUMBER']) if organisation: - service.organisation = organisation - + service.organisation_id = organisation.id + service.organisation_type = organisation.organisation_type if organisation.email_branding: service.email_branding = organisation.email_branding @@ -310,9 +303,15 @@ def dao_create_service( service.letter_branding = organisation.letter_branding elif service.organisation_type in ['nhs_central', 'nhs_local'] or email_address_is_nhs(user.email_address): - service.email_branding = dao_get_email_branding_by_name('NHS') service.letter_branding = dao_get_letter_branding_by_name('NHS') + if organisation: + service.crown = organisation.crown + elif service.organisation_type in CROWN_ORGANISATION_TYPES: + service.crown = True + elif service.organisation_type in NON_CROWN_ORGANISATION_TYPES: + service.crown = False + service.count_as_live = not user.platform_admin db.session.add(service) diff --git a/app/models.py b/app/models.py index d7c2c8a9f..f41b4e50b 100644 --- a/app/models.py +++ b/app/models.py @@ -352,7 +352,7 @@ class Organisation(db.Model): services = db.relationship( 'Service', - secondary='services', + secondary='organisation_to_service', uselist=True) agreement_signed = db.Column(db.Boolean, nullable=True) @@ -480,7 +480,11 @@ class Service(db.Model, Versioned): go_live_at = db.Column(db.DateTime, nullable=True) organisation_id = db.Column(UUID(as_uuid=True), db.ForeignKey('organisation.id'), index=True, nullable=True) - organisation = db.relationship('Organisation', foreign_keys=[organisation_id]) + organisation = db.relationship( + 'Organisation', + secondary=organisation_to_service, + uselist=False, + single_parent=True) email_branding = db.relationship( 'EmailBranding', diff --git a/tests/app/dao/test_organisation_dao.py b/tests/app/dao/test_organisation_dao.py index f5862aaab..39fbde4b5 100644 --- a/tests/app/dao/test_organisation_dao.py +++ b/tests/app/dao/test_organisation_dao.py @@ -175,6 +175,7 @@ def test_add_service_to_organisation(sample_service, sample_organisation): id=sample_service.id, version=2 ).one().organisation_type == sample_organisation.organisation_type + assert sample_service.organisation_id == sample_organisation.id def test_add_service_to_multiple_organisation_raises_error(sample_service, sample_organisation): diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 61c1fc7da..eebdc6590 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -107,6 +107,35 @@ def test_create_service(notify_db_session): assert service_db.organisation_type == 'central' assert service_db.crown is None assert not service.letter_branding + assert not service.organisation_id + + +def test_create_service_with_organisation(notify_db_session): + user = create_user(email='local.authority@local-authority.gov.uk') + organisation = create_organisation( + name='Some local authority', organisation_type='local', domains=['local-authority.gov.uk']) + assert Service.query.count() == 0 + service = Service(name="service_name", + email_from="email_from", + message_limit=1000, + restricted=False, + organisation_type='central', + created_by=user) + dao_create_service(service, user) + assert Service.query.count() == 1 + service_db = Service.query.one() + assert service_db.name == "service_name" + assert service_db.id == service.id + assert service_db.email_from == 'email_from' + assert service_db.research_mode is False + assert service_db.prefix_sms is True + assert service.active is True + assert user in service_db.users + assert service_db.organisation_type == 'local' + assert service_db.crown is None + assert not service.letter_branding + assert service.organisation_id == organisation.id + assert service.organisation == organisation @pytest.mark.parametrize('email_address, organisation_type', (