From 20874dec98797741bf17232bd51660c34878d2b4 Mon Sep 17 00:00:00 2001 From: binarygit Date: Mon, 3 Oct 2022 15:05:01 +0545 Subject: [PATCH] Replace angular for when adding a new unregistered manager to an enterprise Co-Authored-By: David Cook --- app/reflexes/invite_manager_reflex.rb | 43 +++++++++++++++ .../_add_new_unregistered_manager.html.haml | 22 ++++++++ .../admin/enterprises/form/_users.html.haml | 20 +------ spec/system/admin/enterprises_spec.rb | 52 +++++++++++++++++++ 4 files changed, 118 insertions(+), 19 deletions(-) create mode 100644 app/reflexes/invite_manager_reflex.rb create mode 100644 app/views/admin/enterprises/form/_add_new_unregistered_manager.html.haml diff --git a/app/reflexes/invite_manager_reflex.rb b/app/reflexes/invite_manager_reflex.rb new file mode 100644 index 0000000000..ad36d80150 --- /dev/null +++ b/app/reflexes/invite_manager_reflex.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +class InviteManagerReflex < ApplicationReflex + include ManagerInvitations + + def invite + email = params[:email] + enterprise = Enterprise.find(params[:enterprise_id]) + + authorize! :edit, enterprise + + existing_user = Spree::User.find_by(email: email) + + locals = { error: nil, success: nil, email: email, enterprise: enterprise } + + if existing_user + locals[:error] = I18n.t('admin.enterprises.invite_manager.user_already_exists') + + return_morph(locals) + return + end + + begin + new_user = create_new_manager(email, enterprise) + locals[:success] = true + locals[:email] = new_user.email + rescue StandardError => e + @error = e.message + locals[:error] = @error || I18n.t('admin.enterprises.invite_manager.error') + end + + return_morph(locals) + end + + private + + def return_morph(locals) + morph "#add_manager_modal", + with_locale { + render(partial: "admin/enterprises/form/add_new_unregistered_manager", locals: locals) + } + end +end diff --git a/app/views/admin/enterprises/form/_add_new_unregistered_manager.html.haml b/app/views/admin/enterprises/form/_add_new_unregistered_manager.html.haml new file mode 100644 index 0000000000..c24beeba83 --- /dev/null +++ b/app/views/admin/enterprises/form/_add_new_unregistered_manager.html.haml @@ -0,0 +1,22 @@ +%div#add_manager_modal + %form{ "data-reflex": "submit->InviteManager#invite", "data-reflex-serialize-form": true } + .margin-bottom-30.text-center + .text-big + = t('js.admin.modals.invite_title') + + - if success + %p.alert-box.ok= t('user_invited', email: email) + + - if error + %p.alert-box.error= error + + = text_field_tag :email, nil, class: 'fullwidth margin-bottom-20' + = hidden_field_tag :enterprise_id, @enterprise&.id || enterprise.id + + .modal-actions + - if success + %input{ class: "button icon-plus secondary", type: 'button', value: t('js.admin.modals.close'), "data-action": "click->help-modal#close" } + - else + %input{ class: "button icon-plus secondary", type: 'button', value: t('js.admin.modals.cancel'), "data-action": "click->help-modal#close" } + = submit_tag "#{t('js.admin.modals.invite')}" + diff --git a/app/views/admin/enterprises/form/_users.html.haml b/app/views/admin/enterprises/form/_users.html.haml index 01bfdf24a3..2579b35369 100644 --- a/app/views/admin/enterprises/form/_users.html.haml +++ b/app/views/admin/enterprises/form/_users.html.haml @@ -72,22 +72,4 @@ -# add to admin footer to avoid nesting invitation form inside enterprise form - content_for :admin_footer do = render HelpModalComponent.new(id: "invite-manager-modal", close_button: false) do - %div{ng: {app: 'admin.enterprises', controller: 'enterpriseCtrl'}} - - .margin-bottom-30.text-center - .text-big - = t('js.admin.modals.invite_title') - - %p.alert-box.ok{ng: {show: 'invite_success'}} - {{invite_success}} - - %p.alert-box.error{ng: {show: 'invite_errors'}} - {{invite_errors}} - - %input#invite_email.fullwidth.margin-bottom-20{ng: {model: 'newUser'}} - - .margin-bottom-20.text-center - %button.text-center.margin-top-10{ng: {show: '!invite_success', click: 'inviteManager()'}} - = t('js.admin.modals.invite') - %button.text-center.margin-top-10{"data-action": "click->help-modal#close", ng: {show: 'invite_success', click: 'resetModal();'}} - = t('js.admin.modals.close') + = render partial: 'admin/enterprises/form/add_new_unregistered_manager', locals: { error: nil, success: nil } diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index d4302bc462..d377f51635 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -532,5 +532,57 @@ describe ' end end end + + describe "check users tab" do + before do + login_as_admin_and_visit edit_admin_enterprise_path(distributor1) + within ".side_menu" do + click_link 'Users' + end + end + + context "invite user as manager" do + before do + expect(page).to have_selector('a', text: /Add an unregistered user/i) + page.find('a', text: /Add an unregistered user/i).click + end + + it "shows an error message if the email is invalid" do + within ".reveal-modal" do + expect(page).to have_content "Invite an unregistered user" + fill_in "email", with: "invalid_email" + + expect do + click_button "Invite" + expect(page).to have_content "Email is invalid" + end.to_not enqueue_job ActionMailer::MailDeliveryJob + end + end + + it "shows an error message if the email is already linked to an existing user" do + within ".reveal-modal" do + expect(page).to have_content "Invite an unregistered user" + fill_in "email", with: distributor1.owner.email + + expect do + click_button "Invite" + expect(page).to have_content "User already exists" + end.to_not enqueue_job ActionMailer::MailDeliveryJob + end + end + + it "finally, can invite unregistered users" do + within ".reveal-modal" do + expect(page).to have_content "Invite an unregistered user" + fill_in "email", with: "email@email.com" + + expect do + click_button "Invite" + expect(page).to have_content "email@email.com has been invited to manage this enterprise" + end.to enqueue_job(ActionMailer::MailDeliveryJob).exactly(:twice) + end + end + end + end end end