Skip to content

Commit

Permalink
Update HESA placements import
Browse files Browse the repository at this point in the history
- support NOT_APPLICABLE_SCHOOL_URNS
- provide feedback from rake task
  • Loading branch information
tomtrentham committed Jan 29, 2025
1 parent c5e348e commit 50ec00a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
37 changes: 29 additions & 8 deletions app/services/placements/import_from_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,48 @@ module Placements
class ImportFromCsv
include ServicePattern

attr_reader :upload, :unmatched_hesa_ids, :unmatched_urns

def initialize(upload_id:)
@upload = Upload.find(upload_id)
@unmatched_hesa_ids = []
@unmatched_urns = []
end

# Uploaded CSV file should contain hesa_id and urn headers only
def call
table = CSV.parse(upload.file.download, headers: true)
table.each do |row|
next unless (school = School.find_by(urn: row["urn"]))
next unless (trainee = matching_trainee_in_previous_cycle(row["hesa_id"]))
# Uploaded CSV file should contain hesa_id and urn headers only
placement_data = CSV.parse(upload.file.download, headers: true, header_converters: :symbol).first(20000)

placement_data.each do |row|
if (trainee = matching_trainee_in_previous_cycle(row[:hesa_id]))
urn = row[:urn]
school = School.find_by(urn:)

Placement.find_or_create_by(trainee:, school:)
if school.present?
Placement.find_or_create_by(trainee:, school:)
elsif valid_unknown_school_urn?(urn)
# Update name separately to avoid unique constraint violation with index on trainee_id and urn
Placement.find_or_create_by(trainee:, urn:).update(name: I18n.t("components.placement_detail.magic_urn.#{urn}"))
else
unmatched_urns << urn
end
else
unmatched_hesa_ids << row[:hesa_id]
end
end

self
end

private

attr_reader :upload

def matching_trainee_in_previous_cycle(hesa_id)
AcademicCycle.previous.total_trainees.find_by(hesa_id:)
end

def valid_unknown_school_urn?(urn)
# Check if the urn is one of the HESA codes for not applicable school URNs
urn.in?(Trainees::CreateFromHesa::NOT_APPLICABLE_SCHOOL_URNS)
end
end
end
17 changes: 16 additions & 1 deletion lib/tasks/import_hesa_placements.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ namespace :import_hesa_placements do
desc "Import HESA placements from uploaded CSV"
task :from_upload, [:upload_id] => :environment do |_, args|
upload_id = args.upload_id.to_i
Placements::ImportFromCsv.call(upload_id:)
import = Placements::ImportFromCsv.call(upload_id:)

puts("")
puts("HESA ids which don't match any trainees in Register:")
import.unmatched_hesa_ids.uniq.each do |hesa_id|
puts(" hesa_id: #{hesa_id}")
end

puts("")
puts("URNs which don't match any Schools in Register:")
import.unmatched_urns.uniq.each do |urn|
puts(" URN: #{urn}")
end

puts("")
puts("Total number of rows not imported: #{(import.unmatched_hesa_ids.count + import.unmatched_urns.count).to_fs(:delimited)}")
end
end

0 comments on commit 50ec00a

Please sign in to comment.