From 89ed0b425be5e4ab039dec672d9538126e25de11 Mon Sep 17 00:00:00 2001 From: Divyesh Kothari Date: Sun, 26 Jul 2020 12:54:50 +0530 Subject: [PATCH] feat: Ability for the logged in user to create a new account (#985) Co-authored-by: Divyesh Co-authored-by: Pranav Raj S --- app/builders/account_builder.rb | 27 +++-- app/controllers/api/v1/accounts_controller.rb | 7 +- app/controllers/dashboard_controller.rb | 3 +- app/javascript/dashboard/api/account.js | 5 + .../dashboard/components/layout/Sidebar.vue | 99 ++++++++++++++++++- .../dashboard/i18n/locale/en/settings.json | 17 ++++ .../dashboard/store/modules/accounts.js | 12 +++ .../modules/specs/account/actions.spec.js | 27 +++++ app/javascript/shared/store/globalConfig.js | 16 +-- app/models/user.rb | 1 + .../api/v1/accounts/create.json.jbuilder | 23 +++++ config/installation_config.yml | 2 +- .../api/v1/accounts_controller_spec.rb | 8 +- 13 files changed, 219 insertions(+), 28 deletions(-) create mode 100644 app/views/api/v1/accounts/create.json.jbuilder diff --git a/app/builders/account_builder.rb b/app/builders/account_builder.rb index 1c7c1f63d..ae6db3b4e 100644 --- a/app/builders/account_builder.rb +++ b/app/builders/account_builder.rb @@ -2,15 +2,18 @@ class AccountBuilder include CustomExceptions::Account - pattr_initialize [:account_name!, :email!, :confirmed!] + pattr_initialize [:account_name!, :email!, :confirmed!, :user] def perform - validate_email - validate_user + if @user.nil? + validate_email + validate_user + end ActiveRecord::Base.transaction do @account = create_account @user = create_and_link_user end + [@user, @account] rescue StandardError => e @account&.destroy puts e.inspect @@ -42,13 +45,7 @@ class AccountBuilder end def create_and_link_user - password = Time.now.to_i - @user = User.new(email: @email, - password: password, - password_confirmation: password, - name: email_to_name(@email)) - @user.confirm if @confirmed - if @user.save! + if @user.present? || create_user link_user_to_account(@user, @account) @user else @@ -68,4 +65,14 @@ class AccountBuilder name = email[/[^@]+/] name.split('.').map(&:capitalize).join(' ') end + + def create_user + password = Time.now.to_i + @user = User.new(email: @email, + password: password, + password_confirmation: password, + name: email_to_name(@email)) + @user.confirm if @confirmed + @user.save! + end end diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb index cf60de614..933bd5a3a 100644 --- a/app/controllers/api/v1/accounts_controller.rb +++ b/app/controllers/api/v1/accounts_controller.rb @@ -14,14 +14,15 @@ class Api::V1::AccountsController < Api::BaseController with: :render_error_response def create - @user = AccountBuilder.new( + @user, @account = AccountBuilder.new( account_name: account_params[:account_name], email: account_params[:email], - confirmed: confirmed? + confirmed: confirmed?, + user: current_user ).perform if @user send_auth_headers(@user) - render partial: 'devise/auth.json', locals: { resource: @user } + render 'api/v1/accounts/create.json', locals: { resource: @user } else render_error_response(CustomExceptions::Account::SignupFailed.new({})) end diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 272c2f546..20fa6f027 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -15,7 +15,8 @@ class DashboardController < ActionController::Base 'WIDGET_BRAND_URL', 'TERMS_URL', 'PRIVACY_URL', - 'DISPLAY_MANIFEST' + 'DISPLAY_MANIFEST', + 'CREATE_NEW_ACCOUNT_FROM_DASHBOARD' ) end end diff --git a/app/javascript/dashboard/api/account.js b/app/javascript/dashboard/api/account.js index 207420da6..82b0c434c 100644 --- a/app/javascript/dashboard/api/account.js +++ b/app/javascript/dashboard/api/account.js @@ -1,9 +1,14 @@ +/* global axios */ import ApiClient from './ApiClient'; class AccountAPI extends ApiClient { constructor() { super('', { accountScoped: true }); } + + createAccount(data) { + return axios.post(`${this.apiVersion}/accounts`, data); + } } export default new AccountAPI(); diff --git a/app/javascript/dashboard/components/layout/Sidebar.vue b/app/javascript/dashboard/components/layout/Sidebar.vue index d6ab8a75d..fbd10920c 100644 --- a/app/javascript/dashboard/components/layout/Sidebar.vue +++ b/app/javascript/dashboard/components/layout/Sidebar.vue @@ -34,7 +34,7 @@ class="dropdown-pane top" >