Class: Usman::Api::V1::RegistrationsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/usman/api/v1/registrations_controller.rb

Instance Method Summary collapse

Methods included from Usman::ApiHelper

#current_device, #current_user, #require_admin_auth_token, #require_api_token, #require_auth_token, #require_super_admin_auth_token, #store_last_accessed_api

Instance Method Details

#accept_tacObject



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/controllers/usman/api/v1/registrations_controller.rb', line 144

def accept_tac
  proc_code = Proc.new do
    @registration = Registration.where("mobile_number = ? AND dialing_prefix = ?", params[:mobile_number], params[:dialing_prefix]).first
    if @registration
      @device = @registration.devices.where("uuid = ?", params[:uuid]).first
      if @device
        if @device.blocked?
          @success = false
          @errors = {
            heading: I18n.translate("api.accept_tac.device_blocked.heading"),
            message: I18n.translate("api.accept_tac.device_blocked.message"),
            details: {}
          }
        elsif @device.pending?
          @success = false
          @errors = {
            heading: I18n.translate("api.accept_tac.device_pending.heading"),
            message: I18n.translate("api.accept_tac.device_pending.message"),
            details: {}
          }
        else
          valid, validation_errors = @device.accept_tac(params[:terms_and_conditions], params[:dialing_prefix], params[:mobile_number])
          if valid
            @success = true
            @alert = {
              heading: I18n.translate("api.accept_tac.tac_accepted.heading"),
              message: I18n.translate("api.accept_tac.tac_accepted.message")
            }
            if @device.verified? && @device.tac_accepted?
              @data = { api_token: @device.api_token } 
              @data[:registration] = ActiveModelSerializers::SerializableResource.new(@registration, serializer: RegistrationSerializer)
              @data[:profile] = ActiveModelSerializers::SerializableResource.new(@registration.user, serializer: ProfileSerializer)
            end
          else
            @success = false
            @errors = {
              heading: I18n.translate("api.accept_tac.tac_not_accepted.heading"),
              message: I18n.translate("api.accept_tac.tac_not_accepted.message"),
              details: validation_errors
            }
          end
        end
      else
        @success = false
        @errors = {
          heading: I18n.translate("api.accept_tac.device_not_registered.heading"),
          message: I18n.translate("api.accept_tac.device_not_registered.message"),
          details: {
            uuid: "is invalid"
          }
        }
      end
    else
      @success = false
      @errors = {
        heading: I18n.translate("api.verify_otp.mobile_number_not_registered.heading"),
        message: I18n.translate("api.verify_otp.mobile_number_not_registered.message"),
        details: {
          mobile_number: "is invalid"
        }
      }
    end
  end
  render_json_response(proc_code)
end

#accept_tac1Object



210
211
212
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
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/controllers/usman/api/v1/registrations_controller.rb', line 210

def accept_tac1
  proc_code = Proc.new do
    @device = Device.where("uuid = ?", params[:uuid]).first
    if @device
      if @device.blocked?
        @success = false
        @errors = {
          heading: I18n.translate("api.mobile_registration.device_blocked.heading"),
          message: I18n.translate("api.mobile_registration.device_blocked.message"),
          details: {}
        }
      else
        valid, validation_errors = @device.accept_tac(params[:terms_and_conditions], params[:dialing_prefix], params[:mobile_number])
        if valid
          @success = true
          @alert = {
            heading: I18n.translate("api.mobile_registration.tac_accepted.heading"),
            message: I18n.translate("api.mobile_registration.tac_accepted.message")
          }
          @data = { api_token: @device.api_token }
        else
          @success = false
          @errors = {
            heading: I18n.translate("api.mobile_registration.tac_not_accepted.heading"),
            message: I18n.translate("api.mobile_registration.tac_not_accepted.message"),
            details: validation_errors
          }
        end
      end
    else
      @success = false
      @errors = {
        heading: I18n.translate("api.general.unexpected_failure.heading"),
        message: I18n.translate("api.general.unexpected_failure.message"),
        details: {
          uuid: "is invalid"
        }
      }
    end
  end
  render_json_response(proc_code)
end

#registerObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/usman/api/v1/registrations_controller.rb', line 8

def register
  proc_code = Proc.new do
    @reg_data = Usman::MobileRegistrationService.new(params)
    @errors = @reg_data.errors
    @success = false
    if @errors[:heading].blank?
      @success = true
      @alert = {
        heading: I18n.translate("api.register.otp_sent.heading"),
        message: I18n.translate("api.register.otp_sent.message")
      }
      @data = {
                registration: @reg_data.registration,
                device: @reg_data.device
              }
    end
  end
  render_json_response(proc_code)
end

#resend_otpObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/usman/api/v1/registrations_controller.rb', line 28

def resend_otp
  proc_code = Proc.new do
    @registration = Registration.where("mobile_number = ? AND dialing_prefix = ?", params[:mobile_number], params[:dialing_prefix]).first
    if @registration
      @device = @registration.devices.where("uuid = ?", params[:uuid]).first
      if @device
        if @device.blocked?
          @success = false
          @errors = {
            heading: I18n.translate("api.resend_otp.device_blocked.heading"),
            message: I18n.translate("api.resend_otp.device_blocked.message"),
            details: {}
          }
        else
          valid, validation_errors = @device.resend_otp(params[:dialing_prefix], params[:mobile_number])
          if valid
            @success = true
            @alert = {
              heading: I18n.translate("api.resend_otp.new_otp_sent.heading"),
              message: I18n.translate("api.resend_otp.new_otp_sent.message")
            }
            @data = {}
          else
            @success = false
            @errors = {
              heading: I18n.translate("api.resend_otp.otp_not_matching.heading"),
              message: I18n.translate("api.resend_otp.otp_not_matching.message"),
              details: validation_errors
            }
          end
        end
      else
        @success = false
        @errors = {
          heading: I18n.translate("api.resend_otp.device_not_registered.heading"),
          message: I18n.translate("api.resend_otp.device_not_registered.message"),
          details: {
            uuid: "is invalid"
          }
        }
      end
    else
      @success = false
      @errors = {
        heading: I18n.translate("api.resend_otp.mobile_number_not_registered.heading"),
        message: I18n.translate("api.resend_otp.mobile_number_not_registered.message"),
        details: {
          mobile_number: "is invalid"
        }
      }
    end
  end
  render_json_response(proc_code)
end

#verify_otpObject



83
84
85
86
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/usman/api/v1/registrations_controller.rb', line 83

def verify_otp
  proc_code = Proc.new do
    @registration = Registration.where("mobile_number = ? AND dialing_prefix = ?", params[:mobile_number], params[:dialing_prefix]).first
    if @registration
      @device = @registration.devices.where("uuid = ?", params[:uuid]).first
      if @device
        if @device.blocked?
          @success = false
          @errors = {
            heading: I18n.translate("api.verify_otp.device_blocked.heading"),
            message: I18n.translate("api.verify_otp.device_blocked.message"),
            details: {}
          }
        else
          valid, validation_errors = @device.validate_otp(params[:otp], params[:dialing_prefix], params[:mobile_number])
          if valid
            @success = true
            @alert = {
              heading: I18n.translate("api.verify_otp.verification_success.heading"),
              message: I18n.translate("api.verify_otp.verification_success.message")
            }
            if @device.verified? && @device.tac_accepted?
              @data = { api_token: @device.api_token }
            else
              @data = { api_token: "" } 
            end
            @data[:registration] = ActiveModelSerializers::SerializableResource.new(@registration, serializer: RegistrationSerializer)
            @data[:profile] = ActiveModelSerializers::SerializableResource.new(@registration.user, serializer: ProfileSerializer)
          else
            @success = false
            @errors = {
              heading: I18n.translate("api.verify_otp.otp_not_matching.heading"),
              message: I18n.translate("api.verify_otp.otp_not_matching.message"),
              details: validation_errors
            }
          end
        end
      else
        @success = false
        @errors = {
          heading: I18n.translate("api.verify_otp.device_not_registered.heading"),
          message: I18n.translate("api.verify_otp.device_not_registered.message"),
          details: {
            uuid: "is invalid"
          }
        }
      end
    else
      @success = false
      @errors = {
        heading: I18n.translate("api.verify_otp.mobile_number_not_registered.heading"),
        message: I18n.translate("api.verify_otp.mobile_number_not_registered.message"),
        details: {
          mobile_number: "is invalid"
        }
      }
    end
  end
  render_json_response(proc_code)
end