create entry when creating a new service

This commit is contained in:
venusbb
2017-10-25 11:35:13 +01:00
parent 15e3b4171b
commit 8ad98f2806
11 changed files with 107 additions and 67 deletions
+4 -5
View File
@@ -126,11 +126,10 @@ def create_or_update_free_sms_fragment_limit(service_id):
if result: if result:
result.free_sms_fragment_limit = free_sms_fragment_limit result.free_sms_fragment_limit = free_sms_fragment_limit
dao_create_or_update_annual_billing_for_year(result)
else: else:
annual_billing = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start, result = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start,
free_sms_fragment_limit=free_sms_fragment_limit) free_sms_fragment_limit=free_sms_fragment_limit)
dao_create_or_update_annual_billing_for_year(annual_billing)
dao_create_or_update_annual_billing_for_year(result)
return jsonify(data=form), 201 return jsonify(data=form), 201
+14 -2
View File
@@ -5,6 +5,7 @@ from app.dao.dao_utils import (
) )
from app.models import AnnualBilling from app.models import AnnualBilling
from datetime import datetime from datetime import datetime
from app.service.utils import get_current_financial_year_start_year
def dao_get_annual_billing(service_id): def dao_get_annual_billing(service_id):
@@ -14,8 +15,6 @@ def dao_get_annual_billing(service_id):
def dao_create_or_update_annual_billing_for_year(annual_billing): def dao_create_or_update_annual_billing_for_year(annual_billing):
if annual_billing.id is None:
annual_billing.id = create_uuid()
db.session.add(annual_billing) db.session.add(annual_billing)
db.session.commit() db.session.commit()
@@ -33,3 +32,16 @@ def dao_get_all_free_sms_fragment_limit(service_id):
return AnnualBilling.query.filter_by( return AnnualBilling.query.filter_by(
service_id=service_id, service_id=service_id,
).all() ).all()
def insert_annual_billing(service):
"""
This method is called from create_service which is wrapped in a transaction.
"""
annual_billing = AnnualBilling(
free_sms_fragment_limit=service.free_sms_fragment_limit,
financial_year_start=get_current_financial_year_start_year(),
service=service,
)
db.session.add(annual_billing)
+7
View File
@@ -37,10 +37,12 @@ from app.models import (
EMAIL_TYPE, EMAIL_TYPE,
INTERNATIONAL_SMS_TYPE, INTERNATIONAL_SMS_TYPE,
ServiceSmsSender, ServiceSmsSender,
AnnualBilling
) )
from app.service.statistics import format_monthly_template_notification_stats from app.service.statistics import format_monthly_template_notification_stats
from app.statsd_decorators import statsd from app.statsd_decorators import statsd
from app.utils import get_london_month_from_utc_column, get_london_midnight_in_utc from app.utils import get_london_month_from_utc_column, get_london_midnight_in_utc
from app.dao.annual_billing_dao import insert_annual_billing
DEFAULT_SERVICE_PERMISSIONS = [ DEFAULT_SERVICE_PERMISSIONS = [
SMS_TYPE, SMS_TYPE,
@@ -164,6 +166,9 @@ def dao_create_service(service, user, service_id=None, service_permissions=None)
if service_permissions is None: if service_permissions is None:
service_permissions = DEFAULT_SERVICE_PERMISSIONS service_permissions = DEFAULT_SERVICE_PERMISSIONS
if service.free_sms_fragment_limit is None:
service.free_sms_fragment_limit = current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT']
from app.dao.permissions_dao import permission_dao from app.dao.permissions_dao import permission_dao
service.users.append(user) service.users.append(user)
permission_dao.add_default_service_permissions_for_user(user, service) permission_dao.add_default_service_permissions_for_user(user, service)
@@ -176,6 +181,7 @@ def dao_create_service(service, user, service_id=None, service_permissions=None)
service.permissions.append(service_permission) service.permissions.append(service_permission)
insert_service_sms_sender(service, service.sms_sender) insert_service_sms_sender(service, service.sms_sender)
insert_annual_billing(service)
db.session.add(service) db.session.add(service)
@@ -238,6 +244,7 @@ def delete_service_and_all_associated_db_objects(service):
_delete_commit(ServicePermission.query.filter_by(service_id=service.id)) _delete_commit(ServicePermission.query.filter_by(service_id=service.id))
_delete_commit(ApiKey.query.filter_by(service=service)) _delete_commit(ApiKey.query.filter_by(service=service))
_delete_commit(ApiKey.get_history_model().query.filter_by(service_id=service.id)) _delete_commit(ApiKey.get_history_model().query.filter_by(service_id=service.id))
_delete_commit(AnnualBilling.query.filter_by(service_id=service.id))
verify_codes = VerifyCode.query.join(User).filter(User.id.in_([x.id for x in service.users])) verify_codes = VerifyCode.query.join(User).filter(User.id.in_([x.id for x in service.users]))
list(map(db.session.delete, verify_codes)) list(map(db.session.delete, verify_codes))
+18 -17
View File
@@ -179,23 +179,6 @@ class ServicePermissionTypes(db.Model):
name = db.Column(db.String(255), primary_key=True) name = db.Column(db.String(255), primary_key=True)
class AnnualBilling(db.Model):
__tablename__ = "annual_billing"
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=False)
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), unique=False, index=True, nullable=False)
financial_year_start = db.Column(db.Integer, nullable=False, default=True, unique=False)
free_sms_fragment_limit = db.Column(db.Integer, nullable=False, index=False, unique=False)
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
UniqueConstraint('financial_year_start', 'service_id', name='ix_annual_billing_service_id')
def serialize(self):
return {
'free_sms_fragment_limit': self.free_sms_fragment_limit,
'financial_year_start': self.financial_year_start,
}
class Service(db.Model, Versioned): class Service(db.Model, Versioned):
__tablename__ = 'services' __tablename__ = 'services'
@@ -286,6 +269,24 @@ class Service(db.Model, Versioned):
return default_letter_contact[0].contact_block if default_letter_contact else None return default_letter_contact[0].contact_block if default_letter_contact else None
class AnnualBilling(db.Model):
__tablename__ = "annual_billing"
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=False)
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), unique=False, index=True, nullable=False)
financial_year_start = db.Column(db.Integer, nullable=False, default=True, unique=False)
free_sms_fragment_limit = db.Column(db.Integer, nullable=False, index=False, unique=False)
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
UniqueConstraint('financial_year_start', 'service_id', name='ix_annual_billing_service_id')
service = db.relationship(Service, backref=db.backref("annual_billing", uselist=True))
def serialize(self):
return {
'free_sms_fragment_limit': self.free_sms_fragment_limit,
'financial_year_start': self.financial_year_start,
}
class InboundNumber(db.Model): class InboundNumber(db.Model):
__tablename__ = "inbound_numbers" __tablename__ = "inbound_numbers"
+2 -2
View File
@@ -170,8 +170,8 @@ def create_service():
raise InvalidRequest(errors, status_code=400) raise InvalidRequest(errors, status_code=400)
# TODO: to be removed when front-end is updated # TODO: to be removed when front-end is updated
if 'free_sms_fragment_limit' not in data: # if 'free_sms_fragment_limit' not in data:
data['free_sms_fragment_limit'] = current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] # data['free_sms_fragment_limit'] = current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT']
# validate json with marshmallow # validate json with marshmallow
service_schema.load(request.get_json()) service_schema.load(request.get_json())
+11
View File
@@ -6,6 +6,8 @@ from app.models import (
KEY_TYPE_TEST, KEY_TYPE_TEAM, KEY_TYPE_NORMAL) KEY_TYPE_TEST, KEY_TYPE_TEAM, KEY_TYPE_NORMAL)
from notifications_utils.recipients import allowed_to_send_to from notifications_utils.recipients import allowed_to_send_to
from app.dao.notifications_dao import get_financial_year
from datetime import datetime
def get_recipients_from_request(request_json, key, type): def get_recipients_from_request(request_json, key, type):
@@ -51,3 +53,12 @@ def service_allowed_to_send_to(recipient, service, key_type):
whitelist_members whitelist_members
) )
) )
def get_current_financial_year_start_year():
now = datetime.now()
financial_year_start = now.year
start_date, end_date = get_financial_year(now.year)
if now < start_date:
financial_year_start = financial_year_start - 1
return financial_year_start
+21 -19
View File
@@ -259,15 +259,19 @@ def test_transform_billing_calculates_with_different_rate_multipliers(sample_ser
def test_get_free_sms_fragment_limit(client, sample_service): def test_get_free_sms_fragment_limit(client, sample_service):
years = [2016, 2017, 2018] years = [2016, 2017, 2018]
sms_allowance = [1000, 2000, 3000] sms_allowance = [1000, 2000, 3000]
for i in range(0, len(years)): for i in range(0, len(years)):
y = years[i] y = years[i]
sms_l = sms_allowance[i] sms_l = sms_allowance[i]
data = AnnualBilling( annual_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, years[i])
free_sms_fragment_limit=sms_l, if annual_billing:
financial_year_start=y, annual_billing.free_sms_fragment_limit = sms_allowance[i]
service_id=sample_service.id, else:
) annual_billing = AnnualBilling(service_id=sample_service.id,
dao_create_or_update_annual_billing_for_year(data) financial_year_start=years[i],
free_sms_fragment_limit=sms_allowance[i])
dao_create_or_update_annual_billing_for_year(annual_billing)
response = client.get('service/{}/billing/free-sms-fragment-limit?financial_year_start=2017' response = client.get('service/{}/billing/free-sms-fragment-limit?financial_year_start=2017'
.format(sample_service.id), .format(sample_service.id),
@@ -348,13 +352,12 @@ def test_update_free_sms_fragment_limit(client, sample_service):
def test_get_free_sms_fragment_limit_year_return_correct_data(client, sample_service): def test_get_free_sms_fragment_limit_year_return_correct_data(client, sample_service):
years = [2015, 2016, 2017] years = [2015, 2016, 2017]
limits = [1000, 2000, 3000] limits = [1000, 2000, 3000]
for i in range(0, len(years)): for i in range(0, len(years)):
data = AnnualBilling( annual_billing = {'financial_year_start': years[i], 'free_sms_fragment_limit': limits[i]}
free_sms_fragment_limit=limits[i], response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
financial_year_start=years[i], data=json.dumps(annual_billing),
service_id=sample_service.id, headers=[('Content-Type', 'application/json'), create_authorization_header()])
)
dao_create_or_update_annual_billing_for_year(data)
for i in range(0, len(years)): for i in range(0, len(years)):
response_get = client.get( response_get = client.get(
@@ -365,16 +368,15 @@ def test_get_free_sms_fragment_limit_year_return_correct_data(client, sample_ser
assert json_resp['data']['free_sms_fragment_limit'] == limits[i] assert json_resp['data']['free_sms_fragment_limit'] == limits[i]
def test_get_free_sms_fragment_limit_for_all_year(client, sample_service): def test_get_free_sms_fragment_limit_for_all_years(client, sample_service):
years = [2015, 2016, 2017] years = [2015, 2016, 2017]
limits = [1000, 2000, 3000] limits = [1000, 2000, 3000]
for i in range(0, len(years)): for i in range(0, len(years)):
data = AnnualBilling( annual_billing = {'financial_year_start': years[i], 'free_sms_fragment_limit': limits[i]}
free_sms_fragment_limit=limits[i], response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
financial_year_start=years[i], data=json.dumps(annual_billing),
service_id=sample_service.id, headers=[('Content-Type', 'application/json'), create_authorization_header()])
)
dao_create_or_update_annual_billing_for_year(data)
response_get = client.get( response_get = client.get(
# Not specify a particular year to return all data for that service # Not specify a particular year to return all data for that service
+6 -20
View File
@@ -1,8 +1,4 @@
# from datetime import datetime, timedelta from app.service.utils import get_current_financial_year_start_year
# import uuid
# import functools
# import pytest
from app.models import AnnualBilling from app.models import AnnualBilling
from app.dao.annual_billing_dao import ( from app.dao.annual_billing_dao import (
dao_create_or_update_annual_billing_for_year, dao_create_or_update_annual_billing_for_year,
@@ -10,23 +6,13 @@ from app.dao.annual_billing_dao import (
) )
def test_dao_create_get_free_sms_fragment_limit(notify_db_session, sample_service): def test_sample_service_has_free_sms_fragment_limit(notify_db_session, sample_service):
years = [2015, 2016, 2017]
free_limit_data = [1000, 2000, 3000]
for i in range(0, len(years)):
data = AnnualBilling(
free_sms_fragment_limit=free_limit_data[i],
financial_year_start=years[i],
service_id=sample_service.id,
)
dao_create_or_update_annual_billing_for_year(data)
for i in range(0, len(years)): free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, get_current_financial_year_start_year())
free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, years[i])
assert free_limit.free_sms_fragment_limit == free_limit_data[i] assert free_limit.free_sms_fragment_limit == 250000
assert free_limit.financial_year_start == years[i] assert free_limit.financial_year_start == get_current_financial_year_start_year()
assert free_limit.service_id == sample_service.id assert free_limit.service_id == sample_service.id
def test_dao_update_free_sms_fragment_limit(notify_db_session, sample_service): def test_dao_update_free_sms_fragment_limit(notify_db_session, sample_service):
+3 -2
View File
@@ -104,7 +104,7 @@ def test_cannot_create_two_services_with_same_name(sample_user):
email_from="email_from1", email_from="email_from1",
message_limit=1000, message_limit=1000,
restricted=False, restricted=False,
created_by=sample_user) created_by=sample_user,)
service2 = Service(name="service_name", service2 = Service(name="service_name",
email_from="email_from2", email_from="email_from2",
@@ -171,7 +171,8 @@ def test_should_remove_user_from_service(sample_user):
email_from="email_from", email_from="email_from",
message_limit=1000, message_limit=1000,
restricted=False, restricted=False,
created_by=sample_user) created_by=sample_user,
free_sms_fragment_limit=9999)
dao_create_service(service, sample_user) dao_create_service(service, sample_user)
new_user = User( new_user = User(
name='Test User', name='Test User',
+1
View File
@@ -68,6 +68,7 @@ def create_service(
service = Service( service = Service(
name=service_name, name=service_name,
message_limit=1000, message_limit=1000,
free_sms_fragment_limit=7777,
restricted=restricted, restricted=restricted,
email_from=email_from if email_from else service_name.lower().replace(' ', '.'), email_from=email_from if email_from else service_name.lower().replace(' ', '.'),
created_by=user or create_user(email='{}@digital.cabinet-office.gov.uk'.format(uuid.uuid4())), created_by=user or create_user(email='{}@digital.cabinet-office.gov.uk'.format(uuid.uuid4())),
+20
View File
@@ -37,6 +37,7 @@ from tests.app.db import (
create_service_sms_sender create_service_sms_sender
) )
from tests.app.db import create_user from tests.app.db import create_user
from app.service.utils import get_current_financial_year_start_year
def test_get_service_list(client, service_factory): def test_get_service_list(client, service_factory):
@@ -300,6 +301,7 @@ def test_create_service(client, sample_user):
assert not json_resp['data']['research_mode'] assert not json_resp['data']['research_mode']
assert json_resp['data']['dvla_organisation'] == '001' assert json_resp['data']['dvla_organisation'] == '001'
assert json_resp['data']['sms_sender'] == current_app.config['FROM_NUMBER'] assert json_resp['data']['sms_sender'] == current_app.config['FROM_NUMBER']
# TODO: Remove this after the new data is used
assert json_resp['data']['free_sms_fragment_limit'] == current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] assert json_resp['data']['free_sms_fragment_limit'] == current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT']
service_db = Service.query.get(json_resp['data']['id']) service_db = Service.query.get(json_resp['data']['id'])
@@ -365,6 +367,15 @@ def test_create_service_free_sms_fragment_limit_is_optional(client, sample_user)
headers=headers) headers=headers)
json_resp = json.loads(resp.get_data(as_text=True)) json_resp = json.loads(resp.get_data(as_text=True))
assert resp.status_code == 201 assert resp.status_code == 201
# Test data from the new annual billing table
service_id = json_resp['data']['id']
annual_billing = client.get('service/{}/billing/free-sms-fragment-limit?financial_year_start={}'
.format(service_id, get_current_financial_year_start_year()),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(annual_billing.get_data(as_text=True))
assert json_resp['data']['free_sms_fragment_limit'] == 9999
# TODO: Remove this after the new data is used
assert json_resp['data']['free_sms_fragment_limit'] == 9999 assert json_resp['data']['free_sms_fragment_limit'] == 9999
data2 = { data2 = {
@@ -385,6 +396,14 @@ def test_create_service_free_sms_fragment_limit_is_optional(client, sample_user)
headers=headers) headers=headers)
json_resp = json.loads(resp.get_data(as_text=True)) json_resp = json.loads(resp.get_data(as_text=True))
assert resp.status_code == 201 assert resp.status_code == 201
# Test data from the new annual billing table
service_id = json_resp['data']['id']
annual_billing = client.get('service/{}/billing/free-sms-fragment-limit?financial_year_start={}'
.format(service_id, get_current_financial_year_start_year()),
headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(annual_billing.get_data(as_text=True))
assert json_resp['data']['free_sms_fragment_limit'] == current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT']
# TODO: Remove this after the new data is used
assert json_resp['data']['free_sms_fragment_limit'] == current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] assert json_resp['data']['free_sms_fragment_limit'] == current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT']
@@ -623,6 +642,7 @@ def test_update_service_flags_will_remove_service_permissions(client, notify_db,
assert set([p.permission for p in permissions]) == set([SMS_TYPE, EMAIL_TYPE]) assert set([p.permission for p in permissions]) == set([SMS_TYPE, EMAIL_TYPE])
# TODO: Remove after new table is created and verified
def test_update_service_free_sms_fragment_limit(client, notify_db, sample_service): def test_update_service_free_sms_fragment_limit(client, notify_db, sample_service):
org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League') org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League')
notify_db.session.add(org) notify_db.session.add(org)