Class: VoucherForm::Referral
- Inherits:
-
Object
- Object
- VoucherForm::Referral
- Includes:
- DefaultErrorContainer
- Defined in:
- app/forms/voucher_form/referral.rb
Overview
generate voucher with valid referral code
Instance Attribute Summary collapse
-
#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.
-
#referrer ⇒ Object
Returns the value of attribute referrer.
-
#temporary ⇒ Object
Returns the value of attribute temporary.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
-
#build_voucher ⇒ Object
Voucher instance if valid_referral and nil if invalid.
-
#initialize(user_id:, code:, temporary: false, country_code: ApiV5::Constants::COUNTRY_CODE_TH) ⇒ Referral
constructor
A new instance of Referral.
- #ref_voucher_code ⇒ Object
- #valid? ⇒ Boolean
Methods included from DefaultErrorContainer
#error, #error_message_simple, #merge_errors
Constructor Details
#initialize(user_id:, code:, temporary: false, country_code: ApiV5::Constants::COUNTRY_CODE_TH) ⇒ Referral
Returns a new instance of Referral.
13 14 15 16 17 18 19 20 |
# File 'app/forms/voucher_form/referral.rb', line 13 def initialize(user_id:, code:, temporary: false, country_code: ApiV5::Constants::COUNTRY_CODE_TH) @referrer = User.find_referrer_based_on_voucher_code(code) @user = User.find_by(id: user_id) @code = code @temporary = temporary @country = Country.find_by(alpha3: country_code) @currency_code = @country ? @country.currency_code_upcase : Country::THAI_CURRENCY_CODE end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
7 8 9 |
# File 'app/forms/voucher_form/referral.rb', line 7 def code @code end |
#country ⇒ Object
Returns the value of attribute country.
7 8 9 |
# File 'app/forms/voucher_form/referral.rb', line 7 def country @country end |
#currency_code ⇒ Object
Returns the value of attribute currency_code.
7 8 9 |
# File 'app/forms/voucher_form/referral.rb', line 7 def currency_code @currency_code end |
#referrer ⇒ Object
Returns the value of attribute referrer.
7 8 9 |
# File 'app/forms/voucher_form/referral.rb', line 7 def referrer @referrer end |
#temporary ⇒ Object
Returns the value of attribute temporary.
7 8 9 |
# File 'app/forms/voucher_form/referral.rb', line 7 def temporary @temporary end |
#user ⇒ Object
Returns the value of attribute user.
7 8 9 |
# File 'app/forms/voucher_form/referral.rb', line 7 def user @user end |
Instance Method Details
#build_voucher ⇒ Object
Returns Voucher instance if valid_referral and nil if invalid.
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 |
# File 'app/forms/voucher_form/referral.rb', line 23 def build_voucher return nil unless valid_referral? referral_voucher = Voucher.find_by(name: 'Referral program', user_id: user.id) if referral_voucher.present? return nil unless referral_voucher.voucher_code.include?(code.to_s) return referral_voucher end return nil if user.r_code.present? amount = if country&.currency_code_upcase == Country::SINGAPORE_CURRENCY_CODE ::Reservation::AMOUNT_REFERRER_REWARD_POINT_SG elsif country&.currency_code_upcase == Country::MALAYSIA_CURRENCY_CODE ::Reservation::AMOUNT_REFERRER_REWARD_POINT_MY else ::Reservation::AMOUNT_REFERRER_REWARD_POINT_TH end today = Time.thai_time.to_date Voucher.new( id: temporary? ? Voucher.last.id + rand(9999) : nil, country: Voucher.default_country, user_id: user.id, voucher_type: 'specific_customer', name: 'Referral program', voucher_code: Voucher.generate_referral_code(@referrer.short_my_r_code), amount: 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, apply_for: 'package', start_date: today.beginning_of_day, end_date: (today + 30.days).end_of_day, active: true, payment_type_ids: PaymentType.ids, require_full_prepayment: true, first_booking_only: true, country_id: country&.id, ) end |
#ref_voucher_code ⇒ Object
112 113 114 |
# File 'app/forms/voucher_form/referral.rb', line 112 def ref_voucher_code Voucher.generate_referral_code(referrer.short_my_r_code) end |
#valid? ⇒ Boolean
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 |
# File 'app/forms/voucher_form/referral.rb', line 72 def valid? if user.blank? errors.add(:base, I18n.t('views.voucher.require_login_ref_code')) return false end if !valid_referral_code?(code) errors.add(:base, 'Invalid referral code') return false end if (referrer.blank? && !claimed_but_not_used?) || (referrer&.email == user.email) errors.add(:base, 'Invalid referral code') return false end if invalid_first_booking_user?(user&.email, user&.phone) errors.add(:base, I18n.t('views.voucher.invalid_referral_code', email_used: user&.email)) return false end if user.referrer.present? && !claimed_but_not_used? errors.add(:base, 'You already use referral code') return false end if user.vouchers.active.where('voucher_code LIKE ?', "%#{User::REFERRAL_CODE_PREFIX}%").present? && !claimed_but_not_used? errors.add(:base, I18n.t('views.voucher.invalid_usage_ref_code')) return false end if referrer&.email == user&.email errors.add(:base, 'Invalid referral code') return false end true end |