diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index e3e834d55..a159762b0 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -250,7 +250,7 @@ class Admin::ProceduresController < AdminController private def procedure_params - editable_params = [:libelle, :description, :organisation, :direction, :lien_site_web, :lien_notice, :euro_flag, :logo, :auto_archive_on] + editable_params = [:libelle, :description, :organisation, :direction, :lien_site_web, :lien_notice, :web_hook_url, :euro_flag, :logo, :auto_archive_on] if @procedure.try(:locked?) params.require(:procedure).permit(*editable_params) else diff --git a/app/jobs/web_hook_job.rb b/app/jobs/web_hook_job.rb new file mode 100644 index 000000000..836ea29d9 --- /dev/null +++ b/app/jobs/web_hook_job.rb @@ -0,0 +1,16 @@ +class WebHookJob < ApplicationJob + queue_as :default + + TIMEOUT = 10 + + def perform(procedure, dossier) + body = { + procedure_id: procedure.id, + dossier_id: dossier.id, + state: dossier.state, + updated_at: dossier.updated_at + } + + Typhoeus.post(procedure.web_hook_url, body: body, timeout: TIMEOUT) + end +end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 2ba5fddc7..6c925cf1b 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -75,6 +75,7 @@ class Dossier < ApplicationRecord after_save :build_default_champs, if: Proc.new { saved_change_to_procedure_id? } after_save :build_default_individual, if: Proc.new { procedure.for_individual? } after_save :send_dossier_received + after_save :send_web_hook after_create :send_draft_notification_email validates :user, presence: true @@ -326,4 +327,13 @@ class Dossier < ApplicationRecord NotificationMailer.send_draft_notification(self).deliver_now! end end + + def send_web_hook + if saved_change_to_state? && !brouillon? && procedure.web_hook_url + WebHookJob.perform_later( + procedure, + self + ) + end + end end diff --git a/app/views/admin/procedures/_informations.html.haml b/app/views/admin/procedures/_informations.html.haml index 76f5568c8..ba6ccefde 100644 --- a/app/views/admin/procedures/_informations.html.haml +++ b/app/views/admin/procedures/_informations.html.haml @@ -2,15 +2,19 @@ .alert.alert-info Cette procédure est publiée, certains éléments de la description ne sont plus modifiables -- { libelle: 'Libellé*', description: 'Description*', organisation: 'Organisme*', direction: 'Direction', lien_site_web: 'Lien site internet', lien_notice: 'Lien notice' }.each do |key, value| - .form-group - %h4 - = value - - if key == :description - = f.text_area key, rows: '6', placeholder: 'Description du projet', class: 'form-control' +- { libelle: 'Libellé*', description: 'Description*', organisation: 'Organisme*', direction: 'Direction', lien_site_web: 'Lien site internet', lien_notice: 'Lien notice', web_hook_url: 'Lien de rappel HTTP' }.each do |key, value| + - if key != :web_hook_url || current_administrateur&.feature_enabled?(:web_hook_allowed) + .form-group + %h4 + = value + - if key == :web_hook_url + %p + Un lien de rappel HTTP (aussi appelé webhook) est utilisé pour notifier un service tiers du changement de l'état d’un dossier sur demarches-simplifiees.fr. À chaque changement d’état d'un dossier, notre site va effectuer une requête sur le lien renseigné avec en paramètres : le nouvel état du dossier, l’identifiant de la procédure, l'identifiant dossier et la date du changement. Vous pourrez alors utiliser notre API pour récupérer les nouvelles informations du dossier concerné. + - if key == :description + = f.text_area key, rows: '6', placeholder: 'Description du projet', class: 'form-control' - - else - = f.text_field key, class: 'form-control', placeholder: value + - else + = f.text_field key, class: 'form-control', placeholder: value .row .col-md-6 diff --git a/config/environments/test.rb b/config/environments/test.rb index 54fe2f2a8..9de8d2433 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -44,6 +44,8 @@ Rails.application.configure do protocol: :http } + config.active_job.queue_adapter = :test + # Raises error for missing translations # config.action_view.raise_on_missing_translations = true end diff --git a/config/initializers/features.yml b/config/initializers/features.yml index a7b150256..71aea0c20 100644 --- a/config/initializers/features.yml +++ b/config/initializers/features.yml @@ -4,3 +4,5 @@ champ_pj_allowed_for_admin_ids: - 0 champ_siret_allowed_for_admin_ids: - 0 +web_hook_allowed_for_admin_ids: + - 0 diff --git a/db/migrate/20180301123826_add_webhook_to_procedures.rb b/db/migrate/20180301123826_add_webhook_to_procedures.rb new file mode 100644 index 000000000..9aba4a9c8 --- /dev/null +++ b/db/migrate/20180301123826_add_webhook_to_procedures.rb @@ -0,0 +1,5 @@ +class AddWebhookToProcedures < ActiveRecord::Migration[5.2] + def change + add_column :procedures, :web_hook_url, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 51e42562f..aaa50a105 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -453,6 +453,7 @@ ActiveRecord::Schema.define(version: 2018_04_04_113409) do t.datetime "archived_at" t.datetime "whitelisted_at" t.boolean "ask_birthday", default: false, null: false + t.string "web_hook_url" t.index ["hidden_at"], name: "index_procedures_on_hidden_at" end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 68a9b50c7..27b2cede1 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -896,4 +896,34 @@ describe Dossier do it { is_expected.to eq(expected) } end end + + describe 'webhook' do + let(:dossier) { create(:dossier) } + + it 'should not call webhook' do + expect { + dossier.accepte! + }.to_not have_enqueued_job(WebHookJob) + end + + it 'should call webhook' do + dossier.procedure.update_column(:web_hook_url, '/webhook.json') + + expect { + dossier.update_column(:motivation, 'bonjour') + }.to_not have_enqueued_job(WebHookJob) + + expect { + dossier.en_construction! + }.to have_enqueued_job(WebHookJob) + + expect { + dossier.update_column(:motivation, 'bonjour2') + }.to_not have_enqueued_job(WebHookJob) + + expect { + dossier.en_instruction! + }.to have_enqueued_job(WebHookJob) + end + end end