Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Particulier reporters: less emails, group everything in one view, add humanize groups names, add admin #1617

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions app/controllers/api_particulier/reporters_controller.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
class APIParticulier::ReportersController < APIParticulier::AuthenticatedUsersController
def show
if reporters_config.exclude?(reporter_group) || reporters_config[reporter_group].exclude?(current_user.email)
redirect_to_root
return
end
before_action :check_if_reporter_or_admin

helper_method :groups_for_reporter

@datapasses_for_group_url = MetabaseEmbedService.new(487, { group: params[:id] }).url
def index
@datapasses_for_group_url = MetabaseEmbedService.new(487, { groups: groups_for_reporter.join('|') }).url
end

private

def reporter_group
params[:id].to_sym
def check_if_reporter_or_admin
return if current_user.admin? || reporter_emails.include?(current_user.email)

redirect_to root_path
end

def groups_for_reporter
if current_user.admin?
reporters_config.keys
else
reporters_config.select { |_, emails| emails.include?(current_user.email) }.keys
end
end

def reporter_emails
reporters_config.values.flatten.uniq
end

def reporters_config
Rails.application.credentials.api_particulier_reporters
@reporters_config ||= Rails.application.credentials.api_particulier_reporters
end
end
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
class DatapassWebhook::APIParticulier::NotifyReporters < ApplicationInteractor
def call
return if %w[submit approve].exclude?(context.event)
return if groups_to_notify.empty?

reporters_config.each_key do |group_name|
next unless scopes.any? { |scope| scope.start_with?("#{group_name}_") }

APIParticulier::ReportersMailer.with(group: group_name).send(context.event).deliver_later
end
APIParticulier::ReportersMailer.with(groups: groups_to_notify).send(context.event).deliver_later
end

private

def reporters_config
Rails.application.credentials.api_particulier_reporters
def groups_to_notify
reporters_groups_config.select do |group_name|
scopes.any? { |scope| scope.start_with?(group_name.to_s) }
end
end

def reporters_groups_config
Rails.application.credentials.api_particulier_reporters.keys
end

def scopes
Expand Down
12 changes: 8 additions & 4 deletions app/mailers/api_particulier/reporters_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ class APIParticulier::ReportersMailer < APIParticulierMailer
approve
].each do |event|
define_method(event) do
group = params[:group]
groups = params[:groups].map(&:to_sym)

return if reporters_config[group].blank?
return if reporter_emails(groups).empty?

mail(
bcc: reporters_config[group],
subject: t('.subject', group:)
bcc: reporter_emails(groups),
subject: t('.subject')
)
end
end

private

def reporter_emails(groups)
reporters_config.values_at(*groups).flatten
end

def reporters_config
Rails.application.credentials.api_particulier_reporters
end
Expand Down
17 changes: 17 additions & 0 deletions app/views/api_particulier/reporters/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>
Habilitations
</h1>

<p>
La liste des habilitations ci-dessous concernent les groupes de données suivants :

<ul>
<% groups_for_reporter.each do |group| %>
<li>
<%= t("groups_for_reporter.#{group}") %>
</li>
<% end %>
</ul>
</p>

<%= render partial: 'shared/metabase', locals: { metabase_url: @datapasses_for_group_url } %>
4 changes: 0 additions & 4 deletions app/views/api_particulier/reporters/show.html.erb

This file was deleted.

4 changes: 2 additions & 2 deletions app/views/api_particulier/reporters_mailer/approve.text.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Bonjour,

Une nouvelle demande vient d'être validée pour l'ensemble de données associé au groupe '<%= params[:group] %>'.
Une nouvelle demande vient d'être validée pour un ensemble de données associé au groupe auquel vous êtes bonné.

Vous pouvez consulter la liste des demandes relatives aux données associé au groupe '<%= params[:group] %>' sur le lien suivant : <%= api_particulier_dashboard_reporter_url(id: params[:group]) %>
Vous pouvez consulter la liste des demandes relatives aux données associé aux groupes où vous êtes abonnés sur le lien suivant : <%= api_particulier_dashboard_reporter_url %>

Cordialement,
L'équipe API Particulier
4 changes: 2 additions & 2 deletions app/views/api_particulier/reporters_mailer/submit.text.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Bonjour,

Une nouvelle demande vient d'être déposée pour l'ensemble de données associé au groupe '<%= params[:group] %>'.
Une nouvelle demande vient d'être déposée pour un ensemble de données associé à un groupe auquel vous êtes abonné.

Vous pouvez consulter la liste des demandes relatives aux données associé au groupe '<%= params[:group] %>' sur le lien suivant : <%= api_particulier_dashboard_reporter_url(id: params[:group]) %>
Vous pouvez consulter la liste des demandes relatives aux données associé aux groupes où vous êtes abonnés sur le lien suivant : <%= api_particulier_dashboard_reporter_url %>

Cordialement,
L'équipe API Particulier
2 changes: 1 addition & 1 deletion config/credentials/test.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tonkgh6Sc7Corqbt4DpG61lEEgMpniBzoq74Bp8yuSI+1sUZ9ceu7UdZyWvlKQfG2LpU6GSHsmKv5N9x4unJk+BS0A0AVEv81kxH8KLnUVMfwMp4uBceghzDzJTOYOGXNtxQgFxB9CwRJmHrCQMcwZ2wGWFPwDxRbTtJ7gUQWTD5Zdqf2jL0VREKtxQ7B5Ols6Y/Eacf/tQ3V6GjKXON6MkFIPs2YdMrnhau8mjJtGy3virbbSDlgJL03jMB40ipUFFd3dp0yuGI70LebIMCNMTZ43Wtypt+5EnsfkYdl17EkzLENyhu22of+3lNJwbx4fQJe1c+X4QmRDDhzRtjuTMohiEqXLsroucPw7Ae/4q3FMS/kCxPe5uGSLrUDvTWgmaQ8yuJkIgX42m7a+Gl7sVl/hn7hal5+APkNuWW7CWJNrnz3jgVwkYsWNQlqu9kDdWUc8yMWNqyux4BXyr2Aum718j0NDYtUdVjUKhEOWThMQ35VY8DjvilQO1L43dbMl9xkc++jV9a2m9snyvGMrbaxkgE0KS6GevSEFzfQ8FU3/GyKiZKyg2nNcrAyS7yoCdCHzvHsU9wxo99cjhth1nfvM40gWwwQyVGvVT7r7l47qBsSDV1IDQ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HjIbnWspUQxGbnwBhFh4FBIkX7N0a/MPO3iqXusUjhPxjqdrzenEnyS1Dy+lALmadAIHwltE3ZUGIHs1lICN4j/Y9SpVR6Pea50N0Ycs6jFo8YeN6WrKyAr0xA2m1TX8TsyllCUHYZ9dTtSOcOAfnAc9fyzcrSvwSFBnhXofmKS/Sal7aTh4tsZ/hipzrJFEMmvJLjMAkwWTCUMNVEyjZOAydOUTHBzC8TpdssiQfp+ZNvMB4veMKHrf7G7NzQ00JVfm/saHLEAdC4UJ23L8WTJvV83cXMN8GfB0NilYv+/JAutBQDOwT69ZCVFEIL1qVkwtsA/f44sznxr72mxmGcAzVR25S6XrgxoPMjd3HJVf7Z+grz01r1JB0gZFjAqvaaOirzFTsOKiRUl913zstlkFVDRChCLQO/JJ9Se5Qy+7wvDpq99YskbYA5HDTzy1jTBs1sLahYUgmCdKNC2VafQzMvoAtYjRnmYJd+HLywI57cK/9uNqtzcLACNIGs9ZUKM7mueJ9g7IoeN/3fKeH1xtJDFMozq5waZLFV8ke2Lc61UuDNFotVAoqxk+8FcZ+yhWqUj/fL8j31UuMzUIRNxeGnyX4ZdASf7yPrRIm0KUwaQZv1bjxQn5QbFJ3KC7jLw2nesQd+IdcW3y9H0jcfW1qvyKSMPaqdx+VyEmjWuLmbkNbuPodMjIwiYNWJPuaDL+a7GtfKdLTRZPgt1pdXHAFjpGRgVZL3SKOxTtjn--Q+qYENQI6t0r6nvC--1F5p5ywRjRasi7rrULgm1A==
8 changes: 8 additions & 0 deletions config/locales/reporter_groups.fr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fr:
groups_for_reporter:
cnaf: API Quotient Familial (CNAF & MSA)
men: API statut étudiant scolarisé et boursier (Ministère de l'éducation nationale)
pole_emploi: API Statut demandeur d'emploi et paiements (France travail)
cnous: Statut étudiant (CNOUS)
mesri: API Statut étudiant (Ministère de l'enseignement supérieur)
complementaire_sante_solidaire: API Complémentaire santé solidaire (Sécurité sociale)
2 changes: 1 addition & 1 deletion config/routes/api_particulier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@
get '/donnees_personnelles', to: 'pages#donnees_personnelles', as: :donnees_personnelles
get '/accessibilite', to: 'pages#accessibility', as: :accessibilite

get '/datapass/:id', to: 'reporters#show', as: :dashboard_reporter
get '/datapass', to: 'reporters#index', as: :dashboard_reporter
end
end
32 changes: 19 additions & 13 deletions spec/features/api_particulier/reporters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,45 @@

RSpec.describe 'API Particulier', app: :api_particulier do
subject(:visit_dashboard) do
visit api_particulier_dashboard_reporter_path(id:)
visit api_particulier_dashboard_reporter_path
end

before do
login_as(user)
end

context 'with valid user and id' do
let(:id) { 'cnaf' }
context 'with valid user' do
let(:user) { create(:user, email: '[email protected]') }

it 'renders metabase view' do
it 'renders metabase view and groups associated to this user' do
visit_dashboard

expect(page).to have_current_path(api_particulier_dashboard_reporter_path(id:))
expect(page).to have_current_path(api_particulier_dashboard_reporter_path)

expect(page.html).to include('metabase.entreprise.api.gouv.fr')

expect(page).to have_content('Quotient Familial')
expect(page).to have_no_content('France travail')
end
end

context 'with invalid user' do
let(:id) { 'cnaf' }
let(:user) { create(:user, email: '[email protected]') }
context 'with admin user' do
let(:user) { create(:user, email: '[email protected]') }

it 'redirects to root path' do
it 'renders metabase view and all groups' do
visit_dashboard

expect(page).to have_current_path(root_path)
expect(page).to have_current_path(api_particulier_dashboard_reporter_path)

expect(page.html).to include('metabase.entreprise.api.gouv.fr')

expect(page).to have_content('Quotient Familial')
expect(page).to have_content('France travail')
end
end

context 'with invalid id' do
let(:id) { 'whatever' }
let(:user) { create(:user, email: '[email protected]') }
context 'with invalid user' do
let(:user) { create(:user, email: '[email protected]') }

it 'redirects to root path' do
visit_dashboard
Expand Down
6 changes: 4 additions & 2 deletions spec/mailers/api_particulier/reporters_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

describe '#submit' do
subject(:mail) do
described_class.with(group: 'cnaf').submit
described_class.with(groups: %w[cnaf men]).submit
end

it 'sends an email to reporters associated to this group' do
it 'sends an email to reporters associated to these groups' do
expect(mail.bcc).to include('[email protected]')
expect(mail.bcc).to include('[email protected]')
expect(mail.bcc).not_to include('[email protected]')
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ class APIParticulier::ReportersMailerPreview < ActionMailer::Preview
approve
].each do |event|
define_method(event) do
APIParticulier::ReportersMailer.with(group:).send(event)
APIParticulier::ReportersMailer.with(groups:).send(event)
end
end

private

def group
'cnaf'
def groups
%w[cnaf men]
end
end
Loading