From addf9638d27f55c6aa04f8f0de50ef1d1eb5f6ff Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Mon, 25 Jan 2016 17:59:13 +0200 Subject: [PATCH] Story#105846070 - if I don't have orgs in Ariregister, then cache that answer as well --- app/models/business_registry_cache.rb | 40 +++++++++++++-------------- app/models/soap/arireg.rb | 27 ++++++++---------- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/app/models/business_registry_cache.rb b/app/models/business_registry_cache.rb index 6bd509101..e287358b6 100644 --- a/app/models/business_registry_cache.rb +++ b/app/models/business_registry_cache.rb @@ -23,16 +23,16 @@ class BusinessRegistryCache < ActiveRecord::Base def associated_domains domains = [] contact_ids = associated_businesses.map do |bic| - Contact.select(:id).where("ident = ? AND ident_type = 'org' AND ident_country_code = 'EE'", bic).pluck(:id) - end - contact_ids = Contact.select(:id).where("ident = ? AND ident_type = 'priv' AND ident_country_code = ?", - ident, ident_country_code).pluck(:id) + contact_ids - contact_ids.flatten!.compact! unless contact_ids.blank? + Contact.where("ident = ? AND ident_type = 'org' AND ident_country_code = 'EE'", bic).pluck(:id) + end.flatten + contact_ids += Contact.where("ident = ? AND ident_type = 'priv' AND ident_country_code = ?", + ident, ident_country_code).pluck(:id) + unless contact_ids.blank? - contact_ids.uniq! - domains = DomainContact.select(:domain_id).distinct.where("contact_id in (?)", contact_ids).pluck(:domain_id) + domains = DomainContact.select(:domain_id).distinct.where(contact_id: contact_ids).pluck(:domain_id) end - Domain.includes(:registrar, :registrant).where('id in (?)', domains) + + Domain.includes(:registrar, :registrant).where(id: domains) end class << self @@ -50,25 +50,23 @@ class BusinessRegistryCache < ActiveRecord::Base end def fetch_by_ident_and_cc(ident_code, ident_cc) - cache = BusinessRegistryCache.find_by(ident: ident_code, ident_country_code: ident_cc) + cache = BusinessRegistryCache.first_or_initialize(ident: ident_code, ident_country_code: ident_cc) + msg_start = "[Ariregister] #{ident_cc}-#{ident_code}:" + # fetch new data if cache is expired - return cache if cache.present? && cache.retrieved_on > (Time.zone.now - Setting.days_to_keep_business_registry_cache.days) - businesses = business_registry.associated_businesses(ident_code, ident_cc) - unless businesses.nil? - if cache.blank? - cache = BusinessRegistryCache.new(businesses) - else - cache.update businesses - end - cache.save - else - cache = [] # expired data is forbidden + if cache.retrieved_on && cache.retrieved_on > (Time.zone.now - Setting.days_to_keep_business_registry_cache.days) + Rails.logger.info("#{msg_start} Info loaded from cache") + return cache end + + cache.attributes = business_registry.associated_businesses(ident_code, ident_cc) + Rails.logger.info("#{msg_start} Info loaded from server") + + cache.save cache end def business_registry - # TODO: can this be cached and shared? Soap::Arireg.new end diff --git a/app/models/soap/arireg.rb b/app/models/soap/arireg.rb index 5c8db3475..cd8e446b4 100644 --- a/app/models/soap/arireg.rb +++ b/app/models/soap/arireg.rb @@ -95,22 +95,19 @@ module Soap 'fyysilise_isiku_koodi_riik' => country_code_3(ident_cc) ) content = extract response, :paringesindus_v4_response - unless content.blank? - if content[:ettevotjad].key? :item - business_ident = items(content, :ettevotjad).map do |item| - # debug helps users gather data for testing - puts "#{item[:ariregistri_kood]}\t#{item[:arinimi]}\t#{item[:staatus]} #{item[:oiguslik_vorm]}\t" if @debug - item[:ariregistri_kood] - end - { - ident: ident, - ident_country_code: ident_cc, - # ident_type: 'priv', - retrieved_on: Time.now, - associated_businesses: business_ident - } - end + if content.present? && content[:ettevotjad].key?(:item) + business_ident = items(content, :ettevotjad).map{|item| item[:ariregistri_kood]} + else + business_ident = [] end + + { + ident: ident, + ident_country_code: ident_cc, + # ident_type: 'priv', + retrieved_on: Time.now, + associated_businesses: business_ident + } rescue Savon::SOAPFault => fault Rails.logger.error "#{fault} Äriregister arireg #{self.class.username} at #{self.class.host }" raise NotAvailableError.new(exception: fault)