Skip to content

Commit 50e0822

Browse files
Add VaccinationRecordsExport model
Jira-Issue: MAV-1521
1 parent 5b4db69 commit 50e0822

6 files changed

Lines changed: 166 additions & 1 deletion

File tree

app/models/export.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
#
3131
class Export < ApplicationRecord
3232
delegated_type :exportable,
33-
types: %w[LocationPatientsExport SessionPatientsExport],
33+
types: %w[
34+
LocationPatientsExport
35+
SessionPatientsExport
36+
VaccinationRecordsExport
37+
],
3438
dependent: :destroy
3539

3640
belongs_to :team
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: vaccination_records_exports
6+
#
7+
# id :bigint not null, primary key
8+
# academic_year :integer not null
9+
# date_from :date
10+
# date_to :date
11+
# file_format :string not null
12+
# programme_type :string not null
13+
# created_at :datetime not null
14+
# updated_at :datetime not null
15+
#
16+
class VaccinationRecordsExport < ApplicationRecord
17+
include BelongsToProgramme
18+
19+
has_one :export, as: :exportable, touch: true
20+
delegate :team, to: :export, allow_nil: true
21+
22+
def file_type = :csv
23+
24+
def type_label = "Vaccination records"
25+
26+
def name
27+
base = "#{programme.name} vaccination records"
28+
return base unless date_from || date_to
29+
30+
"#{base} (#{date_from_str} to #{date_to_str})"
31+
end
32+
33+
def filename
34+
"#{programme.name} - #{file_format} - #{date_from_str} - #{date_to_str}.csv"
35+
end
36+
37+
def generate_file
38+
exporter_class = {
39+
"careplus" => Reports::ManualCareplusExporter,
40+
"mavis" => Reports::ProgrammeVaccinationsExporter,
41+
"systm_one" => Reports::SystmOneExporter
42+
}.fetch(file_format)
43+
44+
exporter_class.call(
45+
team:,
46+
programme:,
47+
academic_year:,
48+
start_date: date_from,
49+
end_date: date_to
50+
)
51+
end
52+
53+
private
54+
55+
def date_from_str = date_from&.to_fs(:long) || "earliest"
56+
57+
def date_to_str = date_to&.to_fs(:long) || "latest"
58+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class CreateVaccinationRecordsExports < ActiveRecord::Migration[8.1]
4+
def change
5+
create_table :vaccination_records_exports do |t|
6+
t.integer :academic_year, null: false
7+
t.date :date_from
8+
t.date :date_to
9+
t.string :file_format, null: false
10+
t.string :programme_type, null: false
11+
12+
t.timestamps
13+
end
14+
end
15+
end

db/schema.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,16 @@
10911091
t.index ["vaccine_id"], name: "index_vaccination_records_on_vaccine_id"
10921092
end
10931093

1094+
create_table "vaccination_records_exports", force: :cascade do |t|
1095+
t.integer "academic_year", null: false
1096+
t.datetime "created_at", null: false
1097+
t.date "date_from"
1098+
t.date "date_to"
1099+
t.string "file_format", null: false
1100+
t.string "programme_type", null: false
1101+
t.datetime "updated_at", null: false
1102+
end
1103+
10941104
create_table "vaccines", force: :cascade do |t|
10951105
t.text "brand", null: false
10961106
t.boolean "contains_gelatine", null: false
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: vaccination_records_exports
6+
#
7+
# id :bigint not null, primary key
8+
# academic_year :integer not null
9+
# date_from :date
10+
# date_to :date
11+
# file_format :string not null
12+
# programme_type :string not null
13+
# created_at :datetime not null
14+
# updated_at :datetime not null
15+
#
16+
FactoryBot.define do
17+
factory :vaccination_records_export do
18+
programme_type { Programme::TYPES.sample }
19+
academic_year { AcademicYear.current }
20+
file_format { "mavis" }
21+
end
22+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: vaccination_records_exports
6+
#
7+
# id :bigint not null, primary key
8+
# academic_year :integer not null
9+
# date_from :date
10+
# date_to :date
11+
# file_format :string not null
12+
# programme_type :string not null
13+
# created_at :datetime not null
14+
# updated_at :datetime not null
15+
#
16+
describe VaccinationRecordsExport do
17+
subject(:export) do
18+
create(
19+
:vaccination_records_export,
20+
programme_type: "flu",
21+
academic_year: 2024,
22+
file_format: "mavis"
23+
)
24+
end
25+
26+
it { should be_valid }
27+
28+
describe "#name" do
29+
subject(:name) { export.name }
30+
31+
it { should eq("#{Programme.flu.name} vaccination records") }
32+
33+
context "when date_from and date_to are set" do
34+
let(:export) do
35+
create(
36+
:vaccination_records_export,
37+
programme_type: "flu",
38+
academic_year: 2024,
39+
file_format: "mavis",
40+
date_from: Date.new(2024, 9, 1),
41+
date_to: Date.new(2025, 7, 31)
42+
)
43+
end
44+
45+
it { should include("vaccination records") }
46+
it { should include("September 2024") }
47+
end
48+
end
49+
50+
describe "#filename" do
51+
subject { export.filename }
52+
53+
it { should include(Programme.flu.name) }
54+
it { should end_with(".csv") }
55+
end
56+
end

0 commit comments

Comments
 (0)