mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Updates:
* Add notify user id in config * Add dao method to get provider history versions along with tests * BUG: Provider switching did not handle case where priorities were equal. This * adds a fix to properly cover this case along with tests
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
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user