Class: UserMailer

Inherits:
ApplicationMailer show all
Includes:
MoneyRails::ActionViewExtension, RatingUrlHelper
Defined in:
app/mailers/user_mailer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RatingUrlHelper

#guest_rating_url, #rating_url_for

Methods inherited from ApplicationMailer

#log_mailer, #switch_locale

Methods included from Workers::Helpers

#parse_json

Class Method Details

.method_missing(method_name, *args, &block) ⇒ Object

Override method_missing to track any unknown mailer method calls



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/mailers/user_mailer.rb', line 21

def self.method_missing(method_name, *args, &block)
  if method_name == :booking_modification && args&.first&.nil?
    APMErrorHandler.report(
      'UserMailer.booking_modification called via method_missing - possible metaprogramming issue',
      context: {
        method_name: method_name,
        args: args,
        caller_stack: Rails.backtrace_cleaner.clean(caller),
      },
      handled: true,
    )
  end

  super
end

Instance Method Details

#accident(email) ⇒ Object



332
333
334
335
336
337
338
339
340
341
# File 'app/mailers/user_mailer.rb', line 332

def accident(email)
  subject = 'Please ignore rating email'
  mail(to: email, from: 'Hungry Hub Team <contact@hungryhub.com>', subject: subject)
  log_mailer({
               email: email,
               name: 'UserMailer#accident',
               subject: subject,
               from: 'Hungry Hub Team <contact@hungryhub.com>',
             })
end

#announcement(email, subject) ⇒ Object



503
504
505
506
507
508
509
510
511
512
# File 'app/mailers/user_mailer.rb', line 503

def announcement(email, subject)
  @subject = subject.presence || 'ประกาศความขัดข้องระหว่างการจำหน่าย Voucher ในวันที่ 29 มิ.ย. 2566'
  template_name = 'annoucement'
  mail(subject: @subject, to: email, template_name: template_name)
  log_mailer({
               email: email,
               name: 'UserMailer#announcement',
               subject: @subject,
             })
end

#booking_cancel(reservation_id, user_id = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/mailers/user_mailer.rb', line 184

def booking_cancel(reservation_id, user_id = nil)
  @reservation = Reservation.fetch(reservation_id).decorate
  return true if no_user_email?(@reservation) || invalid_user_email?(@reservation)

  user = user_id ? set_locale(user_id) : nil
  return if user && !user.subscribe_transaction_email?

  email = user ? user.email : @reservation.email

  @restaurant = @reservation.restaurant
  reply_to = reply_to_email(@restaurant)
  template_name = 'booking_cancelled_2022'
  @subject = I18n.t('mailer.user.reservation.cancel', name: @restaurant.name)
  @restaurant_profile_page = url_helpers.restaurant_url(id: @restaurant.friendly_id,
                                                        locale: MyLocaleManager.default_locale)

  mail(subject: @subject, to: email,
       cc: [RESERVATION_EMAIL_RECEIVER],
       template_name: template_name, from: BOOKINGS_EMAIL, reply_to: reply_to)
  log_mailer({ reservation_id: @reservation.id,
               restaurant_id: @restaurant.id,
               user_id: user&.id, email: email,
               name: 'UserMailer#booking_cancel', subject: @subject, from: BOOKINGS_EMAIL, reply_to: reply_to })
end

#booking_confirmation(reservation_id, user_id = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/mailers/user_mailer.rb', line 58

def booking_confirmation(reservation_id, user_id = nil)
  @reservation = Reservation.fetch(reservation_id).decorate
  return if @reservation.restaurant_id == HH_TRAINING_ID
  return true if no_user_email?(@reservation) || invalid_user_email?(@reservation)

  user = user_id ? set_locale(user_id) : nil
  return if user && !user.subscribe_transaction_email?

  email = user ? user.email : @reservation.email

  @restaurant = @reservation.restaurant
  reply_to = reply_to_email(@restaurant)

  template_name = 'v6-user-booking-confirmation'
  booking_confirmation_params(@reservation)

  if @reservation.restaurant.any_offers?
    mail(subject: @subject, to: @reservation.email, cc: [RESERVATION_EMAIL_RECEIVER], template_name: template_name,
         from: BOOKINGS_EMAIL, reply_to: reply_to)
  else
    mail(subject: @subject, to: @reservation.email, template_name: template_name, from: BOOKINGS_EMAIL,
         reply_to: reply_to)
  end
  log_mailer({ reservation_id: @reservation.id,
               restaurant_id: @restaurant.id,
               user_id: user&.id, email: email,
               name: 'UserMailer#booking_confirmation', subject: @subject, from: BOOKINGS_EMAIL, reply_to: reply_to })
end

#booking_modification(reservation_id, user_id = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/mailers/user_mailer.rb', line 126

def booking_modification(reservation_id, user_id = nil)
  @reservation = Reservation.fetch(reservation_id).decorate
  return true if no_user_email?(@reservation) || invalid_user_email?(@reservation)

  user = user_id ? set_locale(user_id) : nil
  return if user && !user.subscribe_transaction_email?

  email = user ? user.email : @reservation.email
  @reservation_time_v2 = @reservation.date.strftime("%A, %B #{ActiveSupport::Inflector.ordinalize(@reservation.date.day)}, %Y")
  @restaurant = @reservation.restaurant
  reply_to = reply_to_email(@restaurant)
  template_name = if @reservation.package? || user_id.present?
                    'booking-modification-to-registered-user'
                  else
                    'booking-modification-to-unregistered-user'
                  end

  @reservation_show_page_link = reservation_show_page_link(@reservation)
  @menu_link = menu_link(@reservation)
  @subject = I18n.t('mailer.user.reservation.modification', name: @restaurant.name)
  if @restaurant.any_offers?
    mail(subject: @subject, to: email, cc: [RESERVATION_EMAIL_RECEIVER], template_name: template_name,
         from: BOOKINGS_EMAIL, reply_to: reply_to) do |format|
      format.html do
        render template: "user_mailer/#{template_name}",
               locals: {
                 subject: @subject,
                 reservation: @reservation,
                 reservation_time_v2: @reservation_time_v2,
                 restaurant: @restaurant,
                 reservation_show_page_link: @reservation_show_page_link,
                 year: year(@restaurant.time_zone),
               }
      end
    end
  else
    mail(subject: @subject, to: email, template_name: template_name, from: BOOKINGS_EMAIL,
         reply_to: reply_to) do |format|
      format.html do
        render template: "user_mailer/#{template_name}",
               locals: {
                 subject: @subject,
                 reservation: @reservation,
                 reservation_time_v2: @reservation_time_v2,
                 restaurant: @restaurant,
                 reservation_show_page_link: @reservation_show_page_link,
                 year: year(@restaurant.time_zone),
               }
      end
    end
  end

  log_mailer({ reservation_id: @reservation.id,
               restaurant_id: @restaurant.id,
               user_id: user&.id, email: email,
               name: 'UserMailer#booking_modification', subject: @subject, from: BOOKINGS_EMAIL, reply_to: reply_to })
end

#booking_pending(reservation_id) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/mailers/user_mailer.rb', line 87

def booking_pending(reservation_id)
  @reservation = Reservation.fetch(reservation_id).decorate
  return true if no_user_email?(@reservation) || invalid_user_email?(@reservation)

  @restaurant = @reservation.restaurant
  reply_to = reply_to_email(@restaurant)
  @reservation_time_v2 = @reservation.date.strftime("%A, %B #{ActiveSupport::Inflector.ordinalize(@reservation.date.day)}, %Y")
  template_name = 'booking-pending-to-user'
  @subject = I18n.t('mailer.user.reservation.pending', name: @restaurant.name)
  @reservation_show_page_link = reservation_show_page_link(@reservation)

  mail(subject: @subject, to: @reservation.email, cc: [RESERVATION_EMAIL_RECEIVER], template_name: template_name,
       from: BOOKINGS_EMAIL, reply_to: reply_to) do |format|
    format.html do
      render template: "user_mailer/#{template_name}",
             locals: {
               subject: @subject,
               reservation: @reservation,
               reservation_time_v2: @reservation_time_v2,
               restaurant: @restaurant,
               reservation_show_page_link: @reservation_show_page_link,
               year: year(@restaurant.time_zone),
             }
    end
  end

  log_mailer(
    {
      reservation_id: @reservation.id,
      restaurant_id: @restaurant.id,
      email: @reservation.email,
      name: 'UserMailer#booking_pending',
      subject: @subject,
      from: BOOKINGS_EMAIL,
      reply_to: reply_to,
    },
  )
end

#invalid_user_email?(reservation) ⇒ Boolean

Returns:

  • (Boolean)


349
350
351
352
353
354
355
356
357
358
359
# File 'app/mailers/user_mailer.rb', line 349

def invalid_user_email?(reservation)
  return false if EmailValidator.valid?(reservation.email)

  begin
    false if BlacklistedEmail.find_by(email: reservation.email).blank?
  rescue ActiveRecord::StatementInvalid, Mysql2::Error
    false
  end

  true
end

#no_user_email?(reservation) ⇒ Boolean

Returns:

  • (Boolean)


343
344
345
346
347
# File 'app/mailers/user_mailer.rb', line 343

def no_user_email?(reservation)
  return false if reservation.email.present?

  true
end

#notify_account_deletion(subject, message) ⇒ Object



311
312
313
314
315
316
317
318
319
# File 'app/mailers/user_mailer.rb', line 311

def (subject, message)
  @message = message
  mail(to: DELETION_ACCOUNT_ALERT_EMAIL, subject: subject)
  log_mailer({
               email: DELETION_ACCOUNT_ALERT_EMAIL,
               name: 'UserMailer#notify_account_deletion',
               subject: subject,
             })
end

#notify_data_breach(user) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'app/mailers/user_mailer.rb', line 259

def notify_data_breach(user)
  @user = parse_json user
  @user.hash_email = User.to_safe_hash(@user.email)
  @user.uri_email = User.encode_to_uri(@user.email)
  subject = 'เพื่อความปลอดภัยโปรดรีเซ็ตรหัสผ่านบัญชี Hungry Hub ของคุณ'
  mail to: "\"#{@user.username}\" <#{@user.email}>", subject: subject
  log_mailer({
               user_id: @user.id,
               email: @user.email,
               name: 'UserMailer#notify_data_breach',
               subject: subject,
             })
end

#notify_new_password(user, password) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/mailers/user_mailer.rb', line 244

def notify_new_password(user, password)
  @user = parse_json user
  @user.hash_email = User.to_safe_hash(@user.email)
  @user.uri_email = User.encode_to_uri(@user.email)
  @password = password
  subject = 'รหัสผ่านของคุณได้รับการอัปเดตเรียบร้อยแล้ว'
  mail to: "\"#{@user.username}\" <#{@user.email}>", subject: subject
  log_mailer({
               user_id: @user.id,
               email: @user.email,
               name: 'UserMailer#notify_new_password',
               subject: subject,
             })
end

#notify_points_expiration(user) ⇒ Object



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'app/mailers/user_mailer.rb', line 514

def notify_points_expiration(user)
  points_expiry_date = Reward.points_expiry_date(user)

  # Don't send email if points_expiry_date is nil
  return if points_expiry_date.blank?

  @user = user
  @points_expiry_date = points_expiry_date
  language = @user.language.presence || MyLocaleManager.default_locale
  # Email templates available for EN, TH, and CN only, fallback to EN for other languages
  parsed_lang = MyLocaleManager.parse(language)
  unless MyLocaleManager::AVAILABLE_LOCALES_FOR_EMAIL_WITH_CN.include?(parsed_lang)
    parsed_lang = MyLocaleManager.default_locale
  end
  template_name = "notify_points_expiration_#{parsed_lang}"
  subject = "Don't lose your Benefits!"
  mail(subject: subject, to: @user.email, template_name: template_name)
  log_mailer({
               user_id: @user.id,
               email: @user.email,
               name: 'UserMailer#notify_points_expiration',
               subject: subject,
             })
end

#notify_support(subject, message) ⇒ Object



301
302
303
304
305
306
307
308
309
# File 'app/mailers/user_mailer.rb', line 301

def notify_support(subject, message)
  @message = message
  mail(to: SUPPORT_EMAIL, subject: subject)
  log_mailer({
               email: SUPPORT_EMAIL,
               name: 'UserMailer#notify_support',
               subject: subject,
             })
end

#notify_voucher_code(email, subject, image_link) ⇒ Object



491
492
493
494
495
496
497
498
499
500
501
# File 'app/mailers/user_mailer.rb', line 491

def notify_voucher_code(email, subject, image_link)
  @link = image_link

  mail(subject: subject, to: email)
  log_mailer({
               email: email,
               name: 'UserMailer#notify_voucher_code',
               subject: subject,
               other: { image_link: image_link },
             })
end

#rating_reservation(reservation_id) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'app/mailers/user_mailer.rb', line 273

def rating_reservation(reservation_id)
  @reservation = Reservation.fetch(reservation_id).decorate
  return true if no_user_email?(@reservation) || invalid_user_email?(@reservation)

  @restaurant = @reservation.restaurant
  @subject = "Rate your experiences at #{@restaurant.name}"
  email = @reservation.email
  @reservation_rating_link_1 = rating_url(1, @reservation.id)
  @reservation_rating_link_2 = rating_url(2, @reservation.id)
  @reservation_rating_link_3 = rating_url(3, @reservation.id)
  @reservation_rating_link_4 = rating_url(4, @reservation.id)
  @reservation_rating_link_5 = rating_url(5, @reservation.id)

  url = AdminSetting.web_v2_host
  slugs = HomeSection.where(section_type: ['section_1', 'section_3', 'section_12', 'section_11']).map do |s|
    { s.section_type.to_sym => s.slug }
  end.reduce({}, :merge)

  @new_restaurants = "#{url}/#{slugs[:section_1]}"
  @trending_restaurants = "#{url}/#{slugs[:section_3]}"
  @un_dining_restaurants = "#{url}/#{slugs[:section_12]}"
  @staycation_restaurants = "#{url}/#{slugs[:section_11]}"
  mail(subject: @subject, to: email)
  log_mailer({ reservation_id: @reservation_id,
               restaurant_id: @restaurant.id, email: email,
               name: 'UserMailer#rating_reservation', subject: @subject })
end

#redeem_as_voucher(voucher_id) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
# File 'app/mailers/user_mailer.rb', line 232

def redeem_as_voucher(voucher_id)
  @voucher = Voucher.fetch voucher_id
  @user = @voucher.user

  @subject = "#{humanized_money_with_symbol(@voucher.amount)} Hungry Points Redeemed"
  email = @voucher.user.email

  mail(subject: @subject, to: email, cc: [POINTS_EMAIL])
  log_mailer({ email: email, name: 'UserMailer#redeem_as_voucher',
               subject: @subject })
end

#registration_confirmation(user_id) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'app/mailers/user_mailer.rb', line 209

def registration_confirmation(user_id)
  user = User.find_by(id: user_id)
  return unless user

  set_locale(user_id)
  template_name = 'welcome-to-hungry-hub-2022'
  subject = I18n.t('mailer.user.welcome')
  email = "#{user.username} <#{user.email}>"

  mail(subject: subject, to: email) do |format|
    format.html { render template: "user_mailer/#{template_name}", locals: { user: user } }
  end

  log_mailer(
    {
      user_id: user_id,
      email: email,
      name: 'UserMailer#registration_confirmation',
      subject: subject,
    },
  )
end

#send_otp_code(_otp_code, email) ⇒ Object



321
322
323
324
325
326
327
328
329
330
# File 'app/mailers/user_mailer.rb', line 321

def send_otp_code(_otp_code, email)
  @message = message
  subject = 'hungryhub.com - OTP code'
  mail(to: email, subject: subject)
  log_mailer({
               email: email,
               name: 'UserMailer#send_otp_code',
               subject: subject,
             })
end

#send_voucher_code_only(email, code, amount, expiry_date) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'app/mailers/user_mailer.rb', line 389

def send_voucher_code_only(email, code, amount, expiry_date)
  subject = 'Your Gift Card Confirmation'
  expiry_date = Date.parse(expiry_date)
  url = "https://fast-api.hungryhub.com/voucher_generator?voucher_code=#{code}&amount=#{amount}&expiry_date=#{expiry_date}"

  s3 = Aws::S3::Resource.new
  bucket = s3.bucket(Figaro.env.AWS_BUCKET!)

  response = Faraday.get url
  tmp_file = Tempfile.new(['voucher-code', '.jpeg'], encoding: 'ascii-8bit')
  tmp_file.write response.body

  s3_path = "vouchers/#{expiry_date}/#{CityHash.hash64([email, code, amount, expiry_date])}.jpeg"
  bucket.object(s3_path).upload_file(tmp_file)

  @image_path = "#{Figaro.env.CDN_URL!}/#{s3_path}"
  mail(subject: subject, to: email)
  log_mailer({
               email: email,
               name: 'UserMailer#send_voucher_code_only',
               subject: subject,
               other: { voucher_code: code, amount: amount },
             })
end

#share_booking_confirmation(reservation_id, emails) ⇒ Object

Parameters:

  • emails

    Array



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/mailers/user_mailer.rb', line 38

def share_booking_confirmation(reservation_id, emails)
  @reservation = Reservation.fetch(reservation_id).decorate
  return true if emails.blank?

  @restaurant = @reservation.restaurant
  template_name = 'v6-user-booking-confirmation'
  booking_confirmation_params(@reservation)
  @subject = booking_confirmation_subject(@reservation)

  mail(subject: @subject, to: emails, template_name: template_name, from: BOOKINGS_EMAIL)
  log_mailer({
               reservation_id: @reservation.id,
               restaurant_id: @restaurant.id,
               email: emails,
               name: 'UserMailer#share_booking_confirmation',
               subject: @subject,
               from: BOOKINGS_EMAIL,
             })
end

#ticket_confirmation(ticket_transaction_id) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'app/mailers/user_mailer.rb', line 414

def ticket_confirmation(ticket_transaction_id)
  subject = 'Your Voucher Confirmation'
  ticket_transaction = TicketTransaction.find(ticket_transaction_id)
  results = get_ticket_transaction(ticket_transaction_id)
  total_ticket_amount_cents = get_ticket_sum(ticket_transaction_id)
  if ticket_transaction.guest_id.present?
    user = ticket_transaction.guest
    language = MyLocaleManager.default_locale
  else
    user = ticket_transaction.user
    language = user.language.presence || MyLocaleManager.default_locale
  end

  # Email templates only available for EN and TH, fallback to EN for other languages
  parsed_lang = MyLocaleManager.parse(language)
  unless MyLocaleManager::AVAILABLE_LOCALES_FOR_EMAIL.include?(parsed_lang)
    parsed_lang = MyLocaleManager.default_locale
  end
  template_name = "ticket_confirmation_#{parsed_lang}"

  mail(subject: subject, to: user.email, template_name: template_name) do |format|
    format.html do
      render template: "user_mailer/#{template_name}",
             locals: {
               ticket_transaction: ticket_transaction,
               results: results,
               total_ticket_amount_cents: total_ticket_amount_cents,
               currency_code: results.first&.amount_currency || Country::THAI_CURRENCY_CODE,
               country: results.first&.ticket_group&.country || Country.find_thailand,
             }
    end
  end
  log_mailer({
               user_id: user.id,
               email: user.email,
               name: 'UserMailer#ticket_confirmation',
               subject: subject,
               other: { ticket_transaction_id: ticket_transaction_id },
             })
end

#ticket_redeem(ticket_id) ⇒ Object



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'app/mailers/user_mailer.rb', line 455

def ticket_redeem(ticket_id)
  subject = 'Your Voucher Redeemed'
  ticket = Ticket.find(ticket_id)
  if ticket.guest_id.present?
    user = ticket.guest
    language = MyLocaleManager.default_locale
  else
    user = ticket.user
    language = user.language.presence || MyLocaleManager.default_locale
  end

  # Email templates only available for EN and TH, fallback to EN for other languages
  parsed_lang = MyLocaleManager.parse(language)
  unless MyLocaleManager::AVAILABLE_LOCALES_FOR_EMAIL.include?(parsed_lang)
    parsed_lang = MyLocaleManager.default_locale
  end
  template_name = "ticket_redeemed_#{parsed_lang}"

  mail(subject: subject, to: user.email, template_name: template_name) do |format|
    format.html do
      render template: "user_mailer/#{template_name}",
             locals: {
               ticket: ticket,
               year: year(ticket.ticket_transaction&.restaurant&.time_zone),
             }
    end
  end
  log_mailer({
               user_id: user.id,
               email: user.email,
               name: 'UserMailer#ticket_redeem',
               subject: subject,
               other: { ticket_id: ticket_id },
             })
end

#voucher_confirmation(voucher_transaction_id) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'app/mailers/user_mailer.rb', line 361

def voucher_confirmation(voucher_transaction_id)
  @subject = 'Your Gift Card Confirmation'
  @voucher_transaction = VoucherTransaction.find(voucher_transaction_id)
  @results = get_voucher_transaction(voucher_transaction_id)
  if @voucher_transaction.guest_id.present?
    @user = @voucher_transaction.guest
    language = 'th'
  else
    @user = @voucher_transaction.user
    language = @user.language.presence || MyLocaleManager.default_locale
  end

  # Email templates only available for EN and TH, fallback to EN for other languages
  parsed_lang = MyLocaleManager.parse(language)
  unless MyLocaleManager::AVAILABLE_LOCALES_FOR_EMAIL.include?(parsed_lang)
    parsed_lang = MyLocaleManager.default_locale
  end
  template_name = "voucher_confirmation_#{parsed_lang}"
  mail(subject: @subject, to: @user.email, template_name: template_name)
  log_mailer({
               user_id: @user.id,
               email: @user.email,
               name: 'UserMailer#voucher_confirmation',
               subject: @subject,
               other: { voucher_transaction_id: voucher_transaction_id },
             })
end