-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #772 from DFE-Digital/bulk-search
Enable searching for multiple records
- Loading branch information
Showing
31 changed files
with
616 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TRN,Date of birth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module CheckRecords | ||
class BulkSearchesController < CheckRecordsController | ||
def new | ||
@bulk_search = BulkSearch.new | ||
@organisation_name = current_dsi_user.dsi_user_sessions.last&.organisation_name | ||
end | ||
|
||
def create | ||
@bulk_search = BulkSearch.new(file: bulk_search_params[:file]) | ||
@total, @results, @not_found = @bulk_search.call | ||
unless @results | ||
render :new | ||
end | ||
end | ||
|
||
private | ||
|
||
def bulk_search_params | ||
params.require(:bulk_search).permit(:file) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export default class { | ||
start() { | ||
this.toggleSubmitButton({ context: document.querySelector("div#bulk_search") }); | ||
} | ||
|
||
toggleSubmitButton({ context = document }) { | ||
document.addEventListener("DOMContentLoaded", () => { | ||
const fileInput = context.querySelector("input[type='file']"); | ||
const submitButton = context.querySelector("button[type='submit']"); | ||
|
||
fileInput.addEventListener("change", function () { | ||
if (fileInput.files.length > 0) { | ||
submitButton.removeAttribute("disabled"); | ||
} else { | ||
submitButton.setAttribute("disabled", "disabled"); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
require 'csv' | ||
|
||
class BulkSearch | ||
include ActiveModel::Model | ||
|
||
attr_accessor :file | ||
|
||
validates :file, presence: true | ||
validate :header_row_present?, if: -> { file.present? } | ||
validate :all_rows_have_data, if: -> { file.present? } | ||
|
||
def call | ||
return false if invalid? | ||
|
||
[total, results, not_found] | ||
end | ||
|
||
def not_found | ||
@not_found ||= csv.map { |row| | ||
next if results.any? { |teacher| teacher.trn == row["TRN"] } | ||
|
||
Hashie::Mash.new(trn: row["TRN"], date_of_birth: Date.parse(row["Date of birth"])) | ||
}.compact | ||
end | ||
|
||
def results | ||
@results ||= response["results"].map do |teacher| | ||
QualificationsApi::Teacher.new(teacher) | ||
end | ||
end | ||
|
||
def total | ||
@total ||= response["total"] | ||
end | ||
|
||
private | ||
|
||
def all_rows_have_data | ||
csv.each_with_index do |row, index| | ||
if row["Date of birth"].blank? | ||
errors.add(:file, "The selected file does not have a date of birth in row #{index + 1}") | ||
else | ||
begin | ||
Date.parse(row["Date of birth"]) | ||
rescue Date::Error | ||
errors.add( | ||
:file, | ||
"The date of birth in row #{index + 1} must be in DD/MM/YYYY format" | ||
) | ||
end | ||
end | ||
|
||
if row["TRN"].blank? | ||
errors.add(:file, "The selected file does not have a TRN in row #{index + 1}") | ||
end | ||
end | ||
end | ||
|
||
def csv | ||
@csv ||= CSV.parse(file.read, headers: true) | ||
end | ||
|
||
def header_row_present? | ||
expected_headers = ["TRN", "Date of birth"] | ||
return if csv.headers == expected_headers | ||
|
||
errors.add(:file, "The selected file must use the template") | ||
end | ||
|
||
def find_all(queries) | ||
search_client.bulk_teachers(queries:) | ||
end | ||
|
||
def response | ||
@response ||= begin | ||
queries = csv.map { |row| { trn: row["TRN"], dateOfBirth: row["Date of birth"] } }.compact | ||
find_all(queries) | ||
end | ||
end | ||
|
||
def search_client | ||
@search_client ||= QualificationsApi::Client.new( | ||
token: ENV["QUALIFICATIONS_API_FIXED_TOKEN"] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<% if @organisation_name %> | ||
<div class="govuk-caption-m govuk-!-margin-bottom-4"> | ||
You're signed in as <%= @organisation_name %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<% content_for :page_title, "Find multiple records" %> | ||
<% content_for :breadcrumbs do %> | ||
<%= govuk_breadcrumbs(breadcrumbs: { "Home" => check_records_search_path, "Find multiple records" => new_check_records_bulk_search_path, "Results" => nil }) %> | ||
<%= render ActionAtComponent.new(action: "searched") %> | ||
<% end %> | ||
|
||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
<h1 class="govuk-heading-l"><%= pluralize(@total, 'teacher record') %> found</h1> | ||
<%= govuk_tabs do |tabs| | ||
if @results.any? | ||
tabs.with_tab(label: "Records found", id: "records-found") do %> | ||
<%= render "check_records/search/results_table", teachers: @results %> | ||
<% end %> | ||
<% end %> | ||
<% if @not_found.any? %> | ||
<% tabs.with_tab(label: "Not found", id: "records-not-found") do %> | ||
<%= render "check_records/search/not_found", not_found: @not_found %> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
<%= govuk_link_to "Search again", new_check_records_bulk_search_path %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<% content_for :page_title, "Find multiple records" %> | ||
|
||
<div class="govuk-grid-row" id="bulk_search"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
<%= render 'check_records/organisation_name' %> | ||
|
||
<h1 class="govuk-heading-l">Find multiple teacher records</h1> | ||
|
||
<%= form_with model: [:check_records, @bulk_search] do |f| %> | ||
<%= f.govuk_error_summary %> | ||
<%= govuk_list spaced: true, type: :number do %> | ||
<li> | ||
<h3>Download the CSV template.</h3> | ||
<p class="govuk-body"><%= govuk_link_to "Download CSV template file", asset_path("multiple_records.csv") %></p> | ||
</li> | ||
<li> | ||
<h3>Add each person's details</h3> | ||
<p class="govuk-body"> | ||
Open the CSV file and add the teacher reference number (TRN) and date of birth for each | ||
person you want to find a record for. | ||
</p> | ||
<p class="govuk-body"> | ||
The TRN must be 7 digits and the date of birth | ||
must be in DD/MM/YYYY format. | ||
</p> | ||
<p class="govuk-body"> | ||
You can add a maximum of 20 people. | ||
</p> | ||
</li> | ||
<li> | ||
<h3>Upload file</h3> | ||
<p class="govuk-body">We'll tell you if we find any teacher records.</p> | ||
</li> | ||
<% end %> | ||
<%= f.govuk_file_field :file, class: "govuk-input--width-20", label: { class: "govuk-label--s", text: "Upload file" }, hint: { text: "File type must be a CSV" }, required: true %> | ||
<%= f.govuk_submit "Find records" %> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div class="no-results--table"> | ||
<%= govuk_table do |table| | ||
table.with_caption(size: 'm', text: 'No records found') | ||
|
||
table.with_head do |head| | ||
head.with_row do |row| | ||
row.with_cell(text: 'TRN') | ||
row.with_cell(text: 'Date of birth') | ||
end | ||
end | ||
|
||
table.with_body do |body| | ||
not_found.each do |record| | ||
body.with_row do |row| | ||
row.with_cell(text: record.trn) | ||
row.with_cell(text: record.date_of_birth.to_fs(:long_uk)) | ||
end | ||
end | ||
end | ||
end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,4 @@ | |
end %> | ||
</div> | ||
<% end %> | ||
</div> | ||
|
||
</div> |
Oops, something went wrong.