Skip to content

Commit a5e7927

Browse files
Add LocationExport model
Jira-Issue: MAV-1521
1 parent b870625 commit a5e7927

6 files changed

Lines changed: 160 additions & 1 deletion

File tree

app/models/export.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# fk_rails_... (user_id => users.id)
3030
#
3131
class Export < ApplicationRecord
32-
delegated_type :exportable, types: [], dependent: :destroy
32+
delegated_type :exportable, types: %w[LocationPatientsExport], dependent: :destroy
3333

3434
belongs_to :team
3535
belongs_to :user
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: location_patients_exports
6+
#
7+
# id :bigint not null, primary key
8+
# academic_year :integer not null
9+
# filter_params :jsonb not null
10+
# created_at :datetime not null
11+
# updated_at :datetime not null
12+
# location_id :bigint not null
13+
#
14+
# Foreign Keys
15+
#
16+
# fk_rails_... (location_id => locations.id)
17+
#
18+
class LocationPatientsExport < ApplicationRecord
19+
belongs_to :location
20+
has_one :export, as: :exportable, touch: true
21+
delegate :team, to: :export, allow_nil: true
22+
23+
def file_type = :xlsx
24+
25+
def type_label = "Offline session"
26+
27+
def name
28+
"#{location.name} offline session"
29+
end
30+
31+
def filename
32+
"#{name} - exported on #{Date.current.to_fs(:long)}.xlsx"
33+
end
34+
35+
def generate_file
36+
filter =
37+
PatientFilter.new(team:, academic_year:, **filter_params.symbolize_keys)
38+
patients, programmes =
39+
location.school? ? school_patients(filter) : clinic_patients(filter)
40+
Reports::OfflineExporter.from_patients(
41+
patients,
42+
team:,
43+
programmes:,
44+
academic_year:
45+
)
46+
end
47+
48+
private
49+
50+
def school_patients(filter)
51+
scope =
52+
Patient
53+
.joins(:patient_locations)
54+
.where(patient_locations: { location:, academic_year: })
55+
.where(school: location)
56+
.includes_statuses
57+
[filter.apply(scope), location.programmes]
58+
end
59+
60+
def clinic_patients(filter)
61+
[filter.apply(Patient.includes_statuses), team.programmes]
62+
end
63+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
class CreateLocationPatientsExports < ActiveRecord::Migration[8.1]
4+
def change
5+
create_table :location_patients_exports do |t|
6+
t.integer :academic_year, null: false
7+
t.jsonb :filter_params, null: false, default: {}
8+
t.references :location, null: false, foreign_key: true, index: false
9+
10+
t.timestamps
11+
end
12+
end
13+
end

db/schema.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,14 @@
525525
t.index ["value"], name: "index_local_authority_postcodes_on_value", unique: true
526526
end
527527

528+
create_table "location_patients_exports", force: :cascade do |t|
529+
t.integer "academic_year", null: false
530+
t.datetime "created_at", null: false
531+
t.jsonb "filter_params", default: {}, null: false
532+
t.bigint "location_id", null: false
533+
t.datetime "updated_at", null: false
534+
end
535+
528536
create_table "location_programme_year_groups", force: :cascade do |t|
529537
t.bigint "location_year_group_id", null: false
530538
t.enum "programme_type", null: false, enum_type: "programme_type"
@@ -1167,6 +1175,7 @@
11671175
add_foreign_key "important_notices", "teams"
11681176
add_foreign_key "important_notices", "users", column: "dismissed_by_user_id"
11691177
add_foreign_key "important_notices", "vaccination_records"
1178+
add_foreign_key "location_patients_exports", "locations"
11701179
add_foreign_key "location_programme_year_groups", "location_year_groups", on_delete: :cascade
11711180
add_foreign_key "location_year_groups", "locations", on_delete: :cascade
11721181
add_foreign_key "notes", "patients"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: location_patients_exports
6+
#
7+
# id :bigint not null, primary key
8+
# academic_year :integer not null
9+
# filter_params :jsonb not null
10+
# created_at :datetime not null
11+
# updated_at :datetime not null
12+
# location_id :bigint not null
13+
#
14+
# Foreign Keys
15+
#
16+
# fk_rails_... (location_id => locations.id)
17+
#
18+
FactoryBot.define do
19+
factory :location_patients_export do
20+
academic_year { AcademicYear.current }
21+
filter_params { {} }
22+
association :location, factory: :generic_clinic
23+
end
24+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: location_patients_exports
6+
#
7+
# id :bigint not null, primary key
8+
# academic_year :integer not null
9+
# filter_params :jsonb not null
10+
# created_at :datetime not null
11+
# updated_at :datetime not null
12+
# location_id :bigint not null
13+
#
14+
# Foreign Keys
15+
#
16+
# fk_rails_... (location_id => locations.id)
17+
#
18+
describe LocationPatientsExport do
19+
subject(:export) { create(:location_patients_export) }
20+
21+
describe "associations" do
22+
it { should belong_to(:location) }
23+
end
24+
25+
describe "#name" do
26+
subject(:name) { export.name }
27+
28+
context "when location is a clinic" do
29+
it { should eq("Community clinic offline session") }
30+
end
31+
32+
context "when location is a school" do
33+
let(:export) do
34+
create(:location_patients_export, location: create(:gias_school))
35+
end
36+
37+
it { should eq("#{export.location.name} offline session") }
38+
end
39+
end
40+
41+
describe "#filename" do
42+
subject(:filename) { export.filename }
43+
44+
it "returns a filename with name and export date" do
45+
date_str = export.created_at.to_date.to_fs(:long)
46+
47+
expect(filename).to match(/#{date_str}/)
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)