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:
parent
b47c7c252d
commit
fd8d2a537d
@ -162,9 +162,15 @@ 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
|
||||
"""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,
|
||||
@ -172,3 +178,68 @@ async def get_current_period(user_id: str = ""):
|
||||
"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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user