Files
archived-hc-harbor/app/jobs/check_streak_physical_mail_job.rb
2025-12-01 11:12:21 -05:00

45 lines
1.2 KiB
Ruby

class CheckStreakPhysicalMailJob < ApplicationJob
queue_as :literally_whenever
include GoodJob::ActiveJobExtensions::Concurrency
good_job_control_concurrency_with(
total_limit: 1,
key: -> { "check_streak_physical_mail_job" },
drop: true
)
def perform
streaks = Heartbeat.daily_streaks_for_users(users_with_recent_heartbeats)
over_7_day_streaks = streaks.select { |_, streak| streak > 7 }.keys
existing_user_ids = PhysicalMail.going_out
.where(mission_type: :first_time_7_streak)
.where(user_id: over_7_day_streaks)
.pluck(:user_id)
.to_set
over_7_day_streaks.each do |user_id|
next if existing_user_ids.include?(user_id)
user = User.find(user_id)
# Create the physical mail record
PhysicalMail.create!(
user: user,
mission_type: :first_time_7_streak,
status: :pending
)
end
end
private
def users_with_recent_heartbeats
Heartbeat.where(time: 1.hour.ago..Time.current)
.distinct
.pluck(:user_id)
end
end