Skip to content

Instantly share code, notes, and snippets.

@mpautasso
Created December 23, 2020 19:10
Show Gist options
  • Save mpautasso/33765a9c9b7c0c84cc4f8512a2be923e to your computer and use it in GitHub Desktop.
Save mpautasso/33765a9c9b7c0c84cc4f8512a2be923e to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
desc 'update existing engagements with direction'
task :update_direction_in_engagement, %i[tenant] =>
[:environment] do |_t, args|
logger = Logger.new(Rails.root.join('log', 'rake.log'))
logger.info 'UpdateDirectionInEngagement -- START'
Apartment::Tenant.switch(args[:tenant]) do
ActiveRecord::Base.connection.uncached do
Engagement.in_batches(of: 500) do |engagements |
engagements.each do |engagement|
ordered_events = engagement.events.order(sent_at: :asc)
first_event=ordered_events.where.not(type: 'state_update').first
event_payload = JSON.parse(first_event.payload)
engagement.update(direction: event_payload['direction'] || '')
end
end
end
end
logger.info 'update_direction_in_engagement -- END'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment