diff --git a/app/policies/vaccination_record_policy.rb b/app/policies/vaccination_record_policy.rb index 6b33573753..6c8bbca5d3 100644 --- a/app/policies/vaccination_record_policy.rb +++ b/app/policies/vaccination_record_policy.rb @@ -64,9 +64,28 @@ def resolve team = user.selected_team return scope.none if team.nil? + patient_subquery = + Patient + .joins(patient_sessions: :session) + .select(:id) + .distinct + .where(sessions: { team_id: team.id }) + .arel + .as("patients") scope + .joins( + VaccinationRecord + .arel_table + .join(patient_subquery, Arel::Nodes::OuterJoin) + .on( + VaccinationRecord.arel_table[:patient_id].eq( + patient_subquery[:id] + ) + ) + .join_sources + ) .kept - .where(patient: team.patients) + .where(patient_subquery[:id].not_eq(nil)) .or(scope.kept.where(session: team.sessions)) .or( scope.kept.where(