mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-16 20:48:37 -04:00
Merge pull request #851 from alphagov/feat-add-who-updated-providers
Store who updated providers
This commit is contained in:
@@ -81,6 +81,7 @@ class Config(object):
|
||||
MAX_VERIFY_CODE_COUNT = 10
|
||||
|
||||
NOTIFY_SERVICE_ID = 'd6aa2c68-a2d9-4437-ab19-3ae8eb202553'
|
||||
NOTIFY_USER_ID = '6af522d0-2915-4e52-83a3-3690455a5fe6'
|
||||
INVITATION_EMAIL_TEMPLATE_ID = '4f46df42-f795-4cc4-83bb-65ca312f49cc'
|
||||
SMS_CODE_TEMPLATE_ID = '36fb0730-6259-4da1-8a80-c8de22ad4246'
|
||||
EMAIL_VERIFY_CODE_TEMPLATE_ID = 'ece42649-22a8-4d06-b87f-d52d5d3f0a27'
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import asc
|
||||
from sqlalchemy import asc, desc
|
||||
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.provider_details.switch_providers import (
|
||||
provider_is_already_primary_or_inactive,
|
||||
update_provider_priorities
|
||||
provider_is_inactive,
|
||||
provider_is_primary,
|
||||
switch_providers
|
||||
)
|
||||
from app.models import ProviderDetails, ProviderDetailsHistory
|
||||
from app import db
|
||||
@@ -41,6 +42,14 @@ def get_current_provider(notification_type):
|
||||
).first()
|
||||
|
||||
|
||||
def dao_get_provider_versions(provider_id):
|
||||
return ProviderDetailsHistory.query.filter_by(
|
||||
id=provider_id
|
||||
).order_by(
|
||||
desc(ProviderDetailsHistory.version)
|
||||
).all()
|
||||
|
||||
|
||||
@transactional
|
||||
def dao_toggle_sms_provider(identifier):
|
||||
alternate_provider = get_alternative_sms_provider(identifier)
|
||||
@@ -49,18 +58,24 @@ def dao_toggle_sms_provider(identifier):
|
||||
|
||||
@transactional
|
||||
def dao_switch_sms_provider_to_provider_with_identifier(identifier):
|
||||
current_provider = get_current_provider('sms')
|
||||
new_provider = get_provider_details_by_identifier(identifier)
|
||||
if provider_is_inactive(new_provider):
|
||||
return
|
||||
|
||||
if current_provider.priority == new_provider.priority:
|
||||
# Since both priorities are equal, set the current provider
|
||||
# to the one that we want to switch from
|
||||
current_provider = get_alternative_sms_provider(identifier)
|
||||
# Check first to see if there is another provider with the same priority
|
||||
# as this needs to be updated differently
|
||||
conflicting_provider = dao_get_sms_provider_with_equal_priority(new_provider.identifier, new_provider.priority)
|
||||
providers_to_update = []
|
||||
|
||||
if not provider_is_already_primary_or_inactive(current_provider, new_provider, identifier):
|
||||
update_provider_priorities(current_provider, new_provider)
|
||||
dao_update_provider_details(current_provider)
|
||||
dao_update_provider_details(new_provider)
|
||||
if conflicting_provider:
|
||||
providers_to_update = switch_providers(conflicting_provider, new_provider)
|
||||
else:
|
||||
current_provider = get_current_provider('sms')
|
||||
if not provider_is_primary(current_provider, new_provider, identifier):
|
||||
providers_to_update = switch_providers(current_provider, new_provider)
|
||||
|
||||
for provider in providers_to_update:
|
||||
dao_update_provider_details(provider)
|
||||
|
||||
|
||||
def get_provider_details_by_notification_type(notification_type):
|
||||
@@ -76,3 +91,16 @@ def dao_update_provider_details(provider_details):
|
||||
history = ProviderDetailsHistory.from_original(provider_details)
|
||||
db.session.add(provider_details)
|
||||
db.session.add(history)
|
||||
|
||||
|
||||
def dao_get_sms_provider_with_equal_priority(identifier, priority):
|
||||
provider = db.session.query(ProviderDetails).filter(
|
||||
ProviderDetails.identifier != identifier,
|
||||
ProviderDetails.notification_type == 'sms',
|
||||
ProviderDetails.priority == priority,
|
||||
ProviderDetails.active
|
||||
).order_by(
|
||||
asc(ProviderDetails.priority)
|
||||
).first()
|
||||
|
||||
return provider
|
||||
|
||||
@@ -391,6 +391,8 @@ class ProviderDetails(db.Model):
|
||||
active = db.Column(db.Boolean, default=False, nullable=False)
|
||||
version = db.Column(db.Integer, default=1, nullable=False)
|
||||
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
|
||||
created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), index=True, nullable=True)
|
||||
created_by = db.relationship('User')
|
||||
|
||||
|
||||
class ProviderDetailsHistory(db.Model, HistoryModel):
|
||||
@@ -404,6 +406,8 @@ class ProviderDetailsHistory(db.Model, HistoryModel):
|
||||
active = db.Column(db.Boolean, nullable=False)
|
||||
version = db.Column(db.Integer, primary_key=True, nullable=False)
|
||||
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
|
||||
created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), index=True, nullable=True)
|
||||
created_by = db.relationship('User')
|
||||
|
||||
|
||||
JOB_STATUS_PENDING = 'pending'
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
from app.schemas import provider_details_schema
|
||||
|
||||
from app.schemas import provider_details_schema, provider_details_history_schema
|
||||
from app.dao.provider_details_dao import (
|
||||
get_provider_details,
|
||||
get_provider_details_by_id,
|
||||
dao_update_provider_details
|
||||
dao_update_provider_details,
|
||||
dao_get_provider_versions
|
||||
)
|
||||
|
||||
from app.dao.users_dao import get_user_by_id
|
||||
from app.errors import (
|
||||
register_errors,
|
||||
InvalidRequest
|
||||
@@ -29,19 +29,37 @@ def get_provider_by_id(provider_details_id):
|
||||
return jsonify(provider_details=data)
|
||||
|
||||
|
||||
@provider_details.route('/<uuid:provider_details_id>/versions', methods=['GET'])
|
||||
def get_provider_versions(provider_details_id):
|
||||
versions = dao_get_provider_versions(provider_details_id)
|
||||
data = provider_details_history_schema.dump(
|
||||
versions,
|
||||
many=True
|
||||
).data
|
||||
return jsonify(data=data)
|
||||
|
||||
|
||||
@provider_details.route('/<uuid:provider_details_id>', methods=['POST'])
|
||||
def update_provider_details(provider_details_id):
|
||||
fetched_provider_details = get_provider_details_by_id(provider_details_id)
|
||||
valid_keys = {'priority', 'created_by', 'active'}
|
||||
req_json = request.get_json()
|
||||
|
||||
current_data = dict(provider_details_schema.dump(fetched_provider_details).data.items())
|
||||
current_data.update(request.get_json())
|
||||
update_dict = provider_details_schema.load(current_data).data
|
||||
|
||||
invalid_keys = {'identifier', 'version', 'updated_at'} & set(key for key in request.get_json().keys())
|
||||
invalid_keys = req_json.keys() - valid_keys
|
||||
if invalid_keys:
|
||||
message = "Not permitted to be updated"
|
||||
errors = {key: [message] for key in invalid_keys}
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
dao_update_provider_details(update_dict)
|
||||
return jsonify(provider_details=provider_details_schema.dump(fetched_provider_details).data), 200
|
||||
provider = get_provider_details_by_id(provider_details_id)
|
||||
|
||||
# Handle created_by differently due to how history entry is created
|
||||
if 'created_by' in req_json:
|
||||
user = get_user_by_id(req_json['created_by'])
|
||||
provider.created_by_id = user.id
|
||||
req_json.pop('created_by')
|
||||
|
||||
for key in req_json:
|
||||
setattr(provider, key, req_json[key])
|
||||
dao_update_provider_details(provider)
|
||||
|
||||
return jsonify(provider_details=provider_details_schema.dump(provider).data), 200
|
||||
|
||||
@@ -1,32 +1,39 @@
|
||||
from flask import current_app
|
||||
|
||||
from app.dao.users_dao import get_user_by_id
|
||||
|
||||
def provider_is_already_primary_or_inactive(current_provider, new_provider, identifier):
|
||||
if current_provider.identifier == identifier:
|
||||
current_app.logger.warning('Provider {} is already activated'.format(current_provider.display_name))
|
||||
|
||||
def provider_is_inactive(new_provider):
|
||||
if not new_provider.active:
|
||||
current_app.logger.warning('Cancelling switch to {} as they are inactive'.format(
|
||||
new_provider.identifier,
|
||||
))
|
||||
return True
|
||||
|
||||
elif not new_provider.active:
|
||||
current_app.logger.warning('Cancelling switch from {} to {} as {} is inactive'.format(
|
||||
current_provider.identifier,
|
||||
new_provider.identifier,
|
||||
new_provider.identifier
|
||||
))
|
||||
|
||||
def provider_is_primary(current_provider, new_provider, identifier):
|
||||
if current_provider.identifier == identifier:
|
||||
current_app.logger.warning('Provider {} is already activated'.format(current_provider.display_name))
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def update_provider_priorities(current_provider, new_provider):
|
||||
def switch_providers(current_provider, new_provider):
|
||||
# Automatic update so set as notify user
|
||||
notify_user = get_user_by_id(current_app.config['NOTIFY_USER_ID'])
|
||||
current_provider.created_by_id = new_provider.created_by_id = notify_user.id
|
||||
|
||||
# Swap priority to change primary provider
|
||||
if new_provider.priority > current_provider.priority:
|
||||
new_provider.priority, current_provider.priority = current_provider.priority, new_provider.priority
|
||||
|
||||
# Incease other provider priority if equal
|
||||
# Increase other provider priority if equal
|
||||
elif new_provider.priority == current_provider.priority:
|
||||
current_provider.priority += 10
|
||||
|
||||
_print_provider_switch_logs(current_provider, new_provider)
|
||||
return current_provider, new_provider
|
||||
|
||||
|
||||
def _print_provider_switch_logs(current_provider, new_provider):
|
||||
|
||||
@@ -155,12 +155,25 @@ class UserUpdatePasswordSchema(BaseSchema):
|
||||
|
||||
|
||||
class ProviderDetailsSchema(BaseSchema):
|
||||
created_by_user = fields.Nested(
|
||||
UserSchema,
|
||||
attribute='created_by',
|
||||
dump_to='created_by',
|
||||
only=['id', 'name', 'email_address'],
|
||||
dump_only=True
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = models.ProviderDetails
|
||||
exclude = ("provider_rates", "provider_stats")
|
||||
strict = True
|
||||
|
||||
|
||||
class ProviderDetailsHistorySchema(ProviderDetailsSchema):
|
||||
class Meta:
|
||||
model = models.ProviderDetailsHistory
|
||||
|
||||
|
||||
class ServiceSchema(BaseSchema):
|
||||
|
||||
created_by = field_for(models.Service, 'created_by', required=True)
|
||||
@@ -605,5 +618,6 @@ template_history_schema = TemplateHistorySchema()
|
||||
event_schema = EventSchema()
|
||||
organisation_schema = OrganisationSchema()
|
||||
provider_details_schema = ProviderDetailsSchema()
|
||||
provider_details_history_schema = ProviderDetailsHistorySchema()
|
||||
day_schema = DaySchema()
|
||||
unarchived_template_schema = UnarchivedTemplateSchema()
|
||||
|
||||
Reference in New Issue
Block a user