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

Relocate reminder contract failure handling to `Reminders::SetAttributesService`

Kabiru Mwenja20d01d08501b
diff --git a/app/controllers/work_packages/reminders_controller.rb b/app/controllers/work_packages/reminders_controller.rb
index 5af0df3f16e..3fcaf9fc5f5 100644
--- a/app/controllers/work_packages/reminders_controller.rb
+++ b/app/controllers/work_packages/reminders_controller.rb
@@ -53,13 +53,11 @@ class WorkPackages::RemindersController < ApplicationController
render_success_flash_message_via_turbo_stream(message: I18n.t("work_package.reminders.success_creation_message"))
respond_with_turbo_streams
else
- prepare_errors_from_result(service_result)
-
replace_via_turbo_stream(
component: modal_component_class.new(
remindable: @work_package,
- reminder: @reminder,
- errors: @errors,
+ reminder: service_result.result,
+ errors: service_result.errors,
remind_at_date:,
remind_at_time:
)
@@ -78,13 +76,11 @@ class WorkPackages::RemindersController < ApplicationController
render_success_flash_message_via_turbo_stream(message: I18n.t("work_package.reminders.success_update_message"))
respond_with_turbo_streams
else
- prepare_errors_from_result(service_result)
-
replace_via_turbo_stream(
component: modal_component_class.new(
remindable: @work_package,
- reminder: @reminder,
- errors: @errors,
+ reminder: service_result.result,
+ errors: service_result.errors,
remind_at_date:,
remind_at_time:
)
@@ -118,45 +114,6 @@ class WorkPackages::RemindersController < ApplicationController
@work_package = WorkPackage.visible.find(params[:work_package_id])
end
- # At the form level, we split the date and time into two form fields.
- # In order to be a bit more informative of which field is causing
- # the remind_at attribute to be in the past/invalid, we need to
- # remap the error attribute to the appropriate field.
- def prepare_errors_from_result(service_result)
- # We set the reminder here for "create" case
- # as the record comes from the service.
- @reminder = service_result.result
- @errors = service_result.errors
-
- case @errors.find { |error| error.attribute == :remind_at }&.type
- when :blank
- handle_blank_error
- when :datetime_must_be_in_future
- handle_future_error
- end
-
- @errors.delete(:remind_at)
- end
-
- def handle_blank_error
- @errors.add(:remind_at_date, :blank) if remind_at_date.blank?
- @errors.add(:remind_at_time, :blank) if remind_at_time.blank?
- end
-
- def handle_future_error
- @errors.add(:remind_at_date, :datetime_must_be_in_future) if @reminder.remind_at.to_date < today_in_user_time_zone
- @errors.add(:remind_at_time, :datetime_must_be_in_future) if @reminder.remind_at < now_in_user_time_zone
- end
-
- def now_in_user_time_zone
- @now_in_user_time_zone ||= Time.current
- .in_time_zone(User.current.time_zone)
- end
-
- def today_in_user_time_zone
- @today_in_user_time_zone ||= now_in_user_time_zone.to_date
- end
-
# We assume for now that there is only one reminder per work package
def build_or_find_reminder
@reminder = @work_package.reminders
diff --git a/app/services/reminders/set_attributes_service.rb b/app/services/reminders/set_attributes_service.rb
index 453456e2b5d..af6afe5f4a7 100644
--- a/app/services/reminders/set_attributes_service.rb
+++ b/app/services/reminders/set_attributes_service.rb
@@ -30,23 +30,65 @@
module Reminders
class SetAttributesService < ::BaseServices::SetAttributes
- private
-
- def set_attributes(params)
+ def perform(params = {})
remind_at_date = params.delete(:remind_at_date)
remind_at_time = params.delete(:remind_at_time)
- if (remind_at_date && remind_at_time).present?
- params[:remind_at] = build_remind_at_from_params(remind_at_date, remind_at_time)
+ if remind_at_date.present? && remind_at_time.present?
+ params[:remind_at] = User.current.time_zone.parse("#{remind_at_date} #{remind_at_time}")
+ end
+
+ contract_call = super
+
+ if contract_call.failure?
+ prepare_errors_from_result({ remind_at_date:, remind_at_time: }, contract_call)
end
- super
+ contract_call
end
- def build_remind_at_from_params(remind_at_date, remind_at_time)
- if remind_at_date.present? && remind_at_time.present?
- User.current.time_zone.parse("#{remind_at_date} #{remind_at_time}")
+ private
+
+ # At the form level, we split the date and time into two form fields.
+ # In order to be a bit more informative of which field is causing
+ # the remind_at attribute to be in the past/invalid, we need to
+ # remap the error attribute to the appropriate field.
+ def prepare_errors_from_result(remind_at_params, contract_call)
+ return contract_call unless contract_call.errors.include?(:remind_at)
+
+ case contract_call.errors.find { |error| error.attribute == :remind_at }.type
+ when :blank
+ handle_blank_error(remind_at_params, contract_call)
+ when :datetime_must_be_in_future
+ handle_future_error(contract_call)
+ end
+
+ contract_call.errors.delete(:remind_at)
+ end
+
+ def handle_blank_error(remind_at_params, contract_call)
+ %i[remind_at_date remind_at_time].each do |attribute|
+ contract_call.errors.add(attribute, :blank) if remind_at_params[attribute].blank?
end
end
+
+ def handle_future_error(contract_call)
+ reminder = contract_call.result
+
+ {
+ remind_at_date: (reminder.remind_at.to_date < today_in_user_time_zone),
+ remind_at_time: (reminder.remind_at < now_in_user_time_zone)
+ }.each do |attribute, in_the_past|
+ contract_call.errors.add(attribute, :datetime_must_be_in_future) if in_the_past
+ end
+ end
+
+ def today_in_user_time_zone
+ @today_in_user_time_zone ||= now_in_user_time_zone.to_date
+ end
+
+ def now_in_user_time_zone
+ @now_in_user_time_zone ||= Time.current.in_time_zone(User.current.time_zone)
+ end
end
end
diff --git a/spec/services/reminders/set_attributes_service_spec.rb b/spec/services/reminders/set_attributes_service_spec.rb
index 4c418b82232..bc2ee7f23d5 100644
--- a/spec/services/reminders/set_attributes_service_spec.rb
+++ b/spec/services/reminders/set_attributes_service_spec.rb
@@ -44,40 +44,73 @@ RSpec.describe Reminders::SetAttributesService do
contract_class:)
end
- it "sets the remind_at attribute from date and time params" do
- params = {
- remind_at_date: "2023-10-01",
- remind_at_time: "12:00",
- note: "Some notes",
- remindable:,
- creator: user
- }
-
- service.perform(params)
-
- expect(model_instance).to have_attributes(
- remind_at: current_user.time_zone.parse("2023-10-01 12:00"),
- note: "Some notes",
- remindable:,
- creator: user
- )
- end
+ describe "building remind_at timestamp" do
+ it "sets the remind_at attribute from date and time params" do
+ params = {
+ remind_at_date: "2023-10-01",
+ remind_at_time: "12:00",
+ note: "Some notes",
+ remindable:,
+ creator: user
+ }
+
+ service.perform(params)
+
+ expect(model_instance).to have_attributes(
+ remind_at: current_user.time_zone.parse("2023-10-01 12:00"),
+ note: "Some notes",
+ remindable:,
+ creator: user
+ )
+ end
- context "when remind_at_date or remind_at_time is not provided" do
- it "does not set the remind_at attribute" do
- aggregate_failures "one is nil" do
- service.perform(remind_at_date: nil, remind_at_time: "12:00")
- expect(model_instance.remind_at).to be_nil
+ context "when remind_at_date or remind_at_time is not provided" do
+ it "does not set the remind_at attribute" do
+ aggregate_failures "one is nil" do
+ service.perform(remind_at_date: nil, remind_at_time: "12:00")
+ expect(model_instance.remind_at).to be_nil
+ end
+
+ aggregate_failures "both are nil" do
+ service.perform(remind_at_date: nil, remind_at_time: nil)
+ expect(model_instance.remind_at).to be_nil
+ end
+
+ aggregate_failures "none provided" do
+ service.perform({})
+ expect(model_instance.remind_at).to be_nil
+ end
end
+ end
+ end
+
+ describe "Error results handling" do
+ let(:contract_class) { Reminders::BaseContract }
+
+ context "with remind_at blank active model error" do
+ it "adds blank errors for `remind_at_date` and `remind_at_time` attributes" do
+ result = service.perform({})
- aggregate_failures "both are nil" do
- service.perform(remind_at_date: nil, remind_at_time: nil)
- expect(model_instance.remind_at).to be_nil
+ expect(result).to be_failure
+ expect(result.errors.messages).to include(
+ remind_at_date: ["can't be blank."],
+ remind_at_time: ["can't be blank."]
+ )
end
+ end
+
+ context "with remind_at in the past active model error" do
+ let(:remind_at_date) { 1.day.ago.to_date }
+ let(:remind_at_time) { 1.hour.ago.strftime("%H:%M") }
+
+ it "adds errors for `remind_at_date` and `remind_at_time` attributes" do
+ result = service.perform(remind_at_date:, remind_at_time:)
- aggregate_failures "none provided" do
- service.perform({})
- expect(model_instance.remind_at).to be_nil
+ expect(result).to be_failure
+ expect(result.errors.messages).to include(
+ remind_at_date: ["must be in the future."],
+ remind_at_time: ["must be in the future."]
+ )
end
end
end