docbrown/app/controllers/admin/invitations_controller.rb
Suzanne Aitchison b1e7d8a5ec
Member index (phase 2) - Search invitations (#17210)
* add ability to search invitations

* add an invited scope

* add a cypress test
2022-04-12 10:40:44 +01:00

44 lines
1.4 KiB
Ruby

module Admin
class InvitationsController < Admin::ApplicationController
layout "admin"
def index
@invitations = Admin::UsersQuery.call(relation: User.invited,
options: params.permit(
:search,
)).page(params[:page]).per(50)
end
def new; end
def create
email = params.dig(:user, :email)
name = params.dig(:user, :name)
if User.exists?(email: email.downcase, registered: true)
flash[:error] = "Invitation was not sent. There is already a registered user with the email: #{email}"
redirect_to admin_invitations_path
return
end
username = "#{name.downcase.tr(' ', '_').gsub(/[^0-9a-z ]/i, '')}_#{rand(1000)}"
User.invite!(email: email,
name: name,
username: username,
remote_profile_image_url: ::Users::ProfileImageGenerator.call,
registered: false)
flash[:success] = "The invite has been sent to the user's email."
redirect_to admin_invitations_path
end
def destroy
@invitation = User.where(registered: false).find(params[:id])
if @invitation.destroy
flash[:success] = "The invitation has been deleted."
else
flash[:danger] = @invitation.errors_as_sentence
end
redirect_to admin_invitations_path
end
end
end