Skip to content

Commit

Permalink
Limit De-identified client uploads to selected agency
Browse files Browse the repository at this point in the history
  • Loading branch information
awisema committed Jun 12, 2024
1 parent fd08bd6 commit 209af9c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 168 deletions.
20 changes: 15 additions & 5 deletions app/controllers/deidentified_clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,28 @@ def destroy
end

def choose_upload
@upload = DeidentifiedClientsXlsx.new
@upload = DeidentifiedClientsXlsx.new(agency_id: current_user.agency_id)
end

def import
unless params[:deidentified_clients_xlsx]&.[](:file)
@upload = DeidentifiedClientsXlsx.new
@upload = DeidentifiedClientsXlsx.new(agency_id: current_user.agency_id)
flash[:alert] = Translation.translate('You must attach a file in the form.')
render :choose_upload
return
end

file = import_params[:file]
agency = Agency.find(import_params[:agency_id].to_i)
update_availability = import_params[:update_availability].to_s.in?(['1', 'true'])

unless agency.present?
@upload = DeidentifiedClientsXlsx.new(agency_id: current_user.agency_id)
flash[:alert] = Translation.translate('You must select a valid agency')
render :choose_upload
return
end

begin
@upload = DeidentifiedClientsXlsx.create(
filename: file.original_filename,
Expand All @@ -65,20 +74,20 @@ def import
content: file.read,
)
rescue StandardError
@upload = DeidentifiedClientsXlsx.new
@upload = DeidentifiedClientsXlsx.new(agency_id: current_user.agency_id)
flash[:alert] = Translation.translate('Cannot read uploaded file, is it an XLSX?')
render :choose_upload
return
end

unless @upload.valid_header?
@upload = DeidentifiedClientsXlsx.new
@upload = DeidentifiedClientsXlsx.new(agency_id: current_user.agency_id)
flash[:alert] = Translation.translate('Uploaded file does not have the correct header. Incorrect file?')
render :choose_upload
return
end

@upload.import(current_user.agency, update_availability: update_availability)
@upload.import(agency, update_availability: update_availability)
end

def assessment_type
Expand Down Expand Up @@ -232,6 +241,7 @@ def filter_terms
private def import_params
params.require(:deidentified_clients_xlsx).permit(
:file,
:agency_id,
:update_availability,
)
end
Expand Down
10 changes: 7 additions & 3 deletions app/models/deidentified_clients_xlsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

class DeidentifiedClientsXlsx < ApplicationRecord
mount_uploader :file, DeidentifiedClientsXlsxFileUploader
attr_accessor :update_availability
attr_accessor :agency_id, :update_availability
attr_reader :added, :touched, :problems, :clients

def agency_options_for_select
Agency.order(name: :asc).pluck(:name, :id).to_h
end

def valid_header?
parse_xlsx unless @xlsx

Expand All @@ -24,13 +28,13 @@ def import(agency, update_availability: false)

return unless valid_header?

DeidentifiedClient.update_all(available: false) if @update_availability
DeidentifiedClient.where(agency: agency).update_all(available: false) if @update_availability

@xlsx.each_with_index do |raw, index|
next if skip?(raw, index)

row = Hash[file_attributes.keys.zip(raw)]
client = DeidentifiedClient.where(client_identifier: row[:client_identifier]).first_or_initialize
client = DeidentifiedClient.where(agency: agency, client_identifier: row[:client_identifier]).first_or_initialize
@clients << client
cleaned = begin
clean_row(client, row)
Expand Down
17 changes: 10 additions & 7 deletions app/views/deidentified_clients/choose_upload.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
%ul
- DeidentifiedClientsXlsx.file_header.each do |label|
%ul= label
= simple_form_for @upload, url: import_deidentified_clients_path, method: :post do |f|
= f.error_notification
.form-inputs
= f.input :file, as: :file, label: false
= f.input :update_availability, as: :boolean, label: 'Update client availability', hint: 'Make only the clients that appear in the selected file available'
.form-actions
= f.button :submit, value: 'Upload Excel'
.row
.col-sm-6
= simple_form_for @upload, url: import_deidentified_clients_path, method: :post do |f|
= f.error_notification
.form-inputs
= f.input :file, as: :file, label: false
= f.input :agency_id, as: :select_2, collection: @upload.agency_options_for_select, required: true
= f.input :update_availability, as: :boolean, label: 'Update client availability', hint: 'Make only the clients in the selected agency that appear in the selected file available'
.form-actions
= f.button :submit, value: 'Upload Excel'
Loading

0 comments on commit 209af9c

Please sign in to comment.