Skip to content

Commit

Permalink
Basic implementation of canned responses
Browse files Browse the repository at this point in the history
For a "canned response" dropdown to appear when replying on a ticket, simply create at least one email template of the kind "canned response" beforehand.
  • Loading branch information
Sven Schwyn authored and frenkel committed Oct 4, 2018
1 parent 3fe25b6 commit 877ed79
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added
- Ability to import .eml files for mails sent to wrong email address. Contributed by @fiedl.
- Ability to rename tickets. Contributed by @fiedl.
- Canned responses. Contributed by @svoop.

### Changed
- Better error messages when an invalid input is given for non-signed in users. Contributed by @mickael-kerjean.
Expand Down
14 changes: 14 additions & 0 deletions app/assets/javascripts/tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ jQuery(function() {
}
});

jQuery('#canned-response').on('change', function(){
var editor = jQuery(this).parents('form#new_reply').find('trix-editor')[0].editor;
var url = this.value;
if (url) {
jQuery.ajax({
url: url
}).done(function(response) {
editor.loadHTML(response.message);
});
} else {
editor.loadHTML('');
}
});

if(jQuery('[data-lock-path]').length > 0) {

function keepLock() {
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

class Api::V1::ApplicationController < ActionController::Base
include MultiTenancy

protect_from_forgery with: :null_session

before_action :authenticate_user_from_token!
before_action :authenticate_user_from_token!, unless: -> { current_user }
before_action :load_tenant

check_authorization
Expand Down
23 changes: 23 additions & 0 deletions app/controllers/api/v1/email_templates_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Brimir is a helpdesk system to handle email support requests.
# Copyright (C) 2012-2016 Ivaldi https://ivaldi.nl/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class Api::V1::EmailTemplatesController < Api::V1::ApplicationController

load_and_authorize_resource :email_template

def show
render json: @email_template.to_json
end
end
3 changes: 2 additions & 1 deletion app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class TicketsController < ApplicationController

def show
@users = User.actives

@canned_responses = EmailTemplate.canned_responses.order(:name)

# first time seeing this ticket?
@ticket.mark_read current_user if @ticket.is_unread? current_user

Expand Down
2 changes: 2 additions & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ def limited_agent(user)
# at least one label_id overlap
(reply.ticket.label_ids & user.label_ids).size > 0
end

can :read, EmailTemplate
end

def agent(user)
Expand Down
5 changes: 3 additions & 2 deletions app/models/email_template.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class EmailTemplate < ApplicationRecord

enum kind: [:user_welcome, :ticket_received]
enum kind: [:user_welcome, :ticket_received, :canned_response]

scope :by_kind, -> (k) { where(kind: kinds[k]) }
scope :active, -> { where.not(draft: true) }
scope :canned_responses, -> { where(kind: :canned_response) }

validates_presence_of :kind

Expand All @@ -15,7 +16,7 @@ def self.create_default_templates(kinds, options={})
end

def is_active?
!draft?
kind == 'canned_response' || !draft?
end

def all_others_to_draft(kind)
Expand Down
3 changes: 1 addition & 2 deletions app/views/email_templates/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<td><%= email_template.name %></td>
<td><%= email_template.kind.humanize %></td>
<td>
<% if email_template.is_active? %>
<% if email_template.is_active? %>
<%= fa_icon 'check' %>
<% else %>
<%= link_to t(:set_active), email_template_path(
Expand All @@ -54,4 +54,3 @@
</div>
</div>
</div>

4 changes: 4 additions & 0 deletions app/views/replies/_canned_responses.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div data-canned-responses>
<% options = options_for_select @canned_responses.map { |c| [c.name, api_v1_email_template_path(c)] } %>
<%= select_tag nil, options, id: 'canned-response', include_blank: true %>
</div>
11 changes: 11 additions & 0 deletions app/views/replies/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@
</div>
</div>

<% if @canned_responses.any? %>
<div class="row collapse mbl">
<div class="medium-4 columns">
<label><%= t(:canned_response) %></label>
</div>
<div class="medium-8 columns">
<%= render 'replies/canned_responses' %>
</div>
</div>
<% end %>

<%
if @reply.content.blank? && @reply.errors.count == 0 # not on reply error view

Expand Down
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ de:
attach_note: Notiz anhängen
on_date_author_wrote: 'Am %{date} hat %{author} folgendes geschrieben:'
notification_will_be_sent_to: 'Es wird eine Benachrichtung an folgende Adresse gesendet:'
canned_response: Vorbereitete Antwort
save_as_draft: Als Entwurf speichern
internal_note: Interne Notiz

Expand Down
3 changes: 2 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ en:
attach_note: Attach note
on_date_author_wrote: 'On %{date}, %{author} wrote:'
notification_will_be_sent_to: Your notification will be sent to
canned_response: Canned response
save_as_draft: Save as draft
internal_note: Internal note

Expand Down Expand Up @@ -410,4 +411,4 @@ en:
notify_client_when_ticket_is_created: Notify the client when his newly created ticket is received.
notify_user_when_account_is_created: Notify user when account is created.
stylesheet_url: Add the path to your own custom stylesheet.
ticket_creation_is_open_to_the_world: Tickets creation is open to everybody.
ticket_creation_is_open_to_the_world: Tickets creation is open to everybody.
7 changes: 4 additions & 3 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# author: https://www.facebook.com/aunsinpagina & daniel.seijo_at_urbincasa.es
# revisions: http://colina.net.ve e1th0r
#
#
es:
language_name: Español
# layouts/application
Expand All @@ -23,7 +23,7 @@ es:
change_password: Cambiar contraseña
new_password: Nueva contraseña
new_password_confirmation: Confirmar nueva contraseña

# devise/mailer/reset_password_instructions
hello: Hola
someone_requested_link: Alguien ha solicitado un enlace para cambiar tu contraseña, puedes proceder haciendo click en el siguiente enlace.
Expand Down Expand Up @@ -89,7 +89,7 @@ es:
# notification_mailer/new_reply
new_reply: Nueva respuesta
view_new_reply: Ver nueva respuesta

# attachments/_attachment
download: Descargar

Expand Down Expand Up @@ -197,6 +197,7 @@ es:
new_rule: Nueva regla
no_notifications_sent: 'Ninguna notificación ha sido enviada'
notification_will_be_sent_to: Tus notificaciones serán enviadas a
canned_response: Respuesta preparada
personal_settings: Configuración personal
rule_added: Regla añadida
rule_modified: Regla modificada
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr-CA.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fr-CA:
send_reply: Envoyer réponse
on_date_author_wrote: 'Le %{date}, %{author} a écrit:'
notification_will_be_sent_to: Votre notification sera envoyé a
canned_response: Réponse préparée

# ticket_mailer/notify_assigned.text.erb
ticket_assigned: Un billet de support vous a été attribué
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr-FR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ fr-FR:
send_reply: Envoyer réponse
on_date_author_wrote: 'Le %{date}, %{author} a écrit:'
notification_will_be_sent_to: Votre notification sera envoyé a
canned_response: Réponse préparée
save_as_draft: Enregistrer comme brouillon

# replies/_reply
Expand Down
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 @@

namespace :api do
namespace :v1 do
resources :email_templates, only: [ :show ]
resources :tickets, only: [ :index, :show, :create ]
resources :sessions, only: [ :create ]
resources :users, param: :email, only: [ :create, :show ] do
Expand Down
36 changes: 36 additions & 0 deletions test/controllers/api/v1/email_templates_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Brimir is a helpdesk system to handle email support requests.
# Copyright (C) 2012-2015 Ivaldi https://ivaldi.nl/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

require 'test_helper'

class Api::V1::EmailTemplatesControllerTest < ActionController::TestCase

setup do
@email_template = email_templates(:canned_response)
end

test 'should show email template' do
sign_in users(:alice)

get :show, params: {
auth_token: users(:bob).authentication_token,
id: @email_template.id,
format: :json
}
assert_response :success
end

end
8 changes: 8 additions & 0 deletions test/fixtures/email_templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ inactive_user_welcome:
created_at: <%= Time.now %>
updated_at: <%= Time.now %>
draft: true

canned_response:
name: Canned response
kind: <%= EmailTemplate.kinds[:canned_response] %>
message: 'Been there, done that.'
created_at: <%= Time.now %>
updated_at: <%= Time.now %>
draft: false

0 comments on commit 877ed79

Please sign in to comment.