Browse Source

block log creation at row parser level

bulk-upload-owning-org
Phil Lee 2 years ago
parent
commit
c84bddd8fd
  1. 13
      app/services/bulk_upload/lettings/row_parser.rb
  2. 8
      spec/services/bulk_upload/lettings/row_parser_spec.rb

13
app/services/bulk_upload/lettings/row_parser.rb

@ -3,6 +3,7 @@ class BulkUpload::Lettings::RowParser
include ActiveModel::Attributes
attribute :bulk_upload
attribute :block_log_creation, :boolean, default: -> { false }
attribute :field_1, :integer
attribute :field_2
@ -176,17 +177,26 @@ class BulkUpload::Lettings::RowParser
end
def blank_row?
attribute_set.to_hash.reject { |k, _| %w[bulk_upload].include?(k) }.values.compact.empty?
attribute_set.to_hash.reject { |k, _| %w[bulk_upload block_log_creation].include?(k) }.values.compact.empty?
end
def log
@log ||= LettingsLog.new(attributes_for_log)
end
def block_log_creation!
self.block_log_creation = true
end
def block_log_creation?
block_log_creation
end
private
def validate_owning_org_owns_stock
if owning_organisation && !owning_organisation.holds_own_stock?
block_log_creation!
errors.delete(:field_111)
errors.add(:field_111, "The owning organisation code provided is for an organisation that does not own stock")
end
@ -201,6 +211,7 @@ private
def validate_owning_org_permitted
if owning_organisation && !bulk_upload.user.organisation.affiliated_stock_owners.include?(owning_organisation)
block_log_creation!
errors.delete(:field_111)
errors.add(:field_111, "You do not have permission to add logs for this owning organisation")
end

8
spec/services/bulk_upload/lettings/row_parser_spec.rb

@ -479,6 +479,10 @@ RSpec.describe BulkUpload::Lettings::RowParser do
it "is not permitted" do
expect(parser.errors[:field_111]).to eql(["The owning organisation code provided is for an organisation that does not own stock"])
end
it "blocks log creation" do
expect(parser).to be_block_log_creation
end
end
context "when not affiliated with owning org" do
@ -489,6 +493,10 @@ RSpec.describe BulkUpload::Lettings::RowParser do
it "is not permitted" do
expect(parser.errors[:field_111]).to eql(["You do not have permission to add logs for this owning organisation"])
end
it "blocks log creation" do
expect(parser).to be_block_log_creation
end
end
end

Loading…
Cancel
Save