Updated backend to fix a few bugs and keep things dry. Added unit tests

This commit is contained in:
alexjanousekGSA
2025-02-04 11:26:20 -05:00
parent 7374ac90a2
commit cad5722300
4 changed files with 88 additions and 41 deletions
+24
View File
@@ -1,4 +1,6 @@
import calendar
import pytz
from datetime import date, datetime, time, timedelta
from app.utils import utc_now
@@ -93,3 +95,25 @@ def generate_date_range(start_date, end_date=None, days=0):
current_date += timedelta(days=1)
else:
return "An end_date or number of days must be specified"
def build_local_and_utc_date_range(
start_date_str: str,
days: int = 7,
timezone: str = "UTC"
):
"""
Convert date to local range based on timezone
"""
user_timezone = pytz.timezone(timezone)
local_end_date = datetime.strptime(start_date_str, "%Y-%m-%d").replace(tzinfo=user_timezone)
# Subtract (days - 1) so the entire final day is included in the range
local_start_date = local_end_date - timedelta(days=days)
# Convert to UTC for database queries
utc_start_date = local_start_date.astimezone(pytz.utc).replace(hour=0, minute=0, second=0)
utc_end_date = local_end_date.astimezone(pytz.utc).replace(hour=23, minute=59, second=59)
return (local_start_date, utc_start_date, utc_end_date)
+1 -1
View File
@@ -503,7 +503,7 @@ def dao_fetch_stats_for_service_from_days_for_user(
select(
NotificationAllTimeView.notification_type,
NotificationAllTimeView.status,
func.date_trunc("day", NotificationAllTimeView.created_at).label("day"),
func.date_trunc("day", NotificationAllTimeView.created_at).label("timestamp"),
func.count(NotificationAllTimeView.id).label("count"),
)
.select_from(NotificationAllTimeView)