Skip to content

Commit

Permalink
fix invite join mailer
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Massot committed Oct 30, 2018
1 parent 06954a8 commit d196d1e
Show file tree
Hide file tree
Showing 24 changed files with 259 additions and 19 deletions.
Expand Up @@ -10,8 +10,6 @@
<div class="registration__buttons">
<button type="button" class="small-5 small"><strong><%= price %></strong></button>
<%= render :join_conference %>
<%#= cell "decidim/conferences/join_conference_button", current_participatory_space, big_button: true, show_remaining_slots: false, allowed: allowed_to?(:join, :conference, conference: current_participatory_space) %>
<!-- <button type="button" class="button small-7 button--sc small">Registro</button> -->
</div>
</div>
</div>
Expand Down
@@ -0,0 +1,61 @@
# frozen_string_literal: true

module Decidim
module Conferences
module Admin
# This command is executed when the user joins a conference.
class ConfirmConferenceRegistration < Rectify::Command
# Initializes a JoinConference Command.
#
# conference_registration - The registration to be confirmed
def initialize(conference_registration)
@conference_registration = conference_registration
end

# Creates a conference registration if the conference has registrations enabled
# and there are available slots.
#
# Broadcasts :ok if successful, :invalid otherwise.
def call
conference_registration.with_lock do
return broadcast(:invalid) unless can_join_conference?
confirm_registration
send_email_confirmation
end
broadcast(:ok)
end

private

attr_reader :conference, :user

def confirm_registration
extra_info = {
resource: {
title: conference_registration.conference.title
}
}

Decidim.traceability.perform_action!(
"confirm",
conference_registration,
current_user,
extra_info
) do
conference_registration.update!(confirmed_at: Time.current)
conference_registration
end
end

def can_join_conference?
conference_registration.conference.registrations_enabled? && conference_registration.conference.has_available_slots?
end

def send_email_confirmation
Decidim::Conferences::ConferenceRegistrationMailer.confirmation(conference_registration.user,
conference_registration.conference, conference_registration.registration_type).deliver_later
end
end
end
end
end
Expand Up @@ -59,6 +59,7 @@ def create_invitation!
{
user: user,
conference: conference,
registration_type: form.registration_type,
sent_at: Time.current
},
log_info
Expand All @@ -74,7 +75,7 @@ def invite_user
if user.invited_to_sign_up?
invite_user_to_sign_up
else
InviteJoinConferenceMailer.invite(user, conference, invited_by).deliver_later
InviteJoinConferenceMailer.invite(user, conference, form.registration_type, invited_by).deliver_later
end
else
user.name = form.name
Expand Down
Expand Up @@ -25,7 +25,7 @@ def call
create_registration
create_meetings_registrations
accept_invitation
send_email_confirmation
send_email_pending_validation
send_notification
end
broadcast(:ok)
Expand Down Expand Up @@ -56,8 +56,8 @@ def can_join_conference?
conference.registrations_enabled? && conference.has_available_slots?
end

def send_email_confirmation
Decidim::Conferences::ConferenceRegistrationMailer.confirmation(user, conference).deliver_later
def send_email_pending_validation
Decidim::Conferences::ConferenceRegistrationMailer.pending_validation(user, conference, registration_type).deliver_later
end

def participatory_space_admins
Expand Down
Expand Up @@ -28,11 +28,32 @@ def export
end
end

def confirm
enforce_permission_to :confirm, :conference_registration, conference_registration: conference_registration

ConfirmConferenceRegistration.call(conference_registration) do
on(:ok) do
flash[:notice] = I18n.t("conference_registration.confirm.success", scope: "decidim.admin")
end

on(:invalid) do
flash.now[:alert] = I18n.t("conference_registration.confirm.error", scope: "decidim.admin")
end

redirect_back(fallback_location: conference_conference_registrations_path)
end
end

private

def conference
@conference ||= Decidim::Conference.find_by(slug: params[:conference_slug])
end

def conference_registration
return if params[:id].blank?
@conference_registration ||= conference.conference_registrations.find_by(id: params[:id])
end
end
end
end
Expand Down
Expand Up @@ -6,9 +6,11 @@ module Admin
# A form object used to invite users to join a conference.
#
class ConferenceRegistrationInviteForm < Form
include TranslatableAttributes
attribute :name, String
attribute :email, String
attribute :user_id, Integer
attribute :registration_type_id, Integer
attribute :existing_user, Boolean, default: false

validates :name, presence: true, unless: proc { |object| object.existing_user }
Expand All @@ -18,6 +20,19 @@ class ConferenceRegistrationInviteForm < Form
def user
@user ||= current_organization.users.find_by(id: user_id)
end

def registration_type
@registration_type ||= current_participatory_space.registration_types.find_by(id: registration_type_id)
end

def registration_types_for_select
@registration_types_for_select ||= current_participatory_space.registration_types&.map do |registration_type|
[
translated_attribute(registration_type.title),
registration_type.id
]
end
end
end
end
end
Expand Down
Expand Up @@ -14,7 +14,7 @@ class RegistrationTypeForm < Form
translatable_attribute :description, String

attribute :weight, Integer
attribute :price, Integer
attribute :price, Decimal
attribute :conference_meeting_ids, Array[Integer]

validates :title, :description, :price, :weight, presence: true
Expand Down
Expand Up @@ -19,13 +19,14 @@ class InviteJoinConferenceMailer < Decidim::ApplicationMailer
# user - The user being invited
# conference - The conference being joined.
# invited_by - The user performing the invitation.
def invite(user, conference, invited_by)
def invite(user, conference, registration_type, invited_by)
with_user(user) do
@user = user
@conference = conference
@invited_by = invited_by
@organization = @conference.organization
@locator = Decidim::ResourceLocatorPresenter.new(@conference)
@registration_type = registration_type

subject = I18n.t("invite.subject", scope: "decidim.conferences.mailer.invite_join_conference_mailer")
mail(to: user.email, subject: subject)
Expand Down
Expand Up @@ -10,13 +10,28 @@ class ConferenceRegistrationMailer < Decidim::ApplicationMailer

helper Decidim::ResourceHelper
helper Decidim::TranslationsHelper
helper Decidim::ApplicationHelper

def confirmation(user, conference)
def pending_validation(user, conference, registration_type)
with_user(user) do
@user = user
@conference = conference
@organization = @conference.organization
@locator = Decidim::ResourceLocatorPresenter.new(@conference)
@registration_type = registration_type

subject = I18n.t("pending_validation.subject", scope: "decidim.conferences.mailer.conference_registration_mailer")
mail(to: user.email, subject: subject)
end
end

def confirmation(user, conference, registration_type)
with_user(user) do
@user = user
@conference = conference
@organization = @conference.organization
@locator = Decidim::ResourceLocatorPresenter.new(@conference)
@registration_type = registration_type

add_calendar_attachment

Expand Down
Expand Up @@ -10,6 +10,7 @@ class ConferenceInvite < ApplicationRecord

belongs_to :conference, foreign_key: "decidim_conference_id", class_name: "Decidim::Conference"
belongs_to :user, foreign_key: "decidim_user_id", class_name: "Decidim::User"
belongs_to :registration_type, foreign_key: "decidim_conference_registration_type_id", class_name: "Decidim::Conferences::RegistrationType"

validates :user, uniqueness: { scope: :conference }

Expand Down
Expand Up @@ -19,6 +19,14 @@ def self.user_collection(user)
def self.export_serializer
Decidim::Conferences::DataPortabilityConferenceRegistrationSerializer
end

def confirmed?
confirmed_at.present?
end

def self.log_presenter_class_for(_log)
Decidim::Conferences::AdminLog::ConferenceRegistrationPresenter
end
end
end
end
Expand Up @@ -40,6 +40,7 @@ def permissions
user_can_read_current_conference?
user_can_read_conference_registrations?
user_can_export_conference_registrations?
user_can_confirm_conference_registration?
user_can_create_conference?
user_can_destroy_conference?

Expand Down Expand Up @@ -220,6 +221,13 @@ def user_can_export_conference_registrations?
toggle_allow(user.admin?)
end

def user_can_confirm_conference_registration?
return unless permission_action.action == :confirm &&
permission_action.subject == :conference_registration

toggle_allow(user.admin?)
end

# Everyone can read the conference list
def user_can_read_conference_list?
return unless read_conference_list_permission_action?
Expand Down
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module Decidim
module Conferences
module AdminLog
# This class holds the logic to present a `Decidim::Conferences::ConferenceRegistration`
# for the `AdminLog` log.
#
# Usage should be automatic and you shouldn't need to call this class
# directly, but here's an example:
#
# action_log = Decidim::ActionLog.last
# view_helpers # => this comes from the views
# ConferenceRegistrationPresenter.new(action_log, view_helpers).present
class ConferenceRegistrationPresenter < Decidim::Log::BasePresenter
private

def diff_fields_mapping
{
confirmed_at: :date
}
end

def i18n_labels_scope
"activemodel.attributes.conferences.conference_registration"
end

def action_string
case action
when "confirm"
"decidim.admin_log.conferences.conference_registration.#{action}"
else
super
end
end

def has_diff?
action == "delete" || super
end
end
end
end
end
Expand Up @@ -25,6 +25,14 @@
end %>
</div>
</div>
<div class="grid-x grid-margin-x">
<div class="auto cell">
<%= form.select :registration_type_id,
options_for_select(@form.registration_types_for_select),
{ include_blank: false },
{ multiple: false, class: "chosen-select" } %>
</div>
</div>
</div>

<%= javascript_include_tag "decidim/conferences/admin/conference_invite_form" %>
Expand Up @@ -53,6 +53,7 @@
<th><%= t("models.conference_invite.fields.email", scope: "decidim.conferences") %></th>
<th><%= t("models.conference_invite.fields.sent_at", scope: "decidim.conferences") %></th>
<th><%= t("models.conference_invite.fields.status", scope: "decidim.conferences") %></th>
<th><%= t("models.conference_invite.fields.registration_type", scope: "decidim.conferences") %></th>
</tr>
</thead>
<tbody>
Expand All @@ -73,6 +74,9 @@
<td class="<%= presenter.status_html_class %>">
<%= presenter.status %>
</td>
<td>
<%= translated_attribute(invite.registration_type.title) %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
Expand Up @@ -23,6 +23,8 @@
<th><%= t("models.conference_registration.fields.name", scope: "decidim.conferences") %></th>
<th><%= t("models.conference_registration.fields.email", scope: "decidim.conferences") %></th>
<th><%= t("models.conference_registration.fields.registration_type", scope: "decidim.conferences") %></th>
<th><%= t("models.conference_registration.fields.state", scope: "decidim.conferences") %></th>
<th></th>
</tr>
</thead>
<tbody>
Expand All @@ -37,6 +39,16 @@
<td>
<%= translated_attribute(registration.registration_type.title) %>
</td>
<td>
<%= t("models.conference_registration.fields.states.#{registration.confirmed? ? "confirmed" : "pending"}", scope: "decidim.conferences") %>
</td>
<td>
<% if allowed_to?(:confirm, :conference_registration, conference_registration: registration) %>
<% if !registration.confirmed? %>
<%= icon_link_to "check", confirm_conference_conference_registration_path(current_conference, registration), t("actions.confirm", scope: "decidim.admin"), class: "action-icon--publish", method: :post %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
Expand Up @@ -5,8 +5,8 @@
</p>

<p>
<%= link_to t(".decline", conference_title: translated_attribute(@conference.title)),routes.decline_invitation_conference_conference_registration_path(conference_slug: @conference.slug) %>
<%= link_to t(".decline", conference_title: translated_attribute(@conference.title)),routes.decline_invitation_conference_registration_type_conference_registration_path(conference_slug: @conference.slug, registration_type_id: @registration_type.id) %>
</p>
<p>
<%= link_to t(".join", conference_title: translated_attribute(@conference.title)),routes.conference_conference_registration_url(conference_slug: @conference.slug) %>
<%= link_to t(".join", conference_title: translated_attribute(@conference.title)),routes.conference_registration_type_conference_registration_url(conference_slug: @conference.slug, registration_type_id: @registration_type.id) %>
</p>
@@ -1,3 +1,11 @@
<p><%= t(".confirmed_html", title: translated_attribute(@conference.title), url: @locator.url) %></p>

<p><%= t(".details") %></p>
<p><%= t(".details_1", registration_type: translated_attribute(@registration_type.title), price: number_to_currency(@registration_type.price, locale: I18n.locale, unit: Decidim.currency_unit)) %></p>

<ul>
<% @registration_type.conference_meetings.each do |conference_meeting| %>
<li><%= present(conference_meeting).title %> </li>
<% end %>
</ul>

<p><%= t(".details_2") %></p>

0 comments on commit d196d1e

Please sign in to comment.