Browse Source

support username as email for bulk upload

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

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

@ -325,6 +325,9 @@ class BulkUpload::Lettings::Year2022::RowParser
validate :validate_location_exists
validate :validate_location_data_given
validate :validate_created_by_exists
validate :validate_created_by_related
def self.question_for_field(field)
QUESTIONS[field]
end
@ -382,6 +385,28 @@ class BulkUpload::Lettings::Year2022::RowParser
private
def validate_created_by_exists
return if field_112.blank?
unless created_by
block_log_creation!
errors.add(:field_112, "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_112, "User must be related to owning organisation or managing organisation")
end
end
def created_by
@created_by ||= User.find_by(email: field_112)
end
def validate_location_related
return if scheme.blank? || location.blank?
@ -641,7 +666,7 @@ private
managing_organisation_id: [:field_113],
renewal: [:field_134],
scheme: %i[field_4 field_5],
created_by: [],
created_by: [:field_112],
needstype: [],
rent_type: %i[field_1 field_129 field_130],
startdate: %i[field_98 field_97 field_96],
@ -851,7 +876,7 @@ private
attributes["renewal"] = renewal
attributes["scheme"] = scheme
attributes["location"] = location
attributes["created_by"] = bulk_upload.user
attributes["created_by"] = created_by || bulk_upload.user
attributes["needstype"] = bulk_upload.needstype
attributes["rent_type"] = rent_type
attributes["startdate"] = startdate

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

@ -701,6 +701,62 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do
end
end
describe "#field_112" do # username for created_by
context "when blank" do
let(:attributes) { { bulk_upload:, field_112: "" } }
it "is permitted" do
expect(parser.errors[:field_112]).to be_blank
end
end
context "when user could not be found" do
let(:attributes) { { bulk_upload:, field_112: "idonotexist@example.com" } }
it "is not permitted" do
expect(parser.errors[:field_112]).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_111: owning_org.old_visible_id, field_112: other_user.email, field_113: managing_org.old_visible_id } }
it "is not permitted" do
expect(parser.errors[:field_112]).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_111: owning_org.old_visible_id, field_112: other_user.email, field_113: managing_org.old_visible_id } }
it "is permitted" do
expect(parser.errors[:field_112]).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_111: owning_org.old_visible_id, field_112: other_user.email, field_113: managing_org.old_visible_id } }
it "is permitted" do
expect(parser.errors[:field_112]).to be_blank
end
end
end
describe "#field_113" do # managing org
context "when blank" do
let(:attributes) { { bulk_upload:, field_113: "" } }
@ -775,6 +831,26 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do
end
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_112: other_user.email) }
it "sets to user with specified email" do
expect(parser.log.created_by).to eql(other_user)
end
end
end
[
%w[age1_known age1 field_12],
%w[age2_known age2 field_13],

Loading…
Cancel
Save