From 5d16055d5bf258bf4d9766dcc0f414c1b37c0537 Mon Sep 17 00:00:00 2001
From: vacabor <166112501+vacabor@users.noreply.github.com>
Date: Wed, 31 Jul 2024 16:35:24 +0100
Subject: [PATCH 1/2] WIP: Early years provider import via Journey
Configuration page
---
.../admin/eligible_ey_providers_controller.rb | 45 +++++++++++++++++++
app/forms/admin/eligible_ey_providers_form.rb | 35 +++++++++++++++
app/models/eligible_ey_provider.rb | 28 ++++++++++++
app/models/eligible_ey_providers_importer.rb | 44 ++++++++++++++++++
...edit_early_years_payment_provider.html.erb | 33 ++++++++++++++
config/routes.rb | 1 +
...0731152713_create_eligible_ey_providers.rb | 16 +++++++
db/schema.rb | 17 ++++++-
8 files changed, 218 insertions(+), 1 deletion(-)
create mode 100644 app/controllers/admin/eligible_ey_providers_controller.rb
create mode 100644 app/forms/admin/eligible_ey_providers_form.rb
create mode 100644 app/models/eligible_ey_provider.rb
create mode 100644 app/models/eligible_ey_providers_importer.rb
create mode 100644 app/views/admin/journey_configurations/_edit_early_years_payment_provider.html.erb
create mode 100644 db/migrate/20240731152713_create_eligible_ey_providers.rb
diff --git a/app/controllers/admin/eligible_ey_providers_controller.rb b/app/controllers/admin/eligible_ey_providers_controller.rb
new file mode 100644
index 0000000000..d62fc47df6
--- /dev/null
+++ b/app/controllers/admin/eligible_ey_providers_controller.rb
@@ -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
diff --git a/app/forms/admin/eligible_ey_providers_form.rb b/app/forms/admin/eligible_ey_providers_form.rb
new file mode 100644
index 0000000000..03ad09c1f3
--- /dev/null
+++ b/app/forms/admin/eligible_ey_providers_form.rb
@@ -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
diff --git a/app/models/eligible_ey_provider.rb b/app/models/eligible_ey_provider.rb
new file mode 100644
index 0000000000..3a43175ea5
--- /dev/null
+++ b/app/models/eligible_ey_provider.rb
@@ -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
diff --git a/app/models/eligible_ey_providers_importer.rb b/app/models/eligible_ey_providers_importer.rb
new file mode 100644
index 0000000000..a911d1cdf2
--- /dev/null
+++ b/app/models/eligible_ey_providers_importer.rb
@@ -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
diff --git a/app/views/admin/journey_configurations/_edit_early_years_payment_provider.html.erb b/app/views/admin/journey_configurations/_edit_early_years_payment_provider.html.erb
new file mode 100644
index 0000000000..bcf7b1320c
--- /dev/null
+++ b/app/views/admin/journey_configurations/_edit_early_years_payment_provider.html.erb
@@ -0,0 +1,33 @@
+
+
+
+ Download eligible EY providers
+
+
+<%= 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 %>
+
+
+
+
+ Upload eligible EY providers
+
+
+<%= 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 %>
diff --git a/config/routes.rb b/config/routes.rb
index 59f7a00d71..9362d49dfa 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -113,6 +113,7 @@ def matches?(request)
resources :journey_configurations, only: [:index, :edit, :update]
resources :levelling_up_premium_payments_awards, only: [:index, :create]
+ resource :eligible_ey_providers, only: [:create, :show], path: "eligible-early-years-providers"
resource :eligible_fe_providers, only: [:create, :show], path: "eligible-further-education-providers"
get "refresh-session", to: "sessions#refresh", as: :refresh_session
diff --git a/db/migrate/20240731152713_create_eligible_ey_providers.rb b/db/migrate/20240731152713_create_eligible_ey_providers.rb
new file mode 100644
index 0000000000..d2fa47f29b
--- /dev/null
+++ b/db/migrate/20240731152713_create_eligible_ey_providers.rb
@@ -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
diff --git a/db/schema.rb b/db/schema.rb
index c282dd5529..e94210fb76 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[7.0].define(version: 2024_07_24_092519) do
+ActiveRecord::Schema[7.0].define(version: 2024_07_31_152713) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "pgcrypto"
@@ -180,6 +180,20 @@
t.datetime "updated_at", null: false
end
+ create_table "eligible_ey_providers", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
+ t.string "nursery_name"
+ t.string "urn"
+ t.uuid "local_authority_id", null: false
+ t.string "nursery_address"
+ t.string "primary_key_contact_email_address"
+ t.string "secondary_contact_email_address"
+ t.text "academic_year", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["academic_year", "urn"], name: "index_eligible_ey_providers_on_academic_year_and_urn"
+ t.index ["local_authority_id"], name: "index_eligible_ey_providers_on_local_authority_id"
+ end
+
create_table "eligible_fe_providers", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.integer "ukprn", null: false
t.text "academic_year", null: false
@@ -501,6 +515,7 @@
add_foreign_key "claims", "journeys_sessions"
add_foreign_key "decisions", "dfe_sign_in_users", column: "created_by_id"
add_foreign_key "early_career_payments_eligibilities", "schools", column: "current_school_id"
+ add_foreign_key "eligible_ey_providers", "local_authorities"
add_foreign_key "international_relocation_payments_eligibilities", "schools", column: "current_school_id"
add_foreign_key "levelling_up_premium_payments_eligibilities", "schools", column: "current_school_id"
add_foreign_key "notes", "claims"
From d8be9a2969104ccc488a6696baaebcb2a035eb30 Mon Sep 17 00:00:00 2001
From: vacabor <166112501+vacabor@users.noreply.github.com>
Date: Thu, 1 Aug 2024 20:53:48 +0100
Subject: [PATCH 2/2] Remove academic year from EY import/export
---
.../admin/eligible_ey_providers_controller.rb | 15 +--
app/forms/admin/eligible_ey_providers_form.rb | 13 +--
app/models/eligible_ey_provider.rb | 6 +-
app/models/eligible_ey_providers_importer.rb | 15 +--
...edit_early_years_payment_provider.html.erb | 12 +--
config/analytics_blocklist.yml | 10 ++
...0731152713_create_eligible_ey_providers.rb | 3 +-
db/schema.rb | 3 +-
spec/factories/eligible_ey_providers.rb | 11 +++
.../admin/eligible_ey_providers_spec.rb | 37 ++++++++
.../eligible_ey_providers_importer_spec.rb | 91 +++++++++++++++++++
11 files changed, 160 insertions(+), 56 deletions(-)
create mode 100644 spec/factories/eligible_ey_providers.rb
create mode 100644 spec/features/admin/eligible_ey_providers_spec.rb
create mode 100644 spec/models/eligible_ey_providers_importer_spec.rb
diff --git a/app/controllers/admin/eligible_ey_providers_controller.rb b/app/controllers/admin/eligible_ey_providers_controller.rb
index d62fc47df6..45c2f2ccaa 100644
--- a/app/controllers/admin/eligible_ey_providers_controller.rb
+++ b/app/controllers/admin/eligible_ey_providers_controller.rb
@@ -5,7 +5,6 @@ class EligibleEyProvidersController < BaseAdminController
helper_method :journey_configuration
def create
- @download_form = EligibleEyProvidersForm.new
@upload_form = EligibleEyProvidersForm.new(upload_params)
if @upload_form.invalid?
@@ -14,16 +13,14 @@ def create
@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})
+ redirect_to edit_admin_journey_configuration_path(Journeys::EarlyYearsPayment::Provider::ROUTING_NAME)
end
end
def show
- @download_form = EligibleEyProvidersForm.new(download_params)
-
- send_data EligibleEyProvider.csv_for_academic_year(@download_form.academic_year),
+ send_data EligibleEyProvider.csv,
type: "text/csv",
- filename: "eligible_early_years_providers_#{@download_form.academic_year}.csv"
+ filename: "eligible_early_years_providers.csv"
end
private
@@ -35,11 +32,7 @@ def journey_configuration
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)
+ params.require(:eligible_ey_providers_upload).permit(:file)
end
end
end
diff --git a/app/forms/admin/eligible_ey_providers_form.rb b/app/forms/admin/eligible_ey_providers_form.rb
index 03ad09c1f3..0e4e63335e 100644
--- a/app/forms/admin/eligible_ey_providers_form.rb
+++ b/app/forms/admin/eligible_ey_providers_form.rb
@@ -2,7 +2,6 @@ class Admin::EligibleEyProvidersForm
include ActiveModel::Model
include ActiveModel::Attributes
- attribute :academic_year, AcademicYear::Type.new
attribute :file
validates :file,
@@ -10,18 +9,8 @@ class Admin::EligibleEyProvidersForm
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
- )
+ @importer ||= EligibleEyProvidersImporter.new(file)
end
private
diff --git a/app/models/eligible_ey_provider.rb b/app/models/eligible_ey_provider.rb
index 3a43175ea5..99f8c3008f 100644
--- a/app/models/eligible_ey_provider.rb
+++ b/app/models/eligible_ey_provider.rb
@@ -1,13 +1,11 @@
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)
+ def self.csv
csv_columns = {
"Nursery Name" => :nursery_name,
"EYURN / Ofsted URN" => :urn,
@@ -20,7 +18,7 @@ def self.csv_for_academic_year(academic_year)
CSV.generate(headers: true) do |csv|
csv << csv_columns.keys
- where(academic_year:).each do |row|
+ all.each do |row|
csv << csv_columns.values.map { |attr| row.send(attr) }
end
end
diff --git a/app/models/eligible_ey_providers_importer.rb b/app/models/eligible_ey_providers_importer.rb
index a911d1cdf2..afe36acb92 100644
--- a/app/models/eligible_ey_providers_importer.rb
+++ b/app/models/eligible_ey_providers_importer.rb
@@ -12,24 +12,12 @@ class EligibleEyProvidersImporter < CsvImporter::Base
]
)
- 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"),
@@ -37,8 +25,7 @@ def row_to_hash(row)
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:
+ secondary_contact_email_address: row.fetch("Secondary Contact Email Address (Optional)")
}
end
end
diff --git a/app/views/admin/journey_configurations/_edit_early_years_payment_provider.html.erb b/app/views/admin/journey_configurations/_edit_early_years_payment_provider.html.erb
index bcf7b1320c..ad4ad641de 100644
--- a/app/views/admin/journey_configurations/_edit_early_years_payment_provider.html.erb
+++ b/app/views/admin/journey_configurations/_edit_early_years_payment_provider.html.erb
@@ -4,14 +4,7 @@
Download eligible EY providers
-<%= 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 %>
+<%= link_to "Download CSV", admin_eligible_ey_providers_path, class: "govuk-button" %>
@@ -22,9 +15,6 @@
<%= 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" } %>
diff --git a/config/analytics_blocklist.yml b/config/analytics_blocklist.yml
index 25266fd2e7..0fbca5150c 100644
--- a/config/analytics_blocklist.yml
+++ b/config/analytics_blocklist.yml
@@ -49,6 +49,16 @@
- family_name
- email
- session_token
+ :eligible_ey_providers:
+ - id
+ - nursery_name
+ - urn
+ - local_authority_id
+ - nursery_address
+ - primary_key_contact_email_address
+ - secondary_contact_email_address
+ - created_at
+ - updated_at
:schools:
- postcode_sanitised
:file_uploads:
diff --git a/db/migrate/20240731152713_create_eligible_ey_providers.rb b/db/migrate/20240731152713_create_eligible_ey_providers.rb
index d2fa47f29b..df7fc8bc28 100644
--- a/db/migrate/20240731152713_create_eligible_ey_providers.rb
+++ b/db/migrate/20240731152713_create_eligible_ey_providers.rb
@@ -7,10 +7,9 @@ def change
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]
+ add_index :eligible_ey_providers, :urn
end
end
diff --git a/db/schema.rb b/db/schema.rb
index e94210fb76..cf52a5dc40 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -187,11 +187,10 @@
t.string "nursery_address"
t.string "primary_key_contact_email_address"
t.string "secondary_contact_email_address"
- t.text "academic_year", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
- t.index ["academic_year", "urn"], name: "index_eligible_ey_providers_on_academic_year_and_urn"
t.index ["local_authority_id"], name: "index_eligible_ey_providers_on_local_authority_id"
+ t.index ["urn"], name: "index_eligible_ey_providers_on_urn"
end
create_table "eligible_fe_providers", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
diff --git a/spec/factories/eligible_ey_providers.rb b/spec/factories/eligible_ey_providers.rb
new file mode 100644
index 0000000000..a5b19c7210
--- /dev/null
+++ b/spec/factories/eligible_ey_providers.rb
@@ -0,0 +1,11 @@
+FactoryBot.define do
+ factory :eligible_ey_provider do
+ association :local_authority
+
+ nursery_name { Faker::Company.name }
+ urn { rand(10_000_000..99_999_999) }
+ nursery_address { Faker::Address.full_address }
+ primary_key_contact_email_address { Faker::Internet.email }
+ secondary_contact_email_address { [Faker::Internet.email, nil].sample }
+ end
+end
diff --git a/spec/features/admin/eligible_ey_providers_spec.rb b/spec/features/admin/eligible_ey_providers_spec.rb
new file mode 100644
index 0000000000..9389395ba3
--- /dev/null
+++ b/spec/features/admin/eligible_ey_providers_spec.rb
@@ -0,0 +1,37 @@
+require "rails_helper"
+
+RSpec.feature "Admin of eligible ey providers" do
+ scenario "manage eligible ey providers" do
+ when_early_years_payment_provider_journey_configuration_exists
+ sign_in_as_service_operator
+
+ click_link "Manage services"
+ click_link "Change Claim an early years financial incentive payment - provider"
+
+ attach_file "eligible-ey-providers-upload-file-field", eligible_ey_providers_csv_file.path
+ click_button "Upload CSV"
+
+ click_link "Download CSV"
+
+ downloaded_csv = page.body
+
+ expect(downloaded_csv).to eql(eligible_ey_providers_csv_file.read)
+ end
+
+ def eligible_ey_providers_csv_file
+ return @eligible_ey_providers_csv_file if @eligible_ey_providers_csv_file
+
+ create(:local_authority, code: "101")
+
+ @eligible_ey_providers_csv_file = Tempfile.new
+ @eligible_ey_providers_csv_file.write <<~CSV
+ Nursery Name,EYURN / Ofsted URN,LA Code,Nursery Address,Primary Key Contact Email Address,Secondary Contact Email Address (Optional)
+ First Nursery,1000001,101,"1 Test Street, Test Town, TE1 1ST",primary@example.com,secondary@example.com
+ Second Nursery,1000002,101,"2 Test Street, Test Town, TE1 1ST",primary@example.com,
+ Third Nursery,1000003,101,"3 Test Street, Test Town, TE1 1ST",other@example.com,
+ CSV
+ @eligible_ey_providers_csv_file.rewind
+
+ @eligible_ey_providers_csv_file
+ end
+end
diff --git a/spec/models/eligible_ey_providers_importer_spec.rb b/spec/models/eligible_ey_providers_importer_spec.rb
new file mode 100644
index 0000000000..7260380cbf
--- /dev/null
+++ b/spec/models/eligible_ey_providers_importer_spec.rb
@@ -0,0 +1,91 @@
+require "rails_helper"
+
+RSpec.describe EligibleEyProvidersImporter do
+ subject { described_class.new(file) }
+
+ let(:file) { Tempfile.new }
+ let(:correct_headers) { described_class.mandatory_headers.join(",") + "\n" }
+ let(:local_authority) { create(:local_authority) }
+
+ def to_row(hash)
+ [
+ "\"#{hash[:nursery_name]}\"",
+ hash[:urn],
+ local_authority.code,
+ "\"#{hash[:nursery_address]}\"",
+ hash[:primary_key_contact_email_address],
+ hash[:secondary_contact_email_address]
+ ].join(",") + "\n"
+ end
+
+ describe "#run" do
+ context "when incorrect headers" do
+ before do
+ file.write "incorrect,headers,here,here"
+ file.close
+ end
+
+ it "has errors" do
+ subject.run
+
+ expect(subject.errors).to be_present
+ expect(subject.errors).to include("The selected file is missing some expected columns: Nursery Name, EYURN / Ofsted URN, LA Code, Nursery Address, Primary Key Contact Email Address, Secondary Contact Email Address (Optional)")
+ end
+ end
+
+ context "when csv has no rows" do
+ before do
+ file.write correct_headers
+ file.close
+ end
+
+ it "has no errors" do
+ subject.run
+
+ expect(subject.errors).to be_empty
+ end
+
+ it "does not add any any records" do
+ expect { subject.run }.not_to change { EligibleEyProvider.count }
+ end
+
+ context "when there are existing records" do
+ before do
+ create(:eligible_ey_provider, local_authority:)
+ create(:eligible_ey_provider, local_authority:)
+ end
+
+ it "deletes existing records" do
+ expect { subject.run }.to change { EligibleEyProvider.count }.to(0)
+ end
+ end
+ end
+
+ context "with valid data" do
+ before do
+ file.write correct_headers
+
+ 3.times do
+ file.write to_row(attributes_for(:eligible_ey_provider))
+ end
+
+ file.close
+ end
+
+ it "imports new records" do
+ expect { subject.run }.to change { EligibleEyProvider.count }.by(3)
+ end
+
+ context "when there are existing records" do
+ before do
+ create(:eligible_ey_provider, local_authority:)
+ create(:eligible_ey_provider, local_authority:)
+ end
+
+ it "deletes them with new records" do
+ expect { subject.run }.to change { EligibleEyProvider.count }.by(1)
+ end
+ end
+ end
+ end
+end