Class: EarlyBird::RewardOperation
- Inherits:
-
Trailblazer::Operation
- Object
- Trailblazer::Operation
- EarlyBird::RewardOperation
- Extended by:
- Contract::DSL
- Includes:
- LoyaltyProgram::Benefits::EarlyBird
- Defined in:
- lib/early_bird/reward_operation.rb
Overview
Responsible to give reward to customer This class called asynchronously (executed in Sidekiq)
Constant Summary collapse
- VOUCHER_NAME =
'Early Bird campaign'
Instance Method Summary collapse
- #find_model!(options) ⇒ Object
- #give_reward!(_options) ⇒ Object
- #invalid!(_exception) ⇒ Object
- #report_failure!(exception, options) ⇒ Object
- #update_review_details!(_options) ⇒ Object
-
#validate_duplicate_early_bird_in_reservation!(_options) ⇒ Object
Prevent duplicate early bird voucher for one reservation.
-
#validate_duplicate_early_bird_in_restaurant!(_options) ⇒ Object
Early bird voucher only valid to one user per restaurant.
- #validate_review_description!(_options) ⇒ Object
- #validate_review_rating!(_options) ⇒ Object
- #validate_total_reviews!(_options) ⇒ Object
- #validate_user_registration_status!(_options) ⇒ Object
Instance Method Details
#find_model!(options) ⇒ Object
33 34 35 |
# File 'lib/early_bird/reward_operation.rb', line 33 def find_model!(, *) @review = ['review'] = Review.find(['params']['review_id']) end |
#give_reward!(_options) ⇒ Object
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 |
# File 'lib/early_bird/reward_operation.rb', line 86 def give_reward!(, *) user = reservation.user amount = early_bird_rewards_amount(user, reservation.restaurant.country.alpha3) restaurant_currency = reservation.restaurant&.default_currency || Country::THAI_CURRENCY_CODE voucher = Voucher.new voucher.attributes = { active: true, user_id: user_id, name: VOUCHER_NAME, voucher_code: Voucher.generate_voucher_code, amount: amount, amount_cap_currency: restaurant_currency, amount_currency: restaurant_currency, currency_code: restaurant_currency, min_total_price_currency: restaurant_currency, max_usage: 1, max_usage_user: 1, quota: 1, voucher_type: 'specific_customer', expiry_date: Time.zone.today + 30.days, expiry_type: 'single', voucher_category: 'marketing', payment_type_ids: PaymentType.valid_payment_types.ids, require_full_prepayment: true, early_bird_reservation_id: reservation.id, country_id: reservation.restaurant.country_id, } voucher.save! return if user.blank? user.update!(total_early_bird: user.total_early_bird + 1) end |
#invalid!(_exception) ⇒ Object
126 127 128 |
# File 'lib/early_bird/reward_operation.rb', line 126 def invalid!(_exception, *) false end |
#report_failure!(exception, options) ⇒ Object
130 131 132 133 |
# File 'lib/early_bird/reward_operation.rb', line 130 def report_failure!(exception, , *) APMErrorHandler.report('early bird - reward operation failed', exception: exception, options: ) false end |
#update_review_details!(_options) ⇒ Object
122 123 124 |
# File 'lib/early_bird/reward_operation.rb', line 122 def update_review_details!(, *) review.update!(is_early_bird: true) end |
#validate_duplicate_early_bird_in_reservation!(_options) ⇒ Object
Prevent duplicate early bird voucher for one reservation
77 78 79 80 81 82 83 84 |
# File 'lib/early_bird/reward_operation.rb', line 77 def validate_duplicate_early_bird_in_reservation!(, *) sleep(rand(100)) unless Rails.env.test? # prevent duplicate record duplicate_voucher = Voucher.where(user_id: user_id, early_bird_reservation_id: reservation.id) return true if duplicate_voucher.blank? false end |
#validate_duplicate_early_bird_in_restaurant!(_options) ⇒ Object
Early bird voucher only valid to one user per restaurant
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/early_bird/reward_operation.rb', line 54 def validate_duplicate_early_bird_in_restaurant!(, *) user_early_bird_vouchers = Voucher.where(name: VOUCHER_NAME, user_id: user_id) return true if user_early_bird_vouchers.blank? is_duplicated = false # Check the restaurant from user_early_bird_vouchers if the same with the review restaurant user_early_bird_vouchers.find_each do |voucher| reservation = Reservation.find_by(id: voucher.early_bird_reservation_id) next if reservation.blank? if reservation.restaurant_id == restaurant.id is_duplicated = true break end end return true unless is_duplicated HH_LOGGER.info 'User trying to get early bird reward from the same restaurant', review_id: review.id false end |
#validate_review_description!(_options) ⇒ Object
49 50 51 |
# File 'lib/early_bird/reward_operation.rb', line 49 def validate_review_description!(, *) review.review.present? end |
#validate_review_rating!(_options) ⇒ Object
45 46 47 |
# File 'lib/early_bird/reward_operation.rb', line 45 def (, *) review..present? end |
#validate_total_reviews!(_options) ⇒ Object
41 42 43 |
# File 'lib/early_bird/reward_operation.rb', line 41 def validate_total_reviews!(, *) restaurant.reviews.count <= ::LoyaltyLevel.valid_for_first_x_reviews.to_i end |
#validate_user_registration_status!(_options) ⇒ Object
37 38 39 |
# File 'lib/early_bird/reward_operation.rb', line 37 def validate_user_registration_status!(, *) user_id.present? end |