Class: UserTotalBookingPerTypeWorker

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

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#get_total_booking(user, reservation, package_type) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/workers/user_total_booking_per_type_worker.rb', line 15

def get_total_booking(user, reservation, package_type)
  total = user.send("total_#{package_type}_reservation".to_sym).to_i
  package_type_list = HhPackage.package_short_to_list package_type

  if total > 0 # if user already have total booking for this package type then just add the new booking
    total + reservation.reservation_packages.where(type: "HhPackage::Package::#{package_type_list}").sum(:quantity)
  else # if user doesn't have total booking for this package type then calculate the total booking from all reservations with the same package type
    reached_goal_reservation_ids = user.reservations.reached_goal_scope.pluck(:id)
    ReservationPackage.where(reservation_id: reached_goal_reservation_ids).
      where(type: "HhPackage::Package::#{package_type_list}").
      sum(:quantity)
  end
end

#perform(reservation_id) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
# File 'app/workers/user_total_booking_per_type_worker.rb', line 2

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
  package_type = reservation.package[:package_type]

  if user.respond_to? "total_#{package_type}_reservation".to_sym
    user.update("total_#{package_type}_reservation".to_sym => get_total_booking(user, reservation, package_type))
  end
end