Class: VoucherForm::Reward
- Inherits:
-
Object
- Object
- VoucherForm::Reward
- Includes:
- DefaultErrorContainer
- Defined in:
- app/forms/voucher_form/reward.rb
Overview
typed: ignore frozen_string_literal: true
Instance Attribute Summary collapse
-
#campaign_redeemed_amount ⇒ Object
Returns the value of attribute campaign_redeemed_amount.
-
#code ⇒ Object
Returns the value of attribute code.
-
#country ⇒ Object
Returns the value of attribute country.
-
#currency_code ⇒ Object
Returns the value of attribute currency_code.
-
#redeemed_points ⇒ Object
Returns the value of attribute redeemed_points.
-
#restaurant ⇒ Object
Returns the value of attribute restaurant.
-
#temporary ⇒ Object
Returns the value of attribute temporary.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #build_voucher ⇒ Object
-
#initialize(user_id:, redeemed_points:, temporary: false, campaign_redeemed_amount: nil, country_code: ApiV5::Constants::COUNTRY_CODE_TH, restaurant: nil) ⇒ Reward
constructor
A new instance of Reward.
- #redeem! ⇒ Object
- #reward_voucher_code ⇒ Object
- #valid_redeem_points? ⇒ Boolean
Methods included from DefaultErrorContainer
#error, #error_message_simple, #merge_errors
Constructor Details
#initialize(user_id:, redeemed_points:, temporary: false, campaign_redeemed_amount: nil, country_code: ApiV5::Constants::COUNTRY_CODE_TH, restaurant: nil) ⇒ Reward
Returns a new instance of Reward.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/forms/voucher_form/reward.rb', line 9 def initialize(user_id:, redeemed_points:, temporary: false, campaign_redeemed_amount: nil, country_code: ApiV5::Constants::COUNTRY_CODE_TH, restaurant: nil) @user = User.find_by(id: user_id) raise ArgumentError, 'User not found' unless @user @redeemed_points = redeemed_points @campaign_redeemed_amount = campaign_redeemed_amount.presence || Country.convert_points_to_currency( redeemed_points, currency_code ) @temporary = temporary @country = Country.find_by(alpha3: country_code) @currency_code = @country&.currency_code_upcase.presence || Country::THAI_CURRENCY_CODE @restaurant = restaurant end |
Instance Attribute Details
#campaign_redeemed_amount ⇒ Object
Returns the value of attribute campaign_redeemed_amount.
6 7 8 |
# File 'app/forms/voucher_form/reward.rb', line 6 def campaign_redeemed_amount @campaign_redeemed_amount end |
#code ⇒ Object
Returns the value of attribute code.
6 7 8 |
# File 'app/forms/voucher_form/reward.rb', line 6 def code @code end |
#country ⇒ Object
Returns the value of attribute country.
6 7 8 |
# File 'app/forms/voucher_form/reward.rb', line 6 def country @country end |
#currency_code ⇒ Object
Returns the value of attribute currency_code.
6 7 8 |
# File 'app/forms/voucher_form/reward.rb', line 6 def currency_code @currency_code end |
#redeemed_points ⇒ Object
Returns the value of attribute redeemed_points.
6 7 8 |
# File 'app/forms/voucher_form/reward.rb', line 6 def redeemed_points @redeemed_points end |
#restaurant ⇒ Object
Returns the value of attribute restaurant.
6 7 8 |
# File 'app/forms/voucher_form/reward.rb', line 6 def restaurant @restaurant end |
#temporary ⇒ Object
Returns the value of attribute temporary.
6 7 8 |
# File 'app/forms/voucher_form/reward.rb', line 6 def temporary @temporary end |
#user ⇒ Object
Returns the value of attribute user.
6 7 8 |
# File 'app/forms/voucher_form/reward.rb', line 6 def user @user end |
Instance Method Details
#build_voucher ⇒ Object
Returns @voucher if valid_redeem_points and nil if invalid.
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 |
# File 'app/forms/voucher_form/reward.rb', line 25 def build_voucher raise RedeemError unless valid_redeem_points? today = Time.thai_time.to_date voucher = Voucher.new voucher.attributes = { id: temporary? ? Voucher.last.id + rand(9999) : nil, user_id: user.id, voucher_type: 'specific_customer', name: 'Points Redemption', voucher_code: reward_voucher_code, amount: campaign_redeemed_amount, amount_cap_currency: currency_code, amount_currency: currency_code, currency_code: currency_code, min_total_price_currency: currency_code, max_usage: 1, expiry_range_by: 'created_at', expiry_type: 'range', quota: 1, non_refundable: true, start_date: today.beginning_of_day, end_date: (today + 30.days).end_of_day, active: true, payment_type_ids: PaymentType.ids, voucher_category: 'redemption', require_full_prepayment: true, first_booking_only: false, country_id: country&.id, } ServiceResult.new data: voucher rescue RedeemError ServiceResult.new errors: errors, message: end |
#redeem! ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/forms/voucher_form/reward.rb', line 97 def redeem! points_total = redeemed_points points_total *= -1 if redeemed_points.to_i.positive? date = Time.use_zone(user.time_zone) do Time.zone.today end reward = Reward.new reward.attributes = { points_total: points_total, points_pending: 0, redeemed: false, user: user, description: "Redeemed as voucher #{redeemed_points} points at #{date}", created_at: Time.zone.now, country_id: country&.id, } reward.save! end |
#reward_voucher_code ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/forms/voucher_form/reward.rb', line 83 def reward_voucher_code @codes ||= [] new_code = nil loop do new_code = "#{code.to_s.upcase}#{SecureRandom.hex(5).upcase}" next if @codes.include? code @codes.push new_code break if Voucher.find_by(voucher_code: new_code, amount_currency: currency_code).nil? end new_code end |
#valid_redeem_points? ⇒ Boolean
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/forms/voucher_form/reward.rb', line 62 def valid_redeem_points? # make sure the points will be redeemed from the user is valid for the restaurant if restaurant.present? && restaurant&.country&.currency_code_upcase != currency_code.to_s.upcase errors.add(:base, I18n.t('views.voucher.invalid_points_country', points_country: currency_code, restaurant_country: restaurant.country&.name)) return false end if redeemed_points.zero? errors.add(:base, 'Invalid redeemed points 0') return false end if user.get_credits(country&.id) < redeemed_points errors.add(:base, I18n.t('views.voucher.invalid_point_balance', country: country&.name)) return false end true end |