Browse Source

add class to create logs from bulk upload

pull/1208/head
Phil Lee 3 years ago
parent
commit
5f70b6b2d9
  1. 3
      app/models/bulk_upload.rb
  2. 2
      app/models/log.rb
  3. 66
      app/services/bulk_upload/lettings/log_creator.rb
  4. 6
      db/migrate/20230113154518_add_bulk_upload_to_logs.rb
  5. 2
      db/schema.rb
  6. 1
      spec/fixtures/files/2022_23_lettings_bulk_upload.csv
  7. 31
      spec/services/bulk_upload/lettings/log_creator_spec.rb

3
app/models/bulk_upload.rb

@ -2,7 +2,10 @@ class BulkUpload < ApplicationRecord
enum log_type: { lettings: "lettings", sales: "sales" }
belongs_to :user
has_many :bulk_upload_errors
has_many :lettings_logs
has_many :sales_logs
after_initialize :generate_identifier, unless: :identifier

2
app/models/log.rb

@ -5,6 +5,8 @@ class Log < ApplicationRecord
belongs_to :managing_organisation, class_name: "Organisation", optional: true
belongs_to :created_by, class_name: "User", optional: true
belongs_to :updated_by, class_name: "User", optional: true
belongs_to :bulk_upload, optional: true
before_save :update_status!
STATUS = { "not_started" => 0, "in_progress" => 1, "completed" => 2 }.freeze

66
app/services/bulk_upload/lettings/log_creator.rb

@ -0,0 +1,66 @@
class BulkUpload::Lettings::LogCreator
attr_reader :bulk_upload, :path
def initialize(bulk_upload:, path:)
@bulk_upload = bulk_upload
@path = path
end
def call
row_parsers.each do |row_parser|
next unless row_parser.valid?
row_parser.log.bulk_upload = bulk_upload
row_parser.log.save
end
end
private
def row_offset
5
end
def col_offset
1
end
def row_parsers
@row_parsers ||= body_rows.map do |row|
stripped_row = row[col_offset..]
headers = ("field_1".."field_134").to_a
hash = Hash[headers.zip(stripped_row)]
hash[:bulk_upload] = bulk_upload
BulkUpload::Lettings::RowParser.new(hash)
end
end
def body_rows
rows[row_offset..]
end
def rows
@rows ||= CSV.read(path, row_sep:)
end
# determine the row seperator from CSV
# Windows will use \r\n
def row_sep
contents = ""
File.open(path, "r") do |f|
f.seek(9900)
contents = f.read
end
rn_count = contents.scan("\r\n").count
n_count = contents.scan(/[^\r]\n/).count
if rn_count > n_count
"\r\n"
else
"\n"
end
end
end

6
db/migrate/20230113154518_add_bulk_upload_to_logs.rb

@ -0,0 +1,6 @@
class AddBulkUploadToLogs < ActiveRecord::Migration[7.0]
def change
add_reference :lettings_logs, :bulk_upload
add_reference :sales_logs, :bulk_upload
end
end

2
db/schema.rb

@ -265,6 +265,8 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_16_151942) do
t.integer "housingneeds_other"
t.boolean "unresolved"
t.bigint "updated_by_id"
t.bigint "bulk_upload_id"
t.index ["bulk_upload_id"], name: "index_lettings_logs_on_bulk_upload_id"
t.index ["created_by_id"], name: "index_lettings_logs_on_created_by_id"
t.index ["location_id"], name: "index_lettings_logs_on_location_id"
t.index ["managing_organisation_id"], name: "index_lettings_logs_on_managing_organisation_id"

1
spec/fixtures/files/2022_23_lettings_bulk_upload.csv vendored

@ -70,3 +70,4 @@ or 106 = 15 - 17",No,"Only if 1 = 2, 4, 6, 8, 10 or 12",,,,No,,No,"Yes, if 45 =
Bulk upload format and duplicate check,All lettings,Question removed from 22/23 onwards,,Supported housing only,,Question Removed from 2020/21,,,,,,Duplicate check field,,,,,,,,Duplicate check field,,,,,,,,,,,,,,,Duplicate check field,,,,,,,,,,,,,,,,,,,Question removed from 22/23 onwards,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Duplicate check field,,,,,,,,,,,Question removed from 22/23 onwards,Duplicate check fields,,,,Duplicate check field,General Needs lettings only,,,All lettings,General Needs lettings only,All lettings,General Needs lettings only,,,Question removed from 2020/21,Duplicate check field, “Username does not exist”. ,,,Question removed from 21/22 onwards,,Supported Housing lettings only.,,,,,,,,,,,,Affordable Rent Lettings only,Intermediate Rent Lettings only,,All lettings,All lettings,,
Bulk upload field number,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,
,1,,,,,,123,1,2,,6,55,54,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,1,,,,,,123,1,2,,,55,54,,,,,,,"A",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,13,1,23,,,,,,,,,,,,,123,,123,,,,,,,,,,,,,,,,,,,,,2,

1 Question What is the letting type? [BLANK] [BLANK] Management group code If supported housing Scheme code If supported housing [BLANK] What is the tenant code? This is how you usually refer to this tenancy on your own systems Is this a starter tenancy? What is the tenancy type? If 'Other' what is the tenancy type? What is the length of the fixed-term tenancy to the nearest year? If fixed-term tenancy Age of Person 1 Age of Person 2 Age of Person 3 Age of Person 4 Age of Person 5 Age of Person 6 Age of Person 7 Age of Person 8 Gender identity of Person 1 Gender identity of Person 2 Gender identity of Person 3 Gender identity of Person 4 Gender identity of Person 5 Gender identity of Person 6 Gender identity of Person 7 Gender identity of Person 8 Person 2's relationship to lead tenant Person 3's relationship to lead tenant Person 4's relationship to lead tenant Person 5's relationship to lead tenant Person 6's relationship to lead tenant Person 7's relationship to lead tenant Person 8's relationship to lead tenant Working situation of Person 1 Working situation of Person 2 Working situation of Person 3 Working situation of Person 4 Working situation of Person 5 Working situation of Person 6 Working situation of Person 7 Working situation of Person 8 What is the lead tenant’s ethnic group? What is the lead tenant’s nationality? Does anybody in the household have links to the UK armed forces? Was the person seriously injured or ill as a result of serving in the UK armed forces? Is anybody in the household pregnant? Is the tenant likely to be receiving benefits related to housing? How much of the household's income is from Universal Credit, state pensions or benefits? How much income does the household have in total? Include any income after tax from employment, pensions, and Universal Credit Don't include National Insurance (NI) contributions and tax, housing benefit, child benefit, or council tax support Do you know the household's income? What is the tenant's main reason for the household leaving their last settled home? If 'other', what was the main reason for leaving their last settled home? [BLANK] Disabled access needs a) Fully wheelchair-accessible housing Disabled access needs b) Wheelchair access to essential rooms Disabled access needs c) Level access housing Disabled access needs f) Other disabled access needs Disabled access needs g) No disabled access needs Disabled access needs h) Don’t know Where was the household immediately before this letting? What is the local authority of the household's last settled home? Part 1 of postcode of last settled home Part 2 of postcode of last settled home Do you know the postcode of the last settled home? How long has the household continuously lived in the local authority area of the new letting? How long has the household been on the local authority waiting list for the new letting? Was the tenant homeless directly before this tenancy? Was the household given 'reasonable preference' by the local authority? Reasonable Preference They were homeless or about to lose their home (within 56 days) Reasonable Preference They were living in unsanitary, overcrowded or unsatisfactory housing Reasonable Preference They needed to move due to medical and welfare reasons (including disability) Reasonable Preference They needed to move to avoid hardship to themselves or others Reasonable Preference Don't know Was the letting made under Choice-Based Lettings (CBL)? Was the letting made under the Common Housing Register (CHR)? Was the letting made under the Common Allocation Policy (CAP)? What was the source of referral for this letting? How often does the household pay rent and other charges? What is the basic rent? What is the service charge? What is the personal service charge? What is the support charge? Total charge If this is a care home, how much does the household pay every [time period]? Does the household pay rent or other charges for the accommodation? If supported housing After the household has received any housing-related benefits, will they still need to pay basic rent and other charges? What do you expect the outstanding amount to be? What is the void or renewal day? - DD What is the void or renewal month? - MM What is the void or renewal year? - YYYY What day were Major Repairs completed on? - DD What month were Major Repairs completed on? - MM What year were Major Repairs completed on? - YY [BLANK] What day did the tenancy start? - DD What month did the tenancy start? - MM What year did the tenancy start? - YY Since becoming available, how many times has the property been previously offered? What is the property reference? This is how you usually refer to this property on your own systems How many bedrooms does the property have? If general needs What type of unit is the property? If general needs Which type of building is the property? If general needs Is the property built or adapted to wheelchair-user standards? If general needs What type was the property most recently let as? If re-let What is the reason for the property being vacant? What is the local authority of the property? If general needs Part 1 of postcode of property If general needs Part 2 of postcode of property If general needs [BLANK] Which organisation owns this property? Organisation's CORE ID Username Field Which organisation manages this property?  Organisation's CORE ID Is the person still serving in the UK armed forces? [BLANK] How often does the household receive income? Is this letting in sheltered accommodation? If supported housing Does anybody in the household have a physical or mental health condition (or other illness) expected to last 12 months or more? Vision, for example blindness or partial sight Hearing, for example deatness or partial hearing Mobility, for example walking short distances or climbing stairs Dexterity, for example lifting and carrying objects, using a keyboard Learning or understanding or concentrating Memory Mental health, for example depression or anxiety Stamina or breathing or fatigue Socially or behaviourally (e.g. associated with autism spectrum disorder (ASD) which includes Aspergers', or attention deficit hyperactivity disorder (ADHD)) Other illness or condition Is this letting a London Affordable Rent letting? Which type of Intermediate Rent is this letting? Which 'Other' type of Intermediate Rent is this letting? Has the tenant seen the DLUHC privacy notice? Is this a joint tenancy? Is this a renewal to the same tenant in the same property?
70
71
72
73

31
spec/services/bulk_upload/lettings/log_creator_spec.rb

@ -0,0 +1,31 @@
require "rails_helper"
RSpec.describe BulkUpload::Lettings::LogCreator do
subject(:service) { described_class.new(bulk_upload:, path:) }
let(:owning_org) { create(:organisation, old_visible_id: 123) }
let(:user) { create(:user, organisation: owning_org) }
let(:bulk_upload) { create(:bulk_upload, :lettings, user:) }
let(:path) { file_fixture("2022_23_lettings_bulk_upload.csv") }
describe "#call" do
context "when a valid csv with new log" do
it "creates a new log" do
expect { service.call }.to change(LettingsLog, :count)
end
it "associates log with bulk upload" do
service.call
log = LettingsLog.last
expect(log.bulk_upload).to eql(bulk_upload)
expect(bulk_upload.lettings_logs).to include(log)
end
end
context "when valid csv with existing log" do
xit "what should happen?"
end
end
end
Loading…
Cancel
Save