Skip to content

Commit

Permalink
#2105 - Updated Twilio Status Query (#2289)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-macmillan authored Feb 7, 2025
1 parent 5040f32 commit 69912b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
19 changes: 11 additions & 8 deletions app/celery/twilio_tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime, timedelta, timezone

from flask import current_app
from sqlalchemy import select
from sqlalchemy import and_, select

from app import db, notify_celery, twilio_sms_client
from app.celery.exceptions import NonRetryableException
Expand All @@ -15,15 +15,18 @@ def _get_notifications() -> list:
current_app.logger.info('Getting notifications to update status')
one_hour_ago = datetime.now(timezone.utc) - timedelta(hours=1)
stmt = (
select(Notification)
.where(Notification.notification_type == 'sms')
.where(Notification.sent_by == 'twilio')
.where(~Notification.status.in_(NOTIFICATION_STATUS_TYPES_COMPLETED))
.where(Notification.created_at < one_hour_ago)
.order_by(Notification.created_at)
select(Notification.id, Notification.status, Notification.reference)
.where(
and_(
Notification.notification_type == 'sms',
Notification.created_at < one_hour_ago,
~Notification.status.in_(NOTIFICATION_STATUS_TYPES_COMPLETED),
Notification.sent_by == 'twilio',
)
)
.limit(current_app.config['TWILIO_STATUS_PAGE_SIZE'])
)
return db.session.scalars(stmt).all()
return db.session.execute(stmt).all()


@notify_celery.task(name='update-twilio-status')
Expand Down
9 changes: 6 additions & 3 deletions tests/app/celery/test_twilio_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
NOTIFICATION_TEMPORARY_FAILURE,
NOTIFICATION_PERMANENT_FAILURE,
)
from app.models import Notification


@pytest.mark.parametrize(
Expand Down Expand Up @@ -68,9 +69,11 @@ def test_get_notifications_datefilter(sample_notification, minute_offset, expect
def test_update_twilio_status_with_results(mocker, sample_notification):
"""Test that update_twilio_status() calls twilio_sms_client.update_notification_status_override() with the
notification reference when there are notifications to update."""
notification = sample_notification(status=NOTIFICATION_CREATED, sent_by='twilio')

mocker.patch('app.celery.twilio_tasks._get_notifications', return_value=[notification])
notification: Notification = sample_notification(
status=NOTIFICATION_SENDING,
created_at=datetime.now(timezone.utc) - timedelta(hours=3),
sent_by='twilio',
)

with patch(
'app.celery.twilio_tasks.twilio_sms_client.update_notification_status_override'
Expand Down

0 comments on commit 69912b9

Please sign in to comment.