Skip to content

Commit

Permalink
dm-5018 update facilities importer task (#1085)
Browse files Browse the repository at this point in the history
* update facilities importer task

adds logic to va_facilities:create_or_update_va_facilities that writes file with list of names and ids of non-current va_facilities

adds new task va_facilities:delete_non_current_va_facilities that reads contents of non-current facilities file and deletes the facility if the diffusion_histories and practice_origin_facilities are empty

* update va_facilities:delete_non_current_va_facilities task to create an output file containing a list of deleted and non-deleted facilities
  • Loading branch information
PhilipDeFraties authored Oct 23, 2024
1 parent 558509b commit 807956c
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion lib/tasks/va_facilities.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace :va_facilities do
desc 'Create new VA facility records based on the data from the va_facilities.json file'

task :create_or_update_va_facilities => :environment do
current_facility_ids = []
ctr = 0
file = File.read("#{Rails.root}/lib/assets/va_facilities.json")
va_facilities = JSON.parse(file)
Expand All @@ -13,7 +14,7 @@ namespace :va_facilities do
if visn.number === vaf["VISN"].to_i && VaFacility.where(station_number: vaf["Station Number"]).empty?
puts 'Creating facility - ' + vaf["Official Station Name"]
hidden = vaf["Classification"].blank?
VaFacility.create!(
facility = VaFacility.create!(
visn: visn,
sta3n: vaf["STA3N (For Sorting)"].to_s,
station_number: vaf["Station Number"].to_s,
Expand Down Expand Up @@ -145,11 +146,80 @@ namespace :va_facilities do
puts "Updated facility: #{vaf['Official Station Name']}, #{ctr.to_s}"
end
end
current_facility_ids << facility.id if facility&.persisted?

end
end

non_current_facilities = VaFacility.where.not(id: current_facility_ids).pluck(:official_station_name, :id).to_h
current_date = Date.today.strftime("%Y-%m-%d")
filename = "#{Rails.root}/lib/assets/non_current_facility_ids_#{current_date}.json"

File.open(filename, "w") do |file|
file.write(non_current_facilities.to_json)
end
puts "All VA facilities have been created or updated in the DB!"
puts "Non-current facility names and IDs saved to #{filename}."
end

task :delete_non_current_va_facilities => :environment do
current_date = Date.today.strftime("%Y-%m-%d")
file_path = "#{Rails.root}/lib/assets/non_current_facility_ids_#{current_date}.json"
output_file = "#{Rails.root}/lib/assets/deleted_and_non_deleted_facilities_#{current_date}.json"

if File.exist?(file_path)
non_current_facilities = JSON.parse(File.read(file_path))
deleted_facilities = {}
non_deleted_facilities = {}
deletion_count = 0

non_current_facilities.each do |name, id|
facility = VaFacility.find_by(id: id)

if facility
if facility.diffusion_histories.empty? && facility.practice_origin_facilities.empty?
# facility has no associated practices, proceed with deletion
facility.delete
deleted_facilities[name] = id
puts "Deleted facility: #{name}"
deletion_count += 1
else
# facility has associated practices, don't delete

# get practice names linked via diffusion_histories
diffusion_histories_practice_names = facility.diffusion_histories.includes(:practice).map do |history|
history.practice.name if history.practice.present?
end.compact

# get practice names linked via practice_origin_facilities
origin_facilities_practice_names = facility.practice_origin_facilities.includes(:practice).map do |origin_facility|
origin_facility.practice.name if origin_facility.practice.present?
end.compact

non_deleted_facilities[name] = {
id: id,
diffusion_histories_count: diffusion_histories_practice_names.count,
linked_innovations_via_diffusion_histories: diffusion_histories_practice_names,
origin_facilities_count: origin_facilities_practice_names.count,
linked_innovations_via_origin_facilities: origin_facilities_practice_names
}
end
end
end

File.open(output_file, "w") do |file|
file.write({
deleted_facilities: deleted_facilities,
non_deleted_facilities: non_deleted_facilities
}.to_json)
end

puts "#{deletion_count} non-current facilities have been deleted."
puts "Results saved to #{output_file}."
else
puts "No file found with non-current facility IDs for today: #{file_path}"
end
end

desc 'Fix bad data in the va_facilities table'
task :fix_data_va_facilities => :environment do
Expand Down

0 comments on commit 807956c

Please sign in to comment.