Skip to content

[Feat] Confirmation email and new petitions emails #668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,10 @@ def member_should_exist_and_be_active
redirect_to select_organization_path
end
end

def user_should_be_confirmed
return if current_user.confirmed?

redirect_to please_confirm_users_path
end
end
1 change: 1 addition & 0 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class OrganizationsController < ApplicationController
before_action :load_resource, only: [:show, :edit, :update, :set_current]
before_action :user_should_be_confirmed

def index
if current_user
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/petitions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
class PetitionsController < ApplicationController
before_action :authenticate_user!

def create
petition = Petition.new petition_params

if petition.save
OrganizationNotifier.new_petition(petition).deliver_now
OrganizationNotifier.petition_sent(petition).deliver_now

flash[:notice] = 'Application sent'
else
flash[:error] = 'Something went wrong'
Expand Down
14 changes: 9 additions & 5 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class UsersController < ApplicationController
before_action :authenticate_user!, except: %i[signup create]
before_action :member_should_exist_and_be_active, except: %i[signup create edit show update]
before_action :user_should_be_confirmed, except: %i[signup create please_confirm]
before_action :member_should_exist_and_be_active, except: %i[signup create edit show update please_confirm]

has_scope :tagged_with, as: :tag

Expand Down Expand Up @@ -36,17 +37,16 @@ def edit
def create
authorize User

from_signup = params[:from_signup].present?
email = user_params[:email]
@user = User.find_or_initialize_by(email: email) do |u|
u.attributes = user_params
end
empty_email = @user.email.empty?
@user.from_signup = from_signup
@user.from_signup = params[:from_signup].present?
@user.setup_and_save_user

if @user.persisted?
unless from_signup
unless @user.from_signup
@user.tune_after_persisted(current_organization)
@user.add_tags(current_organization, params[:tag_list] || [])
end
Expand All @@ -55,7 +55,7 @@ def create
else
@user.email = "" if empty_email

render action: from_signup ? 'signup' : 'new'
render action: @user.from_signup ? 'signup' : 'new'
end
end

Expand All @@ -73,6 +73,8 @@ def update
end

def signup
redirect_to root_path and return if current_user

@user = User.new
end

Expand All @@ -88,6 +90,8 @@ def update_avatar
redirect_to current_user
end

def please_confirm; end

private

def search_and_load_members(members_scope, default_search_params)
Expand Down
23 changes: 23 additions & 0 deletions app/mailers/organization_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,27 @@ def recent_posts(posts, locale, users)
mail(bcc: users.map(&:email))
end
end

def new_petition(petition)
@user = petition.user
organization = petition.organization

I18n.with_locale(locale) do
mail(
subject: 'New Application',
to: organization.users.joins(:members).where(members: { manager: true }).pluck(:email).uniq
)
end
end

def petition_sent(petition)
@organization_name = petition.organization.name

I18n.with_locale(locale) do
mail(
subject: 'Application sent correctly',
to: petition.user.email
)
end
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def setup_and_save_user
# this will be updated to user.id@example.com later on
self.empty_email = email.strip.empty?
self.email = "user#{DateTime.now.strftime('%Q')}@example.com" if empty_email && !from_signup
skip_confirmation! # auto-confirm, not sending confirmation email
skip_confirmation! unless from_signup?
save
end

Expand Down
1 change: 1 addition & 0 deletions app/views/organization_notifier/new_petition.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello! New petition from user <%= @user.username %>. manage your applications <%= link_to 'here', manage_petitions_url %>.
1 change: 1 addition & 0 deletions app/views/organization_notifier/petition_sent.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello! your application to <%= @organization_name %> has been sent correctly.
2 changes: 2 additions & 0 deletions app/views/users/please_confirm.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Please, confirm the email</h1>
An email has been sent to <%= current_user.email %>. After confirm you will be able to apply to any organization.
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
collection do
get 'signup'
get 'manage'
get 'please_confirm'
end
end
put :update_avatar, to: 'users#update_avatar'
Expand Down