Class: GiveReservationRewardWorker
- Inherits:
-
ApplicationWorker
- Object
- ApplicationWorker
- GiveReservationRewardWorker
- Includes:
- LoyaltyProgram::Benefits::ReviewPoints, LoyaltyProgram::SpecialBenefits::Campaign, Modules::Reservations::RewardPoints
- Defined in:
- app/workers/give_reservation_reward_worker.rb
Overview
operation class to give reward to user based on reservation id
Constant Summary collapse
- REWARD_POINT =
10
Instance Method Summary collapse
-
#initialize ⇒ GiveReservationRewardWorker
constructor
A new instance of GiveReservationRewardWorker.
- #perform(reservation_id) ⇒ Object
Methods included from LoyaltyProgram::SpecialBenefits::Campaign
#eligible_to_double_point?, #eligible_to_triple_point?
Methods inherited from ApplicationWorker
Constructor Details
#initialize ⇒ GiveReservationRewardWorker
Returns a new instance of GiveReservationRewardWorker.
14 15 16 17 18 |
# File 'app/workers/give_reservation_reward_worker.rb', line 14 def initialize @description = [] @points_total = 0 @note = [] end |
Instance Method Details
#perform(reservation_id) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/workers/give_reservation_reward_worker.rb', line 20 def perform(reservation_id) @reservation = Reservation.fetch(reservation_id) # Skip give reward/points if reservation is created from website if @reservation.created_from_website? BUSINESS_LOGGER.info('Skipping reward creation for reservation created from website', reservation_id: reservation_id) return false end # skip if restaurant is not eligible for rewards return false unless @reservation.restaurant.eligible_for_rewards? # Log at the beginning of the method send_log("Performing reward creation for reservation ID: #{reservation_id}") return false if reservation.user.nil? || !reservation.active || !reservation.ack calc_reward_for_creating_reservation calc_reward_for_creating_review find_reward_note points_description = @description.compact.join(' - ') # Check if reward already exists for this reservation to avoid duplicates # Check based on all key fields: reservation_id, user_id, points_total, description, points_pending existing_reward = Reward.find_by( reservation_id: reservation_id, user_id: reservation.user_id, points_total: @points_total, description: points_description, points_pending: 0, ) # skip if reward already exist to avoid duplicate if existing_reward.present? HH_LOGGER.warn( 'Exact duplicate reward already exists for reservation', existing_reward_id: existing_reward.id, points_total: existing_reward.points_total, calculated_points_total: @points_total, user_id: reservation.user_id, reservation_id: reservation_id, description: existing_reward.description, calculated_description: points_description, points_pending: existing_reward.points_pending, ) return end # Also check if there's any reward for this reservation/user combination # (regardless of points/description) to log potential inconsistencies any_existing_reward = Reward.find_by(reservation_id: reservation_id, user_id: reservation.user_id) if any_existing_reward.present? && any_existing_reward != existing_reward HH_LOGGER.error( 'Different reward already exists for this reservation/user - potential data inconsistency', existing_reward_id: any_existing_reward.id, existing_points_total: any_existing_reward.points_total, calculated_points_total: @points_total, existing_description: any_existing_reward.description, calculated_description: points_description, existing_points_pending: any_existing_reward.points_pending, user_id: reservation.user_id, reservation_id: reservation_id, ) end success = false reward_attributes = { restaurant_id: reservation.restaurant_id, points_total: @points_total, points_pending: 0, points_bonus: point_bonus, description: points_description, user_id: reservation.user_id, note: @note.compact.join(' - '), redeemed: false, reservation_id: reservation_id, country_id: reservation.restaurant.country_id } reward.attributes = reward_attributes if reward.created_at.blank? reward.created_at = Time.zone.now end if reward.updated_at.blank? reward.updated_at = Time.zone.now end begin ActiveRecord::Base.no_touching do reward.transaction do reward.save! success = true end end if success reward.touch_relations = "Reward creation for reservation ID: #{reservation_id} was successful" else = "Failed to create reward for reservation ID: #{reservation_id}" APMErrorHandler.report('Failed to create reward', reservation_id: reservation_id) self.class.perform_in(1.day, reservation_id) end # send log when success or failed send_log(, reward_attributes) rescue StandardError => e = "Error creating reward for reservation ID: #{reservation_id}" send_log(, { error: e }) APMErrorHandler.report(e, reservation_id: reservation_id) self.class.perform_in(1.day, reservation_id) end event_data = { number_of_point: @points_total, reservation_id: reservation.id, } Netcore::EventWorker.perform_async(:send_custom_event, 'Point Earned', event_data, reservation.email) true end |