From f5573e5d697bc89e0ff216d6abc252fb86bbc49a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Mon, 29 Jul 2024 13:57:46 +0200 Subject: [PATCH] Use UpdateService to make use of send_notifcations --- .../app/controllers/meetings_controller.rb | 10 ++-- modules/meeting/app/models/meeting.rb | 2 +- .../meetings/set_attributes_service.rb | 2 +- .../meeting/app/views/meetings/_form.html.erb | 23 ++++---- modules/meeting/config/locales/en.yml | 2 +- .../meeting/spec/requests/meetings_spec.rb | 54 ++++++++++++++++++- 6 files changed, 71 insertions(+), 22 deletions(-) diff --git a/modules/meeting/app/controllers/meetings_controller.rb b/modules/meeting/app/controllers/meetings_controller.rb index 61cd650816b4..847fdd190aee 100644 --- a/modules/meeting/app/controllers/meetings_controller.rb +++ b/modules/meeting/app/controllers/meetings_controller.rb @@ -151,13 +151,15 @@ def cancel_edit end def update - @meeting.participants_attributes = @converted_params.delete(:participants_attributes) - @converted_params.delete(:send_notifications) - @meeting.attributes = @converted_params - if @meeting.save + call = ::Meetings::UpdateService + .new(user: current_user, model: @meeting) + .call(@converted_params) + + if call.success? flash[:notice] = I18n.t(:notice_successful_update) redirect_to action: "show", id: @meeting else + @meeting = call.result render action: "edit" end end diff --git a/modules/meeting/app/models/meeting.rb b/modules/meeting/app/models/meeting.rb index 9116281ae333..66a3a683fc22 100644 --- a/modules/meeting/app/models/meeting.rb +++ b/modules/meeting/app/models/meeting.rb @@ -317,7 +317,7 @@ def add_new_participants_as_watcher end def send_participant_added_mail(participant) - if persisted? + if persisted? && Journal::NotificationConfiguration.active? MeetingMailer.invited(self, participant.user, User.current).deliver_later end end diff --git a/modules/meeting/app/services/meetings/set_attributes_service.rb b/modules/meeting/app/services/meetings/set_attributes_service.rb index 7d107b9c36c3..4473a6776b48 100644 --- a/modules/meeting/app/services/meetings/set_attributes_service.rb +++ b/modules/meeting/app/services/meetings/set_attributes_service.rb @@ -43,7 +43,7 @@ def set_default_attributes(_params) end def set_participants(participants_attributes) - model.participants.clear + model.participants.clear if model.new_record? model.participants_attributes = participants_attributes end end diff --git a/modules/meeting/app/views/meetings/_form.html.erb b/modules/meeting/app/views/meetings/_form.html.erb index 45230408792f..ed8dab007fb8 100644 --- a/modules/meeting/app/views/meetings/_form.html.erb +++ b/modules/meeting/app/views/meetings/_form.html.erb @@ -192,18 +192,15 @@ See COPYRIGHT and LICENSE files for more details. <%= render partial: 'meetings/participants_section' %> - <% if @meeting.new_record? || copy_from.present? %> -
- <%= styled_label_tag 'send_notfications', t(:"meeting.email.send_emails") %> -
- <%= styled_check_box_tag 'send_notifications', - 1, - true %> -
-
- <%= t(:"meeting.email.send_invitation_emails") %> -
+
+ <%= styled_label_tag 'send_notfications', t(:"meeting.email.send_emails") %> +
+ <%= styled_check_box_tag 'send_notifications', + 1, + true %>
- <% end %> - +
+ <%= t(:"meeting.email.send_invitation_emails") %> +
+
diff --git a/modules/meeting/config/locales/en.yml b/modules/meeting/config/locales/en.yml index 5a8a0ed125b3..8ea3f9d6249c 100644 --- a/modules/meeting/config/locales/en.yml +++ b/modules/meeting/config/locales/en.yml @@ -139,7 +139,7 @@ en: agenda_text: "Copy the agenda of the old meeting" email: send_emails: "Send emails" - send_invitation_emails: "Send out invitation emails upon creation" + send_invitation_emails: "Send out invitation emails for all participants." open_meeting_link: "Open meeting" invited: summary: "%{actor} has sent you an invitation for the meeting %{title}" diff --git a/modules/meeting/spec/requests/meetings_spec.rb b/modules/meeting/spec/requests/meetings_spec.rb index 5fce07427a00..07faa0e87b70 100644 --- a/modules/meeting/spec/requests/meetings_spec.rb +++ b/modules/meeting/spec/requests/meetings_spec.rb @@ -32,10 +32,11 @@ :skip_csrf, type: :rails_request do shared_let(:project) { create(:project, enabled_module_names: %i[meetings]) } - shared_let(:user) { create(:user, member_with_permissions: { project => %i[view_meetings create_meetings] }) } - shared_let(:meeting) { create(:structured_meeting, project:) } + shared_let(:user) { create(:user, member_with_permissions: { project => %i[view_meetings create_meetings edit_meetings] }) } + shared_let(:meeting) { create(:structured_meeting, project:, author: user) } before do + meeting.participants.delete_all login_as user end @@ -49,6 +50,55 @@ end end + describe "update with a new particpant" do + let(:other_user) { create(:user, member_with_permissions: { project => %i[view_meetings] }) } + let(:params) do + { + send_notifications:, + title: "Updated meeting", + meeting: { + participants_attributes: [ + { user_id: user.id, invited: true }, + { user_id: other_user.id, invited: true } + ] + } + + } + end + + subject do + meeting.reload + end + + context "with send_notifications" do + let(:send_notifications) { "1" } + + it "sends an invitation mail to the invited users" do + patch(meeting_path(meeting), params:) + + expect(subject.participants.count).to eq(2) + + perform_enqueued_jobs + + expect(ActionMailer::Base.deliveries.size).to eq(2) + end + end + + context "with send_notifications false" do + let(:send_notifications) { "0" } + + it "sends an invitation mail to the invited users" do + patch(meeting_path(meeting), params:) + + expect(subject.participants.count).to eq(2) + + perform_enqueued_jobs + + expect(ActionMailer::Base.deliveries.size).to eq(0) + end + end + end + describe "copy" do let(:base_params) do {