Browse Source
* add start of bulk upload logs journey * split upload controller into 2 * add year page to bulk upload journey * bulk upload years now dynamic * add placeholder for upload your file page * handle bulk upload when not in crossover * add bulk upload csv user journey * bulk upload now persists and sends to S3 * swap ruby-filemagic for file command * match csv or text file for validation * in_crossover_period? now uses overlap of forms - also moved from Form to FormHandler * add production env var for CSV bucket * stub kernel call methods * remove duplicate env var * hardcode env var for review apps * move feature toggle to FeatureToggle * crossover period checks now specific to the form * fix typo in bulk upload journeypull/1070/head v0.2.25
Phil Lee
2 years ago
committed by
GitHub
35 changed files with 837 additions and 267 deletions
@ -1,197 +1,13 @@
|
||||
class BulkUpload |
||||
include ActiveModel::Model |
||||
include ActiveModel::Validations |
||||
include ActiveModel::Conversion |
||||
class BulkUpload < ApplicationRecord |
||||
enum log_type: { lettings: "lettings", sales: "sales" } |
||||
|
||||
SPREADSHEET_CONTENT_TYPES = %w[ |
||||
application/vnd.ms-excel |
||||
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
||||
].freeze |
||||
belongs_to :user |
||||
|
||||
FIRST_DATA_ROW = 7 |
||||
after_initialize :generate_identifier, unless: :identifier |
||||
|
||||
def initialize(file, content_type) |
||||
@file = file |
||||
@content_type = content_type |
||||
end |
||||
|
||||
def process(current_user) |
||||
return unless valid_content_type? |
||||
|
||||
xlsx = Roo::Spreadsheet.open(@file, extension: :xlsx) |
||||
sheet = xlsx.sheet(0) |
||||
last_row = sheet.last_row |
||||
if last_row < FIRST_DATA_ROW |
||||
errors.add(:lettings_log_bulk_upload, "No data found") |
||||
else |
||||
data_range = FIRST_DATA_ROW..last_row |
||||
data_range.map do |row_num| |
||||
row = sheet.row(row_num) |
||||
# owning_organisation = Organisation.find(row[111]) |
||||
# managing_organisation = Organisation.find(row[113]) |
||||
lettings_log = LettingsLog.create!( |
||||
owning_organisation: current_user.organisation, |
||||
managing_organisation: current_user.organisation, |
||||
created_by: current_user, |
||||
) |
||||
map_row(row).each do |attr_key, attr_val| |
||||
_update = lettings_log.update(attr_key => attr_val) |
||||
rescue ArgumentError |
||||
# TODO: determine what we want to do when bulk upload contains totally invalid data for a field. |
||||
end |
||||
end |
||||
end |
||||
end |
||||
|
||||
def valid_content_type? |
||||
if SPREADSHEET_CONTENT_TYPES.include?(@content_type) |
||||
true |
||||
else |
||||
errors.add(:lettings_log_bulk_upload, "Invalid file type") |
||||
false |
||||
end |
||||
end |
||||
|
||||
def map_row(row) |
||||
{ |
||||
lettype: row[1], |
||||
# reg_num_la_core_code: row[3], |
||||
# managementgroup: row[4], |
||||
# schemecode: row[5], |
||||
# firstletting: row[6], |
||||
tenancycode: row[7], |
||||
startertenancy: row[8], |
||||
tenancy: row[9], |
||||
tenancyother: row[10], |
||||
# tenancyduration: row[11], |
||||
hhmemb: hhmemb(row), |
||||
age1: row[12], |
||||
age2: row[13], |
||||
age3: row[14], |
||||
age4: row[15], |
||||
age5: row[16], |
||||
age6: row[17], |
||||
age7: row[18], |
||||
age8: row[19], |
||||
sex1: row[20], |
||||
sex2: row[21], |
||||
sex3: row[22], |
||||
sex4: row[23], |
||||
sex5: row[24], |
||||
sex6: row[25], |
||||
sex7: row[26], |
||||
sex8: row[27], |
||||
relat2: row[28], |
||||
relat3: row[29], |
||||
relat4: row[30], |
||||
relat5: row[31], |
||||
relat6: row[32], |
||||
relat7: row[33], |
||||
relat8: row[34], |
||||
ecstat1: row[35], |
||||
ecstat2: row[36], |
||||
ecstat3: row[37], |
||||
ecstat4: row[38], |
||||
ecstat5: row[39], |
||||
ecstat6: row[40], |
||||
ecstat7: row[41], |
||||
ecstat8: row[42], |
||||
ethnic: row[43], |
||||
national: row[44], |
||||
armedforces: row[45], |
||||
reservist: row[46], |
||||
preg_occ: row[47], |
||||
hb: row[48], |
||||
benefits: row[49], |
||||
net_income_known: row[50].present? ? 1 : nil, |
||||
earnings: row[50], |
||||
# increfused: row[51], |
||||
reason: row[52], |
||||
reasonother: row[53], |
||||
underoccupation_benefitcap: row[54], |
||||
housingneeds_a: row[55], |
||||
housingneeds_b: row[56], |
||||
housingneeds_c: row[57], |
||||
housingneeds_f: row[58], |
||||
housingneeds_g: row[59], |
||||
housingneeds_h: row[60], |
||||
prevten: row[61], |
||||
prevloc: row[62], |
||||
# prevpco_unknown: row[65], |
||||
layear: row[66], |
||||
waityear: row[67], |
||||
homeless: row[68], |
||||
reasonpref: row[69], |
||||
rp_homeless: row[70], |
||||
rp_insan_unsat: row[71], |
||||
rp_medwel: row[72], |
||||
rp_hardship: row[73], |
||||
rp_dontknow: row[74], |
||||
cbl: row[75], |
||||
chr: row[76], |
||||
cap: row[77], |
||||
# referral_source: row[78], |
||||
period: row[79], |
||||
brent: row[80], |
||||
scharge: row[81], |
||||
pscharge: row[82], |
||||
supcharg: row[83], |
||||
tcharge: row[84], |
||||
# tcharge_care_homes: row[85], |
||||
# no_rent_or_charge: row[86], |
||||
hbrentshortfall: row[87], |
||||
tshortfall: row[88], |
||||
voiddate: row[89].to_s + row[90].to_s + row[91].to_s, |
||||
majorrepairs: row[92].present? ? "1" : nil, |
||||
mrcdate: row[92].to_s + row[93].to_s + row[94].to_s, |
||||
# supported_scheme: row[95], |
||||
startdate: date_time(row[98], row[97], row[96]), |
||||
# startdate_day: row[96], |
||||
# startdate_month: row[97], |
||||
# startdate_year: row[98], |
||||
offered: row[99], |
||||
# property_reference: row[100], |
||||
beds: row[101], |
||||
unittype_gn: row[102], |
||||
builtype: row[103], |
||||
wchair: row[104], |
||||
property_relet: row[105], |
||||
rsnvac: row[106], |
||||
la: row[107], |
||||
# row[110] removed |
||||
# row[111] is owning organisation used above |
||||
# username: row[112], |
||||
# row[113] is managing organisation used above |
||||
leftreg: row[114], |
||||
# uprn: row[115], |
||||
incfreq: row[116], |
||||
# sheltered_accom: row[117], |
||||
illness: row[118], |
||||
illness_type_1: row[119], |
||||
illness_type_2: row[120], |
||||
illness_type_3: row[121], |
||||
illness_type_4: row[122], |
||||
illness_type_8: row[123], |
||||
illness_type_5: row[124], |
||||
illness_type_6: row[125], |
||||
illness_type_7: row[126], |
||||
illness_type_9: row[127], |
||||
illness_type_10: row[128], |
||||
# london_affordable: row[129], |
||||
rent_type: row[130], |
||||
irproduct_other: row[131], |
||||
# data_protection: row[132], |
||||
declaration: 1, |
||||
} |
||||
end |
||||
|
||||
def date_time(year, month, day) |
||||
return unless year && month && day |
||||
|
||||
Time.zone.local("20#{year}", month.to_s, day.to_s) |
||||
end |
||||
private |
||||
|
||||
def hhmemb(row) |
||||
[14, 15, 16, 17, 18, 19, 20].count { |idx| row[idx].present? } |
||||
def generate_identifier |
||||
self.identifier ||= SecureRandom.uuid |
||||
end |
||||
end |
||||
|
@ -0,0 +1,27 @@
|
||||
module Forms |
||||
module BulkUploadLettings |
||||
class CheckingFile |
||||
include ActiveModel::Model |
||||
include ActiveModel::Attributes |
||||
include Rails.application.routes.url_helpers |
||||
|
||||
attribute :year, :integer |
||||
|
||||
def view_path |
||||
"bulk_upload_lettings_logs/forms/checking_file" |
||||
end |
||||
|
||||
def back_path |
||||
bulk_upload_lettings_log_path(id: "start") |
||||
end |
||||
|
||||
def year_combo |
||||
"#{year}/#{year + 1 - 2000}" |
||||
end |
||||
|
||||
def save! |
||||
true |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,27 @@
|
||||
module Forms |
||||
module BulkUploadSales |
||||
class CheckingFile |
||||
include ActiveModel::Model |
||||
include ActiveModel::Attributes |
||||
include Rails.application.routes.url_helpers |
||||
|
||||
attribute :year, :integer |
||||
|
||||
def view_path |
||||
"bulk_upload_sales_logs/forms/checking_file" |
||||
end |
||||
|
||||
def back_path |
||||
bulk_upload_sales_log_path(id: "start") |
||||
end |
||||
|
||||
def year_combo |
||||
"#{year}/#{year + 1 - 2000}" |
||||
end |
||||
|
||||
def save! |
||||
true |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,197 @@
|
||||
class LegacyBulkUpload |
||||
include ActiveModel::Model |
||||
include ActiveModel::Validations |
||||
include ActiveModel::Conversion |
||||
|
||||
SPREADSHEET_CONTENT_TYPES = %w[ |
||||
application/vnd.ms-excel |
||||
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
||||
].freeze |
||||
|
||||
FIRST_DATA_ROW = 7 |
||||
|
||||
def initialize(file, content_type) |
||||
@file = file |
||||
@content_type = content_type |
||||
end |
||||
|
||||
def process(current_user) |
||||
return unless valid_content_type? |
||||
|
||||
xlsx = Roo::Spreadsheet.open(@file, extension: :xlsx) |
||||
sheet = xlsx.sheet(0) |
||||
last_row = sheet.last_row |
||||
if last_row < FIRST_DATA_ROW |
||||
errors.add(:lettings_log_bulk_upload, "No data found") |
||||
else |
||||
data_range = FIRST_DATA_ROW..last_row |
||||
data_range.map do |row_num| |
||||
row = sheet.row(row_num) |
||||
# owning_organisation = Organisation.find(row[111]) |
||||
# managing_organisation = Organisation.find(row[113]) |
||||
lettings_log = LettingsLog.create!( |
||||
owning_organisation: current_user.organisation, |
||||
managing_organisation: current_user.organisation, |
||||
created_by: current_user, |
||||
) |
||||
map_row(row).each do |attr_key, attr_val| |
||||
_update = lettings_log.update(attr_key => attr_val) |
||||
rescue ArgumentError |
||||
# TODO: determine what we want to do when bulk upload contains totally invalid data for a field. |
||||
end |
||||
end |
||||
end |
||||
end |
||||
|
||||
def valid_content_type? |
||||
if SPREADSHEET_CONTENT_TYPES.include?(@content_type) |
||||
true |
||||
else |
||||
errors.add(:lettings_log_bulk_upload, "Invalid file type") |
||||
false |
||||
end |
||||
end |
||||
|
||||
def map_row(row) |
||||
{ |
||||
lettype: row[1], |
||||
# reg_num_la_core_code: row[3], |
||||
# managementgroup: row[4], |
||||
# schemecode: row[5], |
||||
# firstletting: row[6], |
||||
tenancycode: row[7], |
||||
startertenancy: row[8], |
||||
tenancy: row[9], |
||||
tenancyother: row[10], |
||||
# tenancyduration: row[11], |
||||
hhmemb: hhmemb(row), |
||||
age1: row[12], |
||||
age2: row[13], |
||||
age3: row[14], |
||||
age4: row[15], |
||||
age5: row[16], |
||||
age6: row[17], |
||||
age7: row[18], |
||||
age8: row[19], |
||||
sex1: row[20], |
||||
sex2: row[21], |
||||
sex3: row[22], |
||||
sex4: row[23], |
||||
sex5: row[24], |
||||
sex6: row[25], |
||||
sex7: row[26], |
||||
sex8: row[27], |
||||
relat2: row[28], |
||||
relat3: row[29], |
||||
relat4: row[30], |
||||
relat5: row[31], |
||||
relat6: row[32], |
||||
relat7: row[33], |
||||
relat8: row[34], |
||||
ecstat1: row[35], |
||||
ecstat2: row[36], |
||||
ecstat3: row[37], |
||||
ecstat4: row[38], |
||||
ecstat5: row[39], |
||||
ecstat6: row[40], |
||||
ecstat7: row[41], |
||||
ecstat8: row[42], |
||||
ethnic: row[43], |
||||
national: row[44], |
||||
armedforces: row[45], |
||||
reservist: row[46], |
||||
preg_occ: row[47], |
||||
hb: row[48], |
||||
benefits: row[49], |
||||
net_income_known: row[50].present? ? 1 : nil, |
||||
earnings: row[50], |
||||
# increfused: row[51], |
||||
reason: row[52], |
||||
reasonother: row[53], |
||||
underoccupation_benefitcap: row[54], |
||||
housingneeds_a: row[55], |
||||
housingneeds_b: row[56], |
||||
housingneeds_c: row[57], |
||||
housingneeds_f: row[58], |
||||
housingneeds_g: row[59], |
||||
housingneeds_h: row[60], |
||||
prevten: row[61], |
||||
prevloc: row[62], |
||||
# prevpco_unknown: row[65], |
||||
layear: row[66], |
||||
waityear: row[67], |
||||
homeless: row[68], |
||||
reasonpref: row[69], |
||||
rp_homeless: row[70], |
||||
rp_insan_unsat: row[71], |
||||
rp_medwel: row[72], |
||||
rp_hardship: row[73], |
||||
rp_dontknow: row[74], |
||||
cbl: row[75], |
||||
chr: row[76], |
||||
cap: row[77], |
||||
# referral_source: row[78], |
||||
period: row[79], |
||||
brent: row[80], |
||||
scharge: row[81], |
||||
pscharge: row[82], |
||||
supcharg: row[83], |
||||
tcharge: row[84], |
||||
# tcharge_care_homes: row[85], |
||||
# no_rent_or_charge: row[86], |
||||
hbrentshortfall: row[87], |
||||
tshortfall: row[88], |
||||
voiddate: row[89].to_s + row[90].to_s + row[91].to_s, |
||||
majorrepairs: row[92].present? ? "1" : nil, |
||||
mrcdate: row[92].to_s + row[93].to_s + row[94].to_s, |
||||
# supported_scheme: row[95], |
||||
startdate: date_time(row[98], row[97], row[96]), |
||||
# startdate_day: row[96], |
||||
# startdate_month: row[97], |
||||
# startdate_year: row[98], |
||||
offered: row[99], |
||||
# property_reference: row[100], |
||||
beds: row[101], |
||||
unittype_gn: row[102], |
||||
builtype: row[103], |
||||
wchair: row[104], |
||||
property_relet: row[105], |
||||
rsnvac: row[106], |
||||
la: row[107], |
||||
# row[110] removed |
||||
# row[111] is owning organisation used above |
||||
# username: row[112], |
||||
# row[113] is managing organisation used above |
||||
leftreg: row[114], |
||||
# uprn: row[115], |
||||
incfreq: row[116], |
||||
# sheltered_accom: row[117], |
||||
illness: row[118], |
||||
illness_type_1: row[119], |
||||
illness_type_2: row[120], |
||||
illness_type_3: row[121], |
||||
illness_type_4: row[122], |
||||
illness_type_8: row[123], |
||||
illness_type_5: row[124], |
||||
illness_type_6: row[125], |
||||
illness_type_7: row[126], |
||||
illness_type_9: row[127], |
||||
illness_type_10: row[128], |
||||
# london_affordable: row[129], |
||||
rent_type: row[130], |
||||
irproduct_other: row[131], |
||||
# data_protection: row[132], |
||||
declaration: 1, |
||||
} |
||||
end |
||||
|
||||
def date_time(year, month, day) |
||||
return unless year && month && day |
||||
|
||||
Time.zone.local("20#{year}", month.to_s, day.to_s) |
||||
end |
||||
|
||||
def hhmemb(row) |
||||
[14, 15, 16, 17, 18, 19, 20].count { |idx| row[idx].present? } |
||||
end |
||||
end |
@ -0,0 +1,28 @@
|
||||
<% content_for :before_content do %> |
||||
<%= govuk_back_link href: @form.back_path %> |
||||
<% end %> |
||||
|
||||
<div class="govuk-grid-row"> |
||||
<div class="govuk-grid-column-two-thirds"> |
||||
<%= form_with model: @form, scope: :form, url: bulk_upload_lettings_log_path(id: "prepare-your-file"), method: :patch do |f| %> |
||||
<%= f.hidden_field :year %> |
||||
|
||||
<span class="govuk-caption-l">Upload lettings logs in bulk (<%= @form.year_combo %>)</span> |
||||
<h1 class="govuk-heading-l">We’re checking the file</h1> |
||||
|
||||
<h2 class="govuk-heading-m">Once this is done, we’ll email you the outcome.</h2> |
||||
|
||||
<p class="govuk-body"> |
||||
We’re checking for any missing data, issues or errors. Depending on the file size, this could take a few minutes. |
||||
</p> |
||||
|
||||
<p class="govuk-body"> |
||||
If there are errors we will email you a link to view all the errors. |
||||
</p> |
||||
|
||||
<p class="govuk-body"> |
||||
If there are no errors we will email you to let you know the upload is complete and that your logs have been created. |
||||
</p> |
||||
<% end %> |
||||
</div> |
||||
</div> |
@ -0,0 +1,28 @@
|
||||
<% content_for :before_content do %> |
||||
<%= govuk_back_link href: @form.back_path %> |
||||
<% end %> |
||||
|
||||
<div class="govuk-grid-row"> |
||||
<div class="govuk-grid-column-two-thirds"> |
||||
<%= form_with model: @form, scope: :form, url: bulk_upload_sales_log_path(id: "prepare-your-file"), method: :patch do |f| %> |
||||
<%= f.hidden_field :year %> |
||||
|
||||
<span class="govuk-caption-l">Upload sales logs in bulk (<%= @form.year_combo %>)</span> |
||||
<h1 class="govuk-heading-l">We’re checking the file</h1> |
||||
|
||||
<h2 class="govuk-heading-m">Once this is done, we’ll email you the outcome.</h2> |
||||
|
||||
<p class="govuk-body"> |
||||
We’re checking for any missing data, issues or errors. Depending on the file size, this could take a few minutes. |
||||
</p> |
||||
|
||||
<p class="govuk-body"> |
||||
If there are errors we will email you a link to view all the errors. |
||||
</p> |
||||
|
||||
<p class="govuk-body"> |
||||
If there are no errors we will email you to let you know the upload is complete and that your logs have been created. |
||||
</p> |
||||
<% end %> |
||||
</div> |
||||
</div> |
@ -0,0 +1,14 @@
|
||||
class CreateBulkUploads < ActiveRecord::Migration[7.0] |
||||
def change |
||||
create_table :bulk_uploads do |t| |
||||
t.references :user |
||||
t.text :log_type, null: false |
||||
t.integer :year, null: false |
||||
t.uuid :identifier, null: false |
||||
|
||||
t.timestamps |
||||
end |
||||
|
||||
add_index :bulk_uploads, :identifier, unique: true |
||||
end |
||||
end |
|
Can't render this file because it contains an unexpected character in line 3 and column 219.
|
@ -0,0 +1,47 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Forms::BulkUploadLettings::UploadYourFile do |
||||
subject(:form) { described_class.new(year:, file:, current_user:) } |
||||
|
||||
let(:year) { 2022 } |
||||
let(:actual_file) { File.open(file_fixture("blank_bulk_upload_sales.csv")) } |
||||
let(:file) { ActionDispatch::Http::UploadedFile.new(tempfile: actual_file) } |
||||
let(:current_user) { create(:user) } |
||||
let(:mock_storage_service) { instance_double("S3Service") } |
||||
|
||||
before do |
||||
vcap_services = { "aws-s3-bucket" => {} } |
||||
|
||||
allow(ENV).to receive(:[]) |
||||
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services.to_json) |
||||
|
||||
allow(Storage::S3Service).to receive(:new).and_return(mock_storage_service) |
||||
allow(mock_storage_service).to receive(:write_file) |
||||
end |
||||
|
||||
describe "#save" do |
||||
it "persists a BulkUpload" do |
||||
expect { form.save! }.to change(BulkUpload, :count).by(1) |
||||
end |
||||
|
||||
it "persists a BulkUpload correctly" do |
||||
form.save! |
||||
|
||||
bulk_upload = BulkUpload.last |
||||
|
||||
expect(bulk_upload.user).to eql(current_user) |
||||
expect(bulk_upload.log_type).to eql("lettings") |
||||
expect(bulk_upload.year).to eql(year) |
||||
expect(bulk_upload.identifier).to be_present |
||||
end |
||||
|
||||
it "uploads file via storage service" do |
||||
form.save! |
||||
|
||||
bulk_upload = BulkUpload.last |
||||
|
||||
expect(Storage::S3Service).to have_received(:new) |
||||
expect(mock_storage_service).to have_received(:write_file).with(bulk_upload.identifier, actual_file.read) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,47 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Forms::BulkUploadSales::UploadYourFile do |
||||
subject(:form) { described_class.new(year:, file:, current_user:) } |
||||
|
||||
let(:year) { 2022 } |
||||
let(:actual_file) { File.open(file_fixture("blank_bulk_upload_sales.csv")) } |
||||
let(:file) { ActionDispatch::Http::UploadedFile.new(tempfile: actual_file) } |
||||
let(:current_user) { create(:user) } |
||||
let(:mock_storage_service) { instance_double("S3Service") } |
||||
|
||||
before do |
||||
vcap_services = { "aws-s3-bucket" => {} } |
||||
|
||||
allow(ENV).to receive(:[]) |
||||
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services.to_json) |
||||
|
||||
allow(Storage::S3Service).to receive(:new).and_return(mock_storage_service) |
||||
allow(mock_storage_service).to receive(:write_file) |
||||
end |
||||
|
||||
describe "#save" do |
||||
it "persists a BulkUpload" do |
||||
expect { form.save! }.to change(BulkUpload, :count).by(1) |
||||
end |
||||
|
||||
it "persists a BulkUpload correctly" do |
||||
form.save! |
||||
|
||||
bulk_upload = BulkUpload.last |
||||
|
||||
expect(bulk_upload.user).to eql(current_user) |
||||
expect(bulk_upload.log_type).to eql("sales") |
||||
expect(bulk_upload.year).to eql(year) |
||||
expect(bulk_upload.identifier).to be_present |
||||
end |
||||
|
||||
it "uploads file via storage service" do |
||||
form.save! |
||||
|
||||
bulk_upload = BulkUpload.last |
||||
|
||||
expect(Storage::S3Service).to have_received(:new) |
||||
expect(mock_storage_service).to have_received(:write_file).with(bulk_upload.identifier, actual_file.read) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue