Author: Braulio Bhavamitra <braulio@prout.io>
stores_app/users: implement signup
%!v(PANIC=String method: strings: negative Repeat count)
diff --git a/plugins/stores_app/app/controllers/profile/stores_app_plugin/users_controller.rb b/plugins/stores_app/app/controllers/profile/stores_app_plugin/users_controller.rb index fb4d03ba2ff7fa11304e5c21c0c4a73a9da3d269..832a19a80047284cedfc81a9b0ae76d275838baa 100644 --- a/plugins/stores_app/app/controllers/profile/stores_app_plugin/users_controller.rb +++ b/plugins/stores_app/app/controllers/profile/stores_app_plugin/users_controller.rb @@ -10,7 +10,45 @@ render json: {error: 'invalid_login_pass'} end end + def signup + @user = User.find_by email: params[:email] + return render json: {error: 'user_exists'} if @user + + user_params = params.slice(:name, :email, :phone, :password) + .merge(login: generate_login, password_confirmation: params[:password]) + person_params = params.slice(:name, :email) + .merge(identifier: user_params[:login], cell_phone: params[:phone]) + + @user = User.new user_params + @user.person = Person.new person_params + @user.person.environment = environment + + if @user.valid? + ApplicationRecord.transaction do + @user.save! + @user.person.user = @user + @user.person.save! + end + profile.add_member @user.person + + render json: UserSerializer.new(@user).to_hash + else + @user.person.valid? + fields = @user.errors.keys + @user.person.errors.keys + render json: {error: 'invalid_fields', fields: fields} + end + end + protected + + def generate_login + login = params[:name].parameterize + 1.step do |i| + break unless Profile[login] + login = "#{params[:name].parameterize}#{i}" + end + login + end end end