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

No place without app type #1010

Open
wants to merge 5 commits into
base: develop
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
1 change: 0 additions & 1 deletion app/controllers/places_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def new
def create
@place = Place.new(place_params)
authorize @place

if @place.save
create_agenda(@place)
redirect_to places_path
Expand Down
10 changes: 8 additions & 2 deletions app/jobs/prepare_place_transfert_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,22 @@ def transfert_appointment_notifications(old_place, new_place, slot)
end

def modify_notif_content(old_place, new_place, notification)
attributes = %i[name adress display_phone contact_email preparation_link]
attributes = %i[name adress contact_email preparation_link]

modified_content = notification.content

attributes.each do |attr|
if old_place.send(attr) && new_place.send(attr)
modified_content = modified_content.gsub(old_place.send(attr), new_place.send(attr))
end
end

if old_place.display_phone && new_place.display_phone
modified_content = modified_content.gsub(old_place.display_phone(spaces: false),
new_place.display_phone(spaces: false))
modified_content = modified_content.gsub(old_place.display_phone(spaces: true),
new_place.display_phone(spaces: false))
end

modified_content
end
end
9 changes: 9 additions & 0 deletions app/models/place.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Place < ApplicationRecord
validates :contact_email, presence: true, if: :email_main_contact_method?
validates :contact_email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_blank: true
validates :preparation_link, presence: true
validate :appointment_types?

has_many :agendas, dependent: :destroy
has_many :place_appointment_types, dependent: :destroy
Expand Down Expand Up @@ -71,4 +72,12 @@ def transfert_out_date
def transfert_in_date
transfert_in&.date
end

private

def appointment_types?
return true unless appointment_types.empty?

errors.add(:appointment_types, I18n.t('activerecord.errors.models.place.attributes.appointment_types.empty'))
end
end
6 changes: 6 additions & 0 deletions app/views/places/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<div class='edit-place-wrapper'>
<div class='edit-place-form-container'>
<%= simple_form_for @place, html: { class: 'edit-place-form' } do |f| %>
<% invalid = @place.errors.include?(:appointment_types) %>
<% if invalid %>
<div class="fr-alert fr-alert--warning fr-my-2w">
<h3 class="fr-alert__title"><%= @place.errors.full_messages_for(:appointment_types).first %></h3>
</div>
<% end %>
<div class='edit-place-data-container'>
<%= f.input :name %>
<%= f.input :adress %>
Expand Down
6 changes: 6 additions & 0 deletions app/views/places/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<h1 class='form-title'><%= t('new_place_title') %></h1>
<%= simple_form_for @place, html: { class: 'basic-form' } do |f| %>
<% invalid = @place.errors.include?(:appointment_types) %>
<% if invalid %>
<div class="fr-alert fr-alert--warning fr-my-2w">
<h3 class="fr-alert__title"><%= @place.errors.messages_for(:appointment_types).first %></h3>
</div>
<% end %>
<%= f.input :name %>
<%= f.input :adress %>
<%= f.input :phone %>
Expand Down
3 changes: 3 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fr:
name: Nom
phone: Téléphone
preparation_link: Lien d'information sur le lieu
appointment_types: Types de convocation
slot:
appointment_type: Type de convocation
available: Disponible
Expand Down Expand Up @@ -177,6 +178,8 @@ fr:
blank: Adresse manquante
name:
blank: Nom manquant
appointment_types:
empty: Veuillez sélectionner au moins un type de convocation
place_transfert:
attributes:
date:
Expand Down
1 change: 1 addition & 0 deletions spec/factories/places.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
adress { 'fake adress' }
phone { '0606060606' }
organization
appointment_types { [create(:appointment_type)] }
end
end
12 changes: 8 additions & 4 deletions spec/features/places_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@

describe 'creation', logged_in_as: 'local_admin' do
it 'creates a place and its first agenda' do
create(:appointment_type, name: 'Premier contact Spip')

visit new_place_path

fill_in 'Nom', with: 'Spip 72'
fill_in 'Adresse', with: '93 rue des charmes 72200 La Flèche'
fill_in 'Téléphone', with: '0606060606'
fill_in "Lien d'information sur le lieu", with: 'https://mon-suivi-justice.beta.gouv.fr/preparer_spip92'

within first('.edit-place-appointment-types-container') do
check 'Premier contact Spip'
end
expect { click_button 'Enregistrer' }.to change { Place.count }.by(1)
.and change { Agenda.count }.by(1)
end
Expand Down Expand Up @@ -72,14 +76,14 @@
end

it 'allows to select appointment_types' do
place = create(:place, name: 'Spip du 91', organization: @user.organization)
apt_type = create(:appointment_type, name: 'Premier contact Spip')

place = build(:place, name: 'Spip du 91', organization: @user.organization, appointment_types: [])
place.save(validate: false)
expect(place.appointment_types).to be_empty

visit edit_place_path(place)
within first('.edit-place-appointment-types-container') do
check 'Premier contact Spip'
check apt_type.name
end

click_button('Enregistrer')
Expand Down
12 changes: 7 additions & 5 deletions spec/jobs/prepare_place_transfert_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
preparation_link: 'https://mon-suivi-justice.beta.gouv.fr/preparer_spip_loiret_montargis')
end
let(:new_place) do
create(:place, organization:, name: 'Nouveau SPIP 45 - Montargis',
adress: '8 av Adolphe Cochery, 45200 Montargis',
phone: '0238858585', contact_email: '[email protected]',
preparation_link: 'https://mon-suivi-justice.beta.gouv.fr/preparer_new_spip_loiret_montargis')
np = build(:place, organization:, name: 'Nouveau SPIP 45 - Montargis',
adress: '8 av Adolphe Cochery, 45200 Montargis',
phone: '0238858585', contact_email: '[email protected]',
preparation_link: 'https://mon-suivi-justice.beta.gouv.fr/preparer_new_spip_loiret_montargis')
np.save(validate: false)
np
end
let(:place_transfert) do
create(:place_transfert, old_place:, new_place:, date: next_valid_day(date: Date.tomorrow))
Expand Down Expand Up @@ -80,7 +82,7 @@
end

it('should copy old place appointment types to new place') do
expect(new_place.appointment_types).to match_array(old_place.appointment_types)
expect(new_place.reload.appointment_types).to match_array(old_place.appointment_types)
end

it('should copy old place slot types to new place') do
Expand Down
4 changes: 4 additions & 0 deletions spec/models/place_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
expect(build(:place, contact_email: 'youpi')).not_to be_valid
end

it 'denies without appointment_types' do
expect(build(:place, appointment_types: [])).not_to be_valid
end

it 'is valid with the factory\'s attributes' do
expect(build(:place)).to be_valid
end
Expand Down
4 changes: 2 additions & 2 deletions spec/support/notification_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module NotificationHelpers
def convocation_template(place)
def convocation_template(place, spaces: false)
"Votre convocation au SPIP a été modifiée. Vous êtes désormais convoqué au #{place.name} le 08/08/2023 à 09h45.
Merci de venir avec une pièce d'identité au #{place.adress}.
En cas de problème, contactez votre conseiller référent ou le standard au #{place.display_phone}
En cas de problème, contactez votre conseiller référent ou le standard au #{place.display_phone(spaces:)}
ou par mail #{place.contact_email}.
Plus d'informations sur #{place.preparation_link}"
end
Expand Down
Loading