mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-09 10:53:55 -04:00
Merge branch 'master' into do-not-write-test-data-to-the-history-table
Conflicts: app/dao/notifications_dao.py
This commit is contained in:
@@ -43,15 +43,13 @@ def deliver_sms(self, notification_id):
|
||||
send_to_providers.send_sms_to_provider(notification)
|
||||
except Exception as e:
|
||||
try:
|
||||
current_app.logger.error(
|
||||
current_app.logger.exception(
|
||||
"RETRY: SMS notification {} failed".format(notification_id)
|
||||
)
|
||||
current_app.logger.exception(e)
|
||||
self.retry(queue="retry", countdown=retry_iteration_to_delay(self.request.retries))
|
||||
except self.MaxRetriesExceededError:
|
||||
current_app.logger.error(
|
||||
current_app.logger.exception(
|
||||
"RETRY FAILED: task send_sms_to_provider failed for notification {}".format(notification_id),
|
||||
e
|
||||
)
|
||||
update_notification_status_by_id(notification_id, 'technical-failure')
|
||||
|
||||
@@ -69,14 +67,12 @@ def deliver_email(self, notification_id):
|
||||
update_notification_status_by_id(notification_id, 'technical-failure')
|
||||
except Exception as e:
|
||||
try:
|
||||
current_app.logger.error(
|
||||
current_app.logger.exception(
|
||||
"RETRY: Email notification {} failed".format(notification_id)
|
||||
)
|
||||
current_app.logger.exception(e)
|
||||
self.retry(queue="retry", countdown=retry_iteration_to_delay(self.request.retries))
|
||||
except self.MaxRetriesExceededError:
|
||||
current_app.logger.error(
|
||||
"RETRY FAILED: task send_email_to_provider failed for notification {}".format(notification_id),
|
||||
e
|
||||
"RETRY FAILED: task send_email_to_provider failed for notification {}".format(notification_id)
|
||||
)
|
||||
update_notification_status_by_id(notification_id, 'technical-failure')
|
||||
|
||||
@@ -132,7 +132,7 @@ def dao_create_notification(notification):
|
||||
|
||||
db.session.add(notification)
|
||||
if _should_record_notification_in_history_table(notification):
|
||||
db.session.add(NotificationHistory.from_notification(notification))
|
||||
db.session.add(NotificationHistory.from_original(notification))
|
||||
|
||||
|
||||
def _should_record_notification_in_history_table(notification):
|
||||
@@ -200,7 +200,7 @@ def dao_update_notification(notification):
|
||||
db.session.add(notification)
|
||||
if _should_record_notification_in_history_table(notification):
|
||||
notification_history = NotificationHistory.query.get(notification.id)
|
||||
notification_history.update_from_notification(notification)
|
||||
notification_history.update_from_original(notification)
|
||||
db.session.add(notification_history)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import asc
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.models import ProviderDetails
|
||||
from app.models import ProviderDetails, ProviderDetailsHistory
|
||||
from app import db
|
||||
|
||||
|
||||
@@ -20,4 +22,8 @@ def get_provider_details_by_notification_type(notification_type):
|
||||
|
||||
@transactional
|
||||
def dao_update_provider_details(provider_details):
|
||||
provider_details.version += 1
|
||||
provider_details.updated_at = datetime.utcnow()
|
||||
history = ProviderDetailsHistory.from_original(provider_details)
|
||||
db.session.add(provider_details)
|
||||
db.session.add(history)
|
||||
|
||||
@@ -35,6 +35,18 @@ def filter_null_value_fields(obj):
|
||||
)
|
||||
|
||||
|
||||
class HistoryModel:
|
||||
@classmethod
|
||||
def from_original(cls, original):
|
||||
history = cls()
|
||||
history.update_from_original(original)
|
||||
return history
|
||||
|
||||
def update_from_original(self, original):
|
||||
for c in self.__table__.columns:
|
||||
setattr(self, c.name, getattr(original, c.name))
|
||||
|
||||
|
||||
class User(db.Model):
|
||||
__tablename__ = 'users'
|
||||
|
||||
@@ -354,7 +366,22 @@ class ProviderDetails(db.Model):
|
||||
identifier = db.Column(db.String, nullable=False)
|
||||
priority = db.Column(db.Integer, nullable=False)
|
||||
notification_type = db.Column(notification_types, nullable=False)
|
||||
active = db.Column(db.Boolean, default=False)
|
||||
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)
|
||||
|
||||
|
||||
class ProviderDetailsHistory(db.Model, HistoryModel):
|
||||
__tablename__ = 'provider_details_history'
|
||||
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True, nullable=False)
|
||||
display_name = db.Column(db.String, nullable=False)
|
||||
identifier = db.Column(db.String, nullable=False)
|
||||
priority = db.Column(db.Integer, nullable=False)
|
||||
notification_type = db.Column(notification_types, nullable=False)
|
||||
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)
|
||||
|
||||
|
||||
JOB_STATUS_PENDING = 'pending'
|
||||
@@ -654,7 +681,7 @@ class Notification(db.Model):
|
||||
return serialized
|
||||
|
||||
|
||||
class NotificationHistory(db.Model):
|
||||
class NotificationHistory(db.Model, HistoryModel):
|
||||
__tablename__ = 'notification_history'
|
||||
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True)
|
||||
@@ -680,14 +707,10 @@ class NotificationHistory(db.Model):
|
||||
client_reference = db.Column(db.String, nullable=True)
|
||||
|
||||
@classmethod
|
||||
def from_notification(cls, notification):
|
||||
history = cls(**{c.name: getattr(notification, c.name) for c in cls.__table__.columns})
|
||||
def from_original(cls, notification):
|
||||
history = super().from_original(notification)
|
||||
return history
|
||||
|
||||
def update_from_notification(self, notification):
|
||||
for c in self.__table__.columns:
|
||||
setattr(self, c.name, getattr(notification, c.name))
|
||||
|
||||
|
||||
INVITED_USER_STATUS_TYPES = ['pending', 'accepted', 'cancelled']
|
||||
|
||||
|
||||
@@ -37,9 +37,10 @@ def update_provider_details(provider_details_id):
|
||||
current_data.update(request.get_json())
|
||||
update_dict = provider_details_schema.load(current_data).data
|
||||
|
||||
if "identifier" in request.get_json().keys():
|
||||
invalid_keys = {'identifier', 'version', 'updated_at'} & set(key for key in request.get_json().keys())
|
||||
if invalid_keys:
|
||||
message = "Not permitted to be updated"
|
||||
errors = {'identifier': [message]}
|
||||
errors = {key: [message] for key in invalid_keys}
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
dao_update_provider_details(update_dict)
|
||||
|
||||
Reference in New Issue
Block a user