mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
Update notification_history table on insert/update of notifications
triggered via calls in dao_create_notification and dao_update_notification - if you don't use those functions (eg update in bulk) you'll have to update the history table yourself!
This commit is contained in:
@@ -15,6 +15,7 @@ from app.dao import days_ago
|
||||
from app.models import (
|
||||
Service,
|
||||
Notification,
|
||||
NotificationHistory,
|
||||
Job,
|
||||
NotificationStatistics,
|
||||
TemplateStatistics,
|
||||
@@ -182,7 +183,10 @@ def dao_create_notification(notification, notification_type):
|
||||
service_id=notification.service_id)
|
||||
db.session.add(template_stats)
|
||||
|
||||
notification_history = NotificationHistory.from_notification(notification)
|
||||
|
||||
db.session.add(notification)
|
||||
db.session.add(notification_history)
|
||||
|
||||
|
||||
def _update_notification_stats_query(notification_type, status):
|
||||
@@ -280,6 +284,8 @@ def update_notification_status_by_reference(reference, status, notification_stat
|
||||
|
||||
def dao_update_notification(notification):
|
||||
notification.updated_at = datetime.utcnow()
|
||||
notification_history = NotificationHistory.query.get(notification.id)
|
||||
notification_history.update_from_notification(notification)
|
||||
db.session.add(notification)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@@ -409,6 +409,16 @@ class NotificationHistory(db.Model):
|
||||
status = db.Column(NOTIFICATION_STATUS_TYPES_ENUM, nullable=False, default='created')
|
||||
reference = db.Column(db.String, nullable=True, index=True)
|
||||
|
||||
@classmethod
|
||||
def from_notification(cls, notification):
|
||||
from app.schemas import notification_status_schema
|
||||
return cls(notification_status_schema.dump(notification))
|
||||
|
||||
def update_from_notification(self, notification):
|
||||
from app.schemas import notification_status_schema
|
||||
new_notification_schema = cls(notification_status_schema.dump(notification))
|
||||
|
||||
|
||||
|
||||
INVITED_USER_STATUS_TYPES = ['pending', 'accepted', 'cancelled']
|
||||
|
||||
|
||||
@@ -288,6 +288,12 @@ class NotificationStatusSchema(BaseSchema):
|
||||
return in_data
|
||||
|
||||
|
||||
class NotificationHistorySchema(BaseSchema):
|
||||
class Meta:
|
||||
model = models.NotificationHistory
|
||||
strict = True
|
||||
|
||||
|
||||
class InvitedUserSchema(BaseSchema):
|
||||
|
||||
class Meta:
|
||||
@@ -492,6 +498,7 @@ email_notification_schema = EmailNotificationSchema()
|
||||
job_email_template_notification_schema = JobEmailTemplateNotificationSchema()
|
||||
notification_status_schema = NotificationStatusSchema()
|
||||
notification_status_schema_load_json = NotificationStatusSchema(load_json=True)
|
||||
notification_history_schema = NotificationHistorySchema()
|
||||
invited_user_schema = InvitedUserSchema()
|
||||
permission_schema = PermissionSchema()
|
||||
email_data_request_schema = EmailDataSchema()
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: 807f0b497157
|
||||
Revises: 0039_fix_notifications
|
||||
Revises: 0041_notification_history
|
||||
Create Date: 2016-07-07 13:15:35.503107
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '807f0b497157'
|
||||
revision = '0040_adjust_mmg_provider_rate'
|
||||
down_revision = '0039_fix_notifications'
|
||||
|
||||
from alembic import op
|
||||
|
||||
@@ -11,6 +11,7 @@ from app import db
|
||||
|
||||
from app.models import (
|
||||
Notification,
|
||||
NotificationHistory,
|
||||
Job,
|
||||
NotificationStatistics,
|
||||
TemplateStatistics,
|
||||
@@ -1010,3 +1011,31 @@ def _assert_job_stats(job_id, sent=0, count=0, delivered=0, failed=0):
|
||||
assert job.notification_count == count
|
||||
assert job.notifications_delivered == delivered
|
||||
assert job.notifications_failed == failed
|
||||
|
||||
|
||||
def test_creating_notification_adds_to_notification_history(sample_template):
|
||||
data = _notification_json(sample_template)
|
||||
notification = Notification(**data)
|
||||
|
||||
dao_create_notification(notification, sample_template.template_type)
|
||||
|
||||
assert Notification.query.count() == 1
|
||||
|
||||
hist = NotificationHistory.query.one()
|
||||
assert hist.id == notifcation.id
|
||||
assert not hasattr(hist.to)
|
||||
assert not hasattr(hist._personalisation)
|
||||
assert not hasattr(hist.content_char_count)
|
||||
|
||||
|
||||
def test_updating_notification_updates_notification_history(sample_notification):
|
||||
hist = NotificationHistory.query.one()
|
||||
assert hist.id == notifcation.id
|
||||
assert hist.status == 'sending'
|
||||
|
||||
sample_notification.status = 'delivered'
|
||||
dao_update_notification(sample_notification)
|
||||
|
||||
hist = NotificationHistory.query.one()
|
||||
assert hist.id == notification.id
|
||||
assert hist.status == 'delivered'
|
||||
|
||||
Reference in New Issue
Block a user