Class: LoyaltyLevel

Inherits:
ApplicationRecord show all
Defined in:
app/models/loyalty_level.rb

Overview

Schema Information

Table name: loyalty_levels

id                                            :bigint           not null, primary key
background_color                              :string(191)
benefits_dine_in_point                        :float(24)
benefits_dine_in_point_active                 :boolean
benefits_dine_in_point_description            :text(65535)
benefits_dine_in_point_description_sg         :text(65535)
benefits_dine_in_point_icon                   :string(191)
benefits_dine_in_point_my                     :float(24)
benefits_dine_in_point_rank                   :integer
benefits_dine_in_point_sg                     :float(24)
benefits_five_reviews_point                   :integer
benefits_five_reviews_point_active            :boolean
benefits_five_reviews_point_description       :text(65535)
benefits_five_reviews_point_description_sg    :text(65535)
benefits_five_reviews_point_icon              :string(191)
benefits_five_reviews_point_my                :integer
benefits_five_reviews_point_rank              :integer
benefits_five_reviews_point_sg                :integer
benefits_referral_point                       :integer
benefits_referral_point_active                :boolean
benefits_referral_point_description           :text(65535)
benefits_referral_point_description_sg        :text(65535)
benefits_referral_point_icon                  :string(191)
benefits_referral_point_my                    :integer
benefits_referral_point_rank                  :integer
benefits_referral_point_sg                    :integer
benefits_review_with_photos                   :integer
benefits_review_with_photos_active            :boolean
benefits_review_with_photos_description       :text(65535)
benefits_review_with_photos_description_sg    :text(65535)
benefits_review_with_photos_icon              :string(191)
benefits_review_with_photos_my                :integer
benefits_review_with_photos_rank              :integer
benefits_review_with_photos_sg                :integer
benefits_review_without_photos                :integer
benefits_review_without_photos_active         :boolean
benefits_review_without_photos_description    :text(65535)
benefits_review_without_photos_description_sg :text(65535)
benefits_review_without_photos_icon           :string(191)
benefits_review_without_photos_my             :integer
benefits_review_without_photos_rank           :integer
benefits_review_without_photos_sg             :integer
benefits_special_package_options              :integer
benefits_special_package_options_active       :boolean
benefits_special_package_options_description  :text(65535)
benefits_special_package_options_icon         :string(191)
benefits_special_package_options_rank         :integer
code                                          :string(191)
divider_color                                 :string(191)
icon_badge                                    :string(191)
logo                                          :string(191)
name                                          :string(191)
profile_header_mobile                         :string(191)
profile_header_web                            :string(191)
profile_text_color                            :string(191)
progress_bar_color                            :string(191)
qualification_total_reservations              :integer
qualification_total_spend                     :integer
rank                                          :integer
regain_total_reservations                     :integer
regain_total_spend                            :integer
text_color                                    :string(191)
created_at                                    :datetime         not null
updated_at                                    :datetime         not null
loyalty_level_id                              :bigint           not null

Indexes

index_loyalty_levels_on_name  (name)

Constant Summary collapse

AVAILABLE_LEVELS =
{
  hunger: 'HUNGER',
  silver: 'SILVER',
  gold: 'GOLD',
  platinum: 'PLATINUM',
}.freeze
LoyaltyLevelData =
ImmutableStruct.new(:total_spending, :total_reservations)
LoyaltyLevelBenefit =
ImmutableStruct.new(:dine_in_point_th,
:five_reviews_point_th,
:review_with_photos_th,
:review_without_photos_th,
:referral_point_th,
:dine_in_point_sg,
:five_reviews_point_sg,
:review_with_photos_sg,
:review_without_photos_sg,
:referral_point_sg,
:dine_in_point_my,
:five_reviews_point_my,
:review_with_photos_my,
:review_without_photos_my,
:referral_point_my)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

sync_carrierwave_url

Class Method Details

.fetch_dataObject



212
213
214
215
216
# File 'app/models/loyalty_level.rb', line 212

def self.fetch_data
  @loyalty_levels = Rails.cache.fetch("loyalty_levels:#{LoyaltyLevel.maximum(:updated_at)}") do
    LoyaltyLevel.all.to_a
  end
end

.find_or_create_level(code) ⇒ Object



218
219
220
# File 'app/models/loyalty_level.rb', line 218

def self.find_or_create_level(code)
  where('lower(code) = ?', code.downcase).first || create(code: code.capitalize)
end

.goldObject



134
135
136
# File 'app/models/loyalty_level.rb', line 134

def self.gold
  find_or_create_level(AVAILABLE_LEVELS[:gold])
end

.hungerObject



126
127
128
# File 'app/models/loyalty_level.rb', line 126

def self.hunger
  find_or_create_level(AVAILABLE_LEVELS[:hunger])
end

.platinumObject



138
139
140
# File 'app/models/loyalty_level.rb', line 138

def self.platinum
  find_or_create_level(AVAILABLE_LEVELS[:platinum])
end

.reservation_points(user) ⇒ Object



186
187
188
189
190
# File 'app/models/loyalty_level.rb', line 186

def self.reservation_points(user)
  return ::GiveReservationRewardWorker::REWARD_POINT if user.blank?

  user.user_loyalty.loyalty_level&.benefits&.dine_in_point_th
end

.review_points(user, is_review_with_photos: false, country_code: nil) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/models/loyalty_level.rb', line 192

def self.review_points(user, is_review_with_photos: false, country_code: nil)
  return ::GiveReservationRewardWorker::REWARD_POINT if user.blank?

  benefit_key = if country_code.present? && country_code == ApiV5::Constants::COUNTRY_CODE_SG
                  base = is_review_with_photos ? 'review_with_photos' : 'review_without_photos'
                  "#{base}_sg"
                elsif country_code.present? && country_code == ApiV5::Constants::COUNTRY_CODE_MY
                  base = is_review_with_photos ? 'review_with_photos' : 'review_without_photos'
                  "#{base}_my"
                else
                  is_review_with_photos ? 'review_with_photos_th' : 'review_without_photos_th'
                end

  user.user_loyalty.benefit_value(benefit_key, cast: :to_i)
end

.silverObject



130
131
132
# File 'app/models/loyalty_level.rb', line 130

def self.silver
  find_or_create_level(AVAILABLE_LEVELS[:silver])
end

.valid_for_first_x_reviewsObject



208
209
210
# File 'app/models/loyalty_level.rb', line 208

def self.valid_for_first_x_reviews
  5
end

Instance Method Details

#benefitsObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/models/loyalty_level.rb', line 166

def benefits
  LoyaltyLevelBenefit.new(
    dine_in_point_th: benefits_dine_in_point.to_f,
    five_reviews_point_th: benefits_five_reviews_point.to_i,
    review_with_photos_th: benefits_review_with_photos.to_i,
    review_without_photos_th: benefits_review_without_photos.to_i,
    referral_point_th: benefits_referral_point.to_i,
    dine_in_point_sg: benefits_dine_in_point_sg.to_f,
    five_reviews_point_sg: benefits_five_reviews_point_sg.to_i,
    review_with_photos_sg: benefits_review_with_photos_sg.to_i,
    review_without_photos_sg: benefits_review_without_photos_sg.to_i,
    referral_point_sg: benefits_referral_point_sg.to_i,
    dine_in_point_my: benefits_dine_in_point_my.to_f,
    five_reviews_point_my: benefits_five_reviews_point_my.to_i,
    review_with_photos_my: benefits_review_with_photos_my.to_i,
    review_without_photos_my: benefits_review_without_photos_my.to_i,
    referral_point_my: benefits_referral_point_my.to_i,
  )
end

#name_as_symbolObject



148
149
150
# File 'app/models/loyalty_level.rb', line 148

def name_as_symbol
  code.try(:downcase).try(:to_sym)
end

#regain_qualificationsObject



152
153
154
155
156
157
# File 'app/models/loyalty_level.rb', line 152

def regain_qualifications
  LoyaltyLevelData.new(
    total_spending: regain_total_spend.to_i,
    total_reservations: regain_total_reservations.to_i,
  )
end

#tier_qualificationsObject



159
160
161
162
163
164
# File 'app/models/loyalty_level.rb', line 159

def tier_qualifications
  LoyaltyLevelData.new(
    total_spending: qualification_total_spend.to_i,
    total_reservations: qualification_total_reservations.to_i,
  )
end