mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-13 23:37:47 +00:00
Move invite enterprise user reflex logic to enterprise controler
This commit is contained in:
@@ -7,6 +7,7 @@ require 'open_food_network/order_cycle_permissions'
|
||||
module Admin
|
||||
class EnterprisesController < Admin::ResourceController
|
||||
include GeocodeEnterpriseAddress
|
||||
include ManagerInvitations
|
||||
include Pagy::Backend
|
||||
|
||||
# These need to run before #load_resource so that @object is initialised with sanitised values
|
||||
@@ -14,7 +15,7 @@ module Admin
|
||||
prepend_before_action :override_sells, only: :create
|
||||
|
||||
before_action :load_countries,
|
||||
except: [:index, :register, :check_permalink, :remove_logo,
|
||||
except: [:index, :register, :check_permalink, :remove_logo, :invite,
|
||||
:remove_terms_and_conditions]
|
||||
before_action :load_methods_and_fees, only: [:edit, :update]
|
||||
before_action :load_groups, only: [:new, :edit, :update, :create]
|
||||
@@ -171,6 +172,36 @@ module Admin
|
||||
end
|
||||
end
|
||||
|
||||
def invite
|
||||
return head :bad_request if params[:email].blank?
|
||||
|
||||
authorize! :edit, @object
|
||||
email = params[:email]
|
||||
existing_user = Spree::User.find_by(email:)
|
||||
|
||||
locals = { error: nil, success: nil, email:, enterprise: @object }
|
||||
|
||||
if existing_user
|
||||
locals[:error] = I18n.t('admin.enterprises.invite_manager.user_already_exists')
|
||||
else
|
||||
new_user = create_new_manager(email, @object)
|
||||
if new_user.errors.empty?
|
||||
locals[:success] = true
|
||||
else
|
||||
locals[:error] = new_user.errors.full_messages.to_sentence
|
||||
end
|
||||
end
|
||||
respond_to do |format|
|
||||
format.html { redirect_to main_app.edit_admin_enterprise_path(@object) }
|
||||
format.turbo_stream do
|
||||
render turbo_stream: turbo_stream.replace(
|
||||
'add_manager_modal',
|
||||
partial: "admin/enterprises/form/add_new_unregistered_manager", locals:
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def remove_terms_and_conditions
|
||||
authorize! :remove_terms_and_conditions, @object
|
||||
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
# 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:)
|
||||
|
||||
locals = { error: nil, success: nil, email:, enterprise: }
|
||||
|
||||
if existing_user
|
||||
locals[:error] = I18n.t('admin.enterprises.invite_manager.user_already_exists')
|
||||
|
||||
return_morph(locals)
|
||||
return
|
||||
end
|
||||
|
||||
new_user = create_new_manager(email, enterprise)
|
||||
|
||||
if new_user.errors.empty?
|
||||
locals[:success] = true
|
||||
else
|
||||
locals[:error] = new_user.errors.full_messages.to_sentence
|
||||
end
|
||||
|
||||
return_morph(locals)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def return_morph(locals)
|
||||
morph "#add_manager_modal",
|
||||
render(partial: "admin/enterprises/form/add_new_unregistered_manager", locals:)
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
%form#add_manager_modal{ 'data-reflex': 'submit->InviteManager#invite', 'data-reflex-serialize-form': true }
|
||||
= form_with(url: invite_admin_enterprise_path, id: 'add_manager_modal', method: :patch, data: { turbo: true }) do
|
||||
.margin-bottom-30.text-center
|
||||
.text-big
|
||||
= t('js.admin.modals.invite_title')
|
||||
|
||||
@@ -34,6 +34,7 @@ Openfoodnetwork::Application.routes.draw do
|
||||
get :welcome
|
||||
patch :register
|
||||
patch :remove_logo
|
||||
patch :invite
|
||||
delete :remove_terms_and_conditions
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user