Skip to content

Commit 5b4db69

Browse files
Add SessionExport model
Jira-Issue: MAV-1521
1 parent a5e7927 commit 5b4db69

6 files changed

Lines changed: 118 additions & 1 deletion

File tree

app/models/export.rb

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

3436
belongs_to :team
3537
belongs_to :user
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: session_patients_exports
6+
#
7+
# id :bigint not null, primary key
8+
# created_at :datetime not null
9+
# updated_at :datetime not null
10+
# session_id :bigint not null
11+
#
12+
# Foreign Keys
13+
#
14+
# fk_rails_... (session_id => sessions.id)
15+
#
16+
class SessionPatientsExport < ApplicationRecord
17+
belongs_to :session
18+
has_one :export, as: :exportable, touch: true
19+
20+
delegate :location, to: :session
21+
22+
def file_type = :xlsx
23+
24+
def type_label = "Offline session"
25+
26+
def name = "#{location.name} offline session"
27+
28+
def filename
29+
"#{location.name} (#{location.urn_and_site}) - exported on #{Date.current.to_fs(:long)}.xlsx"
30+
end
31+
32+
def generate_file
33+
Reports::OfflineExporter.from_session(session)
34+
end
35+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
class CreateSessionPatientsExports < ActiveRecord::Migration[8.1]
4+
def change
5+
create_table :session_patients_exports do |t|
6+
t.references :session, null: false, foreign_key: true, index: false
7+
8+
t.timestamps
9+
end
10+
end
11+
end

db/schema.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,12 @@
885885
t.index ["session_id"], name: "index_session_notifications_on_session_id"
886886
end
887887

888+
create_table "session_patients_exports", force: :cascade do |t|
889+
t.datetime "created_at", null: false
890+
t.bigint "session_id", null: false
891+
t.datetime "updated_at", null: false
892+
end
893+
888894
create_table "session_programme_year_groups", primary_key: ["session_id", "programme_type", "year_group"], force: :cascade do |t|
889895
t.enum "programme_type", null: false, enum_type: "programme_type"
890896
t.bigint "session_id", null: false
@@ -1220,6 +1226,7 @@
12201226
add_foreign_key "session_notifications", "patients"
12211227
add_foreign_key "session_notifications", "sessions"
12221228
add_foreign_key "session_notifications", "users", column: "sent_by_user_id"
1229+
add_foreign_key "session_patients_exports", "sessions"
12231230
add_foreign_key "session_programme_year_groups", "sessions", on_delete: :cascade
12241231
add_foreign_key "sessions", "team_locations"
12251232
add_foreign_key "subteams", "teams"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: session_patients_exports
6+
#
7+
# id :bigint not null, primary key
8+
# created_at :datetime not null
9+
# updated_at :datetime not null
10+
# session_id :bigint not null
11+
#
12+
# Foreign Keys
13+
#
14+
# fk_rails_... (session_id => sessions.id)
15+
#
16+
FactoryBot.define do
17+
factory :session_patients_export do
18+
session
19+
end
20+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: session_patients_exports
6+
#
7+
# id :bigint not null, primary key
8+
# created_at :datetime not null
9+
# updated_at :datetime not null
10+
# session_id :bigint not null
11+
#
12+
# Foreign Keys
13+
#
14+
# fk_rails_... (session_id => sessions.id)
15+
#
16+
describe SessionPatientsExport do
17+
subject(:export) { create(:session_patients_export, session:) }
18+
19+
let(:programme) { Programme.hpv }
20+
let(:team) { create(:team, :with_one_nurse, programmes: [programme]) }
21+
let(:location) { create(:gias_school, team:, programmes: [programme]) }
22+
let(:session) { create(:session, team:, location:, programmes: [programme]) }
23+
24+
it { should be_valid }
25+
26+
describe "#name" do
27+
subject { export.name }
28+
29+
it { should eq("#{location.name} offline session") }
30+
end
31+
32+
describe "#filename" do
33+
subject(:filename) { export.filename }
34+
35+
it "includes location name, URN/site, and export date" do
36+
date_str = export.created_at.to_date.to_fs(:long)
37+
expect(filename).to eq(
38+
"#{location.name} (#{location.urn_and_site}) - exported on #{date_str}.xlsx"
39+
)
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)