Skip to content

Commit

Permalink
Update api_key_last_used for one-off API sends (#2427)
Browse files Browse the repository at this point in the history
* fix: update `api_key_last_used` for one-off API sends

* fix import order

---------

Co-authored-by: Jumana B <[email protected]>
Co-authored-by: Jumana Bahrainwala <[email protected]>
  • Loading branch information
3 people authored Jan 29, 2025
1 parent 651163c commit bb33743
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/v2/notifications/post_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from app.celery.tasks import process_job, seed_bounce_rate_in_redis
from app.clients.document_download import DocumentDownloadError
from app.config import QueueNames, TaskNames
from app.dao.api_key_dao import update_last_used_api_key
from app.dao.jobs_dao import dao_create_job
from app.dao.notifications_dao import update_notification_status_by_reference
from app.dao.templates_dao import get_precompiled_letter_template
Expand Down Expand Up @@ -443,6 +444,14 @@ def process_sms_or_email_notification(
signed_notification_data = signer_notification.sign(_notification)
notification = {**_notification}
scheduled_for = form.get("scheduled_for", None)

# Update the api_key last_used, we will only update this once per job
if api_key:
api_key_id = 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)

if scheduled_for:
notification = persist_notification( # keep scheduled notifications using the old code path for now
template_id=template.id,
Expand Down Expand Up @@ -704,7 +713,7 @@ def check_for_csv_errors(recipient_csv, max_rows, remaining_daily_messages, rema
message=f"You only have {remaining_annual_messages} remaining messages before you reach your annual limit. You've tried to send {nb_rows} messages.",
status_code=400,
)
## TODO: FF_ANNUAL_LIMIT - remove this if block in favour of more_rows_than_can_send_today found below
# TODO: FF_ANNUAL_LIMIT - remove this if block in favour of more_rows_than_can_send_today found below
if recipient_csv.more_rows_than_can_send:
if recipient_csv.template_type == SMS_TYPE:
raise BadRequestError(
Expand Down

0 comments on commit bb33743

Please sign in to comment.