-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Early years provider import via Journey Configuration page
- Loading branch information
Showing
8 changed files
with
218 additions
and
1 deletion.
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,45 @@ | ||
module Admin | ||
class EligibleEyProvidersController < BaseAdminController | ||
before_action :ensure_service_operator | ||
|
||
helper_method :journey_configuration | ||
|
||
def create | ||
@download_form = EligibleEyProvidersForm.new | ||
@upload_form = EligibleEyProvidersForm.new(upload_params) | ||
|
||
if @upload_form.invalid? | ||
render "admin/journey_configurations/edit" | ||
else | ||
@upload_form.importer.run | ||
flash[:notice] = @upload_form.importer.results_message | ||
|
||
redirect_to edit_admin_journey_configuration_path(Journeys::EarlyYearsPayment::Provider::ROUTING_NAME, eligible_ey_providers_upload: {academic_year: @upload_form.academic_year}) | ||
end | ||
end | ||
|
||
def show | ||
@download_form = EligibleEyProvidersForm.new(download_params) | ||
|
||
send_data EligibleEyProvider.csv_for_academic_year(@download_form.academic_year), | ||
type: "text/csv", | ||
filename: "eligible_early_years_providers_#{@download_form.academic_year}.csv" | ||
end | ||
|
||
private | ||
|
||
def journey_configuration | ||
@journey_configuration ||= Journeys::Configuration.find_by( | ||
routing_name: Journeys::EarlyYearsPayment::Provider::ROUTING_NAME | ||
) | ||
end | ||
|
||
def upload_params | ||
params.require(:eligible_ey_providers_upload).permit(:academic_year, :file) | ||
end | ||
|
||
def download_params | ||
params.require(:eligible_ey_providers_download).permit(:academic_year) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class Admin::EligibleEyProvidersForm | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :academic_year, AcademicYear::Type.new | ||
attribute :file | ||
|
||
validates :file, | ||
presence: {message: "Choose a CSV file of eligible EY providers to upload"} | ||
|
||
validate :validate_importer_errors | ||
|
||
def select_options | ||
(0..2).map do |relative_year| | ||
academic_year = AcademicYear.current + relative_year | ||
OpenStruct.new(id: academic_year.to_s, name: academic_year) | ||
end | ||
end | ||
|
||
def importer | ||
@importer ||= EligibleEyProvidersImporter.new( | ||
file, | ||
academic_year | ||
) | ||
end | ||
|
||
private | ||
|
||
# importer is not activemodel::errors compliant | ||
def validate_importer_errors | ||
importer.errors.each do |error| | ||
errors.add(:file, error) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class EligibleEyProvider < ApplicationRecord | ||
attribute :academic_year, AcademicYear::Type.new | ||
|
||
belongs_to :local_authority | ||
|
||
def local_authority_code | ||
local_authority.try :code | ||
end | ||
|
||
def self.csv_for_academic_year(academic_year) | ||
csv_columns = { | ||
"Nursery Name" => :nursery_name, | ||
"EYURN / Ofsted URN" => :urn, | ||
"LA Code" => :local_authority_code, | ||
"Nursery Address" => :nursery_address, | ||
"Primary Key Contact Email Address" => :primary_key_contact_email_address, | ||
"Secondary Contact Email Address (Optional)" => :secondary_contact_email_address | ||
} | ||
|
||
CSV.generate(headers: true) do |csv| | ||
csv << csv_columns.keys | ||
|
||
where(academic_year:).each do |row| | ||
csv << csv_columns.values.map { |attr| row.send(attr) } | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class EligibleEyProvidersImporter < CsvImporter::Base | ||
import_options( | ||
target_data_model: EligibleEyProvider, | ||
transform_rows_with: :row_to_hash, | ||
mandatory_headers: [ | ||
"Nursery Name", | ||
"EYURN / Ofsted URN", | ||
"LA Code", | ||
"Nursery Address", | ||
"Primary Key Contact Email Address", | ||
"Secondary Contact Email Address (Optional)" | ||
] | ||
) | ||
|
||
attr_reader :academic_year | ||
|
||
def initialize(file, academic_year) | ||
super(file) | ||
|
||
@academic_year = academic_year | ||
end | ||
|
||
def results_message | ||
"Replaced #{deleted_row_count} existing providers with #{rows.count} new providers" | ||
end | ||
|
||
private | ||
|
||
def delete_all_scope | ||
target_data_model.where(academic_year:) | ||
end | ||
|
||
def row_to_hash(row) | ||
{ | ||
nursery_name: row.fetch("Nursery Name"), | ||
urn: row.fetch("EYURN / Ofsted URN"), | ||
local_authority_id: LocalAuthority.find_by(code: row.fetch("LA Code")).try(:id), | ||
nursery_address: row.fetch("Nursery Address"), | ||
primary_key_contact_email_address: row.fetch("Primary Key Contact Email Address"), | ||
secondary_contact_email_address: row.fetch("Secondary Contact Email Address (Optional)"), | ||
academic_year: | ||
} | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
app/views/admin/journey_configurations/_edit_early_years_payment_provider.html.erb
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,33 @@ | ||
<hr class="govuk-section-break govuk-section-break--m govuk-section-break--visible"> | ||
|
||
<h2 class="govuk-heading-m"> | ||
Download eligible EY providers | ||
</h2> | ||
|
||
<%= form_with model: @download_form, scope: :eligible_ey_providers_download, url: admin_eligible_ey_providers_path, builder: GOVUKDesignSystemFormBuilder::FormBuilder, method: :get do |f| %> | ||
<%= f.govuk_error_summary %> | ||
|
||
<%= f.govuk_collection_select :academic_year, f.object.select_options, :id, :name, | ||
label: { text: "Academic year" } %> | ||
|
||
<%= f.govuk_submit "Download CSV" %> | ||
<% end %> | ||
|
||
<hr class="govuk-section-break govuk-section-break--m govuk-section-break--visible"> | ||
|
||
<h2 class="govuk-heading-m"> | ||
Upload eligible EY providers | ||
</h2> | ||
|
||
<%= form_with model: @upload_form, scope: :eligible_ey_providers_upload, url: admin_eligible_ey_providers_path, builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> | ||
<%= f.govuk_error_summary %> | ||
|
||
<%= f.govuk_collection_select :academic_year, f.object.select_options, :id, :name, | ||
label: { text: "Academic year" } %> | ||
|
||
<%= f.govuk_file_field :file, | ||
label: { text: "Eligible EY providers" }, | ||
hint: { text: "This file should be a CSV" } %> | ||
|
||
<%= f.govuk_submit "Upload CSV" %> | ||
<% 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,16 @@ | ||
class CreateEligibleEyProviders < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :eligible_ey_providers, id: :uuid do |t| | ||
t.string :nursery_name | ||
t.string :urn | ||
t.references :local_authority, null: false, foreign_key: true, type: :uuid | ||
t.string :nursery_address | ||
t.string :primary_key_contact_email_address | ||
t.string :secondary_contact_email_address | ||
t.text :academic_year, limit: 9, null: false | ||
|
||
t.timestamps | ||
end | ||
add_index :eligible_ey_providers, [:academic_year, :urn] | ||
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