@@ -8,53 +8,46 @@ def resolve
8
8
9
9
return scope . none if team . nil?
10
10
11
- patients_table = Patient . arel_table
12
- associated_patients_table = Arel ::Table . new ( "associated_patients" )
13
-
14
- associated_patients = [
11
+ existence_criteria = [
15
12
PatientSession
16
- . select ( :patient_id )
13
+ . select ( "1" )
17
14
. joins ( :session )
15
+ . where ( "patient_sessions.patient_id = patients.id" )
18
16
. where ( sessions : { team_id : team . id } )
19
17
. arel ,
20
- ArchiveReason . select ( :patient_id ) . where ( team_id : team . id ) . arel ,
21
- SchoolMove . select ( :patient_id ) . where ( team_id : team . id ) . arel ,
22
- SchoolMove . select ( :patient_id ) . where ( school : team . schools ) . arel ,
18
+ ArchiveReason
19
+ . select ( "1" )
20
+ . where ( "archive_reasons.patient_id = patients.id" )
21
+ . where ( team_id : team . id )
22
+ . arel ,
23
+ SchoolMove
24
+ . select ( "1" )
25
+ . where ( "school_moves.patient_id = patients.id" )
26
+ . where ( team_id : team . id )
27
+ . arel ,
28
+ SchoolMove
29
+ . select ( "1" )
30
+ . where ( "school_moves.patient_id = patients.id" )
31
+ . where ( school : team . schools )
32
+ . arel ,
23
33
VaccinationRecord
24
- . select ( :patient_id )
34
+ . select ( "1" )
25
35
. joins ( :session )
36
+ . where ( "vaccination_records.patient_id = patients.id" )
26
37
. where ( sessions : { team_id : team . id } )
27
38
. arel ,
28
39
VaccinationRecord
29
- . select ( :patient_id )
40
+ . select ( "1" )
41
+ . where ( "vaccination_records.patient_id = patients.id" )
30
42
. where ( performed_ods_code : organisation . ods_code , session_id : nil )
31
43
. arel
32
44
]
33
45
34
- associated_patiens_union =
35
- Arel ::Nodes ::Union . new ( associated_patients [ 0 ] , associated_patients [ 1 ] )
36
- associated_patients [ 2 ..] . each do |select |
37
- associated_patiens_union =
38
- Arel ::Nodes ::Union . new ( associated_patiens_union , select )
46
+ condition = existence_criteria [ 0 ] . exists
47
+ existence_criteria [ 1 ..] . each do |filter |
48
+ condition = condition . or ( filter . exists )
39
49
end
40
-
41
- associated_patients_query =
42
- Arel ::SelectManager
43
- . new
44
- . project ( Arel . star )
45
- . from ( associated_patiens_union . as ( "t" ) )
46
- . group ( "t.patient_id" )
47
-
48
- join_associated_patients =
49
- patients_table
50
- . join ( associated_patients_table )
51
- . on ( associated_patients_table [ :patient_id ] . eq ( patients_table [ :id ] ) )
52
- . join_sources
53
- . first
54
-
55
- Patient . with ( associated_patients : associated_patients_query ) . joins (
56
- join_associated_patients
57
- )
50
+ scope . where ( condition )
58
51
end
59
52
end
60
53
end
0 commit comments