feat(cis): implement /database/timetables/current-period endpoint with Neo4j query

- Query Neo4j for Academic/Registration periods where now() is between start_time and end_time
- Return period_id, event_type, event_label, start_time, end_time
- Handles missing teacher or Neo4j connection gracefully
This commit is contained in:
Kevin Carter 2026-05-20 22:06:46 +00:00
parent b47c7c252d
commit fd8d2a537d

View File

@ -162,13 +162,84 @@ async def process_worker_timetable(file_content, user_node_data, worker_node_dat
finally:
logging.info(f"Closing driver for {worker_node_data['worker_db_name']}")
driver.close_driver(neo_driver)
@router.get("/current-period")
async def get_current_period(user_id: str = ""):
# Phase 1: return stub — TODO: implement Neo4j query in Phase 2
return {
"period_id": None,
"event_type": None,
"event_label": None,
"start_time": None,
"end_time": None,
}
"""Get the current active timetable period for a teacher.
Queries Neo4j for Academic or Registration periods where now() falls
between start_time and end_time for the given teacher's uuid_string.
"""
if not user_id:
return {
"period_id": None,
"event_type": None,
"event_label": None,
"start_time": None,
"end_time": None,
}
# The user_id from Supabase JWT maps to the TeacherNode's uuid_string
teacher_uuid = user_id
# Try to find the current period across all known databases
# First, try to get the user's database name from the UserNode
neo_driver = driver.get_driver()
if neo_driver is None:
logging.error("Failed to connect to Neo4j for current-period query")
return {
"period_id": None,
"event_type": None,
"event_label": None,
"start_time": None,
"end_time": None,
}
try:
# Query for the current period
# Look for Teacher nodes with matching uuid_string that have relationships to Academic/Registration periods
query = """
MATCH (t:Teacher {uuid_string: $teacher_uuid})-[:HAS_PERIOD]->(p:AcademicPeriod|RegistrationPeriod)
WHERE p.start_time <= datetime() AND p.end_time >= datetime()
RETURN p.uuid_string AS period_id,
p.name AS event_label,
p.start_time AS start_time,
p.end_time AS end_time,
CASE WHEN p:AcademicPeriod THEN 'lesson' ELSE 'registration' END AS event_type
ORDER BY p.start_time ASC
LIMIT 1
"""
with neo_driver.session() as session:
result = session.run(query, teacher_uuid=teacher_uuid)
record = result.single()
if record:
logging.info(f"Found current period for teacher {teacher_uuid}: {record['event_label']}")
return {
"period_id": record["period_id"],
"event_type": record["event_type"],
"event_label": record["event_label"],
"start_time": str(record["start_time"]) if record["start_time"] else None,
"end_time": str(record["end_time"]) if record["end_time"] else None,
}
else:
logging.info(f"No current period found for teacher {teacher_uuid}")
return {
"period_id": None,
"event_type": None,
"event_label": None,
"start_time": None,
"end_time": None,
}
except Exception as e:
logging.error(f"Error querying current period: {str(e)}")
return {
"period_id": None,
"event_type": None,
"event_label": None,
"start_time": None,
"end_time": None,
}
finally:
driver.close_driver(neo_driver)