@@ -32,15 +32,32 @@ async def patients(
3232 ) -> list [PatientType ]:
3333 query = select (models .Patient )
3434 if location_node_id :
35+ # Recursive CTE to find the selected node and all its descendants
36+ cte = (
37+ select (models .LocationNode .id )
38+ .where (models .LocationNode .id == location_node_id )
39+ .cte (name = "location_descendants" , recursive = True )
40+ )
41+
42+ parent = select (models .LocationNode .id ).join (
43+ cte ,
44+ models .LocationNode .parent_id == cte .c .id ,
45+ )
46+ cte = cte .union_all (parent )
47+
48+ # Filter patients who are assigned to any of these locations
3549 query = query .where (
36- models .Patient .assigned_location_id == location_node_id ,
50+ models .Patient .assigned_location_id . in_ ( select ( cte . c . id )) ,
3751 )
52+
3853 result = await info .context .db .execute (query )
3954 return result .scalars ().all ()
4055
4156 @strawberry .field
4257 async def recent_patients (
43- self , info : Info , limit : int = 5
58+ self ,
59+ info : Info ,
60+ limit : int = 5 ,
4461 ) -> list [PatientType ]:
4562 query = select (models .Patient ).limit (limit )
4663 result = await info .context .db .execute (query )
@@ -59,7 +76,7 @@ async def create_patient(
5976 firstname = data .firstname ,
6077 lastname = data .lastname ,
6178 birthdate = data .birthdate ,
62- gender = data .gender .value ,
79+ sex = data .sex .value ,
6380 assigned_location_id = data .assigned_location_id ,
6481 )
6582 info .context .db .add (new_patient )
@@ -97,8 +114,8 @@ async def update_patient(
97114 patient .lastname = data .lastname
98115 if data .birthdate is not None :
99116 patient .birthdate = data .birthdate
100- if data .gender is not None :
101- patient .gender = data .gender .value
117+ if data .sex is not None :
118+ patient .sex = data .sex .value
102119 if data .assigned_location_id is not None :
103120 patient .assigned_location_id = data .assigned_location_id
104121
0 commit comments