vol1ura/Sat_9am_5km

View on GitHub
app/jobs/funrun_awarding_job.rb

Summary

Maintainability
A
25 mins
Test Coverage
A
90%
# frozen_string_literal: true

# Award by funrun or jubilee badge on published activity
class FunrunAwardingJob < ApplicationJob
  queue_as :default

  def perform(activity_id, badge_id)
    activity = Activity.published.find activity_id
    badge = Badge.find badge_id
    return unless badge.funrun_kind? || badge.jubilee_participating_kind?

    activity.participants.where.not(id: badge.trophies.select(:athlete_id)).find_each do |athlete|
      athlete.trophies.create badge: badge, date: activity.date
      next if athlete.valid?

      Rollbar.error(
        "Awarding by '#{badge.kind}' badge failed",
        errors: athlete.errors.inspect,
        athlete_id: athlete.id,
        badge_id: badge.id,
      )
    end
  end
end