namespace :realogy do def active_job_configured? return Rails.application.config.active_job.queue_adapter.eql?(:async) ? false : true end desc "Sync all active entities" task :sync_active_entities => [:sync_active_agents, :sync_active_companies, :sync_active_listings, :sync_active_offices, :sync_active_teams] do puts end desc "Sync Active Agents" task :sync_active_agents, [:country_code] => [:environment] do |t, args| args.with_defaults(country_code: nil) Realogy::DataSync.client.get_active_agents({countryCode: args["country_code"]}).each do |hash| hash['class'] = "Realogy::Agent" active_job_configured? ? PopulateRealogyEntityJob.perform_later(hash) : Realogy::Agent::triage(hash) end end desc "Sync Active Companies" task :sync_active_companies, [:country_code] => [:environment] do |t, args| args.with_defaults(country_code: nil) Realogy::DataSync.client.get_active_companies({countryCode: args["country_code"]}).each do |hash| hash['class'] = "Realogy::Company" active_job_configured? ? PopulateRealogyEntityJob.perform_later(hash) : Realogy::Company::triage(hash) end end desc "Sync Active Listings" task :sync_active_listings, [:country_code] => [:environment] do |t, args| args.with_defaults(country_code: nil) Realogy::DataSync.client.get_active_listings({countryCode: args["country_code"]}).each do |hash| hash['class'] = "Realogy::Listing" active_job_configured? ? PopulateRealogyEntityJob.perform_later(hash) : Realogy::Listing::triage(hash) end end desc "Sync Active Offices" task :sync_active_offices, [:country_code] => [:environment] do |t, args| args.with_defaults(country_code: nil) Realogy::DataSync.client.get_active_offices({countryCode: args["country_code"]}).each do |hash| hash['class'] = "Realogy::Office" active_job_configured? ? PopulateRealogyEntityJob.perform_later(hash) : Realogy::Office::triage(hash) end end desc "Sync Active Teams" task :sync_active_teams, [:country_code] => [:environment] do |t, args| args.with_defaults(country_code: nil) Realogy::DataSync.client.get_active_teams({countryCode: args["country_code"]}).each do |hash| hash['class'] = "Realogy::Team" active_job_configured? ? PopulateRealogyEntityJob.perform_later(hash) : Realogy::Team::triage(hash) end end def delete_expired_entities_of_type klass return unless %w(agents companies listings offices teams).include?(plural = klass.to_s.tableize.split("/").last) call = ["get_active_", plural].join.to_sym existing = klass.select(:entity_id).pluck(:entity_id) current = Realogy::DataSync.client.__send__(call).map{|e| e["entityId"]} klass.where(entity_id: (existing - current)).delete_all end desc "Delete all expired entities" task :delete_expired_entities => [:delete_expired_agents, :delete_expired_companies, :delete_expired_listings, :delete_expired_offices, :delete_expired_teams] do puts end desc "Delete Expired Agents" task :delete_expired_agents => [:environment] do |t| delete_expired_entities_of_type Realogy::Agent end desc "Delete Expired Companies" task :delete_expired_companies => [:environment] do |t| delete_expired_entities_of_type Realogy::Company end desc "Delete Expired Listings" task :delete_expired_listings => [:environment] do |t| delete_expired_entities_of_type Realogy::Listing end desc "Delete Expired Offices" task :delete_expired_offices => [:environment] do |t| delete_expired_entities_of_type Realogy::Office end desc "Delete Expired Teams" task :delete_expired_teams => [:environment] do |t| delete_expired_entities_of_type Realogy::Team end end