Merge pull request #1733 from tchak/webhooks

Add simple web hooks to procedures
This commit is contained in:
gregoirenovel 2018-04-09 11:08:04 +02:00 committed by GitHub
commit a9768301f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 79 additions and 9 deletions

View file

@ -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

16
app/jobs/web_hook_job.rb Normal file
View file

@ -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

View file

@ -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

View file

@ -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 dun 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, lidentifiant 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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,5 @@
class AddWebhookToProcedures < ActiveRecord::Migration[5.2]
def change
add_column :procedures, :web_hook_url, :string
end
end

View file

@ -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

View file

@ -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