From 1b85b639882ec885fb2914e679cf20b4a2ac8c0c Mon Sep 17 00:00:00 2001 From: Niklaus Giger Date: Tue, 23 Aug 2016 16:37:19 +0200 Subject: [PATCH] Delete all Bauerfeind products before importing. 3 --- lib/migel/util/csv_importer.rb | 39 +++++++++++++++++++++++++++++++++++++-- spec/util/csv_importer_spec.rb | 27 ++++++++++++++++----------- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/lib/migel/util/csv_importer.rb b/lib/migel/util/csv_importer.rb index c85971b..41d4e54 100755 --- a/lib/migel/util/csv_importer.rb +++ b/lib/migel/util/csv_importer.rb @@ -85,7 +85,7 @@ module Migel total = File.readlines(file_name).to_a.length count = 0 @nr_products_before = get_nr_active_bauerfeind_products - set_bauerfeind_products_inactive + delete_all_inactive_bauerfeind_products products = Migel::Util::Server.new.all_products CSV.foreach(file_name, :col_sep => ';') do |line| count += 1 @@ -115,7 +115,6 @@ module Migel :article_name_fr => line[8].gsub(/,([^\s])/, ", \\1"), } # puts "#{Time.now}: Short/long do not match in line #{count}: #{line}" unless line[3].eql?(line[8]) && line[2].eql?(line[7]) - @migel_codes_with_products << migel_code if with_matching_pharmacode.size == 1 update_product_from_csv(migelid, record) @@ -142,6 +141,42 @@ module Migel true end private + def delete_all_inactive_bauerfeind_products + total = 0 + Migel::Model::Migelid.all.each do |migel_id| + nr_deleted = 0 + migel_code = migel_id.migel_code + migel_id.products.find_all {|x| x && x.companyname && x.companyname.de && /bauerfeind/i.match(x.companyname.de.force_encoding('UTF-8')) }.each do |product| + puts("#{Time.now}: delete_all_bauerfeind_products. #{product.odba_id} #{product.pharmacode} #{product.ean_code} #{migel_code}") + product.odba_delete + nr_deleted += 1 + end + next if nr_deleted == 0 + migel_id.products.delete_if{|x| x && x.companyname && x.companyname.de && /bauerfeind/i.match(x.companyname.de.force_encoding('UTF-8'))} + puts("#{Time.now}: Deleted #{nr_deleted} product from #{migel_code}") + migel_id.odba_store + total += nr_deleted + end + Migel::Model::Product.all.odba_store unless defined?(RSpec) + Migel::Model::Migelid.all.odba_store unless defined?(RSpec) + # Now we need to check whether there are products laying around, which no valid migel_code, but still belong to Bauerfeind. + nr_bauerfeind = Migel::Model::Product.all.find_all{|x| /bauerfeind/i.match(x.companyname.to_s)}.size + puts("#{Time.now}: Deleted #{total} products. Having #{get_nr_active_bauerfeind_products} active_bauerfeind_products of #{nr_bauerfeind}") + if nr_bauerfeind > 0 + second = 0 + Migel::Model::Product.all.find_all{|x| /bauerfeind/i.match(x.companyname.to_s)}.each do |product| + puts("#{Time.now}: delete_all_bauerfeind_products. #{product.odba_id} #{product.pharmacode} #{product.ean_code} #{migel_code}") + product.odba_delete + second += 1 + end + Migel::Model::Product.all.odba_store unless defined?(RSpec) + Migel::Model::Migelid.all.odba_store unless defined?(RSpec) + nr_bauerfeind = Migel::Model::Product.all.find_all{|x| /bauerfeind/i.match(x.companyname.to_s)}.size + puts("#{Time.now}: Second deleted of #{second} products. Having #{get_nr_active_bauerfeind_products} active_bauerfeind_products of #{nr_bauerfeind}") + end + get_nr_active_bauerfeind_products + puts("#{Time.now}: Done with delete_all_bauerfeind_products (Deleted #{total} products. Having #{get_nr_active_bauerfeind_products} active_bauerfeind_products") + end def restart_migel_server(sleep_time= defined?(RSpec) ? 0 : 5) pid = `/bin/ps -C ruby -Opid | /bin/grep migeld | /usr/bin/awk '{print $1}'` if pid.to_i != 0 diff --git a/spec/util/csv_importer_spec.rb b/spec/util/csv_importer_spec.rb index c7b5509..5f9725e 100755 --- a/spec/util/csv_importer_spec.rb +++ b/spec/util/csv_importer_spec.rb @@ -72,18 +72,21 @@ module Migel allow(ODBA.cache).to receive(:index_keys).and_return(['migel_code']) multilingual = double('multilingual') multilingual_company = double('multilingual_company') - expect(multilingual_company).to receive(:de=).with('Bauerfeind AG').and_return(nil) - expect(multilingual_company).to receive(:fr=).with('Bauerfeind SA').and_return(nil) + expect(multilingual_company).to receive(:de).with(no_args).and_return('Bauerfeind AG').at_least(1) + # expect(multilingual_company).to receive(:de=).with('Bauerfeind AG').and_return(nil) + # expect(multilingual_company).to receive(:fr=).with('Bauerfeind SA').and_return(nil) multilingual = double('multilingual') - expect(multilingual).to receive(:de=).with('AchilloTrain, titan, rechts, 1').and_return(nil) - expect(multilingual).to receive(:fr=).with('AchilloTrain, titane, droit, 1').and_return(nil) + # expect(multilingual).to receive(:de=).with('AchilloTrain, titan, rechts, 1').and_return(nil) + # expect(multilingual).to receive(:fr=).with('AchilloTrain, titane, droit, 1').and_return(nil) expect(multilingual).to receive(:save).with(no_args).and_return(nil).never product_2244350 = double('product_2244350', :article_name => multilingual, :migel_code => MigelTestCode, :ean_code= => nil, - :pharmacode => '2244350') + :pharmacode => '2244350', + :odba_delete => nil, + ) venotrain_names = double('venotrain_names') expect(venotrain_names).to receive(:de=).with(any_args).and_return(nil).never expect(venotrain_names).to receive(:fr=).with(any_args).and_return(nil).never @@ -101,17 +104,18 @@ module Migel :delete => true, :products => [product_2244350], :add_product => nil, + :odba_store => nil, :save => nil, ) - expect(product_2244350).to receive(:migelid=).and_return(nil) + expect(product_2244350).to receive(:migelid=).and_return(nil).never expect(product_2244350).to receive(:migelid).and_return(migelid).never - expect(product_2244350).to receive(:ppub=).with('120.50').and_return(nil) - expect(product_2244350).to receive(:save).with(no_args).and_return(nil).at_least(:once) - expect(product_2244350).to receive(:ean_code).with(no_args).and_return('4046445108532').at_least(:once) - expect(product_2244350).to receive(:ean_code=).with("4046445108009").and_return(nil) + expect(product_2244350).to receive(:ppub=).with('120.50').and_return(nil).never + expect(product_2244350).to receive(:save).with(no_args).and_return(nil).never + expect(product_2244350).to receive(:ean_code).with(no_args).and_return('4046445108532') + expect(product_2244350).to receive(:ean_code=).with("4046445108009").and_return(nil).never expect(product_2244350).to receive(:ean_code=).with("4046445108532").and_return(nil).never @status_2244350 = nil - expect(product_2244350).to receive(:status=).with("A").and_return(@status_2244350 = 'A') + expect(product_2244350).to receive(:status=).with("A").and_return(@status_2244350 = 'A').never expect(product_2244350).to receive(:status).and_return('A').at_least(1) expect(product_2244350).to receive(:companyname).with(no_args).and_return(multilingual_company).at_least(1) @status_with_ean = nil @@ -124,6 +128,7 @@ module Migel allow(ODBA.cache).to receive(:fetch_named).with('all_products', any_args).and_return( { MigelTestCode => product_2244350, VenoTrainCode => product_with_ean}) expect(Migel::Model::Product).to receive(:all).with(no_args).and_return( [ product_2244350, product_with_ean]).at_least(1) expect(Migel::Model::Migelid).to receive(:all).with(no_args).and_return([migelid]).once + expect(Migel::Model::Migelid).to receive(:all).with(no_args).and_return([]).once expect(Migel::Model::Migelid).to receive(:find_by_migel_code).with('9999.99.99.99').and_return(nil).once migelid_genutrain = double('migelid_genutrain', :migel_code => GenuTrainCode, -- 1.8.5.2