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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Allow moving meeting occurrence earlier
Oliver Günther5142cd8b9e27
diff --git a/modules/meeting/app/contracts/meetings/update_contract.rb b/modules/meeting/app/contracts/meetings/update_contract.rbindex 9bb44d583a0..1ecc5e27f0f 100644--- a/modules/meeting/app/contracts/meetings/update_contract.rb+++ b/modules/meeting/app/contracts/meetings/update_contract.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -31,7 +33,7 @@ module Meetingsinclude Redmine::I18nvalidate :user_allowed_to_edit- validate :not_before_scheduled_time+ validate :valid_rescheduling_date, if: -> { check_reschedule? }attribute :lock_version doif model.lock_version.nil? || model.lock_version_changed?@@ -45,14 +47,39 @@ module Meetingsendend- def not_before_scheduled_time- return unless model.recurring_meeting_id && model.scheduled_meeting- return unless model.changed.intersect?(%w[start_time start_date])+ def valid_rescheduling_date # rubocop:disable Metrics/AbcSize+ if model.start_time < Time.zone.now+ errors.add :start_date, :after_today+ return+ end++ check_before(model.scheduled_meeting.next_occurrence)+ check_after(model.scheduled_meeting.previous_occurrence)+ check_after(model.recurring_meeting.first_occurrence)+ end++ def check_before(time)+ # Avoid adding more errors if we already checked closer candidates+ return if errors.has_key?(:start_date)- scheduled_time = model.scheduled_meeting.start_time- if model.start_time < scheduled_time- errors.add :start_date, :after, date: format_date(scheduled_time)+ if time && model.start_time >= time+ errors.add :start_date, :before, date: format_time(time)endend++ def check_after(time)+ # Avoid adding more errors if we already checked closer candidates+ return if errors.has_key?(:start_date)++ if time && model.start_time <= time+ errors.add :start_date, :after, date: format_time(time)+ end+ end++ def check_reschedule?+ model.recurring_meeting_id &&+ model.scheduled_meeting &&+ model.changed.intersect?(%w[start_time start_date])+ endendenddiff --git a/modules/meeting/app/controllers/recurring_meetings_controller.rb b/modules/meeting/app/controllers/recurring_meetings_controller.rbindex 256ef04e8e9..4c966cd5765 100644--- a/modules/meeting/app/controllers/recurring_meetings_controller.rb+++ b/modules/meeting/app/controllers/recurring_meetings_controller.rb@@ -46,7 +46,7 @@ class RecurringMeetingsController < ApplicationController@recurring_meeting = RecurringMeeting.new(project: @project)end- def show # rubocop:disable Metrics/AbcSize+ def showif @direction == "past"@meetings = @recurring_meeting.scheduled_instances(upcoming: false).limit(@count)else@@ -237,7 +237,7 @@ class RecurringMeetingsController < ApplicationControllerdef init_next_occurrence_job(from_time)# Now we can schedule the job to create the next occurrence- next_occurrence = @recurring_meeting.next_occurrence(from_time:)&.to_time+ next_occurrence = @recurring_meeting.next_occurrence(from_time:)return if next_occurrence.nil?::RecurringMeetings::InitNextOccurrenceJob@@ -352,7 +352,7 @@ class RecurringMeetingsController < ApplicationControllerenddef check_template_completable- @first_occurrence = @recurring_meeting.next_occurrence&.to_time+ @first_occurrence = @recurring_meeting.next_occurrenceif @first_occurrence.nil?render_400(message: I18n.t("recurring_meeting.occurrence.error_no_next"))returndiff --git a/modules/meeting/app/mailers/meeting_series_mailer.rb b/modules/meeting/app/mailers/meeting_series_mailer.rbindex 77b3418faaf..f4e6210df55 100644--- a/modules/meeting/app/mailers/meeting_series_mailer.rb+++ b/modules/meeting/app/mailers/meeting_series_mailer.rb@@ -31,7 +31,7 @@ class MeetingSeriesMailer < UserMailer@actor = actor@series = series@template = series.template- @next_occurrence = series.next_occurrence&.to_time+ @next_occurrence = series.next_occurrence@user = userset_headers(series)diff --git a/modules/meeting/app/models/recurring_meeting.rb b/modules/meeting/app/models/recurring_meeting.rbindex 1c5649e2a06..a73a8255c5c 100644--- a/modules/meeting/app/models/recurring_meeting.rb+++ b/modules/meeting/app/models/recurring_meeting.rb@@ -186,7 +186,11 @@ class RecurringMeeting < ApplicationRecordenddef next_occurrence(from_time: Time.current)- schedule.next_occurrence(from_time)+ schedule.next_occurrence(from_time)&.to_time+ end++ def previous_occurrence(from_time: Time.current)+ schedule.previous_occurrence(from_time)&.to_timeenddef remaining_occurrencesdiff --git a/modules/meeting/app/models/scheduled_meeting.rb b/modules/meeting/app/models/scheduled_meeting.rbindex 39cd83603be..57f8ca6446e 100644--- a/modules/meeting/app/models/scheduled_meeting.rb+++ b/modules/meeting/app/models/scheduled_meeting.rb@@ -41,4 +41,12 @@ class ScheduledMeeting < ApplicationRecordvalidates_uniqueness_of :meeting, allow_nil: truevalidates_presence_of :start_time++ def previous_occurrence+ recurring_meeting.previous_occurrence(from_time: start_time)+ end++ def next_occurrence+ recurring_meeting.next_occurrence(from_time: start_time)+ endenddiff --git a/modules/meeting/spec/services/meetings/update_service_integration_spec.rb b/modules/meeting/spec/services/meetings/update_service_integration_spec.rbindex 697629cfaf5..7cf63711e16 100644--- a/modules/meeting/spec/services/meetings/update_service_integration_spec.rb+++ b/modules/meeting/spec/services/meetings/update_service_integration_spec.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -44,23 +46,145 @@ RSpec.describe Meetings::UpdateService, "integration", type: :model dolet(:updated_meeting) { service_result.result }context "when meeting is in a series and scheduled to the future" do- let!(:recurring_meeting) { create(:recurring_meeting, project:, frequency: "daily") }- let!(:meeting) { create(:structured_meeting, recurring_meeting:, project:, start_time: Time.zone.tomorrow + 10.hours) }- let!(:schedule) do+ shared_let(:recurring_meeting) { create(:recurring_meeting, project:, frequency: "daily") }+ shared_let(:meeting) do+ create(:structured_meeting,+ recurring_meeting:,+ project:,+ start_time: Time.zone.today + 2.days + 10.hours)+ end+ shared_let(:schedule) docreate(:scheduled_meeting,meeting:,- start_time: Time.zone.today + 10.hours,+ start_time: Time.zone.today + 2.days + 10.hours,recurring_meeting:)end- context "when trying to move it to before the scheduled time" do+ context "when scheduled meeting is the first occurrence" do+ before do+ recurring_meeting.update!(start_time: Time.zone.today + 2.days + 10.hours)+ end++ context "when moving it before the start_time of the series" do+ let(:params) do+ { start_time: Time.zone.tomorrow + 10.hours }+ end++ it "is not valid" do+ expect(service_result).not_to be_success+ expect(service_result.errors[:start_date])+ .to include("must be after #{format_time(Time.zone.today + 2.days + 10.hours)}.")+ end+ end++ context "when moving it later" do+ let(:params) do+ { start_time: Time.zone.today + 20.days + 10.hours }+ end++ it "is not valid because needs to be before next occurrence" do+ expect(service_result).not_to be_success+ expect(service_result.errors[:start_date])+ .to include("must be before #{format_time(Time.zone.today + 3.days + 10.hours)}.")+ end+ end+ end++ context "when moving it to the date of the previous schedule" do+ let(:params) do+ { start_time: Time.zone.today + 1.day + 10.hours }+ end++ it "is not valid" do+ expect(service_result).not_to be_success+ expect(service_result.errors[:start_date]).to include("must be after #{format_time(Time.zone.tomorrow + 10.hours)}.")+ end+ end++ context "when moving it to earlier than the date of the previous schedule" do+ let(:params) do+ { start_time: Time.zone.today + 1.day + 8.hours }+ end++ it "is not valid" do+ expect(service_result).not_to be_success+ expect(service_result.errors[:start_date]).to include("must be after #{format_time(Time.zone.tomorrow + 10.hours)}.")+ end+ end++ context "when moving it to after the date of the previous schedule" do+ let(:params) do+ { start_time: Time.zone.today + 1.day + 12.hours }+ end++ it "is valid" do+ expect(service_result).to be_success+ end+ end++ context "when previous schedule exists tomorrow at 10:00" do+ shared_let(:previous_meeting) do+ create(:structured_meeting, recurring_meeting:, project:, start_time: Time.zone.tomorrow + 10.hours)+ end+ shared_let(:previous_schedule) do+ create(:scheduled_meeting,+ meeting: previous_meeting,+ start_time: Time.zone.tomorrow + 10.hours,+ recurring_meeting:)+ end++ context "and we try to move it to that date" do+ let(:params) do+ { start_time: Time.zone.tomorrow + 10.hours }+ end++ it "is not valid" do+ expect(service_result).not_to be_success+ expect(service_result.errors[:start_date]).to include("must be after #{format_time(Time.zone.tomorrow + 10.hours)}.")+ end+ end++ context "and we try to move it to before that date" do+ let(:params) do+ { start_time: Time.zone.tomorrow + 9.hours }+ end++ it "is not valid" do+ expect(service_result).not_to be_success+ expect(service_result.errors[:start_date]).to include("must be after #{format_time(Time.zone.tomorrow + 10.hours)}.")+ end+ end++ context "and we try to move it to after that date" do+ let(:params) do+ { start_time: Time.zone.tomorrow + 12.hours }+ end++ it "is valid" do+ expect(service_result).to be_success+ end+ end+ end++ context "when trying to move it to before the start time" do+ let(:params) do+ { start_time: Time.zone.yesterday + 10.hours }+ end++ it "is not valid" do+ expect(service_result).not_to be_success+ expect(service_result.errors[:start_date]).to include("must be in the future.")+ end+ end++ context "when trying to move it to before today" dolet(:params) do{ start_time: Time.zone.yesterday + 10.hours }endit "does not be valid" doexpect(service_result).not_to be_success- expect(service_result.errors[:start_date]).to include("must be after #{format_date(Time.zone.today + 10.hours)}.")+ expect(service_result.errors[:start_date]).to include("must be in the future.")endendend