From 0c28552247b27a09e0970495608b20a5b446bbf4 Mon Sep 17 00:00:00 2001 From: Kabiru Mwenja Date: Mon, 2 Dec 2024 17:31:37 +0300 Subject: [PATCH] Reinstate "Add reminder note as an embedded notification detail" This reverts commit 220ca16ef02239a0b3294e8d51640619825f7718. Separates commit for easier code review --- config/locales/en.yml | 1 + .../notifications/notification_representer.rb | 2 +- .../property_factory/reminder.rb | 45 +++++++++++++++ .../v3/values/property_generic_representer.rb | 33 +++++++++++ .../v3/values/schemas/value_schema_factory.rb | 13 +++-- ...notification_representer_rendering_spec.rb | 55 +++++++++++++++++++ 6 files changed, 143 insertions(+), 6 deletions(-) create mode 100644 lib/api/v3/notifications/property_factory/reminder.rb create mode 100644 lib/api/v3/values/property_generic_representer.rb diff --git a/config/locales/en.yml b/config/locales/en.yml index 3d2a4e308d20..5ab4b6bae1b7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1510,6 +1510,7 @@ en: login: "Username" mail: "Email" name: "Name" + note: "Note" password: "Password" priority: "Priority" project: "Project" diff --git a/lib/api/v3/notifications/notification_representer.rb b/lib/api/v3/notifications/notification_representer.rb index 5b67c4dd9a3d..6984fe235187 100644 --- a/lib/api/v3/notifications/notification_representer.rb +++ b/lib/api/v3/notifications/notification_representer.rb @@ -104,7 +104,7 @@ def _type "Notification" end - self.to_eager_load = %i[actor journal] + self.to_eager_load = %i[actor journal reminder] end end end diff --git a/lib/api/v3/notifications/property_factory/reminder.rb b/lib/api/v3/notifications/property_factory/reminder.rb new file mode 100644 index 000000000000..e8bd4e210c2f --- /dev/null +++ b/lib/api/v3/notifications/property_factory/reminder.rb @@ -0,0 +1,45 @@ +# --copyright +# OpenProject is an open source project management software. +# Copyright (C) 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 API::V3::Notifications::PropertyFactory + module Reminder + extend ::API::V3::Utilities::PathHelper + + module_function + + def for(notification) + return [] unless notification.reminder + + [ + ::API::V3::Values::PropertyGenericRepresenter + .new(::API::V3::Values::PropertyModel.new(:note, notification.reminder.note), + self_link: api_v3_paths.notification_detail(notification.id, 0)) + ] + end + end +end diff --git a/lib/api/v3/values/property_generic_representer.rb b/lib/api/v3/values/property_generic_representer.rb new file mode 100644 index 000000000000..ed814ef2fa38 --- /dev/null +++ b/lib/api/v3/values/property_generic_representer.rb @@ -0,0 +1,33 @@ +# --copyright +# OpenProject is an open source project management software. +# Copyright (C) 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 API::V3::Values + class PropertyGenericRepresenter < PropertyRepresenter + property :value + end +end diff --git a/lib/api/v3/values/schemas/value_schema_factory.rb b/lib/api/v3/values/schemas/value_schema_factory.rb index de98a68e279e..441bb6734729 100644 --- a/lib/api/v3/values/schemas/value_schema_factory.rb +++ b/lib/api/v3/values/schemas/value_schema_factory.rb @@ -29,7 +29,7 @@ module API::V3::Values::Schemas module ValueSchemaFactory extend ::API::V3::Utilities::PathHelper - SUPPORTED = %w(start_date due_date date).freeze + SUPPORTED = %w(start_date due_date date note).freeze module_function @@ -64,10 +64,13 @@ def i18n_for(property) I18n.t("attributes.#{property}") end - def type_for(_property) - # This is but a stub. Currently, only 'start_date' and 'due_date' - # need to be supported so this simple approach works. - "Date" + def type_for(property) + case property + when "start_date", "due_date", "date" + "Date" + when "note" + "String" + end end end end diff --git a/spec/lib/api/v3/notifications/notification_representer_rendering_spec.rb b/spec/lib/api/v3/notifications/notification_representer_rendering_spec.rb index c0fb12c42cbd..bb3aef99788e 100644 --- a/spec/lib/api/v3/notifications/notification_representer_rendering_spec.rb +++ b/spec/lib/api/v3/notifications/notification_representer_rendering_spec.rb @@ -267,5 +267,60 @@ .at_path("_embedded/details") end end + + shared_examples_for "embeds a Values::Property for reminder note" do + it "embeds a Values::Property" do + expect(generated) + .to be_json_eql("Values::Property".to_json) + .at_path("_embedded/details/0/_type") + end + + it "has a note value for the `property` property" do + expect(generated) + .to be_json_eql("note".to_json) + .at_path("_embedded/details/0/property") + end + + it "has a reminder`s note for the value" do + expect(generated) + .to be_json_eql(notification.reminder.note.to_json) + .at_path("_embedded/details/0/value") + end + end + + context "for a reminder when embedding" do + let(:reminder) { build_stubbed(:reminder) } + let(:reason) { :reminder } + let(:embed_links) { true } + + before do + allow(notification).to receive(:reminder).and_return(reminder) + end + + it_behaves_like "embeds a Values::Property for reminder note" + end + + context "for a reminder when not embedding" do + let(:reminder) { build_stubbed(:reminder) } + let(:reason) { :reminder } + let(:embed_links) { false } + + before do + allow(notification).to receive(:reminder).and_return(reminder) + end + + it_behaves_like "embeds a Values::Property for reminder note" + end + + context "for a reminder with no notification" do + let(:reminder) { nil } + let(:reason) { :reminder } + + it "has an empty details array" do + expect(generated) + .to have_json_size(0) + .at_path("_embedded/details") + end + end end end