Class: NotificationService::Sms

Inherits:
Object
  • Object
show all
Includes:
RatingUrlHelper, Workers::Views
Defined in:
app/services/notification_service/sms.rb

Overview

Responsible to send SMS this class has views inside app/views/workers folder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RatingUrlHelper

#guest_rating_url, #rating_url_for

Methods included from Workers::Views

#render

Constructor Details

#initialize(phones:, reservation: nil, voucher_transaction: nil, ticket: nil, default_locale: I18n.locale) ⇒ Sms

Returns a new instance of Sms.

Parameters:

  • receivers (Array)
  • reservation (Reservation) (defaults to: nil)


19
20
21
22
23
24
25
# File 'app/services/notification_service/sms.rb', line 19

def initialize(phones:, reservation: nil, voucher_transaction: nil, ticket: nil, default_locale: I18n.locale)
  self.phones = phones
  @reservation = reservation
  @voucher_transaction = voucher_transaction
  @ticket = ticket
  @default_locale = default_locale
end

Instance Attribute Details

#default_localeObject (readonly)

Returns the value of attribute default_locale.



13
14
15
# File 'app/services/notification_service/sms.rb', line 13

def default_locale
  @default_locale
end

#phonesObject

Returns the value of attribute phones.



13
14
15
# File 'app/services/notification_service/sms.rb', line 13

def phones
  @phones
end

#reservationObject

Returns the value of attribute reservation.



12
13
14
# File 'app/services/notification_service/sms.rb', line 12

def reservation
  @reservation
end

#ticketObject

Returns the value of attribute ticket.



12
13
14
# File 'app/services/notification_service/sms.rb', line 12

def ticket
  @ticket
end

#voucher_transactionObject

Returns the value of attribute voucher_transaction.



12
13
14
# File 'app/services/notification_service/sms.rb', line 12

def voucher_transaction
  @voucher_transaction
end

Class Method Details

.decide_backup_sms_provider(provider) ⇒ Symbol

This method is responsible for deciding the backup SMS provider based on the given provider.

If the provider is nil, it will return the default SMS provider specified in the AdminSetting. Otherwise, it will switch the provider based on the following rules:

  • If the provider is :alibaba, it will return :kaleyra.

  • If the provider is :kaleyra, it will return :alibaba.

  • If the provider is neither :alibaba nor :kaleyra, it will raise a MySmsError with an error message.

Parameters:

  • provider (Symbol)

    The SMS provider to decide the backup for.

Returns:

  • (Symbol)

    The backup SMS provider.

Raises:

  • (MySmsError)

    If the provider is invalid.



277
278
279
280
281
282
283
284
285
286
# File 'app/services/notification_service/sms.rb', line 277

def self.decide_backup_sms_provider(provider)
  return AdminSetting.sms_provider.to_sym if provider.nil?

  # Update this providers list and the backup provider if add a new SMS provider
  backup_providers = {
    alibaba: :kaleyra,
    kaleyra: :silverstreet,
  }
  backup_providers[provider.to_sym] || raise(StandardError, "Invalid SMS Provider #{provider}")
end

Instance Method Details

#send_big_group_msg_to_hh(locale: default_locale) ⇒ Object



177
178
179
180
181
182
183
# File 'app/services/notification_service/sms.rb', line 177

def send_big_group_msg_to_hh(locale: default_locale)
  options = {
    url: edit_admin_reservation_url(reservation.id),
  }
  msg = render 'sms/big_group_pending_msg_to_hh', locals: locals, locale: locale
  send_sms_async(msg, options)
end

#send_big_group_msg_to_owner(locale: default_locale) ⇒ Object



185
186
187
188
189
190
191
# File 'app/services/notification_service/sms.rb', line 185

def send_big_group_msg_to_owner(locale: default_locale)
  options = {
    url: owner_confirmation_reservation_url(restaurant.owner.to_url_hash, reservation.to_url_hash),
  }
  msg = render 'sms/big_group_pending_msg_to_owner', locals: locals, locale: locale
  send_sms_async(msg, options)
end

#send_cancel_msg(locale: default_locale) ⇒ Object



198
199
200
201
# File 'app/services/notification_service/sms.rb', line 198

def send_cancel_msg(locale: default_locale)
  msg = render 'sms/booking_cancel_msg', locals: locals, locale: locale
  send_sms_async(msg)
end

#send_confirmation_msg_delivery_later(locale: default_locale) ⇒ Object



122
123
124
125
126
127
128
# File 'app/services/notification_service/sms.rb', line 122

def send_confirmation_msg_delivery_later(locale: default_locale)
  msg = render 'sms/booking_confirmation_delivery_later_to_owner', locals: locals, locale: locale
  options = {
    url: owner_confirmation_reservation_url(reservation.courier_tracking_link),
  }
  send_sms_async(msg, options)
end

#send_confirmation_msg_delivery_now(locale: default_locale) ⇒ Object



138
139
140
141
142
143
144
# File 'app/services/notification_service/sms.rb', line 138

def send_confirmation_msg_delivery_now(locale: default_locale)
  msg = render 'sms/booking_confirmation_delivery_now_to_owner', locals: locals, locale: locale
  options = {
    url: owner_confirmation_reservation_url(reservation.courier_tracking_link),
  }
  send_sms_async(msg, options)
end

#send_confirmation_msg_delivery_to_user(locale: default_locale) ⇒ Object



130
131
132
133
134
135
136
# File 'app/services/notification_service/sms.rb', line 130

def send_confirmation_msg_delivery_to_user(locale: default_locale)
  msg = render 'sms/booking_confirmation_delivery_to_user', locals: locals, locale: locale
  options = {
    url: MyUrlShortener.by_url(reservation.courier_tracking_link),
  }
  send_sms_async(msg, options)
end

#send_confirmation_msg_pickup(locale: default_locale) ⇒ Object



146
147
148
149
150
151
152
# File 'app/services/notification_service/sms.rb', line 146

def send_confirmation_msg_pickup(locale: default_locale)
  msg = render 'sms/booking_confirmation_pickup_to_owner', locals: locals, locale: locale
  options = {
    url: owner_confirmation_reservation_url(restaurant.owner.to_url_hash, reservation.to_url_hash),
  }
  send_sms_async(msg, options)
end

#send_confirmation_msg_to_restaurant(locale: default_locale) ⇒ Object



82
83
84
85
86
87
88
# File 'app/services/notification_service/sms.rb', line 82

def send_confirmation_msg_to_restaurant(locale: default_locale)
  msg = render 'sms/booking_confirmation_msg_to_restaurant', locals: locals, locale: locale
  options = {
    url: owner_confirmation_reservation_url(restaurant.owner.to_url_hash, reservation.to_url_hash),
  }
  send_sms_async(msg, options)
end

#send_confirmation_msg_to_user(locale: default_locale) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/services/notification_service/sms.rb', line 66

def send_confirmation_msg_to_user(locale: default_locale)
  dine_in = reservation.service_type == 'dine_in'
  options = if dine_in
              { url: manage_my_reservations_url(reservation.to_url_hash) }
            else
              { url: reservation.web_url }
            end
  msg = if dine_in
          render 'sms/booking_confirmation_msg_to_user', locals: locals, locale: locale
        else
          render 'sms/not_dine_in_booking_confirmation_msg_to_user', locals: locals, locale: locale
        end

  send_sms_async(msg, options)
end

#send_confirmation_msg_with_kids(locale: default_locale) ⇒ Object



90
91
92
93
94
95
96
# File 'app/services/notification_service/sms.rb', line 90

def send_confirmation_msg_with_kids(locale: default_locale)
  msg = render 'sms/booking_confirmation_with_kids_msg_to_owner', locals: locals, locale: locale
  options = {
    url: owner_confirmation_reservation_url(restaurant.owner.to_url_hash, reservation.to_url_hash),
  }
  send_sms_async(msg, options)
end

#send_confirmation_msg_with_kids_to_user(locale: default_locale) ⇒ Object



106
107
108
109
110
111
112
# File 'app/services/notification_service/sms.rb', line 106

def send_confirmation_msg_with_kids_to_user(locale: default_locale)
  msg = render 'sms/booking_confirmation_with_kids_msg_to_user', locals: locals, locale: locale
  options = {
    url: MyUrlShortener.by_url(manage_my_reservations_url(reservation.to_url_hash)),
  }
  send_sms_async(msg, options)
end

#send_confirmation_msg_without_kids(locale: default_locale) ⇒ Object



98
99
100
101
102
103
104
# File 'app/services/notification_service/sms.rb', line 98

def send_confirmation_msg_without_kids(locale: default_locale)
  msg = render 'sms/booking_confirmation_without_kids_msg_to_owner', locals: locals, locale: locale
  options = {
    url: owner_confirmation_reservation_url(restaurant.owner.to_url_hash, reservation.to_url_hash),
  }
  send_sms_async(msg, options)
end

#send_confirmation_msg_without_kids_to_user(locale: default_locale) ⇒ Object



114
115
116
117
118
119
120
# File 'app/services/notification_service/sms.rb', line 114

def send_confirmation_msg_without_kids_to_user(locale: default_locale)
  msg = render 'sms/booking_confirmation_without_kids_msg_to_user', locals: locals, locale: locale
  options = {
    url: MyUrlShortener.by_url(manage_my_reservations_url(reservation.to_url_hash)),
  }
  send_sms_async(msg, options)
end

#send_last_minute_confirmation_to_user(locale: default_locale) ⇒ Object



31
32
33
34
35
36
37
# File 'app/services/notification_service/sms.rb', line 31

def send_last_minute_confirmation_to_user(locale: default_locale)
  options = {
    url: manage_my_reservations_url(reservation.to_url_hash),
  }
  msg = render 'sms/last_minute_booking_to_user', locals: locals, locale: locale
  send_sms_async(msg, options)
end

#send_last_minute_paid_promptpay_to_hh(locale: default_locale) ⇒ Object



231
232
233
234
# File 'app/services/notification_service/sms.rb', line 231

def send_last_minute_paid_promptpay_to_hh(locale: default_locale)
  msg = render 'sms/last_minute_paid_promptpay_to_hh', locals: locals, locale: locale
  send_sms_async(msg)
end

#send_last_minute_paid_promptpay_to_owner(locale: default_locale) ⇒ Object



226
227
228
229
# File 'app/services/notification_service/sms.rb', line 226

def send_last_minute_paid_promptpay_to_owner(locale: default_locale)
  msg = render 'sms/last_minute_paid_promptpay_to_owner', locals: locals, locale: locale
  send_sms_async(msg)
end

#send_last_minute_paid_promptpay_to_user(locale: default_locale) ⇒ Object



221
222
223
224
# File 'app/services/notification_service/sms.rb', line 221

def send_last_minute_paid_promptpay_to_user(locale: default_locale)
  msg = render 'sms/last_minute_paid_promptpay_to_user', locals: locals, locale: locale
  send_sms_async(msg)
end

#send_last_minute_paid_true_wallet_to_hh(locale: default_locale) ⇒ Object



246
247
248
249
# File 'app/services/notification_service/sms.rb', line 246

def send_last_minute_paid_true_wallet_to_hh(locale: default_locale)
  msg = render 'sms/last_minute_paid_true_wallet_to_hh', locals: locals, locale: locale
  send_sms_async(msg)
end

#send_no_ack_group_booking_to_user(locale: default_locale) ⇒ Object



208
209
210
211
# File 'app/services/notification_service/sms.rb', line 208

def send_no_ack_group_booking_to_user(locale: default_locale)
  msg = render 'sms/no_ack_group_booking_to_user', locals: locals, locale: locale
  send_sms_async(msg)
end

#send_pending_msg_to_hh(locale: default_locale) ⇒ Object



168
169
170
171
172
173
174
175
# File 'app/services/notification_service/sms.rb', line 168

def send_pending_msg_to_hh(locale: default_locale)
  options = {
    url: owner_confirmation_reservation_url(restaurant.owner.to_url_hash, reservation.to_url_hash),
  }

  msg = render 'sms/booking_pending_msg_to_hh', locals: locals, locale: locale
  send_sms_async(msg, options)
end

#send_pending_msg_to_restaurant(locale: default_locale) ⇒ Object



154
155
156
157
158
159
160
161
# File 'app/services/notification_service/sms.rb', line 154

def send_pending_msg_to_restaurant(locale: default_locale)
  options = {
    url: owner_confirmation_reservation_url(restaurant.owner.to_url_hash, reservation.to_url_hash),
  }

  msg = render 'sms/booking_pending_msg_to_restaurant', locals: locals, locale: locale
  send_sms_async(msg, options)
end

#send_pending_msg_to_user(locale: default_locale) ⇒ Object



163
164
165
166
# File 'app/services/notification_service/sms.rb', line 163

def send_pending_msg_to_user(locale: default_locale)
  msg = render 'sms/booking_pending_msg_to_user', locals: locals, locale: locale
  send_sms_async(msg)
end

#send_rating_reminder_to_user(locale: default_locale) ⇒ Object



251
252
253
254
# File 'app/services/notification_service/sms.rb', line 251

def send_rating_reminder_to_user(locale: default_locale)
  msg = render 'sms/rating_reminder_to_user', locals: locals, locale: locale
  send_sms_async(msg)
end

#send_rejection_msg_to_user(locale: default_locale) ⇒ Object



193
194
195
196
# File 'app/services/notification_service/sms.rb', line 193

def send_rejection_msg_to_user(locale: default_locale)
  msg = render 'sms/booking_rejection_msg_to_user', locals: locals, locale: locale
  send_sms_async(msg)
end

#send_reminder_booking_to_user(locale: default_locale) ⇒ Object



213
214
215
216
217
218
219
# File 'app/services/notification_service/sms.rb', line 213

def send_reminder_booking_to_user(locale: default_locale)
  options = {
    url: manage_my_reservations_url(reservation.to_url_hash),
  }
  msg = render 'sms/reminder_booking_to_user', locals: locals, locale: locale
  send_sms_async(msg, options)
end

#send_review_msg_to_user(locale: default_locale, rate: nil) ⇒ Object

Sends a review/rating reminder SMS to the user after their dining experience

For Signed User/Member:

Uses new rating frontend: {rating_frontend_host}/{language}/review/{reservation_id}

For Guest:

Uses existing URL format: /reservations/rating/{hash}?rate={rate}

Parameters:

  • locale (Symbol) (defaults to: default_locale)

    The locale for the SMS message (default: default_locale)

  • rate (Integer, nil) (defaults to: nil)

    Optional rating value (1-5) to pre-populate in the URL



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/services/notification_service/sms.rb', line 49

def send_review_msg_to_user(locale: default_locale, rate: nil)
  # Generate full URL (not just path) for SMS since it will be shortened by MyUrlShortener
  url = if reservation.user.present?
          # Member URL is already a full URL
          RatingUrlService.new(reservation).member_url(rate: rate)
        else
          # Guest URL needs only_path: false to get full URL for shortening
          RatingUrlService.new(reservation).guest_url(rate: rate, only_path: false)
        end

  options = { url: url }
  msg = render 'sms/rating_reminder_to_user', locals: locals, locale: locale
  send_sms_async(msg, options)
rescue StandardError => e
  handle_rating_url_error(e, locale, rate: rate)
end

#send_thanks_msgObject



203
204
205
206
# File 'app/services/notification_service/sms.rb', line 203

def send_thanks_msg
  msg = "Thank you for dining at #{restaurant_name}. You can now book a table via Hungry Hub app on your next visit. smarturl.it/hungryhub"
  send_sms_async(msg)
end

#send_ticket_redeemed(locale: default_locale) ⇒ Object



261
262
263
264
# File 'app/services/notification_service/sms.rb', line 261

def send_ticket_redeemed(locale: default_locale)
  msg = render 'sms/ticket_redeemed', locals: { ticket: ticket }
  send_sms_async(msg)
end

#send_voucher_confirmation(locale: default_locale) ⇒ Object



256
257
258
259
# File 'app/services/notification_service/sms.rb', line 256

def send_voucher_confirmation(locale: default_locale)
  msg = render 'sms/voucher_confirmation', locals: { voucher_transaction: voucher_transaction }
  send_sms_async(msg)
end

#send_wrong_paid_promptpay_to_admin(locale: default_locale) ⇒ Object



236
237
238
239
# File 'app/services/notification_service/sms.rb', line 236

def send_wrong_paid_promptpay_to_admin(locale: default_locale)
  msg = render 'sms/wrong_paid_promptpay_to_admin', locals: locals, locale: locale
  send_sms_async(msg)
end

#send_wrong_paid_promptpay_to_user(locale: default_locale) ⇒ Object



241
242
243
244
# File 'app/services/notification_service/sms.rb', line 241

def send_wrong_paid_promptpay_to_user(locale: default_locale)
  msg = render 'sms/wrong_paid_promptpay_to_user', locals: locals, locale: locale
  send_sms_async(msg)
end