Fix syncing articles with non-import sync methods (#633)
This commit is contained in:
parent
d669edb173
commit
9283ed4f5e
3 changed files with 66 additions and 2 deletions
|
@ -29,7 +29,7 @@ class SharedSupplier < ApplicationRecord
|
||||||
def shared_sync_methods
|
def shared_sync_methods
|
||||||
methods = []
|
methods = []
|
||||||
methods += %w(all_available all_unavailable) if shared_articles.count < FoodsoftConfig[:shared_supplier_article_sync_limit]
|
methods += %w(all_available all_unavailable) if shared_articles.count < FoodsoftConfig[:shared_supplier_article_sync_limit]
|
||||||
methods += %w(import) # perhaps, in the future: if shared_articles.count > 20
|
methods += %w(import)
|
||||||
methods
|
methods
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -55,7 +55,7 @@ class Supplier < ApplicationRecord
|
||||||
unless shared_sync_method == 'import'
|
unless shared_sync_method == 'import'
|
||||||
shared_supplier
|
shared_supplier
|
||||||
.shared_articles
|
.shared_articles
|
||||||
.where.not(id: existing_articles)
|
.where.not(id: existing_articles.to_a)
|
||||||
.each { |shared_article| new_articles << shared_article.build_new_article(self) }
|
.each { |shared_article| new_articles << shared_article.build_new_article(self) }
|
||||||
end
|
end
|
||||||
return [updated_article_pairs, outlisted_articles, new_articles]
|
return [updated_article_pairs, outlisted_articles, new_articles]
|
||||||
|
|
|
@ -13,4 +13,68 @@ describe Supplier do
|
||||||
supplier.articles.each {|a| expect(a).to be_valid }
|
supplier.articles.each {|a| expect(a).to be_valid }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'connected to a shared supplier' do
|
||||||
|
let(:shared_sync_method) { nil }
|
||||||
|
let(:shared_supplier) { create :shared_supplier }
|
||||||
|
let(:supplier) { create :supplier, shared_supplier: shared_supplier, shared_sync_method: shared_sync_method }
|
||||||
|
|
||||||
|
let!(:synced_shared_article) { create :shared_article, shared_supplier: shared_supplier }
|
||||||
|
let!(:updated_shared_article) { create :shared_article, shared_supplier: shared_supplier }
|
||||||
|
let!(:new_shared_article) { create :shared_article, shared_supplier: shared_supplier }
|
||||||
|
|
||||||
|
let!(:removed_article) { create :article, supplier: supplier, order_number: '10001-ABC' }
|
||||||
|
let!(:updated_article) do
|
||||||
|
updated_shared_article.build_new_article(supplier).tap do |article|
|
||||||
|
article.article_category = create :article_category
|
||||||
|
article.origin = "FubarX1"
|
||||||
|
article.shared_updated_on = 1.day.ago
|
||||||
|
article.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
let!(:synced_article) do
|
||||||
|
synced_shared_article.build_new_article(supplier).tap do |article|
|
||||||
|
article.article_category = create :article_category
|
||||||
|
article.shared_updated_on = 1.day.ago
|
||||||
|
article.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with sync method import' do
|
||||||
|
let(:shared_sync_method) { 'import' }
|
||||||
|
|
||||||
|
it 'returns the expected articles' do
|
||||||
|
updated_article_pairs, outlisted_articles, new_articles = supplier.sync_all
|
||||||
|
|
||||||
|
expect(updated_article_pairs).to_not be_empty
|
||||||
|
expect(updated_article_pairs[0][0].id).to eq updated_article.id
|
||||||
|
expect(updated_article_pairs[0][1].keys).to include :origin
|
||||||
|
|
||||||
|
expect(outlisted_articles).to eq [removed_article]
|
||||||
|
|
||||||
|
expect(new_articles).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with sync method all_available' do
|
||||||
|
let(:shared_sync_method) { 'all_available' }
|
||||||
|
|
||||||
|
it 'returns the expected articles' do
|
||||||
|
updated_article_pairs, outlisted_articles, new_articles = supplier.sync_all
|
||||||
|
|
||||||
|
expect(updated_article_pairs).to_not be_empty
|
||||||
|
expect(updated_article_pairs[0][0].id).to eq updated_article.id
|
||||||
|
expect(updated_article_pairs[0][1].keys).to include :origin
|
||||||
|
|
||||||
|
expect(outlisted_articles).to eq [removed_article]
|
||||||
|
|
||||||
|
expect(new_articles).to_not be_empty
|
||||||
|
expect(new_articles[0].order_number).to eq new_shared_article.number
|
||||||
|
expect(new_articles[0].availability?).to be true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Setting articles to unavailable with sync method all_unavailable
|
||||||
|
# is handled in the articles controller, so no need to test it here.
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue