Don't update sent notifications (dao)

This commit is contained in:
Imdad Ahad
2017-04-27 16:55:39 +01:00
parent 349fb3529e
commit c5bd685cef
2 changed files with 31 additions and 8 deletions

View File

@@ -175,11 +175,14 @@ def _update_notification_status(notification, status):
def update_notification_status_by_id(notification_id, status): def update_notification_status_by_id(notification_id, status):
notification = Notification.query.with_lockmode("update").filter( notification = Notification.query.with_lockmode("update").filter(
Notification.id == notification_id, Notification.id == notification_id,
or_(Notification.status == NOTIFICATION_CREATED, or_(
Notification.status == NOTIFICATION_CREATED,
Notification.status == NOTIFICATION_SENDING, Notification.status == NOTIFICATION_SENDING,
Notification.status == NOTIFICATION_PENDING)).first() Notification.status == NOTIFICATION_PENDING,
Notification.status == NOTIFICATION_SENT
)).first()
if not notification: if not notification or notification.status == NOTIFICATION_SENT:
return None return None
return _update_notification_status( return _update_notification_status(
@@ -193,10 +196,13 @@ def update_notification_status_by_id(notification_id, status):
def update_notification_status_by_reference(reference, status): def update_notification_status_by_reference(reference, status):
notification = Notification.query.filter( notification = Notification.query.filter(
Notification.reference == reference, Notification.reference == reference,
or_(Notification.status == NOTIFICATION_SENDING, or_(
Notification.status == NOTIFICATION_PENDING)).first() Notification.status == NOTIFICATION_SENDING,
Notification.status == NOTIFICATION_PENDING,
Notification.status == NOTIFICATION_SENT
)).first()
if not notification: if not notification or notification.status == NOTIFICATION_SENT:
return None return None
return _update_notification_status( return _update_notification_status(

View File

@@ -15,6 +15,7 @@ from app.models import (
TemplateStatistics, TemplateStatistics,
NOTIFICATION_STATUS_TYPES, NOTIFICATION_STATUS_TYPES,
NOTIFICATION_STATUS_TYPES_FAILED, NOTIFICATION_STATUS_TYPES_FAILED,
NOTIFICATION_SENT,
KEY_TYPE_NORMAL, KEY_TYPE_NORMAL,
KEY_TYPE_TEAM, KEY_TYPE_TEAM,
KEY_TYPE_TEST KEY_TYPE_TEST
@@ -353,11 +354,27 @@ def test_should_update_status_by_id_if_created(notify_db, notify_db_session):
def test_should_not_update_status_by_reference_if_in_sent_status(notify_db, notify_db_session): def test_should_not_update_status_by_reference_if_in_sent_status(notify_db, notify_db_session):
assert 1 == 2 notification = sample_notification(
notify_db,
notify_db_session,
status=NOTIFICATION_SENT,
reference='foo'
)
update_notification_status_by_reference('foo', 'failed')
assert Notification.query.get(notification.id).status == NOTIFICATION_SENT
def test_should_not_update_status_by_id_if_in_sent_status(notify_db, notify_db_session): def test_should_not_update_status_by_id_if_in_sent_status(notify_db, notify_db_session):
assert 1 == 2 notification = sample_notification(
notify_db,
notify_db_session,
status=NOTIFICATION_SENT
)
update_notification_status_by_id(notification.id, 'failed')
assert Notification.query.get(notification.id).status == NOTIFICATION_SENT
def test_should_not_update_status_by_reference_if_not_sending(notify_db, notify_db_session): def test_should_not_update_status_by_reference_if_not_sending(notify_db, notify_db_session):