vol1ura/Sat_9am_5km

View on GitHub
app/jobs/results_processing_job.rb

Summary

Maintainability
A
45 mins
Test Coverage
A
100%
# frozen_string_literal: true

class ResultsProcessingJob < ApplicationJob
  queue_as :critical

  def perform(activity_id)
    activity = Activity.published.find activity_id

    activity.results.where('personal_best = TRUE OR first_run = TRUE').update_all(personal_best: false, first_run: false) # rubocop:disable Rails/SkipsModelValidations

    activity.results.where.not(athlete: nil).includes(:athlete).find_each do |result|
      past_results =
        result.athlete.results.published.where(activity: { date: ...activity.date }).pluck(:total_time, :event_id)

      next result.update!(personal_best: true, first_run: true) if past_results.empty?

      result.update!(personal_best: true) if past_results.map(&:first).min > result.total_time
      result.update!(first_run: true) unless past_results.map(&:last).include?(activity.event_id)
    end
  end
end