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

feat(rgpd): partition motifs by org type #2540

Open
wants to merge 8 commits into
base: staging
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion app/controllers/category_configurations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class CategoryConfigurationsController < ApplicationController
only: [:index, :new, :create, :show, :edit, :update, :destroy]
before_action :set_category_configuration, :set_file_configuration, :set_template,
only: [:show, :edit, :update, :destroy]
before_action :set_department, :set_file_configurations, only: [:new, :create, :edit, :update]
before_action :set_department, :set_file_configurations, :set_authorized_motif_categories,
only: [:new, :create, :edit, :update]
before_action :set_back_to_users_list_url, :set_messages_configuration, :set_category_configurations, only: [:index]

def index
Expand Down Expand Up @@ -107,6 +108,10 @@ def set_organisation
@organisation = current_organisation
end

def set_authorized_motif_categories
@authorized_motif_categories = MotifCategoryPolicy.authorized_for_organisation(@organisation)
end

def user_count_by_tag_id
User.joins(:tags,
:organisations)
Expand Down
4 changes: 3 additions & 1 deletion app/models/category_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class CategoryConfiguration < ApplicationRecord

validates :organisation, uniqueness: { scope: :motif_category,
message: "a déjà une category_configuration pour cette catégorie de motif" }
validate :minimum_invitation_duration, :invitation_formats_validity, :periodic_invites_can_be_activated
validate :minimum_invitation_duration,
:invitation_formats_validity,
:periodic_invites_can_be_activated

validates :email_to_notify_no_available_slots, :email_to_notify_rdv_changes,
format: {
Expand Down
1 change: 1 addition & 0 deletions app/models/organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def france_travail? = safir_code?
def with_parcours_access?
organisation_type.in?(ORGANISATION_TYPES_WITH_PARCOURS_ACCESS)
end
alias_method :rsa_related?, :with_parcours_access?

def requires_dpa_acceptance?
dpa_agreement.nil?
Expand Down
19 changes: 19 additions & 0 deletions app/policies/motif_category_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class MotifCategoryPolicy < ApplicationPolicy
RSA_RELATED_TYPES = %w[rsa_orientation rsa_accompagnement].freeze

def self.authorized_for_organisation(organisation)
if organisation.rsa_related?
MotifCategory.where(motif_category_type: RSA_RELATED_TYPES)
else
MotifCategory.where(motif_category_type: organisation.organisation_type)
end
end

# We need a dedicated method on top of the class method because we may be validating on a new object
# which the scope is not yet aware of
def authorized_for_organisation?(organisation)
return RSA_RELATED_TYPES.include?(record.motif_category_type) if organisation.rsa_related?

organisation.organisation_type == record.motif_category_type
end
end
10 changes: 9 additions & 1 deletion app/services/category_configurations/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ def initialize(category_configuration:)

def call
CategoryConfiguration.transaction do
activate_motif_category_on_rdvs_territory
ensure_motif_category_is_authorized
save_record!(@category_configuration)
activate_motif_category_on_rdvs_territory
end
end

private

def ensure_motif_category_is_authorized
return if MotifCategoryPolicy.new(nil, @category_configuration.motif_category)
.authorized_for_organisation?(@category_configuration.organisation)

fail!("La catégorie de motif n'est pas autorisée pour cette organisation")
end

def activate_motif_category_on_rdvs_territory
@activate_motif_category_on_rdvs_territory ||= call_service!(
RdvSolidaritesApi::CreateMotifCategoryTerritory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
required="true"
>
<option value=""> - </option>
<% MotifCategory.all.each do |motif_category| %>
<% @authorized_motif_categories.each do |motif_category| %>
<option value="<%= motif_category.id %>"><%= motif_category.name %></option>
<% end %>
</select>
Expand Down
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@
rdv_solidarites_organisation_id: 3,
# rdv_solidarites_organisation_id: vérifier l'id de l'organisation correspondante sur RDV-Solidarites
department_id: yonne.id,
organisation_type: "siae"
organisation_type: "france_travail"
)

CategoryConfiguration.create!(
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/motif_category.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryBot.define do
factory :motif_category do
template
motif_category_type { "autre" }
motif_category_type { "rsa_orientation" }
sequence(:short_name) { |n| "rsa_orientation_#{n}" }
name { "RSA orientation" }
end
Expand Down
63 changes: 63 additions & 0 deletions spec/features/agent_can_create_category_configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
describe "Agents can edit category configuration", :js do
let!(:agent) { create(:agent) }
let!(:organisation) { create(:organisation, organisation_type: "delegataire_rsa") }
let!(:agent_role) { create(:agent_role, organisation: organisation, agent: agent, access_level: "admin") }
let!(:motif_category) do
create(
:motif_category,
name: "RSA Follow up",
short_name: "rsa_follow_up",
motif_category_type: "rsa_accompagnement"
)
end

let!(:category_configuration) { create(:category_configuration, organisation:, file_configuration:) }
let(:file_configuration) { create(:file_configuration) }

before do
stub_rdv_solidarites_activate_motif_category_territories(
organisation.rdv_solidarites_organisation_id,
motif_category.short_name
)
setup_agent_session(agent)
end

context "category configuration edit" do
it "allows to create category configuration" do
visit new_organisation_category_configuration_path(organisation)

fill_in "category_configuration_phone_number", with: "3234"
fill_in "category_configuration_email_to_notify_rdv_changes", with: "[email protected]"
fill_in "category_configuration_email_to_notify_no_available_slots", with: "[email protected]"

select "RSA Follow up", from: "category_configuration[motif_category_id]"

find("input[name=\"category_configuration[file_configuration_id]\"]").click

click_button "Enregistrer"

expect(page).to have_content("3234")
expect(page).to have_content("[email protected]")

new_category_configuration = CategoryConfiguration.last
expect(new_category_configuration.reload.phone_number).to eq("3234")
expect(new_category_configuration.email_to_notify_rdv_changes).to eq("[email protected]")
expect(new_category_configuration.email_to_notify_no_available_slots).to eq("[email protected]")
end

context "motif category selection" do
let!(:motif_category2) do
create(:motif_category, name: "Autre", short_name: "autre", motif_category_type: "autre")
end

it "allows to select authorized motif categories" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ce serait bien d'avoir un test qui vérifie qu'on ne puisse pas créer une config sur une catégorie non autorisée

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je pense pas que ce soit utile ce serait redondant avec le TU

visit new_organisation_category_configuration_path(organisation)

expect(page).to have_select(
"category_configuration[motif_category_id]", options: ["-", "RSA Follow up",
category_configuration.motif_category.name]
)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
it "allows to edit category configuration" do
visit edit_organisation_category_configuration_path(organisation, organisation.category_configurations.first)

fill_in "category_configuration_phone_number", with: "3234"
fill_in "category_configuration_phone_number", with: "3234"
fill_in "category_configuration_email_to_notify_rdv_changes", with: "[email protected]"
fill_in "category_configuration_email_to_notify_no_available_slots", with: "[email protected]"
Expand Down
23 changes: 23 additions & 0 deletions spec/services/category_configurations/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,28 @@
expect(category_configuration).not_to be_persisted
end
end

describe "motif validatity" do
let(:organisation) { create(:organisation, organisation_type: "delegataire_rsa") }
let(:category_configuration) do
build(:category_configuration, organisation: create(:organisation), motif_category:)
end

context "when motif is not authorized" do
let(:motif_category) { create(:motif_category, name: "SIAE", short_name: "siae", motif_category_type: "siae") }

it "stores the error" do
expect(subject.errors[0]).to include("La catégorie de motif n'est pas autorisée")
end
end

context "when motif is authorized" do
let(:motif_category) { create(:motif_category) }

it "is a success" do
is_a_success
end
end
end
end
end
5 changes: 5 additions & 0 deletions spec/services/invitations/send_sms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@
end

context "for siae_interview" do
let!(:organisation) { create(:organisation, department: department, organisation_type: "siae") }
let!(:follow_up) { build(:follow_up, motif_category: category_siae_interview) }
let!(:category_configuration) do
create(:category_configuration, organisation: organisation, motif_category: category_siae_interview)
Expand Down Expand Up @@ -506,6 +507,7 @@
end

context "for siae_collective_information" do
let!(:organisation) { create(:organisation, department: department, organisation_type: "siae") }
let!(:follow_up) { build(:follow_up, motif_category: category_siae_collective_information) }
let!(:category_configuration) do
create(:category_configuration, organisation: organisation,
Expand Down Expand Up @@ -557,6 +559,7 @@
end

context "for siae_follow_up" do
let!(:organisation) { create(:organisation, department: department, organisation_type: "siae") }
let!(:follow_up) { build(:follow_up, motif_category: category_siae_follow_up) }
let!(:category_configuration) do
create(:category_configuration, organisation: organisation, motif_category: category_siae_follow_up)
Expand Down Expand Up @@ -607,6 +610,7 @@
end

context "for psychologue" do
let!(:organisation) { create(:organisation, department: department, organisation_type: "autre") }
let!(:follow_up) { build(:follow_up, motif_category: category_psychologue) }
let!(:category_configuration) do
create(:category_configuration, organisation: organisation, motif_category: category_psychologue)
Expand Down Expand Up @@ -659,6 +663,7 @@
end

context "for atelier_enfants_ados" do
let!(:organisation) { create(:organisation, department: department, organisation_type: "autre") }
let!(:follow_up) { build(:follow_up, motif_category: category_atelier_enfants_ados) }
let!(:category_configuration) do
create(:category_configuration, organisation: organisation, motif_category: category_atelier_enfants_ados)
Expand Down
8 changes: 8 additions & 0 deletions spec/support/stub_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ def stub_rdv_solidarites_assign_organisations(rdv_solidarites_user_id)
)
end

def stub_rdv_solidarites_activate_motif_category_territories(rdv_solidarites_organisation_id, motif_name)
stub_request(
:post, "http://www.rdv-solidarites-test.localhost/api/rdvinsertion/motif_category_territories"
).with(
body: "{\"motif_category_short_name\":\"#{motif_name}\",\"organisation_id\":#{rdv_solidarites_organisation_id}}"
).to_return(status: 200, body: "", headers: {})
end

def stub_rdv_solidarites_assign_many_referents(rdv_solidarites_user_id)
stub_request(
:post, "#{ENV['RDV_SOLIDARITES_URL']}/api/rdvinsertion/referent_assignations/create_many"
Expand Down
Loading