fixing the checkin

This commit is contained in:
2026-09-18 21:59:44 -04:00
parent e26eb302d0
commit db0a2a3042
7 changed files with 194 additions and 23 deletions
+46 -2
View File
@@ -61,7 +61,35 @@ def add_schedule(connection, user_id, enter_time, day_of_week, out_time, shift):
)
def get_days_worked_by_user(connection, user_id):
def _week_bounds(reference):
from datetime import timedelta, timezone
# Convert aware to naive UTC for DB comparison
if getattr(reference, "tzinfo", None) is not None:
reference = reference.astimezone(timezone.utc).replace(tzinfo=None)
# Sunday start
days_from_sunday = (reference.weekday() + 1) % 7
start_date = (reference - timedelta(days=days_from_sunday)).replace(
hour=0, minute=0, second=0, microsecond=0
)
return start_date, start_date + timedelta(days=7)
def get_days_worked_by_user(connection, user_id, reference=None):
if reference is not None:
week_start, week_end = _week_bounds(reference)
with connection.cursor() as cursor:
cursor.execute(
"""
SELECT DISTINCT day_of_week
FROM schedules
WHERE user_id = %s
AND enter_time >= %s AND enter_time < %s
ORDER BY day_of_week
""",
(user_id, week_start, week_end),
)
return [row["day_of_week"] for row in cursor.fetchall()]
with connection.cursor() as cursor:
cursor.execute(
"""
@@ -75,7 +103,23 @@ def get_days_worked_by_user(connection, user_id):
return [row["day_of_week"] for row in cursor.fetchall()]
def get_user_enter_time_by_day(connection, user_id, day_of_week):
def get_user_enter_time_by_day(connection, user_id, day_of_week, reference=None):
if reference is not None:
week_start, week_end = _week_bounds(reference)
with connection.cursor() as cursor:
cursor.execute(
"""
SELECT enter_time
FROM schedules
WHERE user_id = %s AND day_of_week = %s
AND enter_time >= %s AND enter_time < %s
ORDER BY date_created DESC
LIMIT 1
""",
(user_id, day_of_week, week_start, week_end),
)
row = cursor.fetchone()
return None if row is None else row["enter_time"]
with connection.cursor() as cursor:
cursor.execute(
"""