Class: User
Constant Summary
collapse
- PASSWORD_SALT =
'2alksdj9182398as:asd[]q[we-='
- GENDER_MAP =
{ female: 0, male: 1, Mrs: 0, Mr: 1, 'Mrs.': 0, 'Mr.': 1, f: 0, m: 1,
Miss: 0 }.with_indifferent_access
- REFERRAL_CODE_PREFIX =
We use this prefix to generate voucher for referral program
'REF-'
- MIN_REFERRAL_CODE_LENGTH =
6
- DELETED_USER_EMAIL_FLAG =
'archived-user'
- DELETED_USER_USERNAME_FLAG =
'(Deleted user)'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#time_zone
#kinda_like_owner?, #owner?, #restaurant_group?, #staff?, #user?
#corporate_employee?, #fetch_corporate_company
sync_carrierwave_url
Instance Attribute Details
#multiple_booking ⇒ Object
Returns the value of attribute multiple_booking.
24
25
26
|
# File 'app/models/user.rb', line 24
def multiple_booking
@multiple_booking
end
|
#one_booking ⇒ Object
used by app/views/admin/reports/_user.html.slim
23
24
25
|
# File 'app/models/user.rb', line 23
def one_booking
@one_booking
end
|
#transaction_unsubscribe_reason_other ⇒ Object
449
450
451
|
# File 'app/models/user.rb', line 449
def transaction_unsubscribe_reason_other
transaction_unsubscribe_reason
end
|
Class Method Details
.decode_uri(string) ⇒ Object
390
391
392
393
394
|
# File 'app/models/user.rb', line 390
def decode_uri(string)
Base64.urlsafe_decode64 string
rescue ArgumentError
''
end
|
.deletion_reasons(lang) ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'app/models/user.rb', line 178
def deletion_reasons(lang)
reasons = {
en: [
'I no longer use Hungry Hub',
"I couldn't find the deals I like",
'Deals are too expensive',
'Difficult to use',
'I have privacy/security concerns',
],
th: [
'I no longer use Hungry Hub',
"I couldn't find the deals I like",
'Deals are too expensive',
'Difficult to use',
'I have privacy/security concerns',
],
}
reasons[lang]
end
|
.encode_to_uri(string) ⇒ Object
386
387
388
|
# File 'app/models/user.rb', line 386
def encode_to_uri(string)
Base64.urlsafe_encode64 string
end
|
.find_referrer_based_on_voucher_code(voucher_code) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
# File 'app/models/user.rb', line 240
def find_referrer_based_on_voucher_code(voucher_code)
return nil if voucher_code.blank?
referrer = match_by_referral_code(voucher_code)
return referrer if referrer.present?
actual_code = voucher_code.include?('-') ? voucher_code.split('-')[1] : nil
return match_by_referral_code(actual_code) if actual_code.present?
nil
end
|
.generate_short_referral_code ⇒ Object
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'app/models/user.rb', line 198
def generate_short_referral_code
max_attempts = 1000
attempts = 0
loop do
temp_code = SecureRandom.alphanumeric(MIN_REFERRAL_CODE_LENGTH).upcase
unless User.exists?(short_my_r_code: temp_code) || Voucher.exists?(voucher_code: temp_code)
return temp_code
end
attempts += 1
raise 'Max attempts reached for generating unique short referral code' if attempts >= max_attempts
end
end
|
.generate_user_reports ⇒ Object
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'app/models/user.rb', line 213
def generate_user_reports
sql = "SELECT MONTH(created_at) AS month, YEAR(created_at) AS year, COUNT(id) AS user_count FROM #{User.table_name} GROUP BY month, year ORDER BY year, month ASC"
reports = User.find_by_sql(sql)
reports.each_with_index do |r, index|
sql = <<~SQL
SELECT COUNT(*) AS total
FROM `reservations`
JOIN `users` ON `reservations`.`user_id` = `users`.`id` AND
MONTH(`reservations`.`created_at`) = MONTH(`users`.`created_at`) AND
YEAR(`reservations`.`created_at`) = YEAR(`users`.`created_at`)
WHERE
MONTH(`reservations`.`created_at`) = #{r.month}
AND YEAR(`reservations`.`created_at`) = #{r.year}
AND `reservations`.`user_id` IS NOT NULL
AND `reservations`.`active` = 1
GROUP BY `reservations`.`user_id`
SQL
booking = Reservation.find_by_sql(sql).to_a
one_booking = booking.select { |b| b.total == 1 }.count
multiple_booking = booking.select { |b| b.total > 1 }.count
reports[index].one_booking = one_booking
reports[index].multiple_booking = multiple_booking
end
end
|
.is_valid_hash_email?(hash_email, uri_email) ⇒ Boolean
396
397
398
399
|
# File 'app/models/user.rb', line 396
def is_valid_hash_email?(hash_email, uri_email)
email = decode_uri(uri_email)
valid_safe_hash?(hash_email, email)
end
|
.match_by_referral_code(code) ⇒ Object
custom method to find referral code it will try to match with the long and short referral code we add short referral code feature to make it easier for user to share their referral code added at end of May 2024 we don't use `find_by_referral_code` for the method name because it is similar to Rails built-in method `find_by` and it could cause confusion
172
173
174
175
176
|
# File 'app/models/user.rb', line 172
def match_by_referral_code(code)
return nil if code.blank?
with_referral_code(code).first
end
|
.to_safe_hash(email) ⇒ Object
367
368
369
370
371
372
373
|
# File 'app/models/user.rb', line 367
def to_safe_hash(email)
user = User.find_by email: email
return false if user.blank?
hash = BCrypt::Password.create email + User::PASSWORD_SALT, cost: 7
Base64.urlsafe_encode64 hash
end
|
.total_at_month_and_year(month, year) ⇒ Object
423
424
425
|
# File 'app/models/user.rb', line 423
def total_at_month_and_year(month, year)
where("MONTH(created_at) = #{month} AND YEAR(created_at) = #{year}").count
end
|
.update_password(data) ⇒ Object
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
# File 'app/models/user.rb', line 401
def update_password(data)
hash_email = data[:hash_email]
original_email = data[:original_email]
password = data[:password]
password_confirmation = data[:password_confirmation]
if is_valid_hash_email?(hash_email, original_email)
user = User.find_by email: decode_uri(original_email)
user.password = password
user.password_confirmation = password_confirmation
return true if user.save
@update_password_errors = user.errors.full_messages
end
false
end
|
.update_password_errors ⇒ Object
417
418
419
420
421
|
# File 'app/models/user.rb', line 417
def update_password_errors
return @update_password_errors if @update_password_errors.present?
[]
end
|
.valid_safe_hash?(hash_email, original_email) ⇒ Boolean
375
376
377
378
379
380
381
382
383
384
|
# File 'app/models/user.rb', line 375
def valid_safe_hash?(hash_email, original_email)
hash_email = Base64.urlsafe_decode64 hash_email
email = BCrypt::Password.new hash_email
email == original_email + User::PASSWORD_SALT
rescue ArgumentError false
rescue BCrypt::Errors::InvalidHash
false
end
|
Instance Method Details
#active_reservations_count ⇒ Object
305
306
307
308
309
310
|
# File 'app/models/user.rb', line 305
def active_reservations_count
Rails.cache.fetch("User:#{id}:active_reservations_count:#{reservations.latest_timestamp_cache_key}",
expires_in: CACHEFLOW.generate_expiry) do
reservations.reached_goal_scope.count
end
end
|
#age ⇒ Object
256
257
258
259
260
261
262
263
264
265
266
267
268
|
# File 'app/models/user.rb', line 256
def age
Rails.cache.fetch("User:#{id}:age:#{birthday}", expires_in: CACHEFLOW.generate_expiry) do
birthday = self.birthday if birthday.nil?
if birthday.blank? || birthday.nil?
0
else
now = Date.current_date
birthday -= 543.years if birthday.year > 2500
now.year - birthday.year - (now.month > birthday.month || (now.month == birthday.month && now.day >= birthday.day) ? 0 : 1)
end
end
end
|
#all_reservations_count ⇒ Object
298
299
300
301
302
303
|
# File 'app/models/user.rb', line 298
def all_reservations_count
Rails.cache.fetch("User:#{id}:all_reservations_count:#{reservations.latest_timestamp_cache_key}",
expires_in: CACHEFLOW.generate_expiry) do
reservations.exclude_modified_scope.count
end
end
|
#cancel_reservations_count ⇒ Object
319
320
321
322
323
324
|
# File 'app/models/user.rb', line 319
def cancel_reservations_count
Rails.cache.fetch("User:#{id}:cancel_reservations_count:#{reservations.latest_timestamp_cache_key}",
expires_in: CACHEFLOW.generate_expiry) do
reservations.cancel_scope.count
end
end
|
#company_logo_url ⇒ Object
464
465
466
|
# File 'app/models/user.rb', line 464
def company_logo_url
corporate_company&.logo_url&.presence || ''
end
|
#favorites ⇒ Object
358
359
360
|
# File 'app/models/user.rb', line 358
def favorites
favs.distinct
end
|
#fb_uid=(id) ⇒ Object
440
441
442
443
|
# File 'app/models/user.rb', line 440
def fb_uid=(id)
self['fb_uid'] = id
self.uid = id
end
|
#fetch_omise_customer_id(country_code) ⇒ Object
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
# File 'app/models/user.rb', line 501
def formatted_booking_summary(restaurant_id = nil)
@user_booking_summaries = booking_summaries
{
restaurant_booking: {
booking: @user_booking_summaries.select { |bs| bs.restaurant_id == restaurant_id }.first&.booking_count || 0,
cancel: @user_booking_summaries.select { |bs| bs.restaurant_id == restaurant_id }.first&.cancel_count || 0,
no_show: @user_booking_summaries.select { |bs| bs.restaurant_id == restaurant_id }.first&.no_show_count || 0,
},
all_restaurant_booking: {
booking: @user_booking_summaries.select { |bs| bs.restaurant_id.nil? }.first&.booking_count || 0,
cancel: @user_booking_summaries.select { |bs| bs.restaurant_id.nil? }.first&.cancel_count || 0,
no_show: @user_booking_summaries.select { |bs| bs.restaurant_id.nil? }.first&.no_show_count || 0,
},
}
end
|
#gender ⇒ Object
428
429
430
431
432
433
|
# File 'app/models/user.rb', line 428
def gender
g = read_attribute(:gender)
return GENDER_MAP.key(g) unless g.nil?
nil
end
|
#gender=(g) ⇒ Object
435
436
437
438
|
# File 'app/models/user.rb', line 435
def gender=(g)
write_attribute(:gender, GENDER_MAP[g]) unless g.nil?
nil
end
|
#get_credits(country_id = nil) ⇒ Object
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
# File 'app/models/user.rb', line 270
def get_credits(country_id = nil)
Rails.cache.fetch("User:#{id}:get_credits:#{rewards.latest_timestamp_cache_key}:#{country_id}",
expires_in: CACHEFLOW.generate_expiry) do
reward = nil
begin
reward = Reward.count_credits(id, country_id)
if readonly?
User.find(id).update(reward_points: reward)
else
update(reward_points: reward)
end
rescue ActiveModel::RangeError => e
APMErrorHandler.report("#{self.class} #{e.message}", user_id: id)
end
reward
end
end
|
#get_credits_by_country ⇒ Object
291
292
293
294
295
296
|
# File 'app/models/user.rb', line 291
def get_credits_by_country
Rails.cache.fetch("User:#{id}:get_credits_by_country:#{rewards.latest_timestamp_cache_key}",
expires_in: CACHEFLOW.generate_expiry) do
Reward.user_points_by_country(id)
end
end
|
#language=(lang) ⇒ Object
#malaysia_phone_number? ⇒ Boolean
Checks if the user's phone number is a valid Malaysian phone number.
This method parses the user's full phone number (including country code) and verifies if it is valid and specifically valid for Malaysia.
576
577
578
579
580
581
582
583
584
|
# File 'app/models/user.rb', line 576
def malaysia_phone_number?
return false if phone_v2_full.blank?
parsed_phone = Phonelib.parse(phone_v2_full)
parsed_phone.valid? && parsed_phone.valid_for_country?(Country.find_malaysia.alpha2)
rescue Phonelib::Error => e
APMErrorHandler.report("#{self.class} #{e.message}", user_id: id)
false
end
|
#name ⇒ Object
341
342
343
|
# File 'app/models/user.rb', line 341
def name
username
end
|
#name=(name) ⇒ Object
345
346
347
|
# File 'app/models/user.rb', line 345
def name=(name)
self[:username] = name
end
|
#no_show_reservations_count ⇒ Object
312
313
314
315
316
317
|
# File 'app/models/user.rb', line 312
def no_show_reservations_count
Rails.cache.fetch("User:#{id}:no_show_reservations_count:#{reservations.latest_timestamp_cache_key}",
expires_in: CACHEFLOW.generate_expiry) do
reservations.no_show_scope.count
end
end
|
#phone_v2_full ⇒ Object
453
454
455
456
457
458
459
460
461
462
|
# File 'app/models/user.rb', line 453
def phone_v2_full
if phone_v2.present?
return "#{calling_code}#{phone_v2}" if calling_code.present?
return "66#{phone_v2}"
end
return phone if phone.present?
nil
end
|
#referrals_count ⇒ Object
326
327
328
329
330
331
|
# File 'app/models/user.rb', line 326
def referrals_count
Rails.cache.fetch("User:#{id}:all_reservations_count:#{referrals.latest_timestamp_cache_key}",
expires_in: CACHEFLOW.generate_expiry) do
referrals.count
end
end
|
#set_verify_code ⇒ Object
349
350
351
|
# File 'app/models/user.rb', line 349
def set_verify_code
self.verify_code = rand(10**5).to_i
end
|
#setup_user_loyalty ⇒ Object
362
363
364
|
# File 'app/models/user.rb', line 362
def setup_user_loyalty
build_user_loyalty
end
|
#setup_username ⇒ Object
353
354
355
356
|
# File 'app/models/user.rb', line 353
def setup_username
name = username.presence || email.to_s.split('@').first
self.username = name
end
|
#singapore_phone_number? ⇒ Boolean
Checks if the user's phone number is a valid Singaporean phone number.
This method parses the user's full phone number (including country code) and verifies if it is valid and specifically valid for Singapore.
553
554
555
556
557
558
559
560
561
|
# File 'app/models/user.rb', line 553
def singapore_phone_number?
return false if phone_v2_full.blank?
parsed_phone = Phonelib.parse(phone_v2_full)
parsed_phone.valid? && parsed_phone.valid_for_country?(Country.find_singapore.alpha2)
rescue Phonelib::Error => e
APMErrorHandler.report("#{self.class} #{e.message}", user_id: id)
false
end
|
#thai_phone_number? ⇒ Boolean
Checks if the user's phone number is a valid Thai phone number.
This method parses the user's full phone number (including country code) and verifies if it is valid and specifically valid for Thailand.
530
531
532
533
534
535
536
537
538
|
# File 'app/models/user.rb', line 530
def thai_phone_number?
return false if phone_v2_full.blank?
parsed_phone = Phonelib.parse(phone_v2_full)
parsed_phone.valid? && parsed_phone.valid_for_country?(Country.find_thailand.alpha2)
rescue Phonelib::Error => e
APMErrorHandler.report("#{self.class} #{e.message}", user_id: id)
false
end
|
#update_booking_summary(restaurant_id = nil) ⇒ Object
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
# File 'app/models/user.rb', line 479
def update_booking_summary(restaurant_id = nil)
summary = if restaurant_id
{
booking_count: reservations.where(restaurant_id: restaurant_id).exclude_temporary.count,
cancel_count: reservations.where(restaurant_id: restaurant_id).cancel_scope.count,
no_show_count: reservations.where(restaurant_id: restaurant_id).no_show_scope.count,
}
else
{
booking_count: reservations.exclude_temporary.count,
cancel_count: reservations.cancel_scope.count,
no_show_count: reservations.no_show_scope.count,
}
end
booking_summary = BookingSummary.find_or_initialize_by(user_id: id, restaurant_id: restaurant_id)
booking_summary.assign_attributes(summary)
booking_summary.save!
end
|