Skip to content

Commit 794e405

Browse files
committed
Fix archive_moved_out_of_cohort_patients
This fixes an issue with the Rake task that was introduced as part of the introduction of the new `Organisation` model.
1 parent 3249f60 commit 794e405

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

lib/tasks/archive_moved_out_of_cohort_patients.rake

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,32 @@
22

33
desc "Migrate patients who were moved out of cohorts to ensure they're archived."
44
task archive_moved_out_of_cohort_patients: :environment do
5-
Team.find_each do |team|
6-
user = OpenStruct.new(selected_team: team)
5+
Team
6+
.includes(:organisation)
7+
.find_each do |team|
8+
user =
9+
OpenStruct.new(
10+
selected_team: team,
11+
selected_organisation: team.organisation
12+
)
713

8-
patients_in_cohort = team.patients
9-
patients_associated_with_team =
10-
PatientPolicy::Scope.new(user, Patient).resolve
14+
patients_in_cohort = team.patients
15+
patients_associated_with_team =
16+
PatientPolicy::Scope.new(user, Patient).resolve
1117

12-
patients_not_in_cohort = patients_associated_with_team - patients_in_cohort
18+
patients_not_in_cohort =
19+
patients_associated_with_team - patients_in_cohort
1320

14-
archive_reasons =
15-
patients_not_in_cohort.map do |patient|
16-
ArchiveReason.new(
17-
patient:,
18-
team:,
19-
type: "other",
20-
other_details: "Unknown: before reasons added"
21-
)
22-
end
21+
archive_reasons =
22+
patients_not_in_cohort.map do |patient|
23+
ArchiveReason.new(
24+
patient:,
25+
team:,
26+
type: "other",
27+
other_details: "Unknown: before reasons added"
28+
)
29+
end
2330

24-
ArchiveReason.import!(archive_reasons, on_duplicate_key_ignore: true)
25-
end
31+
ArchiveReason.import!(archive_reasons, on_duplicate_key_ignore: true)
32+
end
2633
end

0 commit comments

Comments
 (0)