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

Add non-working day show api endpoint

Dombi Attilae92253aeb70d
diff --git a/app/models/non_working_day.rb b/app/models/non_working_day.rb
new file mode 100644
index 00000000000..b008a9103df
--- /dev/null
+++ b/app/models/non_working_day.rb
@@ -0,0 +1,4 @@
+class NonWorkingDay < ApplicationRecord
+ validates :name, :date, presence: true
+ validates :date, uniqueness: true
+end
diff --git a/db/migrate/20220517113828_create_non_working_days.rb b/db/migrate/20220517113828_create_non_working_days.rb
new file mode 100644
index 00000000000..6200142545c
--- /dev/null
+++ b/db/migrate/20220517113828_create_non_working_days.rb
@@ -0,0 +1,11 @@
+class CreateNonWorkingDays < ActiveRecord::Migration[6.1]
+ def change
+ create_table :non_working_days do |t|
+ t.string :name, null: false
+ t.date :date, null: false
+
+ t.timestamps
+ end
+ add_index :non_working_days, :date, unique: true
+ end
+end
diff --git a/lib/api/v3/days/days_api.rb b/lib/api/v3/days/days_api.rb
index efc828d06ea..cfbcf40ca7c 100644
--- a/lib/api/v3/days/days_api.rb
+++ b/lib/api/v3/days/days_api.rb
@@ -32,6 +32,7 @@ module API::V3::Days
resources :days do
mount WeekAPI
+ mount NonWorkingDaysAPI
end
end
end
diff --git a/lib/api/v3/days/non_working_day_representer.rb b/lib/api/v3/days/non_working_day_representer.rb
new file mode 100644
index 00000000000..4785c083ed5
--- /dev/null
+++ b/lib/api/v3/days/non_working_day_representer.rb
@@ -0,0 +1,43 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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::Days
+ class NonWorkingDayRepresenter < ::API::Decorators::Single
+ include ::API::Decorators::DateProperty
+ include ::API::Caching::CachedRepresenter
+
+ property :name
+ date_property :date
+
+ self_link path: :days_non_working_day, id_attribute: :date
+
+ def _type
+ 'NonWorkingDay'
+ end
+ end
+end
diff --git a/lib/api/v3/days/non_working_days_api.rb b/lib/api/v3/days/non_working_days_api.rb
new file mode 100644
index 00000000000..7233152d268
--- /dev/null
+++ b/lib/api/v3/days/non_working_days_api.rb
@@ -0,0 +1,45 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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::Days
+ class NonWorkingDaysAPI < ::API::OpenProjectAPI
+ helpers ::API::Utilities::UrlPropsParsingHelper
+
+ resources :non_working do
+ route_param :date, type: Date, desc: 'NonWorkingDay DATE' do
+ after_validation do
+ @non_working_day = NonWorkingDay.find_by!(date: declared_params[:date])
+ end
+
+ get &::API::V3::Utilities::Endpoints::Show.new(model: NonWorkingDay,
+ render_representer: NonWorkingDayRepresenter)
+ .mount
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index f998da95d6d..18c3061c911 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -200,6 +200,14 @@ module API
"#{days_week}/#{day}"
end
+ def self.days_non_working
+ "#{root}/days/non_working"
+ end
+
+ def self.days_non_working_day(date)
+ "#{days_non_working}/#{date}"
+ end
+
index :help_text
show :help_text
diff --git a/spec/factories/non_working_day_factory.rb b/spec/factories/non_working_day_factory.rb
new file mode 100644
index 00000000000..305b75b7304
--- /dev/null
+++ b/spec/factories/non_working_day_factory.rb
@@ -0,0 +1,6 @@
+FactoryBot.define do
+ factory :non_working_day do
+ name { "MyString" }
+ sequence(:date) { |n| Time.zone.today + n.day }
+ end
+end
diff --git a/spec/lib/api/v3/days/non_working_day_representer_spec.rb b/spec/lib/api/v3/days/non_working_day_representer_spec.rb
new file mode 100644
index 00000000000..13632928b65
--- /dev/null
+++ b/spec/lib/api/v3/days/non_working_day_representer_spec.rb
@@ -0,0 +1,105 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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'
+
+describe ::API::V3::Days::NonWorkingDayRepresenter do
+ let(:non_working_day) { build_stubbed(:non_working_day, name: "Christmas day", date: Date.tomorrow) }
+ let(:representer) { described_class.new(non_working_day, current_user: instance_double(User, name: 'current_user')) }
+
+ describe '#to_json' do
+ subject(:generated) { representer.to_json }
+
+ it 'has _type: NonWorkingDay' do
+ expect(subject).to be_json_eql('NonWorkingDay'.to_json).at_path('_type')
+ end
+
+ it 'has name string property' do
+ expect(subject).to have_json_type(String).at_path('name')
+ expect(subject).to be_json_eql(non_working_day.name.to_json).at_path('name')
+ end
+
+ it_behaves_like 'has ISO 8601 date only' do
+ let(:date) { non_working_day.date }
+ let(:json_path) { 'date' }
+ end
+
+ describe '_links' do
+ it 'is present' do
+ expect(subject).to have_json_type(Object).at_path('_links')
+ end
+
+ describe 'self' do
+ it 'links to this resource' do
+ expected_json = {
+ href: "/api/v3/days/non_working/#{non_working_day.date}",
+ title: non_working_day.name
+ }.to_json
+ expect(subject).to be_json_eql(expected_json).at_path('_links/self')
+ end
+ end
+ end
+ end
+
+ describe 'caching' do
+ it 'is based on the representer\'s json_cache_key' do
+ allow(OpenProject::Cache)
+ .to receive(:fetch)
+ .and_call_original
+
+ representer.to_json
+
+ expect(OpenProject::Cache)
+ .to have_received(:fetch)
+ .with(representer.json_cache_key)
+ end
+
+ describe '#json_cache_key' do
+ let!(:former_cache_key) { representer.json_cache_key }
+
+ it 'includes the name of the representer class' do
+ expect(representer.json_cache_key)
+ .to include('API', 'V3', 'Days', 'NonWorkingDayRepresenter')
+ end
+
+ it 'changes when the locale changes' do
+ I18n.with_locale(:fr) do
+ expect(representer.json_cache_key)
+ .not_to eql former_cache_key
+ end
+ end
+
+ it 'changes when the non_working_day is updated' do
+ non_working_day.updated_at = 20.seconds.from_now
+
+ expect(representer.json_cache_key)
+ .not_to eql former_cache_key
+ end
+ end
+ end
+end
diff --git a/spec/models/non_working_day_spec.rb b/spec/models/non_working_day_spec.rb
new file mode 100644
index 00000000000..aa322e7bba0
--- /dev/null
+++ b/spec/models/non_working_day_spec.rb
@@ -0,0 +1,30 @@
+require 'rails_helper'
+
+RSpec.describe NonWorkingDay, type: :model do
+ subject { build(:non_working_day) }
+
+ describe 'validations' do
+ it 'is valid when all attributes are present' do
+ expect(subject).to be_valid
+ end
+
+ it 'is invalid without name' do
+ subject.name = nil
+ expect(subject).to be_invalid
+ expect(subject.errors[:name]).to be_present
+ end
+
+ it 'is invalid without date' do
+ subject.date = nil
+ expect(subject).to be_invalid
+ expect(subject.errors[:date]).to be_present
+ end
+
+ it 'is invalid with an already existing date' do
+ existing = create(:non_working_day)
+ subject.date = existing.date
+ expect(subject).to be_invalid
+ expect(subject.errors[:date]).to be_present
+ end
+ end
+end
diff --git a/spec/requests/api/v3/days/non_working_days_show_resource_spec.rb b/spec/requests/api/v3/days/non_working_days_show_resource_spec.rb
new file mode 100644
index 00000000000..024d65205f9
--- /dev/null
+++ b/spec/requests/api/v3/days/non_working_days_show_resource_spec.rb
@@ -0,0 +1,85 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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'
+
+describe ::API::V3::Days::NonWorkingDaysAPI,
+ 'show',
+ content_type: :json,
+ type: :request do
+ include API::V3::Utilities::PathHelper
+
+ let(:non_working_day) { create(:non_working_day) }
+ let(:path) { api_v3_paths.days_non_working_day(non_working_day.date) }
+
+ current_user { user }
+ subject { last_response.body }
+
+ before do
+ get path
+ end
+
+ context 'for an admin user' do
+ let(:user) { build(:admin) }
+
+ it_behaves_like 'successful response'
+
+ it 'responds with the correct day' do
+ expect(subject).to be_json_eql('NonWorkingDay'.to_json).at_path('_type')
+ expect(subject).to be_json_eql(non_working_day.date.to_json).at_path('date')
+ end
+
+ context 'when requesting nonexistent date' do
+ let(:path) { api_v3_paths.days_non_working_day(Time.zone.today) }
+
+ it_behaves_like 'not found'
+ end
+
+ context 'when requesting an incorrect date' do
+ let(:path) { api_v3_paths.days_non_working_day("incorrect") }
+
+ it_behaves_like 'param validation error' do
+ let(:id) { 'incorrect' }
+ end
+ end
+ end
+
+ context 'for a not logged in user' do
+ let(:user) { build(:anonymous) }
+
+ it_behaves_like 'successful response'
+
+ it 'responds with the correct _type' do
+ expect(subject).to be_json_eql('NonWorkingDay'.to_json).at_path('_type')
+ end
+
+ it 'responds with the correct day' do
+ expect(subject).to be_json_eql('NonWorkingDay'.to_json).at_path('_type')
+ expect(subject).to be_json_eql(non_working_day.date.to_json).at_path('date')
+ end
+ end
+end
diff --git a/spec/requests/api/v3/days/show_resource_spec.rb b/spec/requests/api/v3/days/week_show_resource_spec.rb
similarity index 100%
rename from spec/requests/api/v3/days/show_resource_spec.rb
rename to spec/requests/api/v3/days/week_show_resource_spec.rb