Sha256: 78dbf3d1fb12289e035291b05eb6480efae06cff4663fe1f3131da2c3d9d26f0

Contents?: true

Size: 975 Bytes

Versions: 8

Compression:

Stored size: 975 Bytes

Contents

module Workarea
  class UpdateEmail
    include Sidekiq::Worker
    include Sidekiq::CallbacksWorker

    sidekiq_options(
      enqueue_on: { User => :update, with: -> { [id, changes] } }
    )

    def perform(id, changes)
      return unless changes['email'].present? && changes['email'].first.present?

      old_email, new_email = changes['email']
      update_payment_profile(id, old_email, new_email)
      update_metrics(old_email, new_email)
    end

    def update_payment_profile(id, old_email, new_email)
      user = User.find(id)
      user.email = old_email # set old email so we lookup by old email value

      Payment::Profile.update_email(PaymentReference.new(user), new_email)
    end

    def update_metrics(old_email, new_email)
      old_metrics = Metrics::User.find(old_email) rescue nil
      return if old_metrics.blank?

      new_metrics = Metrics::User.find_or_initialize_by(id: new_email)
      new_metrics.merge!(old_metrics)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/workers/workarea/update_email.rb
workarea-core-3.5.26 app/workers/workarea/update_email.rb
workarea-core-3.4.45 app/workers/workarea/update_email.rb
workarea-core-3.5.25 app/workers/workarea/update_email.rb
workarea-core-3.5.23 app/workers/workarea/update_email.rb
workarea-core-3.4.44 app/workers/workarea/update_email.rb
workarea-core-3.5.22 app/workers/workarea/update_email.rb
workarea-core-3.4.43 app/workers/workarea/update_email.rb