Skip to content

Commit

Permalink
Move last used from persist_notifications to process_job (#2425)
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbahrai authored Jan 22, 2025
1 parent b057f5a commit 2e6f633
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
8 changes: 8 additions & 0 deletions app/celery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
put_batch_saving_bulk_processed,
)
from app.config import Config, Priorities, QueueNames
from app.dao.api_key_dao import update_last_used_api_key
from app.dao.fact_notification_status_dao import (
fetch_notification_status_totals_for_service_by_fiscal_year,
)
Expand Down Expand Up @@ -138,6 +139,13 @@ def process_job(job_id):
csv = get_recipient_csv(job, template)

rows = csv.get_rows()

# Update the api_key last_used, we will only update this once per job
api_key_id = job.api_key_id
if api_key_id:
api_key_last_used = datetime.utcnow()
update_last_used_api_key(api_key_id, api_key_last_used)

for result in chunked(rows, Config.BATCH_INSERTION_CHUNK_SIZE):
process_rows(result, template, job, service)
put_batch_saving_bulk_created(
Expand Down
8 changes: 0 additions & 8 deletions app/notifications/process_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ def persist_notifications(notifications: List[VerifiedNotification]) -> List[Not
"""

lofnotifications = []
api_key_last_used = None

for notification in notifications:
notification_created_at = notification.get("created_at") or datetime.utcnow()
Expand Down Expand Up @@ -361,13 +360,6 @@ def persist_notifications(notifications: List[VerifiedNotification]) -> List[Not
notification.get("notification_created_at"), # type: ignore
)
)
# If the bulk message is sent using an api key, we want to keep track of the last time the api key was used
# We will only update the api key once
api_key_id = notification.get("api_key_id")
if api_key_id:
api_key_last_used = datetime.utcnow()
if api_key_last_used:
update_last_used_api_key(api_key_id, api_key_last_used)
bulk_insert_notifications(lofnotifications)

return lofnotifications
Expand Down
4 changes: 0 additions & 4 deletions tests/app/notifications/test_process_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,6 @@ def test_persist_notifications_list(self, sample_job, sample_api_key, notify_db_
assert persisted_notification[1].to == "[email protected]"
assert persisted_notification[0].service == sample_job.service

# Test that the api key last_used_timestamp got updated
api_key = ApiKey.query.get(sample_api_key.id)
assert api_key.last_used_timestamp is not None

def test_persist_notifications_reply_to_text_is_original_value_if_sender_is_changed_later(
self, sample_template, sample_api_key, mocker
):
Expand Down

0 comments on commit 2e6f633

Please sign in to comment.