@@ -30,19 +30,15 @@ def initialize(
3030 team :,
3131 programmes :,
3232 academic_year :,
33- start_date :,
34- end_date :,
33+ vaccination_records :,
3534 include_gender :,
36- include_missing_nhs_number :,
3735 vaccine_columns :
3836 )
3937 @team = team
4038 @programmes = programmes
4139 @academic_year = academic_year
42- @start_date = start_date
43- @end_date = end_date
40+ @vaccination_records = vaccination_records
4441 @include_gender = include_gender
45- @include_missing_nhs_number = include_missing_nhs_number
4642 @vaccine_columns = vaccine_columns
4743 end
4844
@@ -57,7 +53,79 @@ def call
5753 end
5854 end
5955
60- def self . call ( ...) = new ( ...) . call
56+ def self . call (
57+ team :,
58+ programmes :,
59+ academic_year :,
60+ start_date :,
61+ end_date :,
62+ include_gender :,
63+ include_missing_nhs_number :,
64+ vaccine_columns :
65+ )
66+ vaccination_records =
67+ vaccination_records_scope (
68+ team :,
69+ programmes :,
70+ academic_year :,
71+ start_date :,
72+ end_date :,
73+ include_missing_nhs_number :
74+ ) . includes ( :patient , :vaccine , session : %i[ location team_location ] )
75+
76+ from_records (
77+ vaccination_records :,
78+ team :,
79+ programmes :,
80+ academic_year :,
81+ include_gender :,
82+ vaccine_columns :
83+ )
84+ end
85+
86+ def self . from_records (
87+ vaccination_records :,
88+ team :,
89+ programmes :,
90+ academic_year :,
91+ include_gender :,
92+ vaccine_columns :
93+ )
94+ new (
95+ vaccination_records :,
96+ team :,
97+ programmes :,
98+ academic_year :,
99+ include_gender :,
100+ vaccine_columns :
101+ ) . call
102+ end
103+
104+ def self . vaccination_records_scope (
105+ team :,
106+ programmes :,
107+ academic_year :,
108+ start_date :,
109+ end_date :,
110+ include_missing_nhs_number :
111+ )
112+ scope =
113+ team
114+ . vaccination_records
115+ . sourced_from_service
116+ . for_programmes ( programmes )
117+ . for_academic_year ( academic_year )
118+ . administered
119+ . order_by_performed_at
120+ . created_or_updated_between ( start_date , end_date )
121+
122+ scope =
123+ scope . joins ( :patient ) . merge (
124+ Patient . with_nhs_number
125+ ) unless include_missing_nhs_number
126+
127+ scope
128+ end
61129
62130 private_class_method :new
63131
@@ -73,10 +141,8 @@ def self.call(...) = new(...).call
73141 attr_reader :team ,
74142 :programmes ,
75143 :academic_year ,
76- :start_date ,
77- :end_date ,
144+ :vaccination_records ,
78145 :include_gender ,
79- :include_missing_nhs_number ,
80146 :vaccine_columns
81147
82148 def headers
@@ -120,52 +186,6 @@ def gender_row_value(patient)
120186 include_gender ? [ GENDER_CODE_MAPPINGS [ patient . gender_code ] ] : [ ]
121187 end
122188
123- def vaccination_records
124- scope =
125- VaccinationRecord
126- . kept
127- . sourced_from_service
128- . for_programmes ( programmes )
129- . where ( team_location : { team_id : team . id } )
130- . for_academic_year ( academic_year )
131- . administered
132- . order_by_performed_at
133- . includes ( :patient , :vaccine , session : %i[ location team_location ] )
134-
135- if start_date . present?
136- scope =
137- scope . where (
138- "vaccination_records.created_at >= ?" ,
139- start_date . beginning_of_day
140- ) . or (
141- scope . where (
142- "vaccination_records.updated_at >= ?" ,
143- start_date . beginning_of_day
144- )
145- )
146- end
147-
148- if end_date . present?
149- scope =
150- scope . where (
151- "vaccination_records.created_at <= ?" ,
152- end_date . end_of_day
153- ) . or (
154- scope . where (
155- "vaccination_records.updated_at <= ?" ,
156- end_date . end_of_day
157- )
158- )
159- end
160-
161- scope =
162- scope . joins ( :patient ) . merge (
163- Patient . with_nhs_number
164- ) unless include_missing_nhs_number
165-
166- scope
167- end
168-
169189 def consents
170190 @consents ||=
171191 Consent
0 commit comments