Class: SmsWorker

Inherits:
ApplicationWorker show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/sms_worker.rb

Overview

This class is purely just responsible to send sms just need phone number and message when this class has failed to send a message, it will retrying to send again using different provider by executing [RecheckSmsWorker]

Defined Under Namespace

Classes: MySmsError

Constant Summary collapse

ACTIVE_SMS_PROVIDERS =
%i[alibaba kaleyra].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Class Method Details

.notify_password_changed(new_password, phone) ⇒ Object



106
107
108
109
# File 'app/workers/sms_worker.rb', line 106

def notify_password_changed(new_password, phone)
  msg = "(Hungry Hub) Your new password is #{new_password}"
  SmsWorker.perform_async msg, phone
end

Instance Method Details

#perform(msg, phones, reservation_id = nil, provider = nil) ⇒ void

This method returns an undefined value.

Performs the task of sending an SMS message to multiple phone numbers using a chosen SMS provider.

Parameters:

  • msg (String)

    The message to be sent via SMS.

  • phones (Array<String>)

    An array of phone numbers to which the SMS should be sent.

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

    The ID of the reservation associated with the SMS, if any.

  • provider (String, nil) (defaults to: nil)

    The chosen SMS provider. As default, we use data on AdminSetting.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/workers/sms_worker.rb', line 25

def perform(msg, phones, reservation_id = nil, provider = nil)
  return unless valid_to_send_sms?(phones, reservation_id)

  BUSINESS_LOGGER.set_business_context({ reservation_id: reservation_id }) if reservation_id.present?
  BUSINESS_LOGGER.info('SmsWorker perform', message: msg, phone_numbers: phones, provider: provider)

  Rails.logger.debug("Send SMS #{msg}") if Rails.env.development?

  phones, msg = sanitize_phone_numbers_and_message(phones, msg)
  sms_provider = decide_sms_provider(provider)

  if sms_provider.present?
    sms_service_provider = decide_sms_service_provider(sms_provider)
    sms_service_provider.send_sms(msg, phones, reservation_id)
  else
    BUSINESS_LOGGER.info('Fail to send SMS, invalid phone number',
                         reservation_id: reservation_id,
                         message: msg,
                         phone_numbers: phones)
  end
end