Module: UserRegistration::SubscribeVoucherReward

Included in:
AccountCpt::Registration::RegularUser::WithFacebookV2, AccountCpt::Registration::RegularUser::WithFirebase, UsingBirthday, UsingPassword
Defined in:
app/my_lib/user_registration/subscribe_voucher_reward.rb

Overview

typed: ignore frozen_string_literal: true

Instance Method Summary collapse

Instance Method Details

#voucher_reward_subscriptionObject

rubocop:disable Metrics/MethodLength



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
# File 'app/my_lib/user_registration/subscribe_voucher_reward.rb', line 5

def voucher_reward_subscription # rubocop:disable Metrics/MethodLength
  return unless user.subscribe_marketing_email?

  user_country = user.country || Country.find_thailand
  currency_code = case user_country&.alpha3
                  when ApiV5::Constants::COUNTRY_CODE_SG
                    Country::SINGAPORE_CURRENCY_CODE
                  when ApiV5::Constants::COUNTRY_CODE_MY
                    Country::MALAYSIA_CURRENCY_CODE
                  else
                    Country::THAI_CURRENCY_CODE
                  end

  voucher_amount = Voucher.const_get("REGISTRATION_REWARD_AMOUNT_#{currency_code}")

  today = Time.thai_time.to_date
  vouchers = []
  ActiveRecord::Base.transaction do
    3.times do
      v = Voucher.new
      v.name = 'Registration Reward'
      v.voucher_code = Voucher.generate_voucher_code
      v.user = user
      v.active = true
      v.amount = voucher_amount
      v.amount_cap_currency = currency_code
      v.amount_currency = currency_code
      v.currency_code = currency_code
      v.min_total_price_currency = currency_code
      v.max_usage = 1
      v.max_usage_user = 1
      v.country = user_country
      v.quota = 1
      v.apply_for = 'package'
      v.voucher_type = 'specific_customer'
      v.voucher_category = 'marketing'
      v.expiry_type = 'range'
      v.expiry_range_by = 'dining_date'
      v.start_date = today.beginning_of_day
      v.end_date = (today + 31.days).end_of_day
      v.require_full_prepayment = true
      v.payment_type_ids = PaymentType.valid_payment_types.pluck(:id)

      v.save!

      vouchers << v
    end
  end

  vouchers
end