diff --git a/app/services/bulk_upload/lettings/validator.rb b/app/services/bulk_upload/lettings/validator.rb index e60435f75..bc415f3dd 100644 --- a/app/services/bulk_upload/lettings/validator.rb +++ b/app/services/bulk_upload/lettings/validator.rb @@ -38,6 +38,8 @@ class BulkUpload::Lettings::Validator ) end end + + update_expected_log_count end def create_logs? @@ -62,6 +64,14 @@ class BulkUpload::Lettings::Validator private + def update_expected_log_count + bulk_upload.update!(expected_log_count:) + end + + def expected_log_count + row_parsers.count { |row| !row.blank_row? } + end + def over_column_error_threshold? fields = ("field_1".."field_134").to_a percentage_threshold = (row_parsers.size * COLUMN_PERCENTAGE_ERROR_THRESHOLD).ceil diff --git a/db/migrate/20230320163945_add_expected_log_count.rb b/db/migrate/20230320163945_add_expected_log_count.rb new file mode 100644 index 000000000..ca77fe012 --- /dev/null +++ b/db/migrate/20230320163945_add_expected_log_count.rb @@ -0,0 +1,5 @@ +class AddExpectedLogCount < ActiveRecord::Migration[7.0] + def change + add_column :bulk_uploads, :expected_log_count, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 225129dfd..463d7da59 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: 2023_03_20_084057) do +ActiveRecord::Schema[7.0].define(version: 2023_03_20_163945) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -39,6 +39,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_20_084057) do t.datetime "updated_at", null: false t.text "filename" t.integer "needstype" + t.integer "expected_log_count" t.index ["identifier"], name: "index_bulk_uploads_on_identifier", unique: true t.index ["user_id"], name: "index_bulk_uploads_on_user_id" end diff --git a/spec/services/bulk_upload/lettings/validator_spec.rb b/spec/services/bulk_upload/lettings/validator_spec.rb index f7048fa27..1b77f8f43 100644 --- a/spec/services/bulk_upload/lettings/validator_spec.rb +++ b/spec/services/bulk_upload/lettings/validator_spec.rb @@ -69,6 +69,10 @@ RSpec.describe BulkUpload::Lettings::Validator do expect(error.category).to eql("setup") end + + it "updates expected_log_count" do + expect { validator.call }.to change(bulk_upload, :expected_log_count).from(nil).to(3) + end end context "with arbitrary ordered 23/24 csv" do