From 15a9a86eea7e515d52edcac76e8cef72a8eae1f5 Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Wed, 29 Mar 2023 09:44:42 +0100 Subject: [PATCH] CLDC-1893 Support username as email for bulk upload (#1482) * support username as email for bulk upload * support username as email for bulk upload 2023 * do not block log creation if user not found --- .../lettings/year2022/row_parser.rb | 28 +++++++- .../lettings/year2023/row_parser.rb | 28 +++++++- .../lettings/year2022/row_parser_spec.rb | 72 +++++++++++++++++++ .../lettings/year2023/row_parser_spec.rb | 72 +++++++++++++++++++ 4 files changed, 196 insertions(+), 4 deletions(-) diff --git a/app/services/bulk_upload/lettings/year2022/row_parser.rb b/app/services/bulk_upload/lettings/year2022/row_parser.rb index 0726c9a07..162f25436 100644 --- a/app/services/bulk_upload/lettings/year2022/row_parser.rb +++ b/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,27 @@ class BulkUpload::Lettings::Year2022::RowParser private + def validate_created_by_exists + return if field_112.blank? + + unless created_by + 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 +665,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 +875,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 diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 2fc61921f..99b9dde88 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/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_data_given + validate :validate_created_by_exists + validate :validate_created_by_related + def self.question_for_field(field) QUESTIONS[field] end @@ -385,6 +388,27 @@ class BulkUpload::Lettings::Year2023::RowParser private + def validate_created_by_exists + return if field_3.blank? + + unless created_by + 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 if field_4.blank? errors.add(:field_4, I18n.t("validations.not_answered", question: "needs type")) @@ -630,7 +654,7 @@ private renewal: [:field_6], scheme: %i[field_16], location: %i[field_17], - created_by: [], + created_by: [:field_3], needstype: [:field_4], rent_type: %i[field_5 field_10 field_11], startdate: %i[field_7 field_8 field_9], @@ -796,7 +820,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"] = field_4 attributes["rent_type"] = rent_type attributes["startdate"] = startdate diff --git a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb index b771cbcea..a03516852 100644 --- a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb @@ -701,6 +701,58 @@ 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 + 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 +827,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], diff --git a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb index a7dc629c0..aff3b0516 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -242,6 +242,58 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do 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 + 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 context "when null" do let(:attributes) { { bulk_upload:, field_5: nil, field_15: "1" } } @@ -767,6 +819,26 @@ RSpec.describe BulkUpload::Lettings::Year2023::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_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 let(:attributes) { { bulk_upload:, field_18: "100023336956" } }