Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # api/v1/endpoints/registration_endpoint.rb
- class RegistrationEndpoint < Grape::API
- desc 'Create user, or register new user'
- params do
- requires :role, type: String
- optional :username, type: String
- optional :email, type: String
- optional :password, type: String
- optional :password_confirmation, type: String
- optional :phone, type: String
- given role: ->(val) {val == 'mother'} do
- requires :password
- requires :password_confirmation
- requires :full_name
- requires :email
- end
- given role: ->(val) {val == 'midwife'} do
- requires :full_name, type: String, message: :required_field
- requires :phone, type: String
- end
- end
- post :register do
- otp_code = ''
- if params[:role] == 'midwife'
- raise ::JWT::VerificationError, _("You must not give token when register") if auth_token.present?
- @user = UserMidwife.register params
- # Send sms otp
- sms_otp, otp_code = Sms::Otp.registration.for(@user).deliver
- elsif params[:role] == 'mother'
- api_error!(_('Password does not match'), 400) if params[:password] != params[:password_confirmation]
- @user = UserMother.register(
- full_name: params[:full_name],
- phone: params[:phone],
- password: params[:password],
- email: params[:email]
- )
- EmailOTPBackground.enqueue @user.id
- else
- raise ::Exceptions::RequestError, _('You must specify role user')
- end
- show_token = params[:role] == 'mother' ? @user.email_confirmed? : @user.phone_confirmed?
- present @user, with: UserEntity, show_token: show_token
- end
- endv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement