Class: Usman::MobileRegistrationService

Inherits:
Object
  • Object
show all
Defined in:
app/services/usman/mobile_registration_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ MobileRegistrationService

Returns a new instance of MobileRegistrationService.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/usman/mobile_registration_service.rb', line 12

def initialize(params)
  @dialing_prefix = params[:dialing_prefix]
  @mobile_number = params[:mobile_number]
  
  @country = nil
  @city = nil
  @country = Country.find_by_id(params[:country_id])
  @city = City.find_by_id(params[:city_id])

  # Edge case to catch city selected that of a different country
  @city = nil unless @city.country == @country if @city
  
  @uuid = params[:uuid]
  @device_token = params[:device_token]
  @device_name = params[:device_name]
  @device_type = params[:device_type]
  @operating_system = params[:operating_system]
  @software_version = params[:software_version]

  @remote_ip = params[:remote_ip]
  @error_message = nil
  @error_details = {}

  # @registration and @device will be initiated by the
  # below method if the device is already registered
  check_if_device_is_already_registered

  register_new_device

  # Validate the inputs
  @registration.valid?
  @device.valid?

  if @registration.errors.any? or @device.errors.any?
    errors = @registration.errors.to_hash.merge(@device.errors.to_hash)
    set_error("mobile_registration.invalid_inputs", errors)
  end

end

Instance Attribute Details

#cityObject (readonly)

Returns the value of attribute city.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def city
  @city
end

#countryObject (readonly)

Returns the value of attribute country.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def country
  @country
end

#deviceObject (readonly)

Returns the value of attribute device.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def device
  @device
end

#device_nameObject (readonly)

Returns the value of attribute device_name.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def device_name
  @device_name
end

#device_tokenObject (readonly)

Returns the value of attribute device_token.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def device_token
  @device_token
end

#device_typeObject (readonly)

Returns the value of attribute device_type.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def device_type
  @device_type
end

#dialing_prefixObject (readonly)

Returns the value of attribute dialing_prefix.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def dialing_prefix
  @dialing_prefix
end

#error_detailsObject (readonly)

Returns the value of attribute error_details.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def error_details
  @error_details
end

#error_headingObject (readonly)

Returns the value of attribute error_heading.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def error_heading
  @error_heading
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def error_message
  @error_message
end

#mobile_numberObject (readonly)

Returns the value of attribute mobile_number.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def mobile_number
  @mobile_number
end

#operating_systemObject (readonly)

Returns the value of attribute operating_system.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def operating_system
  @operating_system
end

#registrationObject (readonly)

Returns the value of attribute registration.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def registration
  @registration
end

#remote_ipObject (readonly)

Returns the value of attribute remote_ip.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def remote_ip
  @remote_ip
end

#software_versionObject (readonly)

Returns the value of attribute software_version.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def software_version
  @software_version
end

#uuidObject (readonly)

Returns the value of attribute uuid.



4
5
6
# File 'app/services/usman/mobile_registration_service.rb', line 4

def uuid
  @uuid
end

Instance Method Details

#check_if_device_is_already_registeredObject



52
53
54
55
56
57
58
59
# File 'app/services/usman/mobile_registration_service.rb', line 52

def check_if_device_is_already_registered
  @registration = Registration.where("LOWER(mobile_number) = LOWER('#{@mobile_number}')").first
  if @registration
    @registration.country = @country
    @registration.city = @city
  end
  @device = Device.where("LOWER(uuid) = LOWER('#{@uuid}')").first if @registration
end

#errorsObject



120
121
122
123
124
125
126
# File 'app/services/usman/mobile_registration_service.rb', line 120

def errors
  {
    heading: @error_heading,
    message: @error_message,
    details: @error_details
  }
end

#generate_new_otpObject



101
102
103
104
105
106
107
108
# File 'app/services/usman/mobile_registration_service.rb', line 101

def generate_new_otp
  @device.generate_otp
  if send_otp
    @device.update_attribute(:otp_sent_at, Time.now)
  else
    set_error("mobile_registration.otp_not_sent")
  end
end

#register_new_deviceObject



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/services/usman/mobile_registration_service.rb', line 61

def register_new_device
  
  if @device && @device.blocked?
    set_error("mobile_registration.device_blocked")
    return
  end

  ActiveRecord::Base.transaction do
    # Create a new registration if it doesn't exist
    @registration = Registration.new unless @registration
    @registration.country = @country
    @registration.city = @city
    @registration.dialing_prefix = @dialing_prefix
    @registration.mobile_number = @mobile_number
    
    # Create device entry if it doesn't exist
    @device = Device.new unless @device
    @device.registration = @registration
    @device.user = @registration.user
    @device.uuid = @uuid
    @device.device_token = @device_token
    @device.device_name = @device_name
    @device.device_type = @device_type
    @device.operating_system = @operating_system
    @device.software_version = @software_version

    @registration.valid?
    @device.valid?

    if @registration.errors.blank? && @device.errors.blank?
      @registration.save
      @device.save
      generate_new_otp
    else
      raise ActiveRecord::Rollback 
    end
  end

end

#send_otpObject



110
111
112
# File 'app/services/usman/mobile_registration_service.rb', line 110

def send_otp
  return true
end

#set_error(key, hsh = {}) ⇒ Object



114
115
116
117
118
# File 'app/services/usman/mobile_registration_service.rb', line 114

def set_error(key, hsh={})
  @error_heading = I18n.t("#{key}.heading")
  @error_message = I18n.t("#{key}.message")
  @error_details = hsh if hsh.is_a?(Hash)
end