Story#105846070 - if I don't have orgs in Ariregister, then cache that answer as well

This commit is contained in:
Vladimir Krylov 2016-01-25 17:59:13 +02:00
parent 216c4e9fa0
commit addf9638d2
2 changed files with 31 additions and 36 deletions

View file

@ -23,16 +23,16 @@ class BusinessRegistryCache < ActiveRecord::Base
def associated_domains def associated_domains
domains = [] domains = []
contact_ids = associated_businesses.map do |bic| contact_ids = associated_businesses.map do |bic|
Contact.select(:id).where("ident = ? AND ident_type = 'org' AND ident_country_code = 'EE'", bic).pluck(:id) Contact.where("ident = ? AND ident_type = 'org' AND ident_country_code = 'EE'", bic).pluck(:id)
end end.flatten
contact_ids = Contact.select(:id).where("ident = ? AND ident_type = 'priv' AND ident_country_code = ?", contact_ids += Contact.where("ident = ? AND ident_type = 'priv' AND ident_country_code = ?",
ident, ident_country_code).pluck(:id) + contact_ids ident, ident_country_code).pluck(:id)
contact_ids.flatten!.compact! unless contact_ids.blank?
unless contact_ids.blank? unless contact_ids.blank?
contact_ids.uniq! domains = DomainContact.select(:domain_id).distinct.where(contact_id: contact_ids).pluck(:domain_id)
domains = DomainContact.select(:domain_id).distinct.where("contact_id in (?)", contact_ids).pluck(:domain_id)
end end
Domain.includes(:registrar, :registrant).where('id in (?)', domains)
Domain.includes(:registrar, :registrant).where(id: domains)
end end
class << self class << self
@ -50,25 +50,23 @@ class BusinessRegistryCache < ActiveRecord::Base
end end
def fetch_by_ident_and_cc(ident_code, ident_cc) 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 # 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) if cache.retrieved_on && cache.retrieved_on > (Time.zone.now - Setting.days_to_keep_business_registry_cache.days)
businesses = business_registry.associated_businesses(ident_code, ident_cc) Rails.logger.info("#{msg_start} Info loaded from cache")
unless businesses.nil? return cache
if cache.blank?
cache = BusinessRegistryCache.new(businesses)
else
cache.update businesses
end
cache.save
else
cache = [] # expired data is forbidden
end end
cache.attributes = business_registry.associated_businesses(ident_code, ident_cc)
Rails.logger.info("#{msg_start} Info loaded from server")
cache.save
cache cache
end end
def business_registry def business_registry
# TODO: can this be cached and shared?
Soap::Arireg.new Soap::Arireg.new
end end

View file

@ -95,22 +95,19 @@ module Soap
'fyysilise_isiku_koodi_riik' => country_code_3(ident_cc) 'fyysilise_isiku_koodi_riik' => country_code_3(ident_cc)
) )
content = extract response, :paringesindus_v4_response content = extract response, :paringesindus_v4_response
unless content.blank? if content.present? && content[:ettevotjad].key?(:item)
if content[:ettevotjad].key? :item business_ident = items(content, :ettevotjad).map{|item| item[:ariregistri_kood]}
business_ident = items(content, :ettevotjad).map do |item| else
# debug helps users gather data for testing business_ident = []
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
end end
{
ident: ident,
ident_country_code: ident_cc,
# ident_type: 'priv',
retrieved_on: Time.now,
associated_businesses: business_ident
}
rescue Savon::SOAPFault => fault rescue Savon::SOAPFault => fault
Rails.logger.error "#{fault} Äriregister arireg #{self.class.username} at #{self.class.host }" Rails.logger.error "#{fault} Äriregister arireg #{self.class.username} at #{self.class.host }"
raise NotAvailableError.new(exception: fault) raise NotAvailableError.new(exception: fault)