Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Copy meetings with attachments in separate service

Oliver Güntherad8fc8b9077c
diff --git a/modules/meeting/app/contracts/meetings/base_contract.rb b/modules/meeting/app/contracts/meetings/base_contract.rb
index 599cbb58d79..d94ebbf1a85 100644
--- a/modules/meeting/app/contracts/meetings/base_contract.rb
+++ b/modules/meeting/app/contracts/meetings/base_contract.rb
@@ -39,6 +39,7 @@ module Meetings
attribute :duration
attribute :state
attribute :start_date
+ attribute :start_time
attribute :start_time_hour
end
end
diff --git a/modules/meeting/app/contracts/meetings/create_contract.rb b/modules/meeting/app/contracts/meetings/create_contract.rb
new file mode 100644
index 00000000000..583b163b983
--- /dev/null
+++ b/modules/meeting/app/contracts/meetings/create_contract.rb
@@ -0,0 +1,49 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Meetings
+ class CreateContract < BaseContract
+ attribute :type
+ validate :user_allowed_to_add
+ validate :type_in_allowed
+
+ private
+
+ def type_in_allowed
+ unless [StructuredMeeting.name, Meeting.name].include?(model.type)
+ errors.add(:type, :inclusion)
+ end
+ end
+
+ def user_allowed_to_add
+ unless user.allowed_in_project?(:create_meetings, model.project)
+ errors.add :base, :error_unauthorized
+ end
+ end
+ end
+end
diff --git a/modules/meeting/app/controllers/meetings_controller.rb b/modules/meeting/app/controllers/meetings_controller.rb
index 50e605ef11a..afc0b5a8f74 100644
--- a/modules/meeting/app/controllers/meetings_controller.rb
+++ b/modules/meeting/app/controllers/meetings_controller.rb
@@ -29,7 +29,7 @@
class MeetingsController < ApplicationController
around_action :set_time_zone
before_action :find_optional_project, only: %i[index new create]
- before_action :build_meeting, only: %i[new create]
+ before_action :build_meeting, only: %i[new]
before_action :find_meeting, except: %i[index new create]
before_action :find_copy_from_meeting, only: %i[create]
before_action :convert_params, only: %i[create update update_participants]
@@ -71,12 +71,18 @@ class MeetingsController < ApplicationController
end
def create
- @meeting.participants.clear # Start with a clean set of participants
- @meeting.participants_attributes = @converted_params.delete(:participants_attributes)
- @meeting.attributes = @converted_params
- copy_meeting_agenda
+ call =
+ if @copy_from
+ ::Meetings::CopyService
+ .new(user: current_user, model: @copy_from)
+ .call(attributes: @converted_params, copy_agenda: params[:copy_agenda] == '1')
+ else
+ ::Meetings::CreateService
+ .new(user: current_user)
+ .call(@converted_params)
+ end
- if @meeting.save
+ if call.success?
text = I18n.t(:notice_successful_create)
if User.current.time_zone.nil?
link = I18n.t(:notice_timezone_missing, zone: Time.zone)
@@ -84,8 +90,9 @@ class MeetingsController < ApplicationController
end
flash[:notice] = text.html_safe
- redirect_to action: 'show', id: @meeting
+ redirect_to action: 'show', id: call.result
else
+ @meeting = call.result
render template: 'meetings/new', project_id: @project, locals: { copy_from: @copy_from }
end
end
@@ -98,7 +105,11 @@ class MeetingsController < ApplicationController
def copy
copy_from = @meeting
- @meeting = @meeting.copy(author: User.current)
+ call = ::Meetings::CopyService
+ .new(user: current_user, model: copy_from)
+ .call(save: false)
+
+ @meeting = call.result
render action: 'new', project_id: @project, locals: { copy_from: }
end
@@ -269,8 +280,7 @@ class MeetingsController < ApplicationController
end
def build_meeting
- cls = meeting_type(params.dig(:meeting, :type)).constantize
- @meeting = cls.new
+ @meeting = Meeting.new
@meeting.project = @project
@meeting.author = User.current
end
@@ -295,6 +305,7 @@ class MeetingsController < ApplicationController
# instance variable.
@converted_params = meeting_params.to_h
+ @converted_params[:project] = @project
@converted_params[:duration] = @converted_params[:duration].to_hours if @converted_params[:duration].present?
# Force defaults on participants
@converted_params[:participants_attributes] ||= {}
@@ -304,7 +315,7 @@ class MeetingsController < ApplicationController
def meeting_params
if params[:meeting].present?
params.require(:meeting).permit(:title, :location, :start_time,
- :duration, :start_date, :start_time_hour,
+ :duration, :start_date, :start_time_hour, :type,
participants_attributes: %i[email name invited attended user user_id meeting id])
end
end
@@ -317,15 +328,6 @@ class MeetingsController < ApplicationController
end
end
- def meeting_type(given_type)
- case given_type
- when 'dynamic'
- 'StructuredMeeting'
- else
- 'Meeting'
- end
- end
-
def find_copy_from_meeting
return unless params[:copied_from_meeting_id]
@@ -333,19 +335,4 @@ class MeetingsController < ApplicationController
rescue ActiveRecord::RecordNotFound
render_404
end
-
- def copy_meeting_agenda
- return unless params[:copy_agenda] == '1' && @copy_from
-
- if @meeting.is_a?(StructuredMeeting)
- @meeting.agenda_items_attributes = @copy_from.agenda_items.map(&:copy_attributes)
- else
- @meeting.agenda = MeetingAgenda.new(
- author: current_user,
- text: @copy_from.agenda&.text,
- journal_notes: I18n.t('meeting.copied', id: params[:copied_from_meeting_id])
- )
- @meeting.agenda.author = current_user
- end
- end
end
diff --git a/modules/meeting/app/models/meeting.rb b/modules/meeting/app/models/meeting.rb
index b2ffac87090..92cc9fe1ad1 100644
--- a/modules/meeting/app/models/meeting.rb
+++ b/modules/meeting/app/models/meeting.rb
@@ -121,6 +121,7 @@ class Meeting < ApplicationRecord
def start_time=(value)
super(value&.to_datetime)
+ update_derived_fields
end
def start_month
@@ -173,25 +174,6 @@ class Meeting < ApplicationRecord
.uniq(&:id)
end
- def copy(attrs)
- copy = dup
-
- # Set a default to next week
- copy.start_time = start_time + 1.week
-
- copy.author = attrs.delete(:author)
- copy.attributes = attrs
- copy.set_initial_values
- # Initialize virtual attributes
- copy.start_date
- copy.start_time_hour
-
- copy.participants.clear
- copy.participants_attributes = allowed_participants.collect(&:copy_attributes)
-
- copy
- end
-
def self.group_by_time(meetings)
by_start_year_month_date = ActiveSupport::OrderedHash.new do |hy, year|
hy[year] = ActiveSupport::OrderedHash.new do |hm, month|
@@ -236,13 +218,11 @@ class Meeting < ApplicationRecord
def participants_attributes=(attrs)
attrs.each do |participant|
- participant['_destroy'] = true if !(participant['attended'] || participant['invited'])
+ participant['_destroy'] = true if !(participant[:attended] || participant[:invited])
end
self.original_participants_attributes = attrs
end
- protected
-
# Participants of older meetings
# might contain users no longer in the project
#
@@ -254,11 +234,16 @@ class Meeting < ApplicationRecord
.where(user_id: available_members)
end
+ protected
+
def set_initial_values
# set defaults
write_attribute(:start_time, Date.tomorrow + 10.hours) if start_time.nil?
self.duration ||= 1
+ update_derived_fields
+ end
+ def update_derived_fields
@start_date = start_time.to_date.iso8601
@start_time_hour = start_time.strftime('%H:%M')
end
diff --git a/modules/meeting/app/services/meetings/copy_service.rb b/modules/meeting/app/services/meetings/copy_service.rb
new file mode 100644
index 00000000000..e0c5a5f5057
--- /dev/null
+++ b/modules/meeting/app/services/meetings/copy_service.rb
@@ -0,0 +1,118 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Meetings
+ class CopyService
+ include ::Shared::ServiceContext
+ include ::Contracted
+ include ::Copy::Concerns::CopyAttachments
+
+ attr_accessor :user,
+ :meeting,
+ :contract_class
+
+ def initialize(user:, model:, contract_class: Meetings::CreateContract)
+ self.user = user
+ self.meeting = model
+ self.contract_class = contract_class
+ end
+
+ def call(send_notifications: nil, save: true, copy_agenda: true, copy_attachments: true, attributes: {})
+ if save
+ create(meeting, attributes, send_notifications:, copy_agenda:, copy_attachments:)
+ else
+ build(meeting, attributes)
+ end
+ end
+
+ protected
+
+ def create(meeting, attribute_overrides, send_notifications:, copy_agenda:, copy_attachments:)
+ Meetings::CreateService
+ .new(user:, contract_class:)
+ .call(**copied_attributes(meeting, attribute_overrides).merge(send_notifications:).symbolize_keys)
+ .on_success do |call|
+ copy_meeting_agenda(call.result) if copy_agenda
+ copy_meeting_attachment(call.result) if copy_attachments
+ end
+ end
+
+ def build(meeting, attribute_overrides)
+ Meetings::SetAttributesService
+ .new(user:, model: meeting.dup, contract_class:)
+ .call(**copied_attributes(meeting, attribute_overrides).symbolize_keys)
+ end
+
+ def copied_attributes(meeting, override)
+ overwritten_attributes = override.stringify_keys
+
+ meeting
+ .attributes
+ .slice(*writable_meeting_attributes(meeting))
+ .merge('start_time' => meeting.start_time + 1.week)
+ .merge('author' => user)
+ .merge('participants_attributes' => meeting.allowed_participants.collect(&:copy_attributes))
+ .merge(overwritten_attributes)
+ end
+
+ def writable_meeting_attributes(meeting)
+ instantiate_contract(meeting, user).writable_attributes - %w[start_date start_time_hour]
+ end
+
+ def copy_meeting_attachment(copy)
+ copy_attachments(
+ 'Meeting',
+ from: meeting,
+ to: copy
+ )
+ end
+
+ def update_references(attachment_source:, attachment_target:, model_source:, model_target:, references:)
+ model_target
+ .agenda_items
+ .update_all(["notes = REPLACE(notes, '/attachments/?/', '/attachments/?/')",
+ attachment_source,
+ attachment_target])
+ end
+
+ def copy_meeting_agenda(copy)
+ if meeting.is_a?(StructuredMeeting)
+ meeting.agenda_items.each do |agenda_item|
+ copy.agenda_items << agenda_item.dup
+ end
+ else
+ MeetingAgenda.create!(
+ meeting: copy,
+ author: user,
+ text: meeting.agenda&.text,
+ journal_notes: I18n.t('meeting.copied', id: meeting.id)
+ )
+ end
+ end
+ end
+end
diff --git a/modules/meeting/app/services/meetings/create_service.rb b/modules/meeting/app/services/meetings/create_service.rb
new file mode 100644
index 00000000000..78c63494783
--- /dev/null
+++ b/modules/meeting/app/services/meetings/create_service.rb
@@ -0,0 +1,45 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Meetings
+ class CreateService < ::BaseServices::Create
+ protected
+
+ def instance(params)
+
+ # Setting the #type as attributes will not work
+ # as the STI instance is not changed without using e.g., +becomes!+
+ case params.delete(:type)
+ when 'StructuredMeeting'
+ StructuredMeeting.new
+ else
+ Meeting.new
+ end
+ end
+ 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 fa8977e5d1d..7d107b9c36c 100644
--- a/modules/meeting/app/services/meetings/set_attributes_service.rb
+++ b/modules/meeting/app/services/meetings/set_attributes_service.rb
@@ -28,10 +28,23 @@
module Meetings
class SetAttributesService < ::BaseServices::SetAttributes
+ def set_attributes(params)
+ participants = params.delete(:participants_attributes)
+
+ super
+
+ set_participants(participants) if participants
+ end
+
def set_default_attributes(_params)
model.change_by_system do
model.author = user
end
end
+
+ def set_participants(participants_attributes)
+ model.participants.clear
+ model.participants_attributes = participants_attributes
+ end
end
end
diff --git a/modules/meeting/app/views/meetings/_form.html.erb b/modules/meeting/app/views/meetings/_form.html.erb
index c6898c1ada3..90aaa6ca245 100644
--- a/modules/meeting/app/views/meetings/_form.html.erb
+++ b/modules/meeting/app/views/meetings/_form.html.erb
@@ -37,7 +37,7 @@ See COPYRIGHT and LICENSE files for more details.
<% copy_from = local_assigns[:copy_from] %>
<% if copy_from.present? %>
- <%= f.hidden_field :type, value: copy_from.is_a?(StructuredMeeting) ? 'dynamic' : 'classic' %>
+ <%= f.hidden_field :type, value: copy_from.type %>
<%= hidden_field_tag "copied_from_meeting_id", copy_from.id %>
<div class="form--field -required">
<%= styled_label_tag 'copy_agenda', t('meeting.copy.agenda') %>
@@ -62,7 +62,7 @@ See COPYRIGHT and LICENSE files for more details.
data-test-selector="op-tile-block">
<div class="op-tile-block--content">
<%= styled_radio_button_tag 'meeting[type]',
- 'dynamic',
+ 'StructuredMeeting',
true,
no_label: true,
class: 'radio-button' %>
@@ -85,7 +85,7 @@ See COPYRIGHT and LICENSE files for more details.
data-test-selector="op-tile-block">
<div class="op-tile-block--content">
<%= styled_radio_button_tag 'meeting[type]',
- 'classic',
+ 'Meeting',
false,
no_label: true,
class: 'radio-button' %>
diff --git a/modules/meeting/spec/contracts/meetings/create_contract_spec.rb b/modules/meeting/spec/contracts/meetings/create_contract_spec.rb
new file mode 100644
index 00000000000..824be59068a
--- /dev/null
+++ b/modules/meeting/spec/contracts/meetings/create_contract_spec.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+require 'contracts/shared/model_contract_shared_context'
+
+RSpec.describe Meetings::CreateContract do
+ include_context 'ModelContract shared context'
+
+ shared_let(:project) { create(:project) }
+ let(:meeting) { build(:structured_meeting, project:) }
+ let(:contract) { described_class.new(meeting, user) }
+
+ context 'with permission' do
+ let(:user) do
+ create(:user, member_with_permissions: { project => %i[view_meetings create_meetings] })
+ end
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context 'without permission' do
+ let(:user) { build_stubbed(:user) }
+
+ it_behaves_like 'contract is invalid', base: :error_unauthorized
+ end
+
+ include_examples 'contract reuses the model errors' do
+ let(:user) { build_stubbed(:user) }
+ end
+end
diff --git a/modules/meeting/spec/models/meeting_spec.rb b/modules/meeting/spec/models/meeting_spec.rb
index bcea6102257..443dda9dfae 100644
--- a/modules/meeting/spec/models/meeting_spec.rb
+++ b/modules/meeting/spec/models/meeting_spec.rb
@@ -123,7 +123,7 @@ RSpec.describe Meeting do
let(:project_members) { { user1 => role, user2 => role2 } }
it 'does not contain the user' do
- expect(meeting.all_changeable_participants.include?(user2)).to be_falsey
+ expect(meeting.all_changeable_participants).not_to include(user2)
end
end
@@ -131,11 +131,11 @@ RSpec.describe Meeting do
let(:locked_user) { create(:locked_user) }
before do
- meeting.participants_attributes = [{ 'user_id' => locked_user.id, 'invited' => 1 }]
+ meeting.participants_attributes = [{ user_id: locked_user.id, invited: 1 }]
end
it 'contains the user' do
- expect(meeting.all_changeable_participants.include?(locked_user)).to be_truthy
+ expect(meeting.all_changeable_participants).to include(locked_user)
end
end
end
@@ -163,7 +163,7 @@ RSpec.describe Meeting do
end
it 'closes the agenda' do
- expect(meeting.agenda.locked?).to be_truthy
+ expect(meeting.agenda).to be_locked
end
end
@@ -194,48 +194,6 @@ RSpec.describe Meeting do
end
end
- describe 'Copied meetings' do
- let(:project_members) { { user1 => role, user2 => role } }
-
- before do
- meeting.start_date = '2013-03-27'
- meeting.start_time_hour = '15:35'
- meeting.participants.build(user: user2)
- meeting.save!
- end
-
- it 'has the same start_time as the original meeting' do
- copy = meeting.copy({})
- expect(copy.start_time).to eq(meeting.start_time)
- end
-
- it 'deletes the copied meeting author if no author is given as parameter' do
- copy = meeting.copy({})
- expect(copy.author).to be_nil
- end
-
- it 'sets the author to the provided author if one is given' do
- copy = meeting.copy author: user2
- expect(copy.author).to eq(user2)
- end
-
- it 'clears participant ids and attended flags for all copied attendees' do
- copy = meeting.copy({})
- expect(copy.participants.all? { |p| p.id.nil? && !p.attended }).to be_truthy
- end
-
- context 'when old meeting as user no longer in project' do
- before do
- user2.memberships.destroy_all
- end
-
- it 'does not copy that user' do
- copy = meeting.copy({})
- expect(copy.participants.map(&:user_id)).to eq [user1.id]
- end
- end
- end
-
describe 'acts_as_watchable' do
it 'is watchable' do
expect(described_class).to include(Redmine::Acts::Watchable::InstanceMethods)
diff --git a/modules/meeting/spec/requests/meetings_spec.rb b/modules/meeting/spec/requests/meetings_spec.rb
index 21aca1fe69c..09e70761db3 100644
--- a/modules/meeting/spec/requests/meetings_spec.rb
+++ b/modules/meeting/spec/requests/meetings_spec.rb
@@ -46,7 +46,7 @@ RSpec.describe 'Meeting requests',
project_id: project.id,
meeting: {
title: 'Copied meeting',
- type: :dynamic
+ type: 'StructuredMeeting'
}
}
end
diff --git a/modules/meeting/spec/services/meetings/copy_service_integration_spec.rb b/modules/meeting/spec/services/meetings/copy_service_integration_spec.rb
new file mode 100644
index 00000000000..979ae2b76ed
--- /dev/null
+++ b/modules/meeting/spec/services/meetings/copy_service_integration_spec.rb
@@ -0,0 +1,142 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+RSpec.describe Meetings::CopyService, 'integration', type: :model do
+ shared_let(:project) { create(:project, enabled_module_names: %i[meetings]) }
+ shared_let(:user) do
+ create(:user, member_with_permissions: { project => %i(view_meetings create_meetings) })
+ end
+ shared_let(:meeting) { create(:structured_meeting, project:, start_time: Time.parse('2013-03-27T15:35:00Z')) }
+
+ let(:instance) { described_class.new(model: meeting, user:) }
+ let(:attributes) { {} }
+ let(:params) { {} }
+
+ let(:service_result) { instance.call(attributes:, **params) }
+ let(:copy) { service_result.result }
+
+ it 'copies the meeting as is' do
+ expect(service_result).to be_success
+ expect(copy.author).to eq(user)
+ expect(copy.start_time).to eq(meeting.start_time + 1.week)
+ end
+
+ describe 'with participants' do
+ let(:invited_user) { create(:user, member_with_permissions: { project => %i(view_meetings) }) }
+ let(:attending_user) { create(:user, member_with_permissions: { project => %i(view_meetings) }) }
+ let(:invalid_user) { create(:user) }
+
+ it 'copies applicable participants, resetting attended status' do
+ meeting.participants.create!(user: invited_user, invited: true, attended: false)
+ meeting.participants.create!(user: attending_user, invited: true, attended: true)
+ meeting.participants.create!(user: invalid_user, invited: true, attended: true)
+
+ expect(service_result).to be_success
+ expect(copy.participants.count).to eq(2)
+ invited = copy.participants.find_by(user: invited_user)
+ attending = copy.participants.find_by(user: attending_user)
+ expect(invited).to be_invited
+ expect(invited).not_to be_attended
+
+ expect(attending).to be_invited
+ expect(attending).not_to be_attended
+
+ invalid = copy.participants.find_by(user: invalid_user)
+ expect(invalid).to be_nil
+ end
+ end
+
+ describe 'when not saving' do
+ let(:params) { { save: false } }
+
+ it 'builds the meeting' do
+ expect(service_result).to be_success
+ expect(copy.author).to eq(user)
+ expect(copy.start_time).to eq(meeting.start_time + 1.week)
+ expect(copy).to be_new_record
+ end
+ end
+
+ context 'with agenda items' do
+ shared_let(:agenda_item) do
+ create(:meeting_agenda_item,
+ meeting:,
+ notes: "hello there")
+ end
+
+ it 'copies the agenda item' do
+ expect(copy.agenda_items.length)
+ .to eq 1
+
+ expect(copy.agenda_items.first.notes)
+ .to eq agenda_item.notes
+ end
+
+ context 'when asking not to copy agenda' do
+ let(:params) { { copy_agenda: false } }
+
+ it 'does not copy agenda items' do
+ expect(copy.agenda_items).to be_empty
+ end
+ end
+ end
+
+ context 'with attachments' do
+ shared_let(:attachment) do
+ create(:attachment,
+ container: meeting)
+ end
+ shared_let(:agenda_item) do
+ create(:meeting_agenda_item,
+ meeting:,
+ notes: "![](/api/v3/attachments/#{attachment.id}/content")
+ end
+
+ it 'copies the attachment' do
+ expect(copy.attachments.length)
+ .to eq 1
+
+ expect(copy.attachments.first.id)
+ .not_to eq attachment.id
+
+ expect(copy.agenda_items.count).to eq(1)
+ expect(copy.agenda_items.first.notes).to include "attachments/#{copy.attachments.first.id}/content"
+ end
+
+ context 'when asking not to copy attachments' do
+ let(:params) { { copy_attachments: false } }
+
+ it 'does not copy attachments' do
+ expect(copy.attachments).to be_empty
+ expect(copy.agenda_items.first.notes).to include "attachments/#{attachment.id}/content"
+ end
+ end
+ end
+end
diff --git a/modules/meeting/spec/services/meetings/create_service_spec.rb b/modules/meeting/spec/services/meetings/create_service_spec.rb
new file mode 100644
index 00000000000..6e4101a89e2
--- /dev/null
+++ b/modules/meeting/spec/services/meetings/create_service_spec.rb
@@ -0,0 +1,36 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+require 'services/base_services/behaves_like_create_service'
+
+RSpec.describe Meetings::CreateService, type: :model do
+ it_behaves_like 'BaseServices create service' do
+ let(:factory) { :meeting }
+ end
+end