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
Add non-working days to the api/v3/days
Dombi Attila54eca7fddb09
diff --git a/app/models/day.rb b/app/models/day.rbindex f73b4258c48..ab62e303f27 100644--- a/app/models/day.rb+++ b/app/models/day.rb@@ -35,37 +35,50 @@ class Day < ApplicationRecordforeign_key: :day_of_week,primary_key: :day+ has_many :non_working_days,+ inverse_of: false,+ class_name: 'NonWorkingDay',+ foreign_key: :date,+ primary_key: :date+attribute :date, :date, default: nilattribute :day_of_week, :integer, default: nil- attribute :working, :boolean, default: 't'- delegate :name, to: :week_day+ delegate :name, to: :week_day, allow_nil: true- def self.default+ def self.default_scopetoday = Time.zone.todayfrom = today.at_beginning_of_monthto = today.next_month.at_end_of_month- select('days.*')- .includes(:week_day)- .from(Arel.sql(from_sql(from:, to:)))+ from(Arel.sql(from_sql(from:, to:)))+ .includes(:week_day)+ .includes(:non_working_days)+ .order(:date)enddef self.from_sql(from:, to:)<<~SQL.squish- (- SELECT- date_trunc('day', dd)::date date,- extract(isodow from dd) day_of_week,- week_days.working- FROM generate_series- ( '#{from}'::timestamp,+ (SELECT+ to_char(dd, 'YYYYMMDD')::integer id,+ date_trunc('day', dd)::date date,+ extract(isodow from dd) day_of_week+ FROM+ generate_series( '#{from}'::timestamp,'#{to}'::timestamp,'1 day'::interval) dd- LEFT JOIN week_days- ON extract(isodow from dd) = week_days.day- ORDER BY date) daysSQLend++ def working+ week_day&.working && non_working_days.empty?+ end++ ##+ # Since the base table is a generated series of dates that cannot be modified+ # we should mark the records readonly.+ def readonly?+ true+ endenddiff --git a/app/models/queries/days/day_query.rb b/app/models/queries/days/day_query.rbindex 996c04e9f80..f026b7ca8ba 100644--- a/app/models/queries/days/day_query.rb+++ b/app/models/queries/days/day_query.rb@@ -32,7 +32,7 @@ class Queries::Days::DayQuery < Queries::BaseQueryenddef default_scope- Day.default+ Day.default_scopeend##diff --git a/lib/api/v3/days/day_representer.rb b/lib/api/v3/days/day_representer.rbindex 08b0599c513..26566ade244 100644--- a/lib/api/v3/days/day_representer.rb+++ b/lib/api/v3/days/day_representer.rb@@ -34,6 +34,23 @@ module API::V3::Daysself_link path: :day, id_attribute: :date+ links :nonWorkingReasons do+ next if represented.working++ links = []+ unless represented.week_day.working+ links << {+ title: represented.name,+ href: api_v3_paths.days_week_day(represented.week_day.day)+ }+ end++ represented.non_working_days.each do |day|+ links << { title: day.name, href: api_v3_paths.days_non_working_day(day.date) }+ end+ links+ end+def _type'Day'enddiff --git a/spec/factories/day_factory.rb b/spec/factories/day_factory.rbindex 38bb2fe93a4..65785e9a735 100644--- a/spec/factories/day_factory.rb+++ b/spec/factories/day_factory.rb@@ -32,6 +32,5 @@ FactoryBot.define do(1.year.ago + n.days).to_dateendday_of_week { date.wday }- working { date.wday < 6 }endenddiff --git a/spec/lib/api/v3/days/day_representer_spec.rb b/spec/lib/api/v3/days/day_representer_spec.rbindex 689254913cc..70b807a5f63 100644--- a/spec/lib/api/v3/days/day_representer_spec.rb+++ b/spec/lib/api/v3/days/day_representer_spec.rb@@ -29,46 +29,94 @@require 'spec_helper'describe ::API::V3::Days::DayRepresenter do+ let(:working) { true }let(:day) do- build(:day, date: Date.new(2022, 12, 27), week_day: create(:week_day, :tuesday), working: true)+ build(:day, date: Date.new(2022, 12, 27), week_day: create(:week_day, :tuesday, working:))endlet(:current_user) { instance_double(User, name: 'current_user') }let(:representer) { described_class.new(day, current_user:) }- describe '#to_json' do- subject(:generated) { representer.to_json }+ subject(:generated) { representer.to_json }- it 'has _type: Day' do- expect(subject).to be_json_eql('Day'.to_json).at_path('_type')- end+ it 'has _type: Day' do+ expect(subject).to be_json_eql('Day'.to_json).at_path('_type')+ end- it 'has date property' do- expect(subject).to have_json_type(String).at_path('date')- expect(subject).to be_json_eql('2022-12-27'.to_json).at_path('date')- end+ it 'has date property' do+ expect(subject).to have_json_type(String).at_path('date')+ expect(subject).to be_json_eql('2022-12-27'.to_json).at_path('date')+ end++ it 'has name string property' do+ expect(subject).to have_json_type(String).at_path('name')+ expect(subject).to be_json_eql(day.name.to_json).at_path('name')+ end- it 'has name string property' do- expect(subject).to have_json_type(String).at_path('name')- expect(subject).to be_json_eql(day.name.to_json).at_path('name')+ it 'has working boolean property' do+ expect(subject).to have_json_type(TrueClass).at_path('working')+ expect(subject).to be_json_eql(day.working.to_json).at_path('working')+ end++ describe '_links' do+ it 'is present' do+ expect(subject).to have_json_type(Object).at_path('_links')end- it 'has working boolean property' do- expect(subject).to have_json_type(TrueClass).at_path('working')- expect(subject).to be_json_eql(day.working.to_json).at_path('working')+ describe 'self' do+ it 'links to this resource' do+ expected_json = {+ href: '/api/v3/days/2022-12-27',+ title: 'Tuesday'+ }.to_json+ expect(subject).to be_json_eql(expected_json).at_path('_links/self')+ endend- describe '_links' do- it 'is present' do- expect(subject).to have_json_type(Object).at_path('_links')+ describe 'nonWorkingReasons' do+ context 'when the day has working true' do+ it { is_expected.not_to have_json_path('_links/nonWorkingReasons') }+ end++ context 'when day has working false' do+ let(:working) { false }++ it 'links to the day resource' do+ expected_json = [{+ href: '/api/v3/days/week/2',+ title: 'Tuesday'+ }].to_json++ expect(subject).to be_json_eql(expected_json).at_path('_links/nonWorkingReasons')+ endend- describe 'self' do- it 'links to this resource' do- expected_json = {- href: '/api/v3/days/2022-12-27',+ context 'when a non-working day is present' do+ let!(:non_working_day) { create(:non_working_day, date: day.date) }++ it 'links to the non-working day resource' do+ expected_json = [{+ href: "/api/v3/days/non_working/2022-12-27",+ title: non_working_day.name+ }].to_json++ expect(subject).to be_json_eql(expected_json).at_path('_links/nonWorkingReasons')+ end+ end++ context 'when the day has working false and a non-working day is present' do+ let(:working) { false }+ let!(:non_working_day) { create(:non_working_day, date: day.date) }++ it 'links to the day resource and to the non-working day resource' do+ expected_json = [{+ href: '/api/v3/days/week/2',title: 'Tuesday'- }.to_json- expect(subject).to be_json_eql(expected_json).at_path('_links/self')+ }, {+ href: "/api/v3/days/non_working/2022-12-27",+ title: non_working_day.name+ }].to_json++ expect(subject).to be_json_eql(expected_json).at_path('_links/nonWorkingReasons')endendenddiff --git a/spec/models/day_spec.rb b/spec/models/day_spec.rbnew file mode 100644index 00000000000..2df16d929de--- /dev/null+++ b/spec/models/day_spec.rb@@ -0,0 +1,99 @@+require 'spec_helper'++describe Day, type: :model do+ let(:today) { Date.current }++ subject { described_class.new(date: Date.new(2022, 1, 1), day_of_week: 6) }++ it { is_expected.to be_readonly }+ it { is_expected.to respond_to :id }+ it { is_expected.to respond_to :date }+ it { is_expected.to respond_to :day_of_week }+ it { is_expected.to respond_to :name }++ context 'with a collection' do+ let(:days) { described_class.default_scope }++ it 'returns a default date range' do+ expect(days.minmax.pluck(:date)).to eq(+ [today.at_beginning_of_month, today.next_month.at_end_of_month]+ )+ end++ it 'eager loads week_day relation' do+ expect(days).to(be_all { |d| d.association(:week_day).loaded? })+ end++ it 'eager loads non_working_days relation' do+ expect(days).to(be_all { |d| d.association(:non_working_days).loaded? })+ end++ it 'loads the id attribute' do+ expect(days.first.id).to eq(today.at_beginning_of_month.strftime('%Y%m%d').to_i)+ end++ it 'loads the date attribute' do+ expect(days.first.date).to eq(today.at_beginning_of_month)+ end++ it 'loads the day_of_week attribute' do+ expect(days.first.day_of_week % 7).to eq(today.at_beginning_of_month.wday) # wday is from 0-6+ end++ it 'does not have a name' do+ expect(days.first.name).to be_nil+ end+ end++ context 'with the weekday present' do+ before do+ create(:week_day, day: 6)+ end++ it 'loads the name attribute' do+ expect(subject.name).to eq('Saturday')+ end+ end++ describe '#working' do+ context 'when the week day is non-working' do+ before do+ create(:week_day, day: 6, working: false)+ end++ it 'is false' do+ expect(subject.working).to be_falsy+ end++ context 'with a non-working day' do+ before do+ create(:non_working_day, date: subject.date)+ end++ it 'is false' do+ expect(subject.working).to be_falsy+ end+ end+ end++ context 'when the week day is working' do+ before do+ create(:week_day, day: 6, working: true)+ end++ it 'is true' do+ expect(subject.working).to be_truthy+ end++ context 'with a non working day' do+ before do+ create(:non_working_day, date: subject.date)+ end++ it 'is false' do+ expect(subject.working).to be_falsy+ end+ end+ end+ end+enddiff --git a/spec/models/queries/days/day_query_spec.rb b/spec/models/queries/days/day_query_spec.rbindex 9db6195b74e..e52e4b85a34 100644--- a/spec/models/queries/days/day_query_spec.rb+++ b/spec/models/queries/days/day_query_spec.rb@@ -30,7 +30,7 @@ require 'spec_helper'describe Queries::Days::DayQuery, type: :model dolet(:instance) { described_class.new }- let(:base_scope) { Day.default.order(date: :asc) }+ let(:base_scope) { Day.order(date: :asc) }let(:current_user) { build_stubbed(:admin) }before do@@ -63,7 +63,7 @@ describe Queries::Days::DayQuery, type: :model docontext 'with dates within the default range' dolet(:from) { Time.zone.today }let(:to) { 5.days.from_now.to_date }- let(:base_scope) { Day.default.from(Day.from_sql(from:, to:)).order(date: :asc) }+ let(:base_scope) { Day.from(Day.from_sql(from:, to:)).order(date: :asc) }it 'is the same as handwriting the query' do# Expectation has to be weirdly specific to the logic of Queries::Operators::DateRangeClauses