Browse Source

support username as email for bulk upload 2023

pull/1482/head
Phil Lee 3 years ago
parent
commit
828040519d
  1. 29
      app/services/bulk_upload/lettings/year2023/row_parser.rb
  2. 76
      spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb

29
app/services/bulk_upload/lettings/year2023/row_parser.rb

@ -328,6 +328,9 @@ class BulkUpload::Lettings::Year2023::RowParser
validate :validate_location_exists validate :validate_location_exists
validate :validate_location_data_given validate :validate_location_data_given
validate :validate_created_by_exists
validate :validate_created_by_related
def self.question_for_field(field) def self.question_for_field(field)
QUESTIONS[field] QUESTIONS[field]
end end
@ -385,6 +388,28 @@ class BulkUpload::Lettings::Year2023::RowParser
private private
def validate_created_by_exists
return if field_3.blank?
unless created_by
block_log_creation!
errors.add(:field_3, "User with the specified email could not be found")
end
end
def validate_created_by_related
return unless created_by
unless (created_by.organisation == owning_organisation) || (created_by.organisation == managing_organisation)
block_log_creation!
errors.add(:field_3, "User must be related to owning organisation or managing organisation")
end
end
def created_by
@created_by ||= User.find_by(email: field_3)
end
def validate_needs_type_present def validate_needs_type_present
if field_4.blank? if field_4.blank?
errors.add(:field_4, I18n.t("validations.not_answered", question: "needs type")) errors.add(:field_4, I18n.t("validations.not_answered", question: "needs type"))
@ -630,7 +655,7 @@ private
renewal: [:field_6], renewal: [:field_6],
scheme: %i[field_16], scheme: %i[field_16],
location: %i[field_17], location: %i[field_17],
created_by: [], created_by: [:field_3],
needstype: [:field_4], needstype: [:field_4],
rent_type: %i[field_5 field_10 field_11], rent_type: %i[field_5 field_10 field_11],
startdate: %i[field_7 field_8 field_9], startdate: %i[field_7 field_8 field_9],
@ -796,7 +821,7 @@ private
attributes["renewal"] = renewal attributes["renewal"] = renewal
attributes["scheme"] = scheme attributes["scheme"] = scheme
attributes["location"] = location attributes["location"] = location
attributes["created_by"] = bulk_upload.user attributes["created_by"] = created_by || bulk_upload.user
attributes["needstype"] = field_4 attributes["needstype"] = field_4
attributes["rent_type"] = rent_type attributes["rent_type"] = rent_type
attributes["startdate"] = startdate attributes["startdate"] = startdate

76
spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb

@ -242,6 +242,62 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
end end
end end
describe "#field_3" do # created_by
context "when blank" do
let(:attributes) { { bulk_upload:, field_3: "" } }
it "is permitted" do
expect(parser.errors[:field_3]).to be_blank
end
end
context "when user could not be found" do
let(:attributes) { { bulk_upload:, field_3: "idonotexist@example.com" } }
it "is not permitted" do
expect(parser.errors[:field_3]).to be_present
end
it "blocks log creation" do
expect(parser).to be_block_log_creation
end
end
context "when an unaffiliated user" do
let(:other_user) { create(:user) }
let(:attributes) { { bulk_upload:, field_1: owning_org.old_visible_id, field_3: other_user.email, field_2: managing_org.old_visible_id } }
it "is not permitted" do
expect(parser.errors[:field_3]).to be_present
end
it "blocks log creation" do
expect(parser).to be_block_log_creation
end
end
context "when an user part of owning org" do
let(:other_user) { create(:user, organisation: owning_org) }
let(:attributes) { { bulk_upload:, field_1: owning_org.old_visible_id, field_3: other_user.email, field_2: managing_org.old_visible_id } }
it "is permitted" do
expect(parser.errors[:field_3]).to be_blank
end
end
context "when an user part of managing org" do
let(:other_user) { create(:user, organisation: managing_org) }
let(:attributes) { { bulk_upload:, field_1: owning_org.old_visible_id, field_3: other_user.email, field_2: managing_org.old_visible_id } }
it "is permitted" do
expect(parser.errors[:field_3]).to be_blank
end
end
end
describe "#field_5" do describe "#field_5" do
context "when null" do context "when null" do
let(:attributes) { { bulk_upload:, field_5: nil, field_15: "1" } } let(:attributes) { { bulk_upload:, field_5: nil, field_15: "1" } }
@ -767,6 +823,26 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
end end
describe "#log" do describe "#log" do
describe "#created_by" do
context "when blank" do
let(:attributes) { setup_section_params }
it "takes the user that is uploading" do
expect(parser.log.created_by).to eql(bulk_upload.user)
end
end
context "when email specified" do
let(:other_user) { create(:user, organisation: owning_org) }
let(:attributes) { setup_section_params.merge(field_3: other_user.email) }
it "sets to user with specified email" do
expect(parser.log.created_by).to eql(other_user)
end
end
end
describe "#uprn" do describe "#uprn" do
let(:attributes) { { bulk_upload:, field_18: "100023336956" } } let(:attributes) { { bulk_upload:, field_18: "100023336956" } }

Loading…
Cancel
Save