Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. # api/v1/endpoints/registration_endpoint.rb
  2. class RegistrationEndpoint < Grape::API
  3.  
  4. desc 'Create user, or register new user'
  5. params do
  6.  
  7. requires :role, type: String
  8.  
  9. optional :username, type: String
  10. optional :email, type: String
  11. optional :password, type: String
  12. optional :password_confirmation, type: String
  13. optional :phone, type: String
  14.  
  15. given role: ->(val) {val == 'mother'} do
  16. requires :password
  17. requires :password_confirmation
  18. requires :full_name
  19. requires :email
  20. end
  21.  
  22. given role: ->(val) {val == 'midwife'} do
  23. requires :full_name, type: String, message: :required_field
  24. requires :phone, type: String
  25. end
  26. end
  27. post :register do
  28. otp_code = ''
  29. if params[:role] == 'midwife'
  30. raise ::JWT::VerificationError, _("You must not give token when register") if auth_token.present?
  31. @user = UserMidwife.register params
  32.  
  33. # Send sms otp
  34. sms_otp, otp_code = Sms::Otp.registration.for(@user).deliver
  35. elsif params[:role] == 'mother'
  36.  
  37. api_error!(_('Password does not match'), 400) if params[:password] != params[:password_confirmation]
  38.  
  39. @user = UserMother.register(
  40. full_name: params[:full_name],
  41. phone: params[:phone],
  42. password: params[:password],
  43. email: params[:email]
  44. )
  45.  
  46. EmailOTPBackground.enqueue @user.id
  47. else
  48. raise ::Exceptions::RequestError, _('You must specify role user')
  49. end
  50.  
  51. show_token = params[:role] == 'mother' ? @user.email_confirmed? : @user.phone_confirmed?
  52.  
  53. present @user, with: UserEntity, show_token: show_token
  54. end
  55. endv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement