notify-api-332 rename organisation

This commit is contained in:
Kenneth Kehl
2023-07-10 11:06:29 -07:00
parent 8be3864d7a
commit 4940d5e93b
42 changed files with 1301 additions and 1274 deletions

View File

@@ -78,10 +78,10 @@ def set_default_free_allowance_for_service(service, year_start=None):
year_start = 2020
if year_start > 2022:
year_start = 2022
if service.organisation_type:
free_allowance = default_free_sms_fragment_limits[service.organisation_type][year_start]
if service.organization_type:
free_allowance = default_free_sms_fragment_limits[service.organization_type][year_start]
else:
current_app.logger.info(f"no organisation type for service {service.id}. Using other default of "
current_app.logger.info(f"no organization type for service {service.id}. Using other default of "
f"{default_free_sms_fragment_limits['other'][year_start]}")
free_allowance = default_free_sms_fragment_limits['other'][year_start]

View File

@@ -10,7 +10,7 @@ from app.dao.date_util import (
get_calendar_year_dates,
get_calendar_year_for_datetime,
)
from app.dao.organisation_dao import dao_get_organisation_live_services
from app.dao.organization_dao import dao_get_organization_live_services
from app.models import (
EMAIL_TYPE,
KEY_TYPE_NORMAL,
@@ -22,7 +22,7 @@ from app.models import (
FactBilling,
NotificationAllTimeView,
NotificationHistory,
Organisation,
Organization,
Rate,
Service,
)
@@ -77,8 +77,8 @@ def fetch_sms_billing_for_all_services(start_date, end_date):
sms_cost = chargeable_sms * FactBilling.rate
query = db.session.query(
Organisation.name.label('organisation_name'),
Organisation.id.label('organisation_id'),
Organization.name.label('organization_name'),
Organization.id.label('organization_id'),
Service.name.label("service_name"),
Service.id.label("service_id"),
allowance_left_at_start_date_query.c.free_sms_fragment_limit,
@@ -92,7 +92,7 @@ def fetch_sms_billing_for_all_services(start_date, end_date):
).outerjoin(
allowance_left_at_start_date_query, Service.id == allowance_left_at_start_date_query.c.service_id
).outerjoin(
Service.organisation
Service.organization
).join(
FactBilling, FactBilling.service_id == Service.id,
).filter(
@@ -100,15 +100,15 @@ def fetch_sms_billing_for_all_services(start_date, end_date):
FactBilling.local_date <= end_date,
FactBilling.notification_type == SMS_TYPE,
).group_by(
Organisation.name,
Organisation.id,
Organization.name,
Organization.id,
Service.id,
Service.name,
allowance_left_at_start_date_query.c.free_sms_fragment_limit,
allowance_left_at_start_date_query.c.sms_remainder,
FactBilling.rate,
).order_by(
Organisation.name,
Organization.name,
Service.name
)
@@ -497,7 +497,7 @@ def create_billing_record(data, rate, process_day):
return billing_record
def fetch_email_usage_for_organisation(organisation_id, start_date, end_date):
def fetch_email_usage_for_organization(organization_id, start_date, end_date):
query = db.session.query(
Service.name.label("service_name"),
Service.id.label("service_id"),
@@ -510,7 +510,7 @@ def fetch_email_usage_for_organisation(organisation_id, start_date, end_date):
FactBilling.local_date >= start_date,
FactBilling.local_date <= end_date,
FactBilling.notification_type == EMAIL_TYPE,
Service.organisation_id == organisation_id,
Service.organization_id == organization_id,
Service.restricted.is_(False)
).group_by(
Service.id,
@@ -521,9 +521,9 @@ def fetch_email_usage_for_organisation(organisation_id, start_date, end_date):
return query.all()
def fetch_sms_billing_for_organisation(organisation_id, financial_year):
def fetch_sms_billing_for_organization(organization_id, financial_year):
# ASSUMPTION: AnnualBilling has been populated for year.
ft_billing_subquery = query_organisation_sms_usage_for_year(organisation_id, financial_year).subquery()
ft_billing_subquery = query_organization_sms_usage_for_year(organization_id, financial_year).subquery()
sms_billable_units = func.sum(func.coalesce(ft_billing_subquery.c.chargeable_units, 0))
@@ -551,7 +551,7 @@ def fetch_sms_billing_for_organisation(organisation_id, financial_year):
).outerjoin(
ft_billing_subquery, Service.id == ft_billing_subquery.c.service_id
).filter(
Service.organisation_id == organisation_id,
Service.organization_id == organization_id,
Service.restricted.is_(False)
).group_by(
Service.id,
@@ -564,7 +564,7 @@ def fetch_sms_billing_for_organisation(organisation_id, financial_year):
return query.all()
def query_organisation_sms_usage_for_year(organisation_id, year):
def query_organization_sms_usage_for_year(organization_id, year):
"""
See docstring for query_service_sms_usage_for_year()
"""
@@ -616,15 +616,15 @@ def query_organisation_sms_usage_for_year(organisation_id, year):
FactBilling.notification_type == SMS_TYPE,
)
).filter(
Service.organisation_id == organisation_id,
Service.organization_id == organization_id,
AnnualBilling.financial_year_start == year,
)
def fetch_usage_year_for_organisation(organisation_id, year):
def fetch_usage_year_for_organization(organization_id, year):
year_start, year_end = get_calendar_year_dates(year)
today = datetime.utcnow().date()
services = dao_get_organisation_live_services(organisation_id)
services = dao_get_organization_live_services(organization_id)
# if year end date is less than today, we are calculating for data in the past and have no need for deltas.
if year_end >= today:
@@ -646,8 +646,8 @@ def fetch_usage_year_for_organisation(organisation_id, year):
'emails_sent': 0,
'active': service.active
}
sms_usages = fetch_sms_billing_for_organisation(organisation_id, year)
email_usages = fetch_email_usage_for_organisation(organisation_id, year_start, year_end)
sms_usages = fetch_sms_billing_for_organization(organization_id, year)
email_usages = fetch_email_usage_for_organization(organization_id, year_start, year_end)
for usage in sms_usages:
service_with_usage[str(usage.service_id)] = {
'service_id': usage.service_id,
@@ -669,15 +669,15 @@ def fetch_usage_year_for_organisation(organisation_id, year):
def fetch_billing_details_for_all_services():
billing_details = db.session.query(
Service.id.label('service_id'),
func.coalesce(Service.purchase_order_number, Organisation.purchase_order_number).label('purchase_order_number'),
func.coalesce(Service.billing_contact_names, Organisation.billing_contact_names).label('billing_contact_names'),
func.coalesce(Service.purchase_order_number, Organization.purchase_order_number).label('purchase_order_number'),
func.coalesce(Service.billing_contact_names, Organization.billing_contact_names).label('billing_contact_names'),
func.coalesce(
Service.billing_contact_email_addresses,
Organisation.billing_contact_email_addresses
Organization.billing_contact_email_addresses
).label('billing_contact_email_addresses'),
func.coalesce(Service.billing_reference, Organisation.billing_reference).label('billing_reference'),
func.coalesce(Service.billing_reference, Organization.billing_reference).label('billing_reference'),
).outerjoin(
Service.organisation
Service.organization
).all()
return billing_details
@@ -797,8 +797,8 @@ def fetch_volumes_by_service(start_date, end_date):
results = db.session.query(
Service.name.label("service_name"),
Service.id.label("service_id"),
Service.organisation_id.label("organisation_id"),
Organisation.name.label("organisation_name"),
Service.organization_id.label("organization_id"),
Organization.name.label("organization_name"),
annual_billing.c.free_sms_fragment_limit.label("free_allowance"),
func.coalesce(func.sum(volume_stats.c.sms_totals), 0).label("sms_notifications"),
func.coalesce(func.sum(volume_stats.c.sms_fragments_times_multiplier), 0
@@ -807,7 +807,7 @@ def fetch_volumes_by_service(start_date, end_date):
).select_from(
Service
).outerjoin(
Organisation, Service.organisation_id == Organisation.id
Organization, Service.organization_id == Organization.id
).join(
annual_billing, Service.id == annual_billing.c.service_id
).outerjoin( # include services without volume
@@ -819,11 +819,11 @@ def fetch_volumes_by_service(start_date, end_date):
).group_by(
Service.id,
Service.name,
Service.organisation_id,
Organisation.name,
Service.organization_id,
Organization.name,
annual_billing.c.free_sms_fragment_limit
).order_by(
Organisation.name,
Organization.name,
Service.name,
).all()

View File

@@ -1,7 +1,7 @@
from datetime import datetime, timedelta
from app import db
from app.models import InvitedOrganisationUser
from app.models import InvitedOrganizationUser
def save_invited_org_user(invited_org_user):
@@ -9,21 +9,21 @@ def save_invited_org_user(invited_org_user):
db.session.commit()
def get_invited_org_user(organisation_id, invited_org_user_id):
return InvitedOrganisationUser.query.filter_by(organisation_id=organisation_id, id=invited_org_user_id).one()
def get_invited_org_user(organization_id, invited_org_user_id):
return InvitedOrganizationUser.query.filter_by(organization_id=organization_id, id=invited_org_user_id).one()
def get_invited_org_user_by_id(invited_org_user_id):
return InvitedOrganisationUser.query.filter_by(id=invited_org_user_id).one()
return InvitedOrganizationUser.query.filter_by(id=invited_org_user_id).one()
def get_invited_org_users_for_organisation(organisation_id):
return InvitedOrganisationUser.query.filter_by(organisation_id=organisation_id).all()
def get_invited_org_users_for_organization(organization_id):
return InvitedOrganizationUser.query.filter_by(organization_id=organization_id).all()
def delete_org_invitations_created_more_than_two_days_ago():
deleted = db.session.query(InvitedOrganisationUser).filter(
InvitedOrganisationUser.created_at <= datetime.utcnow() - timedelta(days=2)
deleted = db.session.query(InvitedOrganizationUser).filter(
InvitedOrganizationUser.created_at <= datetime.utcnow() - timedelta(days=2)
).delete()
db.session.commit()
return deleted

View File

@@ -1,137 +0,0 @@
from sqlalchemy.sql.expression import func
from app import db
from app.dao.dao_utils import VersionOptions, autocommit, version_class
from app.models import Domain, Organisation, Service, User
def dao_get_organisations():
return Organisation.query.order_by(
Organisation.active.desc(), Organisation.name.asc()
).all()
def dao_count_organisations_with_live_services():
return db.session.query(Organisation.id).join(Organisation.services).filter(
Service.active.is_(True),
Service.restricted.is_(False),
Service.count_as_live.is_(True),
).distinct().count()
def dao_get_organisation_services(organisation_id):
return Organisation.query.filter_by(
id=organisation_id
).one().services
def dao_get_organisation_live_services(organisation_id):
return Service.query.filter_by(
organisation_id=organisation_id,
restricted=False
).all()
def dao_get_organisation_by_id(organisation_id):
return Organisation.query.filter_by(id=organisation_id).one()
def dao_get_organisation_by_email_address(email_address):
email_address = email_address.lower().replace('.gsi.gov.uk', '.gov.uk')
for domain in Domain.query.order_by(func.char_length(Domain.domain).desc()).all():
if (
email_address.endswith("@{}".format(domain.domain)) or
email_address.endswith(".{}".format(domain.domain))
):
return Organisation.query.filter_by(id=domain.organisation_id).one()
return None
def dao_get_organisation_by_service_id(service_id):
return Organisation.query.join(Organisation.services).filter_by(id=service_id).first()
@autocommit
def dao_create_organisation(organisation):
db.session.add(organisation)
@autocommit
def dao_update_organisation(organisation_id, **kwargs):
domains = kwargs.pop('domains', None)
num_updated = Organisation.query.filter_by(id=organisation_id).update(
kwargs
)
if isinstance(domains, list):
Domain.query.filter_by(organisation_id=organisation_id).delete()
db.session.bulk_save_objects([
Domain(domain=domain.lower(), organisation_id=organisation_id)
for domain in domains
])
organisation = Organisation.query.get(organisation_id)
if 'organisation_type' in kwargs:
_update_organisation_services(organisation, 'organisation_type', only_where_none=False)
if 'email_branding_id' in kwargs:
_update_organisation_services(organisation, 'email_branding')
return num_updated
@version_class(
VersionOptions(Service, must_write_history=False),
)
def _update_organisation_services(organisation, attribute, only_where_none=True):
for service in organisation.services:
if getattr(service, attribute) is None or not only_where_none:
setattr(service, attribute, getattr(organisation, attribute))
db.session.add(service)
@autocommit
@version_class(Service)
def dao_add_service_to_organisation(service, organisation_id):
organisation = Organisation.query.filter_by(
id=organisation_id
).one()
service.organisation_id = organisation_id
service.organisation_type = organisation.organisation_type
db.session.add(service)
def dao_get_users_for_organisation(organisation_id):
return db.session.query(
User
).join(
User.organisations
).filter(
Organisation.id == organisation_id,
User.state == 'active'
).order_by(User.created_at).all()
@autocommit
def dao_add_user_to_organisation(organisation_id, user_id):
organisation = dao_get_organisation_by_id(organisation_id)
user = User.query.filter_by(id=user_id).one()
user.organisations.append(organisation)
db.session.add(organisation)
return user
@autocommit
def dao_remove_user_from_organisation(organisation, user):
organisation.users.remove(user)

137
app/dao/organization_dao.py Normal file
View File

@@ -0,0 +1,137 @@
from sqlalchemy.sql.expression import func
from app import db
from app.dao.dao_utils import VersionOptions, autocommit, version_class
from app.models import Domain, Organization, Service, User
def dao_get_organizations():
return Organization.query.order_by(
Organization.active.desc(), Organization.name.asc()
).all()
def dao_count_organizations_with_live_services():
return db.session.query(Organization.id).join(Organization.services).filter(
Service.active.is_(True),
Service.restricted.is_(False),
Service.count_as_live.is_(True),
).distinct().count()
def dao_get_organization_services(organization_id):
return Organization.query.filter_by(
id=organization_id
).one().services
def dao_get_organization_live_services(organization_id):
return Service.query.filter_by(
organization_id=organization_id,
restricted=False
).all()
def dao_get_organization_by_id(organization_id):
return Organization.query.filter_by(id=organization_id).one()
def dao_get_organization_by_email_address(email_address):
email_address = email_address.lower().replace('.gsi.gov.uk', '.gov.uk')
for domain in Domain.query.order_by(func.char_length(Domain.domain).desc()).all():
if (
email_address.endswith("@{}".format(domain.domain)) or
email_address.endswith(".{}".format(domain.domain))
):
return Organization.query.filter_by(id=domain.organization_id).one()
return None
def dao_get_organization_by_service_id(service_id):
return Organization.query.join(Organization.services).filter_by(id=service_id).first()
@autocommit
def dao_create_organization(organization):
db.session.add(organization)
@autocommit
def dao_update_organization(organization_id, **kwargs):
domains = kwargs.pop('domains', None)
num_updated = Organization.query.filter_by(id=organization_id).update(
kwargs
)
if isinstance(domains, list):
Domain.query.filter_by(organization_id=organization_id).delete()
db.session.bulk_save_objects([
Domain(domain=domain.lower(), organization_id=organization_id)
for domain in domains
])
organization = Organization.query.get(organization_id)
if 'organization_type' in kwargs:
_update_organization_services(organization, 'organization_type', only_where_none=False)
if 'email_branding_id' in kwargs:
_update_organization_services(organization, 'email_branding')
return num_updated
@version_class(
VersionOptions(Service, must_write_history=False),
)
def _update_organization_services(organization, attribute, only_where_none=True):
for service in organization.services:
if getattr(service, attribute) is None or not only_where_none:
setattr(service, attribute, getattr(organization, attribute))
db.session.add(service)
@autocommit
@version_class(Service)
def dao_add_service_to_organization(service, organization_id):
organization = Organization.query.filter_by(
id=organization_id
).one()
service.organization_id = organization_id
service.organization_type = organization.organization_type
db.session.add(service)
def dao_get_users_for_organization(organization_id):
return db.session.query(
User
).join(
User.organizations
).filter(
Organization.id == organization_id,
User.state == 'active'
).order_by(User.created_at).all()
@autocommit
def dao_add_user_to_organization(organization_id, user_id):
organization = dao_get_organization_by_id(organization_id)
user = User.query.filter_by(id=user_id).one()
user.organizations.append(organization)
db.session.add(organization)
return user
@autocommit
def dao_remove_user_from_organization(organization, user):
organization.users.remove(user)

View File

@@ -9,7 +9,7 @@ from sqlalchemy.sql.expression import and_, asc, case, func
from app import db
from app.dao.dao_utils import VersionOptions, autocommit, version_class
from app.dao.date_util import get_current_calendar_year
from app.dao.organisation_dao import dao_get_organisation_by_email_address
from app.dao.organization_dao import dao_get_organization_by_email_address
from app.dao.service_sms_sender_dao import insert_service_sms_sender
from app.dao.service_user_dao import dao_get_service_user
from app.dao.template_folder_dao import dao_get_valid_template_folders_by_id
@@ -27,7 +27,7 @@ from app.models import (
Job,
Notification,
NotificationHistory,
Organisation,
Organization,
Permission,
Service,
ServiceEmailReplyTo,
@@ -96,8 +96,8 @@ def dao_fetch_live_services_data():
data = db.session.query(
Service.id.label('service_id'),
Service.name.label("service_name"),
Organisation.name.label("organisation_name"),
Organisation.organisation_type.label('organisation_type'),
Organization.name.label("organization_name"),
Organization.organization_type.label('organization_type'),
Service.consent_to_research.label('consent_to_research'),
User.name.label('contact_name'),
User.email_address.label('contact_email'),
@@ -121,7 +121,7 @@ def dao_fetch_live_services_data():
AnnualBilling.financial_year_start == most_recent_annual_billing.c.year
)
).outerjoin(
Service.organisation
Service.organization
).outerjoin(
this_year_ft_billing, Service.id == this_year_ft_billing.c.service_id
).outerjoin(
@@ -132,8 +132,8 @@ def dao_fetch_live_services_data():
Service.restricted.is_(False),
).group_by(
Service.id,
Organisation.name,
Organisation.organisation_type,
Organization.name,
Organization.organization_type,
Service.name,
Service.consent_to_research,
Service.count_as_live,
@@ -278,7 +278,7 @@ def dao_create_service(
if service_permissions is None:
service_permissions = DEFAULT_SERVICE_PERMISSIONS
organisation = dao_get_organisation_by_email_address(user.email_address)
organization = dao_get_organization_by_email_address(user.email_address)
from app.dao.permissions_dao import permission_dao
service.users.append(user)
@@ -294,12 +294,12 @@ def dao_create_service(
# do we just add the default - or will we get a value from FE?
insert_service_sms_sender(service, current_app.config['FROM_NUMBER'])
if organisation:
service.organisation_id = organisation.id
service.organisation_type = organisation.organisation_type
if organization:
service.organization_id = organization.id
service.organization_type = organization.organization_type
if organisation.email_branding:
service.email_branding = organisation.email_branding
if organization.email_branding:
service.email_branding = organization.email_branding
service.count_as_live = not user.platform_admin
@@ -376,7 +376,7 @@ def delete_service_and_all_associated_db_objects(service):
db.session.commit()
users = [x for x in service.users]
for user in users:
user.organisations = []
user.organizations = []
service.users.remove(user)
_delete_commit(Service.get_history_model().query.filter_by(id=service.id))
db.session.delete(service)
@@ -553,20 +553,20 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
return query.all()
def get_live_services_with_organisation():
def get_live_services_with_organization():
query = db.session.query(
Service.id.label("service_id"),
Service.name.label("service_name"),
Organisation.id.label("organisation_id"),
Organisation.name.label("organisation_name")
Organization.id.label("organization_id"),
Organization.name.label("organization_name")
).outerjoin(
Service.organisation
Service.organization
).filter(
Service.count_as_live.is_(True),
Service.active.is_(True),
Service.restricted.is_(False)
).order_by(
Organisation.name,
Organization.name,
Service.name
)

View File

@@ -136,12 +136,12 @@ def get_user_and_accounts(user_id):
return User.query.filter(
User.id == user_id
).options(
# eagerly load the user's services and organisations, and also the service's org and vice versa
# eagerly load the user's services and organizations, and also the service's org and vice versa
# (so we can see if the user knows about it)
joinedload('services'),
joinedload('organisations'),
joinedload('organisations.services'),
joinedload('services.organisation'),
joinedload('organizations'),
joinedload('organizations.services'),
joinedload('services.organization'),
).one()
@@ -157,7 +157,7 @@ def dao_archive_user(user):
for service_user in service_users:
db.session.delete(service_user)
user.organisations = []
user.organizations = []
user.auth_type = EMAIL_AUTH_TYPE
user.email_address = get_archived_db_column_value(user.email_address)