From 5cceb9773cb3fad2caa21ba78fbafce9e8b0fdd3 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Tue, 28 Feb 2017 22:57:05 +0100 Subject: [PATCH 01/29] Style: simple css for custom emails list --- app/assets/stylesheets/custom-mails.scss | 11 ++++++++ .../admin/mail_templates/index.html.haml | 26 +++++++++---------- app/views/layouts/_main_container.html.haml | 8 +++--- 3 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 app/assets/stylesheets/custom-mails.scss diff --git a/app/assets/stylesheets/custom-mails.scss b/app/assets/stylesheets/custom-mails.scss new file mode 100644 index 000000000..6552f757f --- /dev/null +++ b/app/assets/stylesheets/custom-mails.scss @@ -0,0 +1,11 @@ +#custom-mails { + padding: 20px; + + .wrapper { + background-color: #FFF; + box-shadow: 0 0 1px 0 rgba(0, 0, 0, 0.5); + margin: 15px auto; + max-width: 800px; + padding: 20px; + } +} diff --git a/app/views/admin/mail_templates/index.html.haml b/app/views/admin/mail_templates/index.html.haml index 8f7df9420..776088300 100644 --- a/app/views/admin/mail_templates/index.html.haml +++ b/app/views/admin/mail_templates/index.html.haml @@ -1,15 +1,13 @@ -.row.white-back - %h1 E-mails personnalisables - - .row - .col-md-6 - %table.table +#custom-mails + .wrapper + %h1 E-mails personnalisables + %table.table + %tr + %th{ colspan: 2 } + Type d'email + - @procedure.mail_templates.each do |mt| %tr - %th{ colspan: 2 } - Type d'email - - @procedure.mail_templates.each do |mt| - %tr - %td - = mt.decorate.name - %td.text-right - = link_to "Personnaliser l'e-mail", edit_admin_procedure_mail_template_path(@procedure, mt) + %td + = mt.decorate.name + %td.text-right + = link_to "Personnaliser l'e-mail", edit_admin_procedure_mail_template_path(@procedure, mt) diff --git a/app/views/layouts/_main_container.html.haml b/app/views/layouts/_main_container.html.haml index 9e04a4a32..a1d728b3a 100644 --- a/app/views/layouts/_main_container.html.haml +++ b/app/views/layouts/_main_container.html.haml @@ -1,6 +1,6 @@ -%div#main-container{class: "col-xs-#{main_container_size}"} - %div.row +#main-container{ class: "col-xs-#{main_container_size}" } + .row = render partial: 'layouts/flash_messages' - %div.row + .row = yield - %div.row + .row From 7eb77ba667a8c35457cbf7668f0177672838118b Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Tue, 28 Feb 2017 23:08:54 +0100 Subject: [PATCH 02/29] Mail Received: remove useless template_decorator --- app/decorators/mail_template_decorator.rb | 12 ------------ app/models/mail_received.rb | 5 +++++ app/views/admin/mail_templates/edit.html.haml | 2 +- app/views/admin/mail_templates/index.html.haml | 2 +- spec/decorators/mail_template_decorator_spec.rb | 17 ----------------- 5 files changed, 7 insertions(+), 31 deletions(-) delete mode 100644 app/decorators/mail_template_decorator.rb delete mode 100644 spec/decorators/mail_template_decorator_spec.rb diff --git a/app/decorators/mail_template_decorator.rb b/app/decorators/mail_template_decorator.rb deleted file mode 100644 index b9c153553..000000000 --- a/app/decorators/mail_template_decorator.rb +++ /dev/null @@ -1,12 +0,0 @@ -class MailTemplateDecorator < Draper::Decorator - delegate_all - - def name - case object.type - when "MailReceived" - "E-mail d'accusé de réception" - else - object.type - end - end -end diff --git a/app/models/mail_received.rb b/app/models/mail_received.rb index d2980f188..2199e0543 100644 --- a/app/models/mail_received.rb +++ b/app/models/mail_received.rb @@ -1,6 +1,11 @@ class MailReceived < MailTemplate before_save :default_values + + def name + "E-mail d'accusé de réception" + end + def default_values self.object ||= "[TPS] Accusé de réception pour votre dossier n°--numero_dossier--" self.body ||= "Bonjour, diff --git a/app/views/admin/mail_templates/edit.html.haml b/app/views/admin/mail_templates/edit.html.haml index f166c6879..4b2503f8e 100644 --- a/app/views/admin/mail_templates/edit.html.haml +++ b/app/views/admin/mail_templates/edit.html.haml @@ -1,6 +1,6 @@ .white-back %h3 - = @mail_template.decorate.name + = @mail_template.name = simple_form_for @mail_template.becomes(MailTemplate), url: admin_procedure_mail_template_path(@procedure, @mail_template) do |f| .row diff --git a/app/views/admin/mail_templates/index.html.haml b/app/views/admin/mail_templates/index.html.haml index 776088300..b7dce9af4 100644 --- a/app/views/admin/mail_templates/index.html.haml +++ b/app/views/admin/mail_templates/index.html.haml @@ -8,6 +8,6 @@ - @procedure.mail_templates.each do |mt| %tr %td - = mt.decorate.name + = mt.name %td.text-right = link_to "Personnaliser l'e-mail", edit_admin_procedure_mail_template_path(@procedure, mt) diff --git a/spec/decorators/mail_template_decorator_spec.rb b/spec/decorators/mail_template_decorator_spec.rb deleted file mode 100644 index de8a3f167..000000000 --- a/spec/decorators/mail_template_decorator_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'spec_helper' - -describe MailTemplateDecorator do - let(:mail_template) {create :mail_template} - let(:decorator) { mail_template.decorate } - - context '#name' do - subject { decorator.name } - - context 'when mail_template is a MailReceived' do - let(:mail_template) {create :mail_template, :dossier_received} - it { is_expected.to eq "E-mail d'accusé de réception" } - end - - end - -end From 6be76095c68914ca774341f8877c1719aa1b0a9c Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Sat, 4 Mar 2017 09:28:31 +0100 Subject: [PATCH 03/29] Conf: reactivate spring and guard --- Gemfile | 6 +++--- Gemfile.lock | 15 +++++++++++++++ Guardfile | 42 +++++++++++++++++++++--------------------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/Gemfile b/Gemfile index 080855505..a389a030a 100644 --- a/Gemfile +++ b/Gemfile @@ -110,7 +110,7 @@ group :test do gem 'poltergeist' gem 'timecop' gem 'guard' - # gem 'guard-rspec', require: false + gem 'guard-rspec', require: false gem 'guard-livereload', '~> 2.4', require: false gem 'vcr' gem 'rails-controller-testing' @@ -134,8 +134,8 @@ group :development, :test do gem 'pry-byebug' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring - # gem 'spring' - # gem 'spring-commands-rspec' + gem 'spring' + gem 'spring-commands-rspec' gem 'rspec-rails', '~> 3.0' gem 'railroady' diff --git a/Gemfile.lock b/Gemfile.lock index ffe16811d..aa235f499 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -295,6 +295,10 @@ GEM guard (~> 2.8) guard-compat (~> 1.0) multi_json (~> 1.8) + guard-rspec (4.7.3) + guard (~> 2.1) + guard-compat (~> 1.1) + rspec (>= 2.99.0, < 4.0) haml (4.0.7) tilt haml-rails (0.9.0) @@ -483,6 +487,10 @@ GEM activesupport (>= 3.0, < 6.0) builder (~> 3.0) rubyzip (~> 1.0) + rspec (3.5.0) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) rspec-core (3.5.4) rspec-support (~> 3.5.0) rspec-expectations (3.5.0) @@ -557,6 +565,10 @@ GEM spreadsheet_architect (1.4.8) axlsx (>= 2.0) rodf (= 0.3.7) + spring (2.0.1) + activesupport (>= 4.2) + spring-commands-rspec (1.0.4) + spring (>= 0.9.1) sprockets (3.7.0) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -657,6 +669,7 @@ DEPENDENCIES font-awesome-rails guard guard-livereload (~> 2.4) + guard-rspec haml-rails hashie jbuilder (~> 2.0) @@ -696,6 +709,8 @@ DEPENDENCIES simplecov smart_listing spreadsheet_architect + spring + spring-commands-rspec sqlite3 therubyracer timecop diff --git a/Guardfile b/Guardfile index a937e06b8..22f0a831c 100644 --- a/Guardfile +++ b/Guardfile @@ -64,24 +64,24 @@ guard 'livereload' do watch(%r{config/locales/.+\.yml}) end -# guard :rspec, cmd: 'bin/rspec' do -# watch(%r{^spec/.+_spec\.rb$}) -# watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } -# watch('spec/spec_helper.rb') { "spec" } -# -# # Rails example -# watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } -# watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" } -# watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] } -# watch(%r{^spec/support/(.+)\.rb$}) { "spec" } -# watch('config/routes.rb') { "spec/routing" } -# watch('app/controllers/application_controller.rb') { "spec/controllers" } -# watch('spec/rails_helper.rb') { "spec" } -# -# # Capybara features specs -# watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" } -# -# # Turnip features and steps -# watch(%r{^spec/acceptance/(.+)\.feature$}) -# watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' } -# end +guard :rspec, cmd: 'spring rspec' do + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } + watch('spec/spec_helper.rb') { "spec" } + + # Rails example + watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } + watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" } + watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] } + watch(%r{^spec/support/(.+)\.rb$}) { "spec" } + watch('config/routes.rb') { "spec/routing" } + watch('app/controllers/application_controller.rb') { "spec/controllers" } + watch('spec/rails_helper.rb') { "spec" } + + # Capybara features specs + watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" } + + # Turnip features and steps + watch(%r{^spec/acceptance/(.+)\.feature$}) + watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' } +end From 47fc6e6957cf20f66e6e4251b0855fb89c14d2c8 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Tue, 7 Mar 2017 10:25:28 +0100 Subject: [PATCH 04/29] InitiatedEmail: add initiated email --- app/models/initiated_mail.rb | 15 +++++++++++++++ app/models/procedure.rb | 2 ++ .../notification_mailer/initiated_mail.html.erb | 11 +++++++++++ .../20170302105557_create_initiated_mails.rb | 12 ++++++++++++ db/schema.rb | 12 +++++++++++- 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 app/models/initiated_mail.rb create mode 100644 app/views/notification_mailer/initiated_mail.html.erb create mode 100644 db/migrate/20170302105557_create_initiated_mails.rb diff --git a/app/models/initiated_mail.rb b/app/models/initiated_mail.rb new file mode 100644 index 000000000..2aeaa086c --- /dev/null +++ b/app/models/initiated_mail.rb @@ -0,0 +1,15 @@ +class InitiatedMail < MailTemplate + def name + "E-mail d'accusé de réception" + end + + def self.default + obj = "[TPS] Accusé de réception pour votre dossier n°--numero_dossier--" + body = ActionController::Base.new.render_to_string(template: 'notification_mailer/initiated_mail') + InitiatedMail.new(object: obj, body: body) + end + + def self.slug + self.name.parameterize + end +end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 8c6f55b1e..0ccbbd2c4 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -6,6 +6,8 @@ class Procedure < ActiveRecord::Base has_many :mail_templates has_one :mail_received + has_one :initiated_mail + has_one :procedure_path, dependent: :destroy has_one :module_api_carto, dependent: :destroy diff --git a/app/views/notification_mailer/initiated_mail.html.erb b/app/views/notification_mailer/initiated_mail.html.erb new file mode 100644 index 000000000..5d29ae64b --- /dev/null +++ b/app/views/notification_mailer/initiated_mail.html.erb @@ -0,0 +1,11 @@ +Bonjour, +
+
+Votre administration vous confirme la bonne réception de votre dossier n°--numero_dossier-- complet. Celui-ci sera instruit dans le délai légal déclaré par votre interlocuteur.
+
+En vous souhaitant une bonne journée, +
+
+--- +
+L'équipe TPS diff --git a/db/migrate/20170302105557_create_initiated_mails.rb b/db/migrate/20170302105557_create_initiated_mails.rb new file mode 100644 index 000000000..88eb127a2 --- /dev/null +++ b/db/migrate/20170302105557_create_initiated_mails.rb @@ -0,0 +1,12 @@ +class CreateInitiatedMails < ActiveRecord::Migration[5.0] + def change + create_table :initiated_mails do |t| + t.text :object + t.text :body + t.belongs_to :procedure, index: true, unique: true, foreign_key: true + + t.column :created_at, :timestamp, null: true + t.column :updated_at, :timestamp, null: true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index d303bafba..2b5f81ba3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170228150522) do +ActiveRecord::Schema.define(version: 20170302105557) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -225,6 +225,15 @@ ActiveRecord::Schema.define(version: 20170228150522) do t.index ["dossier_id"], name: "index_individuals_on_dossier_id", using: :btree end + create_table "initiated_mails", force: :cascade do |t| + t.text "object" + t.text "body" + t.integer "procedure_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["procedure_id"], name: "index_initiated_mails_on_procedure_id", using: :btree + end + create_table "invites", force: :cascade do |t| t.string "email" t.string "email_sender" @@ -388,6 +397,7 @@ ActiveRecord::Schema.define(version: 20170228150522) do add_foreign_key "cerfas", "dossiers" add_foreign_key "commentaires", "dossiers" add_foreign_key "dossiers", "users" + add_foreign_key "initiated_mails", "procedures" add_foreign_key "procedure_paths", "administrateurs" add_foreign_key "procedure_paths", "procedures" From 2f5dde381aeeda4bbaa96a3d38dcb52d3e08eb80 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Sat, 4 Mar 2017 09:46:38 +0100 Subject: [PATCH 05/29] NotificationMailer: simplify using default method --- app/mailers/notification_mailer.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index d88191812..4e2d78d5f 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -1,4 +1,7 @@ class NotificationMailer < ApplicationMailer + default from: 'tps@apientreprise.fr', + to: Proc.new { @user.email } + def new_answer dossier send_mail dossier, "Nouveau message pour votre dossier TPS N°#{dossier.id}" end @@ -33,7 +36,6 @@ class NotificationMailer < ApplicationMailer def send_mail dossier, subject vars_mailer dossier - mail(from: "tps@apientreprise.fr", to: @user.email, - subject: subject) + mail(subject: subject) end end From c22c84f1f9e625c993b0cc1b4cffdbd89a705426 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Sat, 4 Mar 2017 09:56:09 +0100 Subject: [PATCH 06/29] Procedure: add inititad_mail with default provider --- app/models/procedure.rb | 5 +++++ spec/models/procedure_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 0ccbbd2c4..158cb8b26 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -37,6 +37,11 @@ class Procedure < ActiveRecord::Base MailReceived.create(procedure: self) unless mail_received end + def initiated_mail_with_override + initiated_mail_without_override || InitiatedMail.default + end + alias_method_chain :initiated_mail, :override + def path procedure_path.path unless procedure_path.nil? end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 331f2373b..240c98a34 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -55,6 +55,34 @@ describe Procedure do end end + describe 'initiated_mail' do + subject { create(:procedure) } + + context 'when initiated_mail is not customize' do + it { expect(subject.initiated_mail.body).to eq(InitiatedMail.default.body) } + end + + context 'when initiated_mail is customize' do + before :each do + subject.initiated_mail = InitiatedMail.new(body: 'sisi') + subject.save + subject.reload + end + it { expect(subject.initiated_mail.body).to eq('sisi') } + end + + context 'when initiated_mail is customize ... again' do + before :each do + subject.initiated_mail = InitiatedMail.new(body: 'toto') + subject.save + subject.reload + end + it { expect(subject.initiated_mail.body).to eq('toto') } + + it { expect(InitiatedMail.count).to eq(1) } + end + end + describe 'validation' do context 'libelle' do it { is_expected.not_to allow_value(nil).for(:libelle) } From 89763ec8d4aa49f4e55a62a81dab41396127beab Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Sun, 5 Mar 2017 11:05:28 +0100 Subject: [PATCH 07/29] MailTemplateController: use new initiated_mail --- .../admin/mail_templates_controller.rb | 21 +++++++--- app/views/admin/mail_templates/edit.html.haml | 4 +- .../admin/mail_templates/index.html.haml | 6 +-- .../admin/mail_templates_controller_spec.rb | 41 ++++++++----------- 4 files changed, 38 insertions(+), 34 deletions(-) diff --git a/app/controllers/admin/mail_templates_controller.rb b/app/controllers/admin/mail_templates_controller.rb index 39f2b7955..0d3b5e71a 100644 --- a/app/controllers/admin/mail_templates_controller.rb +++ b/app/controllers/admin/mail_templates_controller.rb @@ -2,23 +2,34 @@ class Admin::MailTemplatesController < AdminController before_action :retrieve_procedure def index - @mail_templates = @procedure.mail_templates + @mails = mails end def edit - @mail_template = @procedure.mail_templates.find(params[:id]) + @mail_template = find_the_right_mail params[:id] end def update - mail_template = @procedure.mail_templates.find(params[:id]) + mail_template = find_the_right_mail params[:id] mail_template.update_attributes(update_params) - redirect_to admin_procedure_mail_templates_path end private + def mails + [@procedure.initiated_mail] + end + + def find_the_right_mail type + mails.find { |m| m.class.slug == type } + end + def update_params - params.require(:mail_template).permit(:body, :object) + { + procedure_id: params[:procedure_id], + object: params[:mail_template][:object], + body: params[:mail_template][:body], + } end end diff --git a/app/views/admin/mail_templates/edit.html.haml b/app/views/admin/mail_templates/edit.html.haml index 4b2503f8e..010f3a82f 100644 --- a/app/views/admin/mail_templates/edit.html.haml +++ b/app/views/admin/mail_templates/edit.html.haml @@ -2,7 +2,9 @@ %h3 = @mail_template.name - = simple_form_for @mail_template.becomes(MailTemplate), url: admin_procedure_mail_template_path(@procedure, @mail_template) do |f| + = simple_form_for @mail_template.becomes(MailTemplate), + url: admin_procedure_mail_template_path(@procedure, @mail_template.class.slug), + method: :put do |f| .row .col-md-6 = f.input :object, label: "Objet de l'email" diff --git a/app/views/admin/mail_templates/index.html.haml b/app/views/admin/mail_templates/index.html.haml index b7dce9af4..26a55b850 100644 --- a/app/views/admin/mail_templates/index.html.haml +++ b/app/views/admin/mail_templates/index.html.haml @@ -5,9 +5,9 @@ %tr %th{ colspan: 2 } Type d'email - - @procedure.mail_templates.each do |mt| + - @mails.each do |mail| %tr %td - = mt.name + = mail.name %td.text-right - = link_to "Personnaliser l'e-mail", edit_admin_procedure_mail_template_path(@procedure, mt) + = link_to "Personnaliser l'e-mail", edit_admin_procedure_mail_template_path(@procedure, mail.class.slug) diff --git a/spec/controllers/admin/mail_templates_controller_spec.rb b/spec/controllers/admin/mail_templates_controller_spec.rb index e80ba6c5b..dc9b2db35 100644 --- a/spec/controllers/admin/mail_templates_controller_spec.rb +++ b/spec/controllers/admin/mail_templates_controller_spec.rb @@ -1,8 +1,8 @@ require 'spec_helper' describe Admin::MailTemplatesController, type: :controller do - let(:mail_template) { create :mail_template, :dossier_received } - let(:procedure) { create :procedure, mail_templates: [mail_template]} + let(:initiated_mail) { InitiatedMail.default } + let(:procedure) { create :procedure } before do sign_in procedure.administrateur @@ -11,41 +11,32 @@ describe Admin::MailTemplatesController, type: :controller do describe 'GET index' do render_views - subject { get :index, params: {procedure_id: procedure.id} } + subject { get :index, params: { procedure_id: procedure.id } } it { expect(subject.status).to eq 200 } it { expect(subject.body).to include("E-mails personnalisables") } - it { expect(subject.body).to include(*procedure.mail_templates.map{ |mt| mt.decorate.name }) } + it { expect(subject.body).to include(initiated_mail.name ) } end describe 'PATCH update' do let(:object) { 'plop modif' } let(:body) { 'plip modif' } - context 'when is mail_template id' do - subject { patch :update, - params: {procedure_id: mail_template.procedure.id, - id: mail_template.id, - mail_template: { - object: object, - body: body - }} } + before :each do + patch :update, + params: { procedure_id: procedure.id, + id: initiated_mail.class.slug, + mail_template: { object: object, body: body } + } + end - it { expect(subject).to redirect_to admin_procedure_mail_templates_path(mail_template.procedure) } + it { expect(response).to redirect_to admin_procedure_mail_templates_path(procedure) } - it { - expect { - subject - mail_template.reload - }.to change(mail_template, :object).from("[TPS] Accusé de réception pour votre dossier n°--numero_dossier--").to(object) - } + context 'the mail template' do + subject { procedure.reload ; procedure.initiated_mail } - it { - expect { - subject - mail_template.reload - }.to change(mail_template, :body).from("Votre administration vous confirme la bonne réception de votre dossier n°--numero_dossier--").to(body) - } + it { expect(subject.object).to eq(object) } + it { expect(subject.body).to eq(body) } end end end From 65d399b4b0539e9194d9450afe7199f1160a3d3b Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Sun, 5 Mar 2017 20:20:29 +0100 Subject: [PATCH 08/29] NotificationMailer: add send notification --- app/mailers/notification_mailer.rb | 9 +++++++++ spec/mailers/notification_mailer_spec.rb | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 4e2d78d5f..068e8ae27 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -2,6 +2,15 @@ class NotificationMailer < ApplicationMailer default from: 'tps@apientreprise.fr', to: Proc.new { @user.email } + def send_notification dossier, email + vars_mailer(dossier) + + obj = email.object_for_dossier dossier + body = email.body_for_dossier dossier + + mail(subject: obj) { |format| format.html { body } } + end + def new_answer dossier send_mail dossier, "Nouveau message pour votre dossier TPS N°#{dossier.id}" end diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index 10d6b5e24..9abd89322 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -1,6 +1,16 @@ require "spec_helper" RSpec.describe NotificationMailer, type: :mailer do + describe '.send_notification' do + let(:user) { create(:user) } + let(:dossier) { create(:dossier, user: user) } + let(:email) { instance_double('email', object_for_dossier: 'object', body_for_dossier: 'body') } + subject { described_class.send_notification(dossier, email) } + + it { expect(subject.subject).to eq(email.object_for_dossier) } + it { expect(subject.body).to eq(email.body_for_dossier) } + end + describe ".new_answer" do let(:user) { create(:user) } let(:dossier) { create(:dossier, user: user) } From 36500407b481401f87bc9d57ff983ceb864321c6 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Sun, 5 Mar 2017 22:14:21 +0100 Subject: [PATCH 09/29] DossierController: use the new method send_notification --- app/controllers/backoffice/dossiers_controller.rb | 6 ++++-- spec/controllers/backoffice/dossiers_controller_spec.rb | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index 267b5cfee..ee112563d 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -88,10 +88,12 @@ class Backoffice::DossiersController < Backoffice::DossiersListController def receive create_dossier_facade params[:dossier_id] - @facade.dossier.received! + dossier = @facade.dossier + + dossier.received! flash.notice = 'Dossier considéré comme reçu.' - NotificationMailer.dossier_received(@facade.dossier).deliver_now! + NotificationMailer.send_notification(dossier, dossier.procedure.initiated_mail).deliver_now! redirect_to backoffice_dossier_path(id: @facade.dossier.id) end diff --git a/spec/controllers/backoffice/dossiers_controller_spec.rb b/spec/controllers/backoffice/dossiers_controller_spec.rb index de76e9436..50559e02d 100644 --- a/spec/controllers/backoffice/dossiers_controller_spec.rb +++ b/spec/controllers/backoffice/dossiers_controller_spec.rb @@ -236,7 +236,8 @@ describe Backoffice::DossiersController, type: :controller do end it 'Notification email is send' do - expect(NotificationMailer).to receive(:dossier_received).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:send_notification) + .with(dossier, kind_of(InitiatedMail)).and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_now!) subject From 7b336922cc1af1ddf845ceae4a5acbb44b0b55ad Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Sun, 5 Mar 2017 22:15:19 +0100 Subject: [PATCH 10/29] Cleaning --- app/mailers/notification_mailer.rb | 4 - app/models/procedure.rb | 7 -- .../dossier_received.html.erb | 1 - spec/mailers/notification_mailer_spec.rb | 12 --- .../remove_duplicate_email_received_spec.rb | 25 ------ spec/models/mail_template_spec.rb | 80 ++----------------- spec/models/procedure_spec.rb | 36 --------- 7 files changed, 5 insertions(+), 160 deletions(-) delete mode 100644 app/views/notification_mailer/dossier_received.html.erb delete mode 100644 spec/migrations/remove_duplicate_email_received_spec.rb diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 068e8ae27..4341eb06f 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -15,10 +15,6 @@ class NotificationMailer < ApplicationMailer send_mail dossier, "Nouveau message pour votre dossier TPS N°#{dossier.id}" end - def dossier_received dossier - send_mail dossier, dossier.procedure.mail_received.object_for_dossier(dossier) - end - def dossier_submitted dossier send_mail dossier, "Votre dossier TPS N°#{dossier.id} a été déposé" end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 158cb8b26..424598959 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -4,7 +4,6 @@ class Procedure < ActiveRecord::Base has_many :types_de_champ_private, dependent: :destroy has_many :dossiers has_many :mail_templates - has_one :mail_received has_one :initiated_mail @@ -31,12 +30,6 @@ class Procedure < ActiveRecord::Base validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :description, presence: true, allow_blank: false, allow_nil: false - after_create :build_default_mails - - def build_default_mails - MailReceived.create(procedure: self) unless mail_received - end - def initiated_mail_with_override initiated_mail_without_override || InitiatedMail.default end diff --git a/app/views/notification_mailer/dossier_received.html.erb b/app/views/notification_mailer/dossier_received.html.erb deleted file mode 100644 index 6c2c537d2..000000000 --- a/app/views/notification_mailer/dossier_received.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= @dossier.procedure.mail_received.body_for_dossier(@dossier).html_safe %> diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index 9abd89322..3971f3ea4 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -35,16 +35,4 @@ RSpec.describe NotificationMailer, type: :mailer do it { expect(subject.body).to match("ce jour à #{dossier.updated_at}.") } it { expect(subject.subject).to eq("Votre dossier TPS N°#{dossier.id} a été déposé") } end - - describe '.dossier_received' do - let(:user) { create(:user) } - let(:dossier) { create(:dossier, user: user) } - - subject(:subject) { described_class.dossier_received(dossier) } - - before { dossier.reload } - - it { expect(subject.subject).to eq("[TPS] Accusé de réception pour votre dossier n°#{dossier.id}") } - it { expect(subject.body).to match("Votre administration vous confirme la bonne réception de votre dossier n°#{dossier.id}") } - end end diff --git a/spec/migrations/remove_duplicate_email_received_spec.rb b/spec/migrations/remove_duplicate_email_received_spec.rb deleted file mode 100644 index 9fd83c40a..000000000 --- a/spec/migrations/remove_duplicate_email_received_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -load 'spec/spec_helper.rb' -load 'db/migrate/20170215102943_remove_duplicate_email_received.rb' - -describe RemoveDuplicateEmailReceived do - context 'with one procedure and one associated mail_received' do - let!(:procedure) { create(:procedure) } - - it 'keeps the procedure mails' do - RemoveDuplicateEmailReceived.new.change - expect(MailReceived.count).to eq(1) - end - - context 'and another mail_received' do - before :each do - MailReceived.create!(procedure: procedure) - end - - it 'destroys the unecessary maiL_received' do - RemoveDuplicateEmailReceived.new.change - expect(MailReceived.count).to eq(1) - expect(procedure.mail_received).not_to be_nil - end - end - end -end diff --git a/spec/models/mail_template_spec.rb b/spec/models/mail_template_spec.rb index fb27e5d94..b161c77da 100644 --- a/spec/models/mail_template_spec.rb +++ b/spec/models/mail_template_spec.rb @@ -6,85 +6,15 @@ describe MailTemplate do it { is_expected.to belong_to(:procedure) } - describe '.tags' do - subject { MailTemplate::TAGS } - - it { expect(subject.size).to eq 3 } - - describe 'numero_dossier' do - subject { super()[:numero_dossier] } - - describe 'attr and description value' do - - it { expect(subject[:description]).to eq "Permet d'afficher le numéro de dossier de l'utilisateur." } - end - end - - describe 'libelle_procedure' do - subject { super()[:libelle_procedure] } - - describe 'attr and description value' do - - it { expect(subject[:description]).to eq "Permet d'afficher le libellé de la procédure." } - end - end - - describe 'lien_dossier' do - subject { super()[:lien_dossier] } - - describe 'attr and description value' do - - it { expect(subject[:description]).to eq "Permet d'afficher un lien vers le dossier de l'utilisateur." } - end - end - end - describe '.replace_tags' do let(:dossier) { create :dossier } - let(:procedure) { dossier.procedure } - let(:mail_received) { procedure.mail_received } + let(:initiated_mail) { InitiatedMail.default } - describe 'for tag --numero_dossier--' do - before do - procedure.mail_received.update_column(:object, '[TPS] Dossier n°--numero_dossier--') - end + it 'works' do + initiated_mail.object = '[TPS] --numero_dossier-- --libelle_procedure-- --lien_dossier--' + expected = "[TPS] 1 Demande de subvention http://localhost:3000/users/dossiers/1/recapitulatif" - subject { procedure.mail_received.object_for_dossier dossier } - - it { expect(subject).to eq "[TPS] Dossier n°#{dossier.id}" } - end - - describe 'for tag --libelle_procedure--' do - before do - procedure.mail_received.update_column(:object, '[TPS] Dossier pour la procédure --libelle_procedure--') - end - - subject { procedure.mail_received.object_for_dossier dossier } - - it { expect(subject).to eq "[TPS] Dossier pour la procédure #{procedure.libelle}" } - end - - describe 'for tag --lien_dossier--' do - include Rails.application.routes.url_helpers - include ActionView::Helpers::UrlHelper - - before do - procedure.mail_received.update_column(:body, 'Consultez votre dossier ici --lien_dossier--') - end - - subject { procedure.mail_received.body_for_dossier dossier } - - it { is_expected.to eq "Consultez votre dossier ici #{link_to users_dossier_recapitulatif_url(dossier), users_dossier_recapitulatif_url(dossier), target: '_blank'}" } - end - - describe 'multiple tags' do - before do - procedure.mail_received.update_column(:object, '[TPS] Dossier n°--numero_dossier-- pour la procédure --libelle_procedure-- et encore le numéro : --numero_dossier--') - end - - subject { procedure.mail_received.object_for_dossier dossier } - - it { expect(subject).to eq "[TPS] Dossier n°#{dossier.id} pour la procédure #{procedure.libelle} et encore le numéro : #{dossier.id}" } + expect(initiated_mail.object_for_dossier(dossier)).to eq(expected) end end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 240c98a34..5e3db166a 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -21,38 +21,6 @@ describe Procedure do it { is_expected.to have_db_column(:logo_secure_token) } it { is_expected.to have_db_column(:cerfa_flag) } it { is_expected.to have_db_column(:published) } - - describe 'mail_received' do - let(:procedure) { create :procedure } - - before do - create :mail_received, procedure: procedure - end - - it { expect(procedure.mail_received).not_to be_nil } - end - - end - - describe '#build_default_mails' do - subject { build :procedure, mail_templates: [] } - - it 'call the fonction build_default_mails' do - expect(subject).to receive(:build_default_mails) - subject.save - end - - describe 'accessible values' do - - before do - subject.save - subject.reload - end - - it { expect(subject.mail_templates.size).to eq 1 } - - it { expect(subject.mail_received).not_to be_nil } - end end describe 'initiated_mail' do @@ -196,10 +164,6 @@ describe Procedure do let!(:piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) } let!(:piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) } - before do - procedure.mail_received.object = "Je vais être cloné" - end - subject { procedure.clone } it 'should duplicate specific objects with different id' do From 695dc16b85945350627d2e1fe2dca112eaec8820 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 6 Mar 2017 11:51:34 +0100 Subject: [PATCH 11/29] Mails: add refused, without_continuation, draft, closed, received mails object --- app/models/closed_mail.rb | 13 +++++ app/models/concerns/mail_template_concern.rb | 54 +++++++++++++++++++ app/models/initiated_mail.rb | 12 ++--- app/models/received_mail.rb | 13 +++++ app/models/refused_mail.rb | 13 +++++ app/models/without_continuation_mail.rb | 13 +++++ .../notification_mailer/closed_mail.html.haml | 17 ++++++ .../dossier_closed.text.erb | 14 ----- .../dossier_refused.text.erb | 14 ----- .../dossier_submitted.text.erb | 12 ----- .../dossier_without_continuation.text.erb | 14 ----- .../initiated_mail.html.erb | 11 ---- .../initiated_mail.html.haml | 17 ++++++ .../received_mail.html.haml | 14 +++++ .../refused_mail.html.haml | 17 ++++++ .../without_continuation_mail.html.haml | 17 ++++++ .../20170306102116_create_received_mails.rb | 11 ++++ .../20170306102238_create_closed_mails.rb | 11 ++++ .../20170306102256_create_refused_mails.rb | 11 ++++ ...02320_create_without_continuation_mails.rb | 11 ++++ db/schema.rb | 42 ++++++++++++++- .../concern/mail_template_concern_spec.rb | 17 ++++++ 22 files changed, 295 insertions(+), 73 deletions(-) create mode 100644 app/models/closed_mail.rb create mode 100644 app/models/concerns/mail_template_concern.rb create mode 100644 app/models/received_mail.rb create mode 100644 app/models/refused_mail.rb create mode 100644 app/models/without_continuation_mail.rb create mode 100644 app/views/notification_mailer/closed_mail.html.haml delete mode 100644 app/views/notification_mailer/dossier_closed.text.erb delete mode 100644 app/views/notification_mailer/dossier_refused.text.erb delete mode 100644 app/views/notification_mailer/dossier_submitted.text.erb delete mode 100644 app/views/notification_mailer/dossier_without_continuation.text.erb delete mode 100644 app/views/notification_mailer/initiated_mail.html.erb create mode 100644 app/views/notification_mailer/initiated_mail.html.haml create mode 100644 app/views/notification_mailer/received_mail.html.haml create mode 100644 app/views/notification_mailer/refused_mail.html.haml create mode 100644 app/views/notification_mailer/without_continuation_mail.html.haml create mode 100644 db/migrate/20170306102116_create_received_mails.rb create mode 100644 db/migrate/20170306102238_create_closed_mails.rb create mode 100644 db/migrate/20170306102256_create_refused_mails.rb create mode 100644 db/migrate/20170306102320_create_without_continuation_mails.rb create mode 100644 spec/models/concern/mail_template_concern_spec.rb diff --git a/app/models/closed_mail.rb b/app/models/closed_mail.rb new file mode 100644 index 000000000..92d8aed80 --- /dev/null +++ b/app/models/closed_mail.rb @@ -0,0 +1,13 @@ +class ClosedMail < ActiveRecord::Base + include MailTemplateConcern + + def name + "Accusé d'acceptation" + end + + def self.default + obj = "Votre dossier TPS N°--numero_dossier-- a été accepté" + body = ActionController::Base.new.render_to_string(template: 'notification_mailer/closed_mail') + ClosedMail.new(object: obj, body: body) + end +end diff --git a/app/models/concerns/mail_template_concern.rb b/app/models/concerns/mail_template_concern.rb new file mode 100644 index 000000000..f63d68990 --- /dev/null +++ b/app/models/concerns/mail_template_concern.rb @@ -0,0 +1,54 @@ +module MailTemplateConcern + extend ActiveSupport::Concern + + include Rails.application.routes.url_helpers + include ActionView::Helpers::UrlHelper + + TAGS = { + numero_dossier: { + description: "Permet d'afficher le numéro de dossier de l'utilisateur." + }, + lien_dossier: { + description: "Permet d'afficher un lien vers le dossier de l'utilisateur." + }, + libelle_procedure: { + description: "Permet d'afficher le libellé de la procédure." + } + } + + def object_for_dossier dossier + replace_tags(object, dossier) + end + + def body_for_dossier dossier + replace_tags(body, dossier) + end + + def replace_tags string, dossier + TAGS.inject(string) do |acc, tag| + acc.gsub!("--#{tag.first}--", replace_tag(tag.first.to_sym, dossier)) || acc + end + end + + module ClassMethods + def slug + self.name.underscore + end + end + + private + + def replace_tag tag, dossier + case tag + when :numero_dossier + dossier.id.to_s + when :lien_dossier + # TPS::Application::URL # quickfix + link_to users_dossier_recapitulatif_url(dossier), users_dossier_recapitulatif_url(dossier), target: '_blank' + when :libelle_procedure + dossier.procedure.libelle + else + '--BALISE_NON_RECONNUE--' + end + end +end diff --git a/app/models/initiated_mail.rb b/app/models/initiated_mail.rb index 2aeaa086c..7a220867a 100644 --- a/app/models/initiated_mail.rb +++ b/app/models/initiated_mail.rb @@ -1,15 +1,13 @@ -class InitiatedMail < MailTemplate +class InitiatedMail < ActiveRecord::Base + include MailTemplateConcern + def name - "E-mail d'accusé de réception" + "Accusé de réception" end def self.default - obj = "[TPS] Accusé de réception pour votre dossier n°--numero_dossier--" + obj = "Votre dossier TPS N°--numero_dossier-- a été bien reçu" body = ActionController::Base.new.render_to_string(template: 'notification_mailer/initiated_mail') InitiatedMail.new(object: obj, body: body) end - - def self.slug - self.name.parameterize - end end diff --git a/app/models/received_mail.rb b/app/models/received_mail.rb new file mode 100644 index 000000000..5af010789 --- /dev/null +++ b/app/models/received_mail.rb @@ -0,0 +1,13 @@ +class ReceivedMail < ActiveRecord::Base + include MailTemplateConcern + + def name + "Accusé de passage en instruction" + end + + def self.default + obj = "Votre dossier TPS N°--numero_dossier-- va être instruit" + body = ActionController::Base.new.render_to_string(template: 'notification_mailer/received_mail') + ReceivedMail.new(object: obj, body: body) + end +end diff --git a/app/models/refused_mail.rb b/app/models/refused_mail.rb new file mode 100644 index 000000000..30b347b77 --- /dev/null +++ b/app/models/refused_mail.rb @@ -0,0 +1,13 @@ +class RefusedMail < ApplicationRecord + include MailTemplateConcern + + def name + "Accusé de rejet du dossier" + end + + def self.default + obj = "Votre dossier TPS N°--numero_dossier-- a été refusé" + body = ActionController::Base.new.render_to_string(template: 'notification_mailer/refused_mail') + RefusedMail.new(object: obj, body: body) + end +end diff --git a/app/models/without_continuation_mail.rb b/app/models/without_continuation_mail.rb new file mode 100644 index 000000000..9bde76f64 --- /dev/null +++ b/app/models/without_continuation_mail.rb @@ -0,0 +1,13 @@ +class WithoutContinuationMail < ApplicationRecord + include MailTemplateConcern + + def name + "Accusé de classement sans suite" + end + + def self.default + obj = "Votre dossier TPS N°--numero_dossier-- a été classé sans suite" + body = ActionController::Base.new.render_to_string(template: 'notification_mailer/without_continuation_mail') + WithoutContinuationMail.new(object: obj, body: body) + end +end diff --git a/app/views/notification_mailer/closed_mail.html.haml b/app/views/notification_mailer/closed_mail.html.haml new file mode 100644 index 000000000..22321d848 --- /dev/null +++ b/app/views/notification_mailer/closed_mail.html.haml @@ -0,0 +1,17 @@ +Bonjour +
+
+Votre dossier N°--numero_dossier-- a été accepté. +
+
+A tout moment, vous pouvez consulter le contenu de vos dossiers et les éventuels commentaires de l'administration à cette adresse : --lien_dossier-- +
+
+Bonne journée +
+
+\--- +
+Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. +
+\--- diff --git a/app/views/notification_mailer/dossier_closed.text.erb b/app/views/notification_mailer/dossier_closed.text.erb deleted file mode 100644 index 81d692a64..000000000 --- a/app/views/notification_mailer/dossier_closed.text.erb +++ /dev/null @@ -1,14 +0,0 @@ -Bonjour <%= @user.email %> - -votre dossier N°<%=@dossier.id%> déposé auprès de <%= @dossier.procedure.organisation %> a été accepté ce jour à <%= @dossier.updated_at %>. - -A tout moment, vous pouvez consulter le contenu de vos dossiers et les éventuels commentaires de l'administration à cette adresse : <%=users_dossier_recapitulatif_url(dossier_id: @dossier.id)%> - -Bonne journée - ---- -Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. ---- - ---- -L'équide TPS - tps@apientreprise.fr \ No newline at end of file diff --git a/app/views/notification_mailer/dossier_refused.text.erb b/app/views/notification_mailer/dossier_refused.text.erb deleted file mode 100644 index 2024297be..000000000 --- a/app/views/notification_mailer/dossier_refused.text.erb +++ /dev/null @@ -1,14 +0,0 @@ -Bonjour <%= @user.email %> - -votre dossier N°<%=@dossier.id%> déposé auprès de <%= @dossier.procedure.organisation %> a été refusé ce jour à <%= @dossier.updated_at %>. - -Pour en savoir plus sur le motif du refus, vous pouvez consulter le contenu de vos dossiers et les éventuels commentaires de l'administration à cette adresse : <%=users_dossier_recapitulatif_url(dossier_id: @dossier.id)%> - -Bonne journée, - ---- -Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. ---- - ---- -L'équide TPS - tps@apientreprise.fr \ No newline at end of file diff --git a/app/views/notification_mailer/dossier_submitted.text.erb b/app/views/notification_mailer/dossier_submitted.text.erb deleted file mode 100644 index 2357b8bbf..000000000 --- a/app/views/notification_mailer/dossier_submitted.text.erb +++ /dev/null @@ -1,12 +0,0 @@ -Bonjour <%= @user.email %> - -Nous vous confirmons que votre dossier N°<%=@dossier.id%> a été déposé auprès de <%= @dossier.procedure.organisation %> avec succès ce jour à <%= @dossier.updated_at %>. - -Bonne journée, - ---- -Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. ---- - ---- -L'équide TPS - tps@apientreprise.fr \ No newline at end of file diff --git a/app/views/notification_mailer/dossier_without_continuation.text.erb b/app/views/notification_mailer/dossier_without_continuation.text.erb deleted file mode 100644 index 73fe7d8e2..000000000 --- a/app/views/notification_mailer/dossier_without_continuation.text.erb +++ /dev/null @@ -1,14 +0,0 @@ -Bonjour <%= @user.email %> - -votre dossier N°<%=@dossier.id%> déposé auprès de <%= @dossier.procedure.organisation %> a été classé sans suite ce jour à <%= @dossier.updated_at %>. - -Pour en savoir plus sur les raisons de ce classement sans suite, vous pouvez consulter le contenu de vos dossiers et les éventuels commentaires de l'administration à cette adresse : <%=users_dossier_recapitulatif_url(dossier_id: @dossier.id)%> - -Bonne journée, - ---- -Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. ---- - ---- -L'équide TPS - tps@apientreprise.fr \ No newline at end of file diff --git a/app/views/notification_mailer/initiated_mail.html.erb b/app/views/notification_mailer/initiated_mail.html.erb deleted file mode 100644 index 5d29ae64b..000000000 --- a/app/views/notification_mailer/initiated_mail.html.erb +++ /dev/null @@ -1,11 +0,0 @@ -Bonjour, -
-
-Votre administration vous confirme la bonne réception de votre dossier n°--numero_dossier-- complet. Celui-ci sera instruit dans le délai légal déclaré par votre interlocuteur.
-
-En vous souhaitant une bonne journée, -
-
---- -
-L'équipe TPS diff --git a/app/views/notification_mailer/initiated_mail.html.haml b/app/views/notification_mailer/initiated_mail.html.haml new file mode 100644 index 000000000..5b008a02c --- /dev/null +++ b/app/views/notification_mailer/initiated_mail.html.haml @@ -0,0 +1,17 @@ +Bonjour +
+
+Votre administration vous confirme la bonne réception de votre dossier n°--numero_dossier--. +
+
+A tout moment, vous pouvez consulter le contenu de vos dossiers et les éventuels commentaires de l'administration à cette adresse : --lien_dossier-- +
+
+Bonne journée +
+
+\--- +
+Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. +
+\--- diff --git a/app/views/notification_mailer/received_mail.html.haml b/app/views/notification_mailer/received_mail.html.haml new file mode 100644 index 000000000..ea25ec013 --- /dev/null +++ b/app/views/notification_mailer/received_mail.html.haml @@ -0,0 +1,14 @@ +Bonjour +
+
+Votre administration vous confirme la bonne réception de votre dossier n°--numero_dossier--. Celui-ci sera instruit dans le délai légal déclaré par votre interlocuteur. +
+
+Bonne journée +
+
+\--- +
+Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. +
+\--- diff --git a/app/views/notification_mailer/refused_mail.html.haml b/app/views/notification_mailer/refused_mail.html.haml new file mode 100644 index 000000000..5ea109569 --- /dev/null +++ b/app/views/notification_mailer/refused_mail.html.haml @@ -0,0 +1,17 @@ +Bonjour +
+
+Votre dossier N°--numero_dossier-- a été refusé. +
+
+Pour en savoir plus sur le motif du refus, vous pouvez consulter le contenu de vos dossiers et les éventuels commentaires de l'administration à cette adresse : --lien_dossier-- +
+
+Bonne journée +
+
+\--- +
+Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. +
+\--- diff --git a/app/views/notification_mailer/without_continuation_mail.html.haml b/app/views/notification_mailer/without_continuation_mail.html.haml new file mode 100644 index 000000000..9acff2424 --- /dev/null +++ b/app/views/notification_mailer/without_continuation_mail.html.haml @@ -0,0 +1,17 @@ +Bonjour +
+
+Votre dossier N°--numero_dossier-- a été classé sans suite. +
+
+Pour en savoir plus sur les raisons de ce classement sans suite, vous pouvez consulter le contenu de vos dossiers et les éventuels commentaires de l'administration à cette adresse : --lien_dossier-- +
+
+Bonne journée +
+
+\--- +
+Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. +
+\--- diff --git a/db/migrate/20170306102116_create_received_mails.rb b/db/migrate/20170306102116_create_received_mails.rb new file mode 100644 index 000000000..dbe07a298 --- /dev/null +++ b/db/migrate/20170306102116_create_received_mails.rb @@ -0,0 +1,11 @@ +class CreateReceivedMails < ActiveRecord::Migration[5.0] + def change + create_table :received_mails do |t| + t.text :body + t.text :object + t.references :procedure, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20170306102238_create_closed_mails.rb b/db/migrate/20170306102238_create_closed_mails.rb new file mode 100644 index 000000000..11f6c07ed --- /dev/null +++ b/db/migrate/20170306102238_create_closed_mails.rb @@ -0,0 +1,11 @@ +class CreateClosedMails < ActiveRecord::Migration[5.0] + def change + create_table :closed_mails do |t| + t.text :body + t.text :object + t.belongs_to :procedure, index: true, unique: true, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20170306102256_create_refused_mails.rb b/db/migrate/20170306102256_create_refused_mails.rb new file mode 100644 index 000000000..527866530 --- /dev/null +++ b/db/migrate/20170306102256_create_refused_mails.rb @@ -0,0 +1,11 @@ +class CreateRefusedMails < ActiveRecord::Migration[5.0] + def change + create_table :refused_mails do |t| + t.text :body + t.text :object + t.belongs_to :procedure, index: true, unique: true, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20170306102320_create_without_continuation_mails.rb b/db/migrate/20170306102320_create_without_continuation_mails.rb new file mode 100644 index 000000000..da65dc8de --- /dev/null +++ b/db/migrate/20170306102320_create_without_continuation_mails.rb @@ -0,0 +1,11 @@ +class CreateWithoutContinuationMails < ActiveRecord::Migration[5.0] + def change + create_table :without_continuation_mails do |t| + t.text :body + t.text :object + t.belongs_to :procedure, index: true, unique: true, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 2b5f81ba3..1aff0250d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170302105557) do +ActiveRecord::Schema.define(version: 20170306102320) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -104,6 +104,15 @@ ActiveRecord::Schema.define(version: 20170302105557) do t.index ["type_de_champ_id"], name: "index_champs_on_type_de_champ_id", using: :btree end + create_table "closed_mails", force: :cascade do |t| + t.text "body" + t.text "object" + t.integer "procedure_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["procedure_id"], name: "index_closed_mails_on_procedure_id", using: :btree + end + create_table "commentaires", force: :cascade do |t| t.string "email" t.datetime "created_at", null: false @@ -343,6 +352,24 @@ ActiveRecord::Schema.define(version: 20170302105557) do t.integer "dossier_id" end + create_table "received_mails", force: :cascade do |t| + t.text "body" + t.text "object" + t.integer "procedure_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["procedure_id"], name: "index_received_mails_on_procedure_id", using: :btree + end + + create_table "refused_mails", force: :cascade do |t| + t.text "body" + t.text "object" + t.integer "procedure_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["procedure_id"], name: "index_refused_mails_on_procedure_id", using: :btree + end + create_table "rna_informations", force: :cascade do |t| t.string "association_id" t.string "titre" @@ -394,12 +421,25 @@ ActiveRecord::Schema.define(version: 20170302105557) do t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree end + create_table "without_continuation_mails", force: :cascade do |t| + t.text "body" + t.text "object" + t.integer "procedure_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["procedure_id"], name: "index_without_continuation_mails_on_procedure_id", using: :btree + end + add_foreign_key "cerfas", "dossiers" + add_foreign_key "closed_mails", "procedures" add_foreign_key "commentaires", "dossiers" add_foreign_key "dossiers", "users" add_foreign_key "initiated_mails", "procedures" add_foreign_key "procedure_paths", "administrateurs" add_foreign_key "procedure_paths", "procedures" + add_foreign_key "received_mails", "procedures" + add_foreign_key "refused_mails", "procedures" + add_foreign_key "without_continuation_mails", "procedures" create_view :searches, sql_definition: <<-SQL SELECT dossiers.id AS dossier_id, diff --git a/spec/models/concern/mail_template_concern_spec.rb b/spec/models/concern/mail_template_concern_spec.rb new file mode 100644 index 000000000..028a28f40 --- /dev/null +++ b/spec/models/concern/mail_template_concern_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe MailTemplateConcern do + describe '.replace_tags' do + let(:dossier) { create :dossier } + let(:initiated_mail) { InitiatedMail.default } + + it 'works' do + initiated_mail.object = '[TPS] --numero_dossier-- --libelle_procedure-- --lien_dossier--' + expected = + "[TPS] 1 Demande de subvention " + + "http://localhost:3000/users/dossiers/1/recapitulatif" + + expect(initiated_mail.object_for_dossier(dossier)).to eq(expected) + end + end +end From 206d56f106cfd8f1d56c3836d06f6ff5673d8700 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 6 Mar 2017 15:00:05 +0100 Subject: [PATCH 12/29] Procedure: add the new mails --- app/models/procedure.rb | 10 ++++++---- spec/models/procedure_spec.rb | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 424598959..ddd96612b 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -5,7 +5,6 @@ class Procedure < ActiveRecord::Base has_many :dossiers has_many :mail_templates - has_one :initiated_mail has_one :procedure_path, dependent: :destroy @@ -30,10 +29,13 @@ class Procedure < ActiveRecord::Base validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :description, presence: true, allow_blank: false, allow_nil: false - def initiated_mail_with_override - initiated_mail_without_override || InitiatedMail.default + %w(InitiatedMail ReceivedMail ClosedMail RefusedMail WithoutContinuationMail).each do |name| + has_one "#{name.underscore}".to_sym + define_method("#{name.underscore}_with_override") do + self.send("#{name.underscore}_without_override") || Object.const_get(name).default + end + alias_method_chain "#{name.underscore.to_sym}".to_s, :override end - alias_method_chain :initiated_mail, :override def path procedure_path.path unless procedure_path.nil? diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 5e3db166a..4ea81e427 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -23,6 +23,14 @@ describe Procedure do it { is_expected.to have_db_column(:published) } end + describe 'mails' do + it { expect(subject.initiated_mail).to be_a(InitiatedMail) } + it { expect(subject.received_mail).to be_a(ReceivedMail) } + it { expect(subject.closed_mail).to be_a(ClosedMail) } + it { expect(subject.refused_mail).to be_a(RefusedMail) } + it { expect(subject.without_continuation_mail).to be_a(WithoutContinuationMail) } + end + describe 'initiated_mail' do subject { create(:procedure) } From 70b20206567fc4f90cf97721b61d29f01dc9c7a0 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 6 Mar 2017 15:19:34 +0100 Subject: [PATCH 13/29] Procedure: remove mail_template --- app/models/procedure.rb | 13 ++++++++++--- spec/factories/procedure.rb | 1 - spec/models/procedure_spec.rb | 6 ------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index ddd96612b..e27101cdf 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -3,8 +3,6 @@ class Procedure < ActiveRecord::Base has_many :types_de_champ, class_name: 'TypeDeChampPublic', dependent: :destroy has_many :types_de_champ_private, dependent: :destroy has_many :dossiers - has_many :mail_templates - has_one :procedure_path, dependent: :destroy @@ -29,6 +27,15 @@ class Procedure < ActiveRecord::Base validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :description, presence: true, allow_blank: false, allow_nil: false + # for all those mails do + # has_one initiated_mail + # + # add a method to return default mail if none is saved + # define method :initiated_mail_with_override + # self initiated_mail_without_override || InitiatedMail.default + # end + # alias_method_chain :initiated_mail, :override + %w(InitiatedMail ReceivedMail ClosedMail RefusedMail WithoutContinuationMail).each do |name| has_one "#{name.underscore}".to_sym define_method("#{name.underscore}_with_override") do @@ -91,7 +98,7 @@ class Procedure < ActiveRecord::Base end def clone - procedure = self.deep_clone(include: [:types_de_piece_justificative, :types_de_champ, :types_de_champ_private, :module_api_carto, :mail_templates, types_de_champ: [:drop_down_list]]) + procedure = self.deep_clone(include: [:types_de_piece_justificative, :types_de_champ, :types_de_champ_private, :module_api_carto, types_de_champ: [:drop_down_list]]) procedure.archived = false procedure.published = false procedure.logo_secure_token = nil diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index d3b7cc066..e97e10ee1 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -9,7 +9,6 @@ FactoryGirl.define do published false cerfa_flag false administrateur { create(:administrateur) } - mail_templates { [create(:mail_template, :dossier_received)]} after(:build) do |procedure, _evaluator| if procedure.module_api_carto.nil? diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 4ea81e427..3656f5358 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -5,7 +5,6 @@ describe Procedure do it { is_expected.to have_many(:types_de_piece_justificative) } it { is_expected.to have_many(:types_de_champ) } it { is_expected.to have_many(:dossiers) } - it { is_expected.to have_many(:mail_templates) } it { is_expected.to have_one(:module_api_carto) } it { is_expected.to belong_to(:administrateur) } it { is_expected.to have_many(:preference_list_dossiers) } @@ -182,7 +181,6 @@ describe Procedure do expect(subject.types_de_piece_justificative.size).to eq procedure.types_de_piece_justificative.size expect(subject.types_de_champ.size).to eq procedure.types_de_champ.size expect(subject.types_de_champ_private.size).to eq procedure.types_de_champ_private.size - expect(subject.mail_templates.size).to eq procedure.mail_templates.size subject.types_de_champ.zip(procedure.types_de_champ).each do |stc, ptc| expect(stc).to have_same_attributes_as(ptc) @@ -195,10 +193,6 @@ describe Procedure do subject.types_de_piece_justificative.zip(procedure.types_de_piece_justificative).each do |stc, ptc| expect(stc).to have_same_attributes_as(ptc) end - - subject.mail_templates.zip(procedure.mail_templates).each do |stc, ptc| - expect(stc).to have_same_attributes_as(ptc) - end end it 'should not duplicate specific related objects' do From 6c560e65ebd27983f5447d46714f6f4343d1ae8e Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 6 Mar 2017 15:43:38 +0100 Subject: [PATCH 14/29] Notification Mailer: use send_notification for all the mails --- .../backoffice/dossiers_controller.rb | 26 ++++++++++++------- app/mailers/notification_mailer.rb | 16 ------------ .../backoffice/dossiers_controller_spec.rb | 9 ++++--- spec/mailers/notification_mailer_spec.rb | 14 ---------- 4 files changed, 22 insertions(+), 43 deletions(-) diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index ee112563d..24ef6633e 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -95,40 +95,46 @@ class Backoffice::DossiersController < Backoffice::DossiersListController NotificationMailer.send_notification(dossier, dossier.procedure.initiated_mail).deliver_now! - redirect_to backoffice_dossier_path(id: @facade.dossier.id) + redirect_to backoffice_dossier_path(id: dossier.id) end def refuse create_dossier_facade params[:dossier_id] - @facade.dossier.next_step! 'gestionnaire', 'refuse' + dossier = @facade.dossier + + dossier.next_step! 'gestionnaire', 'refuse' flash.notice = 'Dossier considéré comme refusé.' - NotificationMailer.dossier_refused(@facade.dossier).deliver_now! + NotificationMailer.send_notification(dossier, dossier.procedure.refused_mail).deliver_now! - redirect_to backoffice_dossier_path(id: @facade.dossier.id) + redirect_to backoffice_dossier_path(id: dossier.id) end def without_continuation create_dossier_facade params[:dossier_id] - @facade.dossier.next_step! 'gestionnaire', 'without_continuation' + dossier = @facade.dossier + + dossier.next_step! 'gestionnaire', 'without_continuation' flash.notice = 'Dossier considéré comme sans suite.' - NotificationMailer.dossier_without_continuation(@facade.dossier).deliver_now! + NotificationMailer.send_notification(dossier, dossier.procedure.without_continuation_mail).deliver_now! - redirect_to backoffice_dossier_path(id: @facade.dossier.id) + redirect_to backoffice_dossier_path(id: dossier.id) end def close create_dossier_facade params[:dossier_id] - @facade.dossier.next_step! 'gestionnaire', 'close' + dossier = @facade.dossier + + dossier.next_step! 'gestionnaire', 'close' flash.notice = 'Dossier traité avec succès.' - NotificationMailer.dossier_closed(@facade.dossier).deliver_now! + NotificationMailer.send_notification(dossier, dossier.procedure.closed_mail).deliver_now! - redirect_to backoffice_dossier_path(id: @facade.dossier.id) + redirect_to backoffice_dossier_path(id: dossier.id) end def follow diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 4341eb06f..cdd587493 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -15,22 +15,6 @@ class NotificationMailer < ApplicationMailer send_mail dossier, "Nouveau message pour votre dossier TPS N°#{dossier.id}" end - def dossier_submitted dossier - send_mail dossier, "Votre dossier TPS N°#{dossier.id} a été déposé" - end - - def dossier_without_continuation dossier - send_mail dossier, "Votre dossier TPS N°#{dossier.id} a été classé sans suite" - end - - def dossier_refused dossier - send_mail dossier, "Votre dossier TPS N°#{dossier.id} a été refusé" - end - - def dossier_closed dossier - send_mail dossier, "Votre dossier TPS N°#{dossier.id} a été accepté" - end - private def vars_mailer dossier diff --git a/spec/controllers/backoffice/dossiers_controller_spec.rb b/spec/controllers/backoffice/dossiers_controller_spec.rb index 50559e02d..dbb3317c3 100644 --- a/spec/controllers/backoffice/dossiers_controller_spec.rb +++ b/spec/controllers/backoffice/dossiers_controller_spec.rb @@ -262,7 +262,8 @@ describe Backoffice::DossiersController, type: :controller do end it 'Notification email is sent' do - expect(NotificationMailer).to receive(:dossier_refused).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:send_notification) + .with(dossier, kind_of(RefusedMail)).and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_now!) subject @@ -287,7 +288,8 @@ describe Backoffice::DossiersController, type: :controller do end it 'Notification email is sent' do - expect(NotificationMailer).to receive(:dossier_without_continuation).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:send_notification) + .with(dossier, kind_of(WithoutContinuationMail)).and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_now!) subject @@ -311,7 +313,8 @@ describe Backoffice::DossiersController, type: :controller do end it 'Notification email is sent' do - expect(NotificationMailer).to receive(:dossier_closed).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:send_notification) + .with(dossier, kind_of(ClosedMail)).and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_now!) subject diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index 3971f3ea4..0f9aeba80 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -21,18 +21,4 @@ RSpec.describe NotificationMailer, type: :mailer do it { expect(subject.body).to include("Pour le consulter, merci de vous rendre sur #{users_dossier_recapitulatif_url(dossier_id: dossier.id)}") } it { expect(subject.subject).to eq("Nouveau message pour votre dossier TPS N°#{dossier.id}") } end - - describe ".dossier_submitted" do - let(:user) { create(:user) } - let(:dossier) { create(:dossier, user: user) } - - subject(:subject) { described_class.dossier_submitted(dossier) } - - before { dossier.reload } - - it { expect(subject.body).to match("Nous vous confirmons que votre dossier N°#{dossier.id} a été déposé") } - it { expect(subject.body).to match("auprès de #{dossier.procedure.organisation} avec succès") } - it { expect(subject.body).to match("ce jour à #{dossier.updated_at}.") } - it { expect(subject.subject).to eq("Votre dossier TPS N°#{dossier.id} a été déposé") } - end end From c8f515832bce1e9a19eb74010e5d32facf829400 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 6 Mar 2017 15:44:19 +0100 Subject: [PATCH 15/29] MailTemplate: add email and nom_organisation tag --- app/models/mail_template.rb | 10 ++++++++++ spec/models/mail_template_spec.rb | 7 +++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/models/mail_template.rb b/app/models/mail_template.rb index eac1020f9..ca7f752ae 100644 --- a/app/models/mail_template.rb +++ b/app/models/mail_template.rb @@ -13,6 +13,12 @@ class MailTemplate < ActiveRecord::Base }, libelle_procedure: { description: "Permet d'afficher le libellé de la procédure." + }, + email: { + description: "Permet d'afficher l'email du porteur de projet." + }, + nom_organisation: { + description: "Permet d'afficher le nom de l'organisation." } } @@ -34,6 +40,10 @@ class MailTemplate < ActiveRecord::Base def replace_tag tag, dossier case tag + when :email + dossier.user.email.to_s + when :nom_organisation + dossier.procedure.organisation.to_s when :numero_dossier dossier.id.to_s when :lien_dossier diff --git a/spec/models/mail_template_spec.rb b/spec/models/mail_template_spec.rb index b161c77da..6b05056bf 100644 --- a/spec/models/mail_template_spec.rb +++ b/spec/models/mail_template_spec.rb @@ -11,8 +11,11 @@ describe MailTemplate do let(:initiated_mail) { InitiatedMail.default } it 'works' do - initiated_mail.object = '[TPS] --numero_dossier-- --libelle_procedure-- --lien_dossier--' - expected = "[TPS] 1 Demande de subvention http://localhost:3000/users/dossiers/1/recapitulatif" + initiated_mail.object = '[TPS] --numero_dossier-- --libelle_procedure-- --lien_dossier-- --email-- --nom_organisation--' + expected = + "[TPS] 1 Demande de subvention " + + "http://localhost:3000/users/dossiers/1/recapitulatif " + + "#{dossier.user.email} Orga SGMAP" expect(initiated_mail.object_for_dossier(dossier)).to eq(expected) end From 9d710336b3d8040629ef68a3f6831d3b8a3e2749 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 6 Mar 2017 15:44:38 +0100 Subject: [PATCH 16/29] MailTemplate: allow edition of all the new mails --- app/controllers/admin/mail_templates_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/mail_templates_controller.rb b/app/controllers/admin/mail_templates_controller.rb index 0d3b5e71a..7c94d4a0f 100644 --- a/app/controllers/admin/mail_templates_controller.rb +++ b/app/controllers/admin/mail_templates_controller.rb @@ -18,7 +18,8 @@ class Admin::MailTemplatesController < AdminController private def mails - [@procedure.initiated_mail] + %w(initiated received closed refused without_continuation) + .map { |name| @procedure.send(name + "_mail") } end def find_the_right_mail type From 3d7f04ad3d4a5c1112725aaeb55f49cfab1bb66f Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 6 Mar 2017 16:24:29 +0100 Subject: [PATCH 17/29] DossierController: send receivedMail when the mail is ... received --- app/controllers/backoffice/dossiers_controller.rb | 2 +- spec/controllers/backoffice/dossiers_controller_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index 24ef6633e..ee1f69ac1 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -93,7 +93,7 @@ class Backoffice::DossiersController < Backoffice::DossiersListController dossier.received! flash.notice = 'Dossier considéré comme reçu.' - NotificationMailer.send_notification(dossier, dossier.procedure.initiated_mail).deliver_now! + NotificationMailer.send_notification(dossier, dossier.procedure.received_mail).deliver_now! redirect_to backoffice_dossier_path(id: dossier.id) end diff --git a/spec/controllers/backoffice/dossiers_controller_spec.rb b/spec/controllers/backoffice/dossiers_controller_spec.rb index dbb3317c3..c4392a3b0 100644 --- a/spec/controllers/backoffice/dossiers_controller_spec.rb +++ b/spec/controllers/backoffice/dossiers_controller_spec.rb @@ -237,7 +237,7 @@ describe Backoffice::DossiersController, type: :controller do it 'Notification email is send' do expect(NotificationMailer).to receive(:send_notification) - .with(dossier, kind_of(InitiatedMail)).and_return(NotificationMailer) + .with(dossier, kind_of(ReceivedMail)).and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_now!) subject From 2536774519e7f32d2f498a835ae6351fb13d67eb Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 6 Mar 2017 16:24:56 +0100 Subject: [PATCH 18/29] DescriptionController: send initiatedMail notification --- app/controllers/users/description_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index 4dcd7f287..770e2d8c8 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -71,6 +71,7 @@ class Users::DescriptionController < UsersController if mandatory if @dossier.draft? @dossier.initiated! + NotificationMailer.send_notification(@dossier, @dossier.procedure.initiated_mail).deliver_now! end flash.notice = 'Félicitations, votre demande a bien été enregistrée.' From 335caed65ec44e3690c3ca6f520054e478985be6 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 6 Mar 2017 16:52:07 +0100 Subject: [PATCH 19/29] Mail Template: Delete --- app/models/mail_template.rb | 58 ------------------- app/views/admin/mail_templates/edit.html.haml | 5 +- ...3170808_delete_all_mail_validated_in_db.rb | 3 +- spec/models/mail_template_spec.rb | 23 -------- 4 files changed, 5 insertions(+), 84 deletions(-) delete mode 100644 app/models/mail_template.rb delete mode 100644 spec/models/mail_template_spec.rb diff --git a/app/models/mail_template.rb b/app/models/mail_template.rb deleted file mode 100644 index ca7f752ae..000000000 --- a/app/models/mail_template.rb +++ /dev/null @@ -1,58 +0,0 @@ -class MailTemplate < ActiveRecord::Base - include Rails.application.routes.url_helpers - include ActionView::Helpers::UrlHelper - - belongs_to :procedure - - TAGS = { - numero_dossier: { - description: "Permet d'afficher le numéro de dossier de l'utilisateur." - }, - lien_dossier: { - description: "Permet d'afficher un lien vers le dossier de l'utilisateur." - }, - libelle_procedure: { - description: "Permet d'afficher le libellé de la procédure." - }, - email: { - description: "Permet d'afficher l'email du porteur de projet." - }, - nom_organisation: { - description: "Permet d'afficher le nom de l'organisation." - } - } - - def object_for_dossier dossier - replace_tags(object, dossier) - end - - def body_for_dossier dossier - replace_tags(body, dossier) - end - - def replace_tags string, dossier - TAGS.inject(string) do |acc, tag| - acc.gsub!("--#{tag.first}--", replace_tag(tag.first.to_sym, dossier)) || acc - end - end - - private - - def replace_tag tag, dossier - case tag - when :email - dossier.user.email.to_s - when :nom_organisation - dossier.procedure.organisation.to_s - when :numero_dossier - dossier.id.to_s - when :lien_dossier - # TPS::Application::URL # quickfix - link_to users_dossier_recapitulatif_url(dossier), users_dossier_recapitulatif_url(dossier), target: '_blank' - when :libelle_procedure - dossier.procedure.libelle - else - '--BALISE_NON_RECONNUE--' - end - end -end diff --git a/app/views/admin/mail_templates/edit.html.haml b/app/views/admin/mail_templates/edit.html.haml index 010f3a82f..4e1aa67ba 100644 --- a/app/views/admin/mail_templates/edit.html.haml +++ b/app/views/admin/mail_templates/edit.html.haml @@ -2,7 +2,8 @@ %h3 = @mail_template.name - = simple_form_for @mail_template.becomes(MailTemplate), + = simple_form_for @mail_template, + as: 'mail_template', url: admin_procedure_mail_template_path(@procedure, @mail_template.class.slug), method: :put do |f| .row @@ -21,7 +22,7 @@ Balise %th Description - - MailTemplate::TAGS.each do |balise| + - MailTemplateConcern::TAGS.each do |balise| %tr %td.center = "--#{balise.first}--" diff --git a/db/migrate/20170223170808_delete_all_mail_validated_in_db.rb b/db/migrate/20170223170808_delete_all_mail_validated_in_db.rb index 74d89af2a..dbd8860d1 100644 --- a/db/migrate/20170223170808_delete_all_mail_validated_in_db.rb +++ b/db/migrate/20170223170808_delete_all_mail_validated_in_db.rb @@ -1,5 +1,6 @@ class DeleteAllMailValidatedInDb < ActiveRecord::Migration[5.0] def change - MailTemplate.where(type: "MailValidated").delete_all + mail_template_exist = Object.const_get(:MailTemplate).is_a?(Class) rescue false + MailTemplate.where(type: "MailValidated").delete_all if mail_template_exist end end diff --git a/spec/models/mail_template_spec.rb b/spec/models/mail_template_spec.rb deleted file mode 100644 index 6b05056bf..000000000 --- a/spec/models/mail_template_spec.rb +++ /dev/null @@ -1,23 +0,0 @@ -require 'spec_helper' - -describe MailTemplate do - it { is_expected.to have_db_column(:body) } - it { is_expected.to have_db_column(:type) } - - it { is_expected.to belong_to(:procedure) } - - describe '.replace_tags' do - let(:dossier) { create :dossier } - let(:initiated_mail) { InitiatedMail.default } - - it 'works' do - initiated_mail.object = '[TPS] --numero_dossier-- --libelle_procedure-- --lien_dossier-- --email-- --nom_organisation--' - expected = - "[TPS] 1 Demande de subvention " + - "http://localhost:3000/users/dossiers/1/recapitulatif " + - "#{dossier.user.email} Orga SGMAP" - - expect(initiated_mail.object_for_dossier(dossier)).to eq(expected) - end - end -end From 02bbf0543fb36eff402d0dc97a2bf51b666574e0 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 6 Mar 2017 22:37:37 +0100 Subject: [PATCH 20/29] Mails: move to their own namespace --- app/models/closed_mail.rb | 13 ------------- app/models/initiated_mail.rb | 13 ------------- app/models/mails/closed_mail.rb | 15 +++++++++++++++ app/models/mails/initiated_mail.rb | 15 +++++++++++++++ app/models/mails/received_mail.rb | 15 +++++++++++++++ app/models/mails/refused_mail.rb | 15 +++++++++++++++ app/models/mails/without_continuation_mail.rb | 15 +++++++++++++++ app/models/procedure.rb | 10 +++++----- app/models/received_mail.rb | 13 ------------- app/models/refused_mail.rb | 13 ------------- app/models/without_continuation_mail.rb | 13 ------------- .../admin/mail_templates_controller_spec.rb | 2 +- .../backoffice/dossiers_controller_spec.rb | 8 ++++---- .../concern/mail_template_concern_spec.rb | 2 +- spec/models/procedure_spec.rb | 18 +++++++++--------- 15 files changed, 95 insertions(+), 85 deletions(-) delete mode 100644 app/models/closed_mail.rb delete mode 100644 app/models/initiated_mail.rb create mode 100644 app/models/mails/closed_mail.rb create mode 100644 app/models/mails/initiated_mail.rb create mode 100644 app/models/mails/received_mail.rb create mode 100644 app/models/mails/refused_mail.rb create mode 100644 app/models/mails/without_continuation_mail.rb delete mode 100644 app/models/received_mail.rb delete mode 100644 app/models/refused_mail.rb delete mode 100644 app/models/without_continuation_mail.rb diff --git a/app/models/closed_mail.rb b/app/models/closed_mail.rb deleted file mode 100644 index 92d8aed80..000000000 --- a/app/models/closed_mail.rb +++ /dev/null @@ -1,13 +0,0 @@ -class ClosedMail < ActiveRecord::Base - include MailTemplateConcern - - def name - "Accusé d'acceptation" - end - - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- a été accepté" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/closed_mail') - ClosedMail.new(object: obj, body: body) - end -end diff --git a/app/models/initiated_mail.rb b/app/models/initiated_mail.rb deleted file mode 100644 index 7a220867a..000000000 --- a/app/models/initiated_mail.rb +++ /dev/null @@ -1,13 +0,0 @@ -class InitiatedMail < ActiveRecord::Base - include MailTemplateConcern - - def name - "Accusé de réception" - end - - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- a été bien reçu" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/initiated_mail') - InitiatedMail.new(object: obj, body: body) - end -end diff --git a/app/models/mails/closed_mail.rb b/app/models/mails/closed_mail.rb new file mode 100644 index 000000000..0e6052a75 --- /dev/null +++ b/app/models/mails/closed_mail.rb @@ -0,0 +1,15 @@ +module Mails + class ClosedMail < ActiveRecord::Base + include MailTemplateConcern + + def name + "Accusé d'acceptation" + end + + def self.default + obj = "Votre dossier TPS N°--numero_dossier-- a été accepté" + body = ActionController::Base.new.render_to_string(template: 'notification_mailer/closed_mail') + ClosedMail.new(object: obj, body: body) + end + end +end diff --git a/app/models/mails/initiated_mail.rb b/app/models/mails/initiated_mail.rb new file mode 100644 index 000000000..03b34c59b --- /dev/null +++ b/app/models/mails/initiated_mail.rb @@ -0,0 +1,15 @@ +module Mails + class InitiatedMail < ActiveRecord::Base + include MailTemplateConcern + + def name + "Accusé de réception" + end + + def self.default + obj = "Votre dossier TPS N°--numero_dossier-- a été bien reçu" + body = ActionController::Base.new.render_to_string(template: 'notification_mailer/initiated_mail') + InitiatedMail.new(object: obj, body: body) + end + end +end diff --git a/app/models/mails/received_mail.rb b/app/models/mails/received_mail.rb new file mode 100644 index 000000000..04d4b98ef --- /dev/null +++ b/app/models/mails/received_mail.rb @@ -0,0 +1,15 @@ +module Mails + class ReceivedMail < ActiveRecord::Base + include MailTemplateConcern + + def name + "Accusé de passage en instruction" + end + + def self.default + obj = "Votre dossier TPS N°--numero_dossier-- va être instruit" + body = ActionController::Base.new.render_to_string(template: 'notification_mailer/received_mail') + ReceivedMail.new(object: obj, body: body) + end + end +end diff --git a/app/models/mails/refused_mail.rb b/app/models/mails/refused_mail.rb new file mode 100644 index 000000000..45c876c02 --- /dev/null +++ b/app/models/mails/refused_mail.rb @@ -0,0 +1,15 @@ +module Mails + class RefusedMail < ApplicationRecord + include MailTemplateConcern + + def name + "Accusé de rejet du dossier" + end + + def self.default + obj = "Votre dossier TPS N°--numero_dossier-- a été refusé" + body = ActionController::Base.new.render_to_string(template: 'notification_mailer/refused_mail') + RefusedMail.new(object: obj, body: body) + end + end +end diff --git a/app/models/mails/without_continuation_mail.rb b/app/models/mails/without_continuation_mail.rb new file mode 100644 index 000000000..d5c61a323 --- /dev/null +++ b/app/models/mails/without_continuation_mail.rb @@ -0,0 +1,15 @@ +module Mails + class WithoutContinuationMail < ApplicationRecord + include MailTemplateConcern + + def name + "Accusé de classement sans suite" + end + + def self.default + obj = "Votre dossier TPS N°--numero_dossier-- a été classé sans suite" + body = ActionController::Base.new.render_to_string(template: 'notification_mailer/without_continuation_mail') + WithoutContinuationMail.new(object: obj, body: body) + end + end +end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index e27101cdf..267fa8e17 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -28,18 +28,18 @@ class Procedure < ActiveRecord::Base validates :description, presence: true, allow_blank: false, allow_nil: false # for all those mails do - # has_one initiated_mail + # has_one :initiated_mail, class_name: 'Mails::InitiatedMail' # # add a method to return default mail if none is saved - # define method :initiated_mail_with_override - # self initiated_mail_without_override || InitiatedMail.default + # def initiated_mail_with_override + # self.initiated_mail_without_override || InitiatedMail.default # end # alias_method_chain :initiated_mail, :override %w(InitiatedMail ReceivedMail ClosedMail RefusedMail WithoutContinuationMail).each do |name| - has_one "#{name.underscore}".to_sym + has_one "#{name.underscore}".to_sym, class_name: "Mails::#{name}" define_method("#{name.underscore}_with_override") do - self.send("#{name.underscore}_without_override") || Object.const_get(name).default + self.send("#{name.underscore}_without_override") || Object.const_get("Mails::#{name}").default end alias_method_chain "#{name.underscore.to_sym}".to_s, :override end diff --git a/app/models/received_mail.rb b/app/models/received_mail.rb deleted file mode 100644 index 5af010789..000000000 --- a/app/models/received_mail.rb +++ /dev/null @@ -1,13 +0,0 @@ -class ReceivedMail < ActiveRecord::Base - include MailTemplateConcern - - def name - "Accusé de passage en instruction" - end - - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- va être instruit" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/received_mail') - ReceivedMail.new(object: obj, body: body) - end -end diff --git a/app/models/refused_mail.rb b/app/models/refused_mail.rb deleted file mode 100644 index 30b347b77..000000000 --- a/app/models/refused_mail.rb +++ /dev/null @@ -1,13 +0,0 @@ -class RefusedMail < ApplicationRecord - include MailTemplateConcern - - def name - "Accusé de rejet du dossier" - end - - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- a été refusé" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/refused_mail') - RefusedMail.new(object: obj, body: body) - end -end diff --git a/app/models/without_continuation_mail.rb b/app/models/without_continuation_mail.rb deleted file mode 100644 index 9bde76f64..000000000 --- a/app/models/without_continuation_mail.rb +++ /dev/null @@ -1,13 +0,0 @@ -class WithoutContinuationMail < ApplicationRecord - include MailTemplateConcern - - def name - "Accusé de classement sans suite" - end - - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- a été classé sans suite" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/without_continuation_mail') - WithoutContinuationMail.new(object: obj, body: body) - end -end diff --git a/spec/controllers/admin/mail_templates_controller_spec.rb b/spec/controllers/admin/mail_templates_controller_spec.rb index dc9b2db35..1bcae94b8 100644 --- a/spec/controllers/admin/mail_templates_controller_spec.rb +++ b/spec/controllers/admin/mail_templates_controller_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Admin::MailTemplatesController, type: :controller do - let(:initiated_mail) { InitiatedMail.default } + let(:initiated_mail) { Mails::InitiatedMail.default } let(:procedure) { create :procedure } before do diff --git a/spec/controllers/backoffice/dossiers_controller_spec.rb b/spec/controllers/backoffice/dossiers_controller_spec.rb index c4392a3b0..1dae5ea60 100644 --- a/spec/controllers/backoffice/dossiers_controller_spec.rb +++ b/spec/controllers/backoffice/dossiers_controller_spec.rb @@ -237,7 +237,7 @@ describe Backoffice::DossiersController, type: :controller do it 'Notification email is send' do expect(NotificationMailer).to receive(:send_notification) - .with(dossier, kind_of(ReceivedMail)).and_return(NotificationMailer) + .with(dossier, kind_of(Mails::ReceivedMail)).and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_now!) subject @@ -263,7 +263,7 @@ describe Backoffice::DossiersController, type: :controller do it 'Notification email is sent' do expect(NotificationMailer).to receive(:send_notification) - .with(dossier, kind_of(RefusedMail)).and_return(NotificationMailer) + .with(dossier, kind_of(Mails::RefusedMail)).and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_now!) subject @@ -289,7 +289,7 @@ describe Backoffice::DossiersController, type: :controller do it 'Notification email is sent' do expect(NotificationMailer).to receive(:send_notification) - .with(dossier, kind_of(WithoutContinuationMail)).and_return(NotificationMailer) + .with(dossier, kind_of(Mails::WithoutContinuationMail)).and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_now!) subject @@ -314,7 +314,7 @@ describe Backoffice::DossiersController, type: :controller do it 'Notification email is sent' do expect(NotificationMailer).to receive(:send_notification) - .with(dossier, kind_of(ClosedMail)).and_return(NotificationMailer) + .with(dossier, kind_of(Mails::ClosedMail)).and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_now!) subject diff --git a/spec/models/concern/mail_template_concern_spec.rb b/spec/models/concern/mail_template_concern_spec.rb index 028a28f40..f177a360a 100644 --- a/spec/models/concern/mail_template_concern_spec.rb +++ b/spec/models/concern/mail_template_concern_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe MailTemplateConcern do describe '.replace_tags' do let(:dossier) { create :dossier } - let(:initiated_mail) { InitiatedMail.default } + let(:initiated_mail) { Mails::InitiatedMail.default } it 'works' do initiated_mail.object = '[TPS] --numero_dossier-- --libelle_procedure-- --lien_dossier--' diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 3656f5358..c054f15d7 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -23,23 +23,23 @@ describe Procedure do end describe 'mails' do - it { expect(subject.initiated_mail).to be_a(InitiatedMail) } - it { expect(subject.received_mail).to be_a(ReceivedMail) } - it { expect(subject.closed_mail).to be_a(ClosedMail) } - it { expect(subject.refused_mail).to be_a(RefusedMail) } - it { expect(subject.without_continuation_mail).to be_a(WithoutContinuationMail) } + it { expect(subject.initiated_mail).to be_a(Mails::InitiatedMail) } + it { expect(subject.received_mail).to be_a(Mails::ReceivedMail) } + it { expect(subject.closed_mail).to be_a(Mails::ClosedMail) } + it { expect(subject.refused_mail).to be_a(Mails::RefusedMail) } + it { expect(subject.without_continuation_mail).to be_a(Mails::WithoutContinuationMail) } end describe 'initiated_mail' do subject { create(:procedure) } context 'when initiated_mail is not customize' do - it { expect(subject.initiated_mail.body).to eq(InitiatedMail.default.body) } + it { expect(subject.initiated_mail.body).to eq(Mails::InitiatedMail.default.body) } end context 'when initiated_mail is customize' do before :each do - subject.initiated_mail = InitiatedMail.new(body: 'sisi') + subject.initiated_mail = Mails::InitiatedMail.new(body: 'sisi') subject.save subject.reload end @@ -48,13 +48,13 @@ describe Procedure do context 'when initiated_mail is customize ... again' do before :each do - subject.initiated_mail = InitiatedMail.new(body: 'toto') + subject.initiated_mail = Mails::InitiatedMail.new(body: 'toto') subject.save subject.reload end it { expect(subject.initiated_mail.body).to eq('toto') } - it { expect(InitiatedMail.count).to eq(1) } + it { expect(Mails::InitiatedMail.count).to eq(1) } end end From 65e83dd6ec9a342c3fef40ba0a6c0ac3e576a7a6 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 6 Mar 2017 23:18:16 +0100 Subject: [PATCH 21/29] Mails: factorize default and change slug --- app/models/concerns/mail_template_concern.rb | 7 ++++++- app/models/mails/closed_mail.rb | 10 ++-------- app/models/mails/initiated_mail.rb | 10 ++-------- app/models/mails/received_mail.rb | 10 ++-------- app/models/mails/refused_mail.rb | 10 ++-------- app/models/mails/without_continuation_mail.rb | 10 ++-------- app/views/admin/mail_templates/edit.html.haml | 2 +- app/views/admin/mail_templates/index.html.haml | 2 +- .../closed_mail.html.haml | 0 .../initiated_mail.html.haml | 0 .../received_mail.html.haml | 0 .../refused_mail.html.haml | 0 .../without_continuation_mail.html.haml | 0 .../admin/mail_templates_controller_spec.rb | 2 +- 14 files changed, 19 insertions(+), 44 deletions(-) rename app/views/{notification_mailer => mails}/closed_mail.html.haml (100%) rename app/views/{notification_mailer => mails}/initiated_mail.html.haml (100%) rename app/views/{notification_mailer => mails}/received_mail.html.haml (100%) rename app/views/{notification_mailer => mails}/refused_mail.html.haml (100%) rename app/views/{notification_mailer => mails}/without_continuation_mail.html.haml (100%) diff --git a/app/models/concerns/mail_template_concern.rb b/app/models/concerns/mail_template_concern.rb index f63d68990..f0e9863e9 100644 --- a/app/models/concerns/mail_template_concern.rb +++ b/app/models/concerns/mail_template_concern.rb @@ -32,7 +32,12 @@ module MailTemplateConcern module ClassMethods def slug - self.name.underscore + self.name.underscore.parameterize + end + + def default + body = ActionController::Base.new.render_to_string(template: self.name.underscore) + self.new(object: self.const_get(:DEFAULT_OBJECT), body: body) end end diff --git a/app/models/mails/closed_mail.rb b/app/models/mails/closed_mail.rb index 0e6052a75..3db0d84dc 100644 --- a/app/models/mails/closed_mail.rb +++ b/app/models/mails/closed_mail.rb @@ -2,14 +2,8 @@ module Mails class ClosedMail < ActiveRecord::Base include MailTemplateConcern - def name - "Accusé d'acceptation" - end + DISPLAYED_NAME = "Accusé d'acceptation" + DEFAULT_OBJECT = 'Votre dossier TPS N°--numero_dossier-- a été accepté' - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- a été accepté" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/closed_mail') - ClosedMail.new(object: obj, body: body) - end end end diff --git a/app/models/mails/initiated_mail.rb b/app/models/mails/initiated_mail.rb index 03b34c59b..e39aa0e7a 100644 --- a/app/models/mails/initiated_mail.rb +++ b/app/models/mails/initiated_mail.rb @@ -2,14 +2,8 @@ module Mails class InitiatedMail < ActiveRecord::Base include MailTemplateConcern - def name - "Accusé de réception" - end + DISPLAYED_NAME = 'Accusé de réception' + DEFAULT_OBJECT = 'Votre dossier TPS N°--numero_dossier-- a été bien reçu' - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- a été bien reçu" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/initiated_mail') - InitiatedMail.new(object: obj, body: body) - end end end diff --git a/app/models/mails/received_mail.rb b/app/models/mails/received_mail.rb index 04d4b98ef..41344f6ca 100644 --- a/app/models/mails/received_mail.rb +++ b/app/models/mails/received_mail.rb @@ -2,14 +2,8 @@ module Mails class ReceivedMail < ActiveRecord::Base include MailTemplateConcern - def name - "Accusé de passage en instruction" - end + DISPLAYED_NAME = 'Accusé de passage en instruction' + DEFAULT_OBJECT = 'Votre dossier TPS N°--numero_dossier-- va être instruit' - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- va être instruit" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/received_mail') - ReceivedMail.new(object: obj, body: body) - end end end diff --git a/app/models/mails/refused_mail.rb b/app/models/mails/refused_mail.rb index 45c876c02..a1a1af8b4 100644 --- a/app/models/mails/refused_mail.rb +++ b/app/models/mails/refused_mail.rb @@ -2,14 +2,8 @@ module Mails class RefusedMail < ApplicationRecord include MailTemplateConcern - def name - "Accusé de rejet du dossier" - end + DISPLAYED_NAME = 'Accusé de rejet du dossier' + DEFAULT_OBJECT = 'Votre dossier TPS N°--numero_dossier-- a été refusé' - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- a été refusé" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/refused_mail') - RefusedMail.new(object: obj, body: body) - end end end diff --git a/app/models/mails/without_continuation_mail.rb b/app/models/mails/without_continuation_mail.rb index d5c61a323..da9724cf6 100644 --- a/app/models/mails/without_continuation_mail.rb +++ b/app/models/mails/without_continuation_mail.rb @@ -2,14 +2,8 @@ module Mails class WithoutContinuationMail < ApplicationRecord include MailTemplateConcern - def name - "Accusé de classement sans suite" - end + DISPLAYED_NAME = 'Accusé de classement sans suite' + DEFAULT_OBJECT = 'Votre dossier TPS N°--numero_dossier-- a été classé sans suite' - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- a été classé sans suite" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/without_continuation_mail') - WithoutContinuationMail.new(object: obj, body: body) - end end end diff --git a/app/views/admin/mail_templates/edit.html.haml b/app/views/admin/mail_templates/edit.html.haml index 4e1aa67ba..35efe2506 100644 --- a/app/views/admin/mail_templates/edit.html.haml +++ b/app/views/admin/mail_templates/edit.html.haml @@ -1,6 +1,6 @@ .white-back %h3 - = @mail_template.name + = @mail_template.class.const_get(:DISPLAYED_NAME) = simple_form_for @mail_template, as: 'mail_template', diff --git a/app/views/admin/mail_templates/index.html.haml b/app/views/admin/mail_templates/index.html.haml index 26a55b850..b8eff250a 100644 --- a/app/views/admin/mail_templates/index.html.haml +++ b/app/views/admin/mail_templates/index.html.haml @@ -8,6 +8,6 @@ - @mails.each do |mail| %tr %td - = mail.name + = mail.class.const_get(:DISPLAYED_NAME) %td.text-right = link_to "Personnaliser l'e-mail", edit_admin_procedure_mail_template_path(@procedure, mail.class.slug) diff --git a/app/views/notification_mailer/closed_mail.html.haml b/app/views/mails/closed_mail.html.haml similarity index 100% rename from app/views/notification_mailer/closed_mail.html.haml rename to app/views/mails/closed_mail.html.haml diff --git a/app/views/notification_mailer/initiated_mail.html.haml b/app/views/mails/initiated_mail.html.haml similarity index 100% rename from app/views/notification_mailer/initiated_mail.html.haml rename to app/views/mails/initiated_mail.html.haml diff --git a/app/views/notification_mailer/received_mail.html.haml b/app/views/mails/received_mail.html.haml similarity index 100% rename from app/views/notification_mailer/received_mail.html.haml rename to app/views/mails/received_mail.html.haml diff --git a/app/views/notification_mailer/refused_mail.html.haml b/app/views/mails/refused_mail.html.haml similarity index 100% rename from app/views/notification_mailer/refused_mail.html.haml rename to app/views/mails/refused_mail.html.haml diff --git a/app/views/notification_mailer/without_continuation_mail.html.haml b/app/views/mails/without_continuation_mail.html.haml similarity index 100% rename from app/views/notification_mailer/without_continuation_mail.html.haml rename to app/views/mails/without_continuation_mail.html.haml diff --git a/spec/controllers/admin/mail_templates_controller_spec.rb b/spec/controllers/admin/mail_templates_controller_spec.rb index 1bcae94b8..31adfd4ee 100644 --- a/spec/controllers/admin/mail_templates_controller_spec.rb +++ b/spec/controllers/admin/mail_templates_controller_spec.rb @@ -15,7 +15,7 @@ describe Admin::MailTemplatesController, type: :controller do it { expect(subject.status).to eq 200 } it { expect(subject.body).to include("E-mails personnalisables") } - it { expect(subject.body).to include(initiated_mail.name ) } + it { expect(subject.body).to include(Mails::InitiatedMail::DISPLAYED_NAME) } end describe 'PATCH update' do From a88b150c3c12f3ed6d5c415d43cd0059f4975639 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 7 Mar 2017 10:15:33 +0100 Subject: [PATCH 22/29] Add remember me on login --- app/controllers/users/sessions_controller.rb | 12 ++++---- app/views/users/sessions/new.html.haml | 29 +++++++------------- config/initializers/devise.rb | 2 +- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb index 6fef62105..fee9dfad2 100644 --- a/app/controllers/users/sessions_controller.rb +++ b/app/controllers/users/sessions_controller.rb @@ -10,7 +10,7 @@ class Users::SessionsController < Sessions::SessionsController # GET /resource/sign_in def new - unless user_return_to_procedure_id.nil? + unless user_return_to_procedure_id.nil? # WTF ? @dossier = Dossier.new(procedure: Procedure.active(user_return_to_procedure_id)) end @@ -21,9 +21,10 @@ class Users::SessionsController < Sessions::SessionsController #POST /resource/sign_in def create - try_to_authenticate(User) - try_to_authenticate(Gestionnaire) - try_to_authenticate(Administrateur) + remember_me = params[:user][:remember_me] == '1' + try_to_authenticate(User, remember_me) + try_to_authenticate(Gestionnaire, remember_me) + try_to_authenticate(Administrateur, remember_me) if user_signed_in? current_user.update_attributes(loged_in_with_france_connect: '') @@ -83,9 +84,10 @@ class Users::SessionsController < Sessions::SessionsController NumberService.to_number session["user_return_to"].split("?procedure_id=").second end - def try_to_authenticate(klass) + def try_to_authenticate(klass, remember_me = false) if resource = klass.find_for_database_authentication(email: params[:user][:email]) if resource.valid_password?(params[:user][:password]) + resource.remember_me = remember_me sign_in resource resource.force_sync_credentials set_flash_message :notice, :signed_in diff --git a/app/views/users/sessions/new.html.haml b/app/views/users/sessions/new.html.haml index f7ece8315..7b242bab4 100644 --- a/app/views/users/sessions/new.html.haml +++ b/app/views/users/sessions/new.html.haml @@ -13,25 +13,16 @@ Qu’est-ce que FranceConnect ? %hr - = form_for @user, url: user_session_path, method: :post do |f| - %h4 - = f.label :email - .input-group - .input-group-addon - %span.fa.fa-user - = f.email_field :email, class: 'form-control' - %br - %h4 - = f.label 'Mot de passe' - .input-group - .input-group-addon - %span.fa.fa-asterisk - = f.password_field :password, class: 'form-control', value: "#{@user.password}" - %br - %br - .actions - = f.submit "Se connecter", class:'btn btn-primary' - %br + + .text-left + = simple_form_for @user, url: user_session_path do |f| + = f.input :email + = f.input :password, label: 'Mot de passe', input_html: { value: @user.password } + - if devise_mapping.rememberable? + = f.input :remember_me, as: :boolean, label: 'Se souvenir de moi' + .text-center + = f.submit "Se connecter", class:'btn btn-primary' + - if @user.email != DemoEmails[:gestionnaire] && @user.email != DemoEmails[:admin] = render "users/shared/links" diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 16112041c..8d3886482 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -126,7 +126,7 @@ Devise.setup do |config| # ==> Configuration for :rememberable # The time the user will be remembered without asking for credentials again. - # config.remember_for = 2.weeks + config.remember_for = 2.weeks # Invalidates all the remember me tokens when the user signs out. config.expire_all_remember_me_on_sign_out = true From 8cd46b7fdf639e2a37f104e9503874be64fecd6a Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Tue, 7 Mar 2017 10:39:39 +0100 Subject: [PATCH 23/29] InitiatedMail: set default timestamp --- db/migrate/20170302105557_create_initiated_mails.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/db/migrate/20170302105557_create_initiated_mails.rb b/db/migrate/20170302105557_create_initiated_mails.rb index 88eb127a2..36c0ef332 100644 --- a/db/migrate/20170302105557_create_initiated_mails.rb +++ b/db/migrate/20170302105557_create_initiated_mails.rb @@ -5,8 +5,7 @@ class CreateInitiatedMails < ActiveRecord::Migration[5.0] t.text :body t.belongs_to :procedure, index: true, unique: true, foreign_key: true - t.column :created_at, :timestamp, null: true - t.column :updated_at, :timestamp, null: true + t.timestamps end end end From c1953592b79319b673304304dc4efa97ad00249e Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Tue, 7 Mar 2017 10:39:56 +0100 Subject: [PATCH 24/29] ReceivedMail: change timestamp for migration --- db/migrate/20170306102116_create_received_mails.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/db/migrate/20170306102116_create_received_mails.rb b/db/migrate/20170306102116_create_received_mails.rb index dbe07a298..2b1526ce8 100644 --- a/db/migrate/20170306102116_create_received_mails.rb +++ b/db/migrate/20170306102116_create_received_mails.rb @@ -5,7 +5,8 @@ class CreateReceivedMails < ActiveRecord::Migration[5.0] t.text :object t.references :procedure, foreign_key: true - t.timestamps + t.column :created_at, :timestamp, null: true + t.column :updated_at, :timestamp, null: true end end end From 63681cfded9b889ee59227029fd19c8b38266bd9 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Tue, 7 Mar 2017 10:40:16 +0100 Subject: [PATCH 25/29] Migrate mails to new system --- db/migrate/20170307092820_move_mails_to_new_system.rb | 11 +++++++++++ db/schema.rb | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20170307092820_move_mails_to_new_system.rb diff --git a/db/migrate/20170307092820_move_mails_to_new_system.rb b/db/migrate/20170307092820_move_mails_to_new_system.rb new file mode 100644 index 000000000..02b41ced3 --- /dev/null +++ b/db/migrate/20170307092820_move_mails_to_new_system.rb @@ -0,0 +1,11 @@ +class MoveMailsToNewSystem < ActiveRecord::Migration[5.0] + def up + execute 'INSERT INTO received_mails (object, body, procedure_id, created_at, updated_at) + SELECT object, body, procedure_id, mail_templates.created_at, mail_templates.updated_at from mail_templates inner join procedures on mail_templates.procedure_id = procedures.id;' + + execute "UPDATE received_mails set created_at='1980-01-01 00:00', updated_at='1980-01-01 00:00' where created_at is NULL" + + change_column_null :received_mails, :created_at, false + change_column_null :received_mails, :updated_at, false + end +end diff --git a/db/schema.rb b/db/schema.rb index 1aff0250d..3e739f4ef 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170306102320) do +ActiveRecord::Schema.define(version: 20170307092820) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" From 4ea7635dfcadb488590f950bd5481cae747e8572 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 7 Mar 2017 16:23:44 +0100 Subject: [PATCH 26/29] Fix after review --- app/mailers/notification_mailer.rb | 6 ++--- app/models/concerns/mail_template_concern.rb | 3 +-- app/models/mail_received.rb | 23 ------------------- app/views/mails/closed_mail.html.haml | 20 ++++++++-------- app/views/mails/initiated_mail.html.haml | 20 ++++++++-------- app/views/mails/received_mail.html.haml | 16 ++++++------- app/views/mails/refused_mail.html.haml | 20 ++++++++-------- .../mails/without_continuation_mail.html.haml | 20 ++++++++-------- .../20170302105557_create_initiated_mails.rb | 2 +- ...2_remove_deposit_datetime_from_dossiers.rb | 11 ++++++++- .../20170306102116_create_received_mails.rb | 2 +- .../20170306102238_create_closed_mails.rb | 2 +- .../20170306102256_create_refused_mails.rb | 2 +- ...02320_create_without_continuation_mails.rb | 2 +- db/schema.rb | 11 ++++----- .../previews/notification_mailer_preview.rb | 4 ++-- 16 files changed, 74 insertions(+), 90 deletions(-) delete mode 100644 app/models/mail_received.rb diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index cdd587493..185543d22 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -2,11 +2,11 @@ class NotificationMailer < ApplicationMailer default from: 'tps@apientreprise.fr', to: Proc.new { @user.email } - def send_notification dossier, email + def send_notification dossier, mail_template vars_mailer(dossier) - obj = email.object_for_dossier dossier - body = email.body_for_dossier dossier + obj = mail_template.object_for_dossier dossier + body = mail_template.body_for_dossier dossier mail(subject: obj) { |format| format.html { body } } end diff --git a/app/models/concerns/mail_template_concern.rb b/app/models/concerns/mail_template_concern.rb index f0e9863e9..0da40c322 100644 --- a/app/models/concerns/mail_template_concern.rb +++ b/app/models/concerns/mail_template_concern.rb @@ -32,7 +32,7 @@ module MailTemplateConcern module ClassMethods def slug - self.name.underscore.parameterize + self.name.demodulize.underscore.parameterize end def default @@ -48,7 +48,6 @@ module MailTemplateConcern when :numero_dossier dossier.id.to_s when :lien_dossier - # TPS::Application::URL # quickfix link_to users_dossier_recapitulatif_url(dossier), users_dossier_recapitulatif_url(dossier), target: '_blank' when :libelle_procedure dossier.procedure.libelle diff --git a/app/models/mail_received.rb b/app/models/mail_received.rb deleted file mode 100644 index 2199e0543..000000000 --- a/app/models/mail_received.rb +++ /dev/null @@ -1,23 +0,0 @@ -class MailReceived < MailTemplate - before_save :default_values - - - def name - "E-mail d'accusé de réception" - end - - def default_values - self.object ||= "[TPS] Accusé de réception pour votre dossier n°--numero_dossier--" - self.body ||= "Bonjour, -
-
- Votre administration vous confirme la bonne réception de votre dossier n°--numero_dossier-- complet. Celui-ci sera instruit dans le délai légal déclaré par votre interlocuteur.
-
- En vous souhaitant une bonne journée, -
-
- --- -
- L'équipe TPS" - end -end diff --git a/app/views/mails/closed_mail.html.haml b/app/views/mails/closed_mail.html.haml index 22321d848..636817517 100644 --- a/app/views/mails/closed_mail.html.haml +++ b/app/views/mails/closed_mail.html.haml @@ -1,17 +1,17 @@ Bonjour -
-
+%br +%br Votre dossier N°--numero_dossier-- a été accepté. -
-
+%br +%br A tout moment, vous pouvez consulter le contenu de vos dossiers et les éventuels commentaires de l'administration à cette adresse : --lien_dossier-- -
-
+%br +%br Bonne journée -
-
+%br +%br \--- -
+%br Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. -
+%br \--- diff --git a/app/views/mails/initiated_mail.html.haml b/app/views/mails/initiated_mail.html.haml index 5b008a02c..d1e0ab917 100644 --- a/app/views/mails/initiated_mail.html.haml +++ b/app/views/mails/initiated_mail.html.haml @@ -1,17 +1,17 @@ Bonjour -
-
+%br +%br Votre administration vous confirme la bonne réception de votre dossier n°--numero_dossier--. -
-
+%br +%br A tout moment, vous pouvez consulter le contenu de vos dossiers et les éventuels commentaires de l'administration à cette adresse : --lien_dossier-- -
-
+%br +%br Bonne journée -
-
+%br +%br \--- -
+%br Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. -
+%br \--- diff --git a/app/views/mails/received_mail.html.haml b/app/views/mails/received_mail.html.haml index ea25ec013..bc0b64ffa 100644 --- a/app/views/mails/received_mail.html.haml +++ b/app/views/mails/received_mail.html.haml @@ -1,14 +1,14 @@ Bonjour -
-
+%br +%br Votre administration vous confirme la bonne réception de votre dossier n°--numero_dossier--. Celui-ci sera instruit dans le délai légal déclaré par votre interlocuteur. -
-
+%br +%br Bonne journée -
-
+%br +%br \--- -
+%br Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. -
+%br \--- diff --git a/app/views/mails/refused_mail.html.haml b/app/views/mails/refused_mail.html.haml index 5ea109569..d98bacab6 100644 --- a/app/views/mails/refused_mail.html.haml +++ b/app/views/mails/refused_mail.html.haml @@ -1,17 +1,17 @@ Bonjour -
-
+%br +%br Votre dossier N°--numero_dossier-- a été refusé. -
-
+%br +%br Pour en savoir plus sur le motif du refus, vous pouvez consulter le contenu de vos dossiers et les éventuels commentaires de l'administration à cette adresse : --lien_dossier-- -
-
+%br +%br Bonne journée -
-
+%br +%br \--- -
+%br Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. -
+%br \--- diff --git a/app/views/mails/without_continuation_mail.html.haml b/app/views/mails/without_continuation_mail.html.haml index 9acff2424..5277c51a6 100644 --- a/app/views/mails/without_continuation_mail.html.haml +++ b/app/views/mails/without_continuation_mail.html.haml @@ -1,17 +1,17 @@ Bonjour -
-
+%br +%br Votre dossier N°--numero_dossier-- a été classé sans suite. -
-
+%br +%br Pour en savoir plus sur les raisons de ce classement sans suite, vous pouvez consulter le contenu de vos dossiers et les éventuels commentaires de l'administration à cette adresse : --lien_dossier-- -
-
+%br +%br Bonne journée -
-
+%br +%br \--- -
+%br Merci de ne pas répondre à ce mail. Postez directement vos questions dans votre dossier sur la plateforme. -
+%br \--- diff --git a/db/migrate/20170302105557_create_initiated_mails.rb b/db/migrate/20170302105557_create_initiated_mails.rb index 36c0ef332..91e005e6a 100644 --- a/db/migrate/20170302105557_create_initiated_mails.rb +++ b/db/migrate/20170302105557_create_initiated_mails.rb @@ -1,7 +1,7 @@ class CreateInitiatedMails < ActiveRecord::Migration[5.0] def change create_table :initiated_mails do |t| - t.text :object + t.string :object t.text :body t.belongs_to :procedure, index: true, unique: true, foreign_key: true diff --git a/db/migrate/20170302112312_remove_deposit_datetime_from_dossiers.rb b/db/migrate/20170302112312_remove_deposit_datetime_from_dossiers.rb index 600beb4a3..823290306 100644 --- a/db/migrate/20170302112312_remove_deposit_datetime_from_dossiers.rb +++ b/db/migrate/20170302112312_remove_deposit_datetime_from_dossiers.rb @@ -1,8 +1,17 @@ class RemoveDepositDatetimeFromDossiers < ActiveRecord::Migration[5.0] def change + remove_column :dossiers, :deposit_datetime, :datetime + end + + def up Dossier.where.not(deposit_datetime: nil).each do |dossier| dossier.update(initiated_at: dossier.deposit_datetime) end - remove_column :dossiers, :deposit_datetime, :datetime + end + + def down + Dossier.where.not(initiated_at: nil).each do |dossier| + dossier.update(deposit_datetime: dossier.initiated_at) + end end end diff --git a/db/migrate/20170306102116_create_received_mails.rb b/db/migrate/20170306102116_create_received_mails.rb index 2b1526ce8..aaaaefb42 100644 --- a/db/migrate/20170306102116_create_received_mails.rb +++ b/db/migrate/20170306102116_create_received_mails.rb @@ -2,7 +2,7 @@ class CreateReceivedMails < ActiveRecord::Migration[5.0] def change create_table :received_mails do |t| t.text :body - t.text :object + t.string :object t.references :procedure, foreign_key: true t.column :created_at, :timestamp, null: true diff --git a/db/migrate/20170306102238_create_closed_mails.rb b/db/migrate/20170306102238_create_closed_mails.rb index 11f6c07ed..49f3bf5e8 100644 --- a/db/migrate/20170306102238_create_closed_mails.rb +++ b/db/migrate/20170306102238_create_closed_mails.rb @@ -2,7 +2,7 @@ class CreateClosedMails < ActiveRecord::Migration[5.0] def change create_table :closed_mails do |t| t.text :body - t.text :object + t.string :object t.belongs_to :procedure, index: true, unique: true, foreign_key: true t.timestamps diff --git a/db/migrate/20170306102256_create_refused_mails.rb b/db/migrate/20170306102256_create_refused_mails.rb index 527866530..ea7b58846 100644 --- a/db/migrate/20170306102256_create_refused_mails.rb +++ b/db/migrate/20170306102256_create_refused_mails.rb @@ -2,7 +2,7 @@ class CreateRefusedMails < ActiveRecord::Migration[5.0] def change create_table :refused_mails do |t| t.text :body - t.text :object + t.string :object t.belongs_to :procedure, index: true, unique: true, foreign_key: true t.timestamps diff --git a/db/migrate/20170306102320_create_without_continuation_mails.rb b/db/migrate/20170306102320_create_without_continuation_mails.rb index da65dc8de..09dbe1b8e 100644 --- a/db/migrate/20170306102320_create_without_continuation_mails.rb +++ b/db/migrate/20170306102320_create_without_continuation_mails.rb @@ -2,7 +2,7 @@ class CreateWithoutContinuationMails < ActiveRecord::Migration[5.0] def change create_table :without_continuation_mails do |t| t.text :body - t.text :object + t.string :object t.belongs_to :procedure, index: true, unique: true, foreign_key: true t.timestamps diff --git a/db/schema.rb b/db/schema.rb index 770109651..db86ebfaf 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,6 @@ # # It's strongly recommended that you check this file into your version control system. - ActiveRecord::Schema.define(version: 20170307092820) do # These are extensions that must be enabled in order to support this database @@ -107,7 +106,7 @@ ActiveRecord::Schema.define(version: 20170307092820) do create_table "closed_mails", force: :cascade do |t| t.text "body" - t.text "object" + t.string "object" t.integer "procedure_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -238,7 +237,7 @@ ActiveRecord::Schema.define(version: 20170307092820) do end create_table "initiated_mails", force: :cascade do |t| - t.text "object" + t.string "object" t.text "body" t.integer "procedure_id" t.datetime "created_at", null: false @@ -357,7 +356,7 @@ ActiveRecord::Schema.define(version: 20170307092820) do create_table "received_mails", force: :cascade do |t| t.text "body" - t.text "object" + t.string "object" t.integer "procedure_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -366,7 +365,7 @@ ActiveRecord::Schema.define(version: 20170307092820) do create_table "refused_mails", force: :cascade do |t| t.text "body" - t.text "object" + t.string "object" t.integer "procedure_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -426,7 +425,7 @@ ActiveRecord::Schema.define(version: 20170307092820) do create_table "without_continuation_mails", force: :cascade do |t| t.text "body" - t.text "object" + t.string "object" t.integer "procedure_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false diff --git a/spec/mailers/previews/notification_mailer_preview.rb b/spec/mailers/previews/notification_mailer_preview.rb index 5eb4a0bb3..25fa735c2 100644 --- a/spec/mailers/previews/notification_mailer_preview.rb +++ b/spec/mailers/previews/notification_mailer_preview.rb @@ -1,7 +1,7 @@ class NotificationMailerPreview < ActionMailer::Preview - def dossier_received - NotificationMailer.dossier_received(Dossier.last) + def send_notification + NotificationMailer.send_notification(Dossier.last, Dossier.last.procedure.initiated_mail) end end From 6cb88a995fdacc8a487b8ac6f23ed1d4eb7a3acb Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Tue, 7 Mar 2017 18:09:09 +0100 Subject: [PATCH 27/29] Stats: add adminitrations stats view --- .../stylesheets/administrations/stats.scss | 10 ++++++++ .../administrations/stats_controller.rb | 24 +++++++++++++++++++ app/views/administrations/index.html.haml | 3 +++ .../administrations/stats/index.html.haml | 10 ++++++++ config/routes.rb | 5 +++- 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/administrations/stats.scss create mode 100644 app/controllers/administrations/stats_controller.rb create mode 100644 app/views/administrations/stats/index.html.haml diff --git a/app/assets/stylesheets/administrations/stats.scss b/app/assets/stylesheets/administrations/stats.scss new file mode 100644 index 000000000..170a526c2 --- /dev/null +++ b/app/assets/stylesheets/administrations/stats.scss @@ -0,0 +1,10 @@ +.stats { + padding: 15px; + + .stat-card { + background-color: white; + margin: 15px auto; + max-width: 1200px; + padding: 15px; + } +} diff --git a/app/controllers/administrations/stats_controller.rb b/app/controllers/administrations/stats_controller.rb new file mode 100644 index 000000000..ea04c0ebb --- /dev/null +++ b/app/controllers/administrations/stats_controller.rb @@ -0,0 +1,24 @@ +module Administrations + class StatsController < ApplicationController + before_action :authenticate_administration! + + def index + procedures = Procedure.where(created_at: Time.current.all_quarter).group("date_trunc('day', created_at)").count + dossiers = Dossier.where(created_at: Time.current.all_quarter).group("date_trunc('day', created_at)").count + @procedures = clean_hash(procedures) + @dossiers = clean_hash(dossiers) + end + + private + + def clean_hash h + h.keys.each{ |key| h[key.to_date] = h[key]; h.delete(key) } + min_date = h.keys.min + max_date = h.keys.max + (min_date..max_date).each do |date| + h[date] = 0 if h[date].nil? + end + h + end + end +end diff --git a/app/views/administrations/index.html.haml b/app/views/administrations/index.html.haml index b0f2e40b7..bf4e66d9a 100644 --- a/app/views/administrations/index.html.haml +++ b/app/views/administrations/index.html.haml @@ -10,6 +10,9 @@ %br +.center + =link_to 'Stats', administrations_stats_path, style: 'margin-bottom: 50px; display: block', 'data-no-turbolink': true + = smart_listing_render :admins %br diff --git a/app/views/administrations/stats/index.html.haml b/app/views/administrations/stats/index.html.haml new file mode 100644 index 000000000..603438d3d --- /dev/null +++ b/app/views/administrations/stats/index.html.haml @@ -0,0 +1,10 @@ += javascript_include_tag 'https://code.highcharts.com/highcharts.js', 'chartkick' + +.stats + .stat-card + %h1 Procédures crées + = line_chart @procedures + + .stat-card + %h1 Dossiers créés + = line_chart @dossiers diff --git a/config/routes.rb b/config/routes.rb index 2f96e0031..716893f09 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -42,7 +42,10 @@ Rails.application.routes.draw do get 'admin' => 'admin#index' get 'backoffice' => 'backoffice#index' - resources :administrations + resources :administrations, only: [:index, :create] + namespace :administrations do + resources :stats, only: [:index] + end namespace :france_connect do get 'particulier' => 'particulier#login' From 9cd506e2657d2568f9f44dcf0b2d5ae84b58a14a Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 7 Mar 2017 18:19:48 +0100 Subject: [PATCH 28/29] Clone mail_templates with procedure --- app/models/procedure.rb | 17 +++++++- .../{mail_received.rb => received_mail.rb} | 3 +- spec/models/procedure_spec.rb | 40 +++++++++---------- 3 files changed, 35 insertions(+), 25 deletions(-) rename spec/factories/{mail_received.rb => received_mail.rb} (72%) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 267fa8e17..947499b30 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -36,7 +36,9 @@ class Procedure < ActiveRecord::Base # end # alias_method_chain :initiated_mail, :override - %w(InitiatedMail ReceivedMail ClosedMail RefusedMail WithoutContinuationMail).each do |name| + MAIL_TEMPLATE_TYPES = %w(InitiatedMail ReceivedMail ClosedMail RefusedMail WithoutContinuationMail) + + MAIL_TEMPLATE_TYPES.each do |name| has_one "#{name.underscore}".to_sym, class_name: "Mails::#{name}" define_method("#{name.underscore}_with_override") do self.send("#{name.underscore}_without_override") || Object.const_get("Mails::#{name}").default @@ -98,11 +100,22 @@ class Procedure < ActiveRecord::Base end def clone - procedure = self.deep_clone(include: [:types_de_piece_justificative, :types_de_champ, :types_de_champ_private, :module_api_carto, types_de_champ: [:drop_down_list]]) + procedure = self.deep_clone(include: + [:types_de_piece_justificative, + :types_de_champ, + :types_de_champ_private, + :module_api_carto, + types_de_champ: [:drop_down_list] + ]) procedure.archived = false procedure.published = false procedure.logo_secure_token = nil procedure.remote_logo_url = self.logo_url + + MAIL_TEMPLATE_TYPES.each do |mtt| + procedure.send("#{mtt.underscore}=", self.send("#{mtt.underscore}_without_override").try(:dup)) + end + return procedure if procedure.save end diff --git a/spec/factories/mail_received.rb b/spec/factories/received_mail.rb similarity index 72% rename from spec/factories/mail_received.rb rename to spec/factories/received_mail.rb index 2e6ef674c..d97effd96 100644 --- a/spec/factories/mail_received.rb +++ b/spec/factories/received_mail.rb @@ -1,7 +1,6 @@ FactoryGirl.define do - factory :mail_received do + factory :received_mail, class: Mails::ReceivedMail do object "Mail d'accusé de bonne reception de votre dossier" body "Votre dossier est correctement reçu" - type 'MailReceived' end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index c054f15d7..5a6074a30 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -1,26 +1,6 @@ require 'spec_helper' describe Procedure do - describe 'assocations' do - it { is_expected.to have_many(:types_de_piece_justificative) } - it { is_expected.to have_many(:types_de_champ) } - it { is_expected.to have_many(:dossiers) } - it { is_expected.to have_one(:module_api_carto) } - it { is_expected.to belong_to(:administrateur) } - it { is_expected.to have_many(:preference_list_dossiers) } - end - - describe 'attributes' do - it { is_expected.to have_db_column(:libelle) } - it { is_expected.to have_db_column(:description) } - it { is_expected.to have_db_column(:organisation) } - it { is_expected.to have_db_column(:direction) } - it { is_expected.to have_db_column(:euro_flag) } - it { is_expected.to have_db_column(:logo) } - it { is_expected.to have_db_column(:logo_secure_token) } - it { is_expected.to have_db_column(:cerfa_flag) } - it { is_expected.to have_db_column(:published) } - end describe 'mails' do it { expect(subject.initiated_mail).to be_a(Mails::InitiatedMail) } @@ -163,13 +143,14 @@ describe Procedure do describe 'clone' do let(:archived) { false } let(:published) { false } - let(:procedure) { create(:procedure, archived: archived, published: published) } + let(:procedure) { create(:procedure, archived: archived, published: published, received_mail: received_mail) } let!(:type_de_champ_0) { create(:type_de_champ_public, procedure: procedure, order_place: 0) } let!(:type_de_champ_1) { create(:type_de_champ_public, procedure: procedure, order_place: 1) } let!(:type_de_champ_private_0) { create(:type_de_champ_private, procedure: procedure, order_place: 0) } let!(:type_de_champ_private_1) { create(:type_de_champ_private, procedure: procedure, order_place: 1) } let!(:piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) } let!(:piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) } + let(:received_mail){ create(:received_mail) } subject { procedure.clone } @@ -193,6 +174,23 @@ describe Procedure do subject.types_de_piece_justificative.zip(procedure.types_de_piece_justificative).each do |stc, ptc| expect(stc).to have_same_attributes_as(ptc) end + + end + + it 'should duplicate existing mail_templates' do + expect(subject.received_mail.attributes.except("id", "procedure_id", "created_at", "updated_at")).to eq procedure.received_mail.attributes.except("id", "procedure_id", "created_at", "updated_at") + expect(subject.received_mail.id).not_to eq procedure.received_mail.id + expect(subject.received_mail.id).not_to be nil + expect(subject.received_mail.procedure_id).not_to eq procedure.received_mail.procedure_id + expect(subject.received_mail.procedure_id).not_to be nil + expect(subject.received_mail.created_at).not_to eq procedure.received_mail.created_at + expect(subject.received_mail.created_at).not_to be nil + expect(subject.received_mail.updated_at).not_to eq procedure.received_mail.updated_at + expect(subject.received_mail.updated_at).not_to be nil + end + + it 'should not duplicate default mail_template' do + expect(subject.initiated_mail.attributes).to eq Mails::InitiatedMail.default.attributes end it 'should not duplicate specific related objects' do From 342a6b239e2e315ab2f070214fa35691d418b803 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Wed, 8 Mar 2017 10:55:10 +0100 Subject: [PATCH 29/29] Small css changes --- app/assets/stylesheets/_card.scss | 6 ++++++ app/assets/stylesheets/administrations/stats.scss | 7 +++---- app/views/administrations/stats/index.html.haml | 15 ++++++++------- 3 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 app/assets/stylesheets/_card.scss diff --git a/app/assets/stylesheets/_card.scss b/app/assets/stylesheets/_card.scss new file mode 100644 index 000000000..5e1e54578 --- /dev/null +++ b/app/assets/stylesheets/_card.scss @@ -0,0 +1,6 @@ +.card { + background: white; + padding: 15px; + box-shadow: 0 1px 3px rgba(0, 0, 0, .15); + border-radius: 2px; +} \ No newline at end of file diff --git a/app/assets/stylesheets/administrations/stats.scss b/app/assets/stylesheets/administrations/stats.scss index 170a526c2..005075eff 100644 --- a/app/assets/stylesheets/administrations/stats.scss +++ b/app/assets/stylesheets/administrations/stats.scss @@ -1,10 +1,9 @@ -.stats { - padding: 15px; +@import "card"; +.stats { .stat-card { - background-color: white; + @extend .card; margin: 15px auto; max-width: 1200px; - padding: 15px; } } diff --git a/app/views/administrations/stats/index.html.haml b/app/views/administrations/stats/index.html.haml index 603438d3d..7c9a3f696 100644 --- a/app/views/administrations/stats/index.html.haml +++ b/app/views/administrations/stats/index.html.haml @@ -1,10 +1,11 @@ = javascript_include_tag 'https://code.highcharts.com/highcharts.js', 'chartkick' -.stats - .stat-card - %h1 Procédures crées - = line_chart @procedures +.container + .stats + .stat-card + %h1 Procédures crées + = line_chart @procedures - .stat-card - %h1 Dossiers créés - = line_chart @dossiers + .stat-card + %h1 Dossiers créés + = line_chart @dossiers