Class: UserSpendingPerPersonWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/user_spending_per_person_worker.rb

Overview

typed: ignore Update User#total_spending after user made booking and arrived

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#perform(reservation_id) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/workers/user_spending_per_person_worker.rb', line 4

def perform(reservation_id)
  reservation = Reservation.find(reservation_id)

  return false if reservation.cancelled? || reservation.user.blank? || !reservation.package? || !reservation.reached_goal?

  user = reservation.user
  avg_spend_per_person = user.avg_spend_per_person.presence || 0
  reservations_total_adult = user.reservations.reached_goal_scope.sum(:adult)
  return if reservations_total_adult.zero?

  total_avg_spend_per_person = user.reservations.reached_goal_scope.sum(:total_price) / reservations_total_adult

  user.update(avg_spend_per_person: total_avg_spend_per_person)
end