Add UTC datetime helpers to attempt to fix running issue
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
from datetime import date, datetime, time, timedelta
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..core.datetime import utc_now
|
||||
from ..models.models import AttendanceCheckoutSource, AttendanceSession
|
||||
|
||||
|
||||
def duration_seconds(start: datetime, end: datetime) -> int:
|
||||
return max(0, int((end - start).total_seconds()))
|
||||
|
||||
|
||||
def close_stale_attendance_sessions(
|
||||
db: Session,
|
||||
cutoff_date: date | None = None,
|
||||
checkout_hour: int = 17,
|
||||
) -> int:
|
||||
cutoff = cutoff_date or date.today()
|
||||
cutoff_at = datetime.combine(cutoff, time.min)
|
||||
sessions = (
|
||||
db.query(AttendanceSession)
|
||||
.filter(
|
||||
AttendanceSession.is_open == True,
|
||||
AttendanceSession.checked_in_at < cutoff_at,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
|
||||
now = utc_now()
|
||||
for session in sessions:
|
||||
checkout_at = datetime.combine(session.checked_in_at.date(), time(hour=checkout_hour))
|
||||
if checkout_at < session.checked_in_at:
|
||||
checkout_at = session.checked_in_at + timedelta(minutes=1)
|
||||
session.checked_out_at = checkout_at
|
||||
session.checkout_source = AttendanceCheckoutSource.SYSTEM
|
||||
session.system_flag_reason = "User did not check out before midnight; checkout time was system-set."
|
||||
session.duration_seconds = duration_seconds(session.checked_in_at, checkout_at)
|
||||
session.is_open = False
|
||||
session.updated_at = now
|
||||
|
||||
db.commit()
|
||||
return len(sessions)
|
||||
Reference in New Issue
Block a user