Created
December 23, 2020 19:10
-
-
Save mpautasso/33765a9c9b7c0c84cc4f8512a2be923e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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