Add simple web hooks to procedures
This commit is contained in:
parent
efaa99c489
commit
27592ae072
9 changed files with 79 additions and 9 deletions
|
@ -250,7 +250,7 @@ class Admin::ProceduresController < AdminController
|
||||||
private
|
private
|
||||||
|
|
||||||
def procedure_params
|
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?)
|
if @procedure.try(:locked?)
|
||||||
params.require(:procedure).permit(*editable_params)
|
params.require(:procedure).permit(*editable_params)
|
||||||
else
|
else
|
||||||
|
|
16
app/jobs/web_hook_job.rb
Normal file
16
app/jobs/web_hook_job.rb
Normal 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
|
|
@ -75,6 +75,7 @@ class Dossier < ApplicationRecord
|
||||||
after_save :build_default_champs, if: Proc.new { saved_change_to_procedure_id? }
|
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 :build_default_individual, if: Proc.new { procedure.for_individual? }
|
||||||
after_save :send_dossier_received
|
after_save :send_dossier_received
|
||||||
|
after_save :send_web_hook
|
||||||
after_create :send_draft_notification_email
|
after_create :send_draft_notification_email
|
||||||
|
|
||||||
validates :user, presence: true
|
validates :user, presence: true
|
||||||
|
@ -326,4 +327,13 @@ class Dossier < ApplicationRecord
|
||||||
NotificationMailer.send_draft_notification(self).deliver_now!
|
NotificationMailer.send_draft_notification(self).deliver_now!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def send_web_hook
|
||||||
|
if saved_change_to_state? && !brouillon? && procedure.web_hook_url
|
||||||
|
WebHookJob.perform_later(
|
||||||
|
procedure,
|
||||||
|
self
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,10 +2,14 @@
|
||||||
.alert.alert-info
|
.alert.alert-info
|
||||||
Cette procédure est publiée, certains éléments de la description ne sont plus modifiables
|
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|
|
- { 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
|
.form-group
|
||||||
%h4
|
%h4
|
||||||
= value
|
= 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
|
- if key == :description
|
||||||
= f.text_area key, rows: '6', placeholder: 'Description du projet', class: 'form-control'
|
= f.text_area key, rows: '6', placeholder: 'Description du projet', class: 'form-control'
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,8 @@ Rails.application.configure do
|
||||||
protocol: :http
|
protocol: :http
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config.active_job.queue_adapter = :test
|
||||||
|
|
||||||
# Raises error for missing translations
|
# Raises error for missing translations
|
||||||
# config.action_view.raise_on_missing_translations = true
|
# config.action_view.raise_on_missing_translations = true
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,3 +4,5 @@ champ_pj_allowed_for_admin_ids:
|
||||||
- 0
|
- 0
|
||||||
champ_siret_allowed_for_admin_ids:
|
champ_siret_allowed_for_admin_ids:
|
||||||
- 0
|
- 0
|
||||||
|
web_hook_allowed_for_admin_ids:
|
||||||
|
- 0
|
||||||
|
|
5
db/migrate/20180301123826_add_webhook_to_procedures.rb
Normal file
5
db/migrate/20180301123826_add_webhook_to_procedures.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class AddWebhookToProcedures < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
add_column :procedures, :web_hook_url, :string
|
||||||
|
end
|
||||||
|
end
|
|
@ -453,6 +453,7 @@ ActiveRecord::Schema.define(version: 2018_04_04_113409) do
|
||||||
t.datetime "archived_at"
|
t.datetime "archived_at"
|
||||||
t.datetime "whitelisted_at"
|
t.datetime "whitelisted_at"
|
||||||
t.boolean "ask_birthday", default: false, null: false
|
t.boolean "ask_birthday", default: false, null: false
|
||||||
|
t.string "web_hook_url"
|
||||||
t.index ["hidden_at"], name: "index_procedures_on_hidden_at"
|
t.index ["hidden_at"], name: "index_procedures_on_hidden_at"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -896,4 +896,34 @@ describe Dossier do
|
||||||
it { is_expected.to eq(expected) }
|
it { is_expected.to eq(expected) }
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue