Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14930 from opf/fix/meeting-attachments-copy
Copy meetings with attachments in separate service
- Loading branch information
Showing
13 changed files
with
499 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.