bump project
This commit is contained in:
51
app/services/attendance.py
Normal file
51
app/services/attendance.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from app.extensions import db
|
||||
from app.models import AttendanceEvent, AttendanceType, Person
|
||||
|
||||
|
||||
def record_attendance(
|
||||
person: Person,
|
||||
event_type: AttendanceType,
|
||||
*,
|
||||
created_by_id: int | None = None,
|
||||
confidence: float | None = None,
|
||||
source: str = "web",
|
||||
) -> AttendanceEvent:
|
||||
event = AttendanceEvent(
|
||||
person=person,
|
||||
event_type=event_type.value,
|
||||
occurred_at=datetime.now(timezone.utc),
|
||||
created_by_id=created_by_id,
|
||||
confidence=confidence,
|
||||
source=source,
|
||||
)
|
||||
db.session.add(event)
|
||||
db.session.commit()
|
||||
return event
|
||||
|
||||
|
||||
def last_presence_summary(person: Person) -> str:
|
||||
check_in = (
|
||||
AttendanceEvent.query.filter_by(person_id=person.id, event_type=AttendanceType.CHECK_IN.value)
|
||||
.order_by(AttendanceEvent.occurred_at.desc())
|
||||
.first()
|
||||
)
|
||||
check_out = (
|
||||
AttendanceEvent.query.filter_by(person_id=person.id, event_type=AttendanceType.CHECK_OUT.value)
|
||||
.order_by(AttendanceEvent.occurred_at.desc())
|
||||
.first()
|
||||
)
|
||||
|
||||
if not check_in and not check_out:
|
||||
return "No attendance recorded"
|
||||
|
||||
check_in_text = _format_event_time(check_in) if check_in else "no check-in"
|
||||
check_out_text = _format_event_time(check_out) if check_out else "no check-out"
|
||||
return f"From {check_in_text} to {check_out_text}"
|
||||
|
||||
|
||||
def _format_event_time(event: AttendanceEvent) -> str:
|
||||
return event.occurred_at.strftime("%Y-%m-%d %H:%M")
|
||||
Reference in New Issue
Block a user