From ab777a790c4cf9cf14ae77ec74701e7c0bbc11e7 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 1 Apr 2022 18:20:12 +0200 Subject: [PATCH 1/4] fix(Dossier.processed_in_month): using PG between operator with a date does not play nicely when it is compared to DateTime in postgres. meaning given a Date as 2022/03/31, when compared with a DateTime in postgres, the casting of 2022/03/31 to Datetime becomes 2022/03/31 00:00. So we skiped all dossiers from last date in month --- app/models/dossier.rb | 4 ++-- spec/models/dossier_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 4a3109c90..53e3bebc0 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -233,10 +233,10 @@ class Dossier < ApplicationRecord scope :en_instruction, -> { not_archived.state_en_instruction } scope :termine, -> { not_archived.state_termine } - scope :processed_in_month, -> (month) do + scope :processed_in_month, -> (date) do state_termine .joins(:traitements) - .where(traitements: { processed_at: month.beginning_of_month..month.end_of_month }) + .where(traitements: { processed_at: date.beginning_of_month..date.end_of_month }) end scope :downloadable_sorted, -> { state_not_brouillon diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 4dcc91063..ce5249bc6 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1720,4 +1720,29 @@ describe Dossier do expect(rebased_datetime_champ.rebased_at).not_to be_nil end end + + describe '#processed_in_month' do + include ActiveSupport::Testing::TimeHelpers + + let(:dossier_accepte_at) { DateTime.new(2022, 3, 31, 12, 0) } + before do + travel_to(dossier_accepte_at) do + dossier = create(:dossier, :accepte) + end + end + + context 'given a date' do + let(:archive_date) { Date.new(2022, 3, 1) } + it 'includes a dossier processed_at at last day of month' do + expect(Dossier.processed_in_month(archive_date).count).to eq(1) + end + end + + context 'given a datetime' do + let(:archive_date) { DateTime.new(2022, 3, 1, 12, 0) } + it 'includes a dossier processed_at at last day of month' do + expect(Dossier.processed_in_month(archive_date).count).to eq(1) + end + end + end end From ec78340c06df620ae378d43abc7e9d564541f0f8 Mon Sep 17 00:00:00 2001 From: Martin Date: Sat, 2 Apr 2022 06:53:30 +0200 Subject: [PATCH 2/4] fix(Dossier.processed_in_month): recast date to datetime in scope --- app/models/dossier.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 53e3bebc0..fbef6d1a3 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -234,6 +234,7 @@ class Dossier < ApplicationRecord scope :termine, -> { not_archived.state_termine } scope :processed_in_month, -> (date) do + date = date.to_datetime state_termine .joins(:traitements) .where(traitements: { processed_at: date.beginning_of_month..date.end_of_month }) From d6d9db9721b01ec92abda968c21b8750c4a17418 Mon Sep 17 00:00:00 2001 From: Martin Date: Sat, 2 Apr 2022 09:04:09 +0200 Subject: [PATCH 3/4] tech(Sorbet): prepare usage of sorbet to avoid defensive programming style [casting date to datetime] --- Gemfile | 2 ++ Gemfile.lock | 14 ++++++++++++++ app/models/dossier.rb | 4 +++- sorbet/config | 3 +++ spec/rails_helper.rb | 1 + 5 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 sorbet/config diff --git a/Gemfile b/Gemfile index e210be466..37f48fab8 100644 --- a/Gemfile +++ b/Gemfile @@ -120,6 +120,8 @@ group :development do end group :development, :test do + gem 'sorbet' + gem 'sorbet-runtime' gem 'graphql-schema_comparator' gem 'mina', require: false # Deploy gem 'pry-byebug' # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index b91f5e62a..5737b95dc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -677,6 +677,18 @@ GEM tilt (~> 2.0) skylight (5.0.1) activesupport (>= 5.2.0) + sorbet (0.5.9827) + sorbet-static (= 0.5.9827) + sorbet-runtime (0.5.9827) + sorbet-static (0.5.9827-universal-darwin-14) + sorbet-static (0.5.9827-universal-darwin-15) + sorbet-static (0.5.9827-universal-darwin-16) + sorbet-static (0.5.9827-universal-darwin-17) + sorbet-static (0.5.9827-universal-darwin-18) + sorbet-static (0.5.9827-universal-darwin-19) + sorbet-static (0.5.9827-universal-darwin-20) + sorbet-static (0.5.9827-universal-darwin-21) + sorbet-static (0.5.9827-x86_64-linux) spreadsheet_architect (4.1.0) axlsx_styler (>= 1.0.0, < 2) caxlsx (>= 2.0.2, < 4) @@ -874,6 +886,8 @@ DEPENDENCIES sib-api-v3-sdk simple_xlsx_reader skylight + sorbet + sorbet-runtime spreadsheet_architect spring spring-commands-rspec diff --git a/app/models/dossier.rb b/app/models/dossier.rb index fbef6d1a3..f5991efc0 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -1,3 +1,4 @@ +# typed: true # == Schema Information # # Table name: dossiers @@ -38,6 +39,7 @@ # user_id :integer # class Dossier < ApplicationRecord + extend T::Sig self.ignored_columns = [:en_construction_conservation_extension] include DossierFilteringConcern include DossierRebaseConcern @@ -233,8 +235,8 @@ class Dossier < ApplicationRecord scope :en_instruction, -> { not_archived.state_en_instruction } scope :termine, -> { not_archived.state_termine } + sig { params(args: DateTime).returns(T.untyped) } scope :processed_in_month, -> (date) do - date = date.to_datetime state_termine .joins(:traitements) .where(traitements: { processed_at: date.beginning_of_month..date.end_of_month }) diff --git a/sorbet/config b/sorbet/config new file mode 100644 index 000000000..534118e61 --- /dev/null +++ b/sorbet/config @@ -0,0 +1,3 @@ +--dir +. +--ignore=/vendor/bundle diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index b58e5553d..e52754382 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -13,6 +13,7 @@ require 'rspec/rails' require 'axe-rspec' require 'devise' require 'shoulda-matchers' +require 'sorbet-runtime' # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are From c07e0fc13e8d6032a578c1a08608f7f94d272b53 Mon Sep 17 00:00:00 2001 From: Martin Date: Sat, 2 Apr 2022 09:05:27 +0200 Subject: [PATCH 4/4] fix(Dossier.processed_in_month): ensure proper usage via method sig instead of defensive programming style --- Gemfile | 4 ++-- app/services/procedure_archive_service.rb | 2 +- spec/models/dossier_spec.rb | 7 ------- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index 37f48fab8..6ed2f11a0 100644 --- a/Gemfile +++ b/Gemfile @@ -120,13 +120,13 @@ group :development do end group :development, :test do - gem 'sorbet' - gem 'sorbet-runtime' gem 'graphql-schema_comparator' gem 'mina', require: false # Deploy gem 'pry-byebug' # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'rspec-rails' gem 'simple_xlsx_reader' + gem 'sorbet' + gem 'sorbet-runtime' gem 'spring' # Spring speeds up development by keeping your application running in the background gem 'spring-commands-rspec' end diff --git a/app/services/procedure_archive_service.rb b/app/services/procedure_archive_service.rb index 31f0786f6..fc7162bff 100644 --- a/app/services/procedure_archive_service.rb +++ b/app/services/procedure_archive_service.rb @@ -22,7 +22,7 @@ class ProcedureArchiveService dossiers = if archive.time_span_type == 'everything' dossiers.state_termine else - dossiers.processed_in_month(archive.month) + dossiers.processed_in_month(archive.month.to_datetime) end attachments = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers) diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index ce5249bc6..130328b83 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1731,13 +1731,6 @@ describe Dossier do end end - context 'given a date' do - let(:archive_date) { Date.new(2022, 3, 1) } - it 'includes a dossier processed_at at last day of month' do - expect(Dossier.processed_in_month(archive_date).count).to eq(1) - end - end - context 'given a datetime' do let(:archive_date) { DateTime.new(2022, 3, 1, 12, 0) } it 'includes a dossier processed_at at last day of month' do