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
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