Validate enterprise.is_primary_producer?

This commit is contained in:
Matt-Yorkley
2018-08-16 15:29:19 +01:00
committed by Maikel Linke
parent 616b42a14d
commit 666fbf53bf
3 changed files with 33 additions and 3 deletions

View File

@@ -62,6 +62,11 @@ module ProductImport
return
end
unless Enterprise.find_by_name(supplier_name).is_primary_producer?
mark_as_invalid(entry, attribute: "supplier", error: I18n.t(:error_not_primary_producer, name: supplier_name))
return
end
entry.supplier_id = @spreadsheet_data.suppliers_index[supplier_name]
end

View File

@@ -1739,6 +1739,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using
error_number: "must be number"
error_email: "must be email address"
error_not_found_in_database: "%{name} not found in database"
error_not_primary_producer: "%{name} is not enabled as a producer"
error_no_permission_for_enterprise: "\"%{name}\": you do not have permission to manage products for this enterprise"
item_handling_fees: "Item Handling Fees (included in item totals)"
january: "January"

View File

@@ -8,9 +8,10 @@ describe ProductImport::ProductImporter do
let!(:user) { create_enterprise_user }
let!(:user2) { create_enterprise_user }
let!(:user3) { create_enterprise_user }
let!(:enterprise) { create(:enterprise, owner: user, name: "User Enterprise") }
let!(:enterprise2) { create(:distributor_enterprise, owner: user2, name: "Another Enterprise") }
let!(:enterprise3) { create(:distributor_enterprise, owner: user3, name: "And Another Enterprise") }
let!(:enterprise) { create(:enterprise, is_primary_producer: true, owner: user, name: "User Enterprise") }
let!(:enterprise2) { create(:distributor_enterprise, is_primary_producer: true, owner: user2, name: "Another Enterprise") }
let!(:enterprise3) { create(:distributor_enterprise, is_primary_producer: true, owner: user3, name: "And Another Enterprise") }
let!(:enterprise4) { create(:enterprise, is_primary_producer: false, owner: user, name: "Non-Producer") }
let!(:relationship) { create(:enterprise_relationship, parent: enterprise, child: enterprise2, permissions_list: [:create_variant_overrides]) }
let!(:category) { create(:taxon, name: 'Vegetables') }
@@ -163,6 +164,29 @@ describe ProductImport::ProductImporter do
end
end
describe "when enterprises are not valid" do
before do
csv_data = CSV.generate do |csv|
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"]
csv << ["Product 1", "Non-existent Enterprise", "Vegetables", "5", "5.50", "500", "g"]
csv << ["Product 2", "Non-Producer", "Vegetables", "5", "5.50", "500", "g"]
end
File.write('/tmp/test-m.csv', csv_data)
file = File.new('/tmp/test-m.csv')
settings = {'import_into' => 'product_list'}
@importer = ProductImport::ProductImporter.new(file, admin, start: 1, end: 100, settings: settings)
end
after { File.delete('/tmp/test-m.csv') }
it "adds enterprise errors" do
@importer.validate_entries
entries = JSON.parse(@importer.entries_json)
expect(entries['2']['errors']['supplier']).to include "not found in database"
expect(entries['3']['errors']['supplier']).to include "not enabled as a producer"
end
end
describe "adding new variants to existing products and updating exiting products" do
before do
csv_data = CSV.generate do |csv|