Skip to content

Commit

Permalink
Associate Locations created via import
Browse files Browse the repository at this point in the history
Add the model associations and populate the field when importing.
  • Loading branch information
tvararu committed Jul 16, 2024
1 parent f02851d commit a8eb561
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
9 changes: 7 additions & 2 deletions app/models/immunisation_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class ImmunisationImport < ApplicationRecord
has_many :vaccination_records,
dependent: :restrict_with_exception,
foreign_key: :imported_from_id
has_many :locations,
dependent: :restrict_with_exception,
foreign_key: :imported_from_id

EXPECTED_HEADERS = %w[
ANATOMICAL_SITE
Expand Down Expand Up @@ -73,8 +76,10 @@ def process!(patient_session:)
.map(&:to_location)
.uniq(&:urn)
.reject(&:invalid?)
.map(&:attributes)
.tap { Location.create! _1 }
.each do |location|
location.imported_from = self
location.save!
end

rows
.map(&:to_vaccination_record)
Expand Down
1 change: 1 addition & 0 deletions app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Location < ApplicationRecord
has_many :sessions
has_many :patients
has_many :consent_forms, through: :sessions
belongs_to :imported_from, class_name: "ImmunisationImport", optional: true

validates :name, presence: true
validates :urn, presence: true, uniqueness: true
Expand Down
2 changes: 1 addition & 1 deletion spec/models/immunisation_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
expect { immunisation_import.process!(patient_session:) }.to change(
immunisation_import.vaccination_records,
:count
).by(11).and change(Location, :count).by(4)
).by(11).and change(immunisation_import.locations, :count).by(4)
end
end
end

0 comments on commit a8eb561

Please sign in to comment.