From 96ef9a83e4d309f66ec1a474dcc6b98db60f8506 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 27 Sep 2024 08:22:23 -0300 Subject: [PATCH] chore: refactor `delete_notifications_older_than_retention_by_type` to use common date method in `date_utils` (#2308) --- app/dao/notifications_dao.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index b95ad178e1..3c78e02703 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -1,6 +1,6 @@ import functools import string -from datetime import datetime, time, timedelta, timezone +from datetime import datetime, timedelta from flask import current_app from itsdangerous import BadSignature @@ -25,7 +25,10 @@ from app import create_uuid, db, signer_personalisation from app.dao.dao_utils import transactional -from app.dao.date_util import utc_midnight_n_days_ago +from app.dao.date_util import ( + get_query_date_based_on_retention_period, + utc_midnight_n_days_ago, +) from app.errors import InvalidRequest from app.models import ( EMAIL_TYPE, @@ -384,7 +387,7 @@ def delete_notifications_older_than_retention_by_type(notification_type, qry_lim flexible_data_retention = ServiceDataRetention.query.filter(ServiceDataRetention.notification_type == notification_type).all() deleted = 0 for f in flexible_data_retention: - days_of_retention = datetime.combine(datetime.now(timezone.utc) - timedelta(days=f.days_of_retention), time.max) + days_of_retention = get_query_date_based_on_retention_period(f.days_of_retention) insert_update_notification_history(notification_type, days_of_retention, f.service_id) @@ -397,7 +400,7 @@ def delete_notifications_older_than_retention_by_type(notification_type, qry_lim current_app.logger.info("Deleting {} notifications for services without flexible data retention".format(notification_type)) - seven_days_ago = datetime.combine(datetime.now(timezone.utc) - timedelta(days=7), time.max) + seven_days_ago = get_query_date_based_on_retention_period(7) services_with_data_retention = [x.service_id for x in flexible_data_retention] service_ids_to_purge = db.session.query(Service.id).filter(Service.id.notin_(services_with_data_retention)).all()