From ac6f5ba0250fe6b1f8e451684bad5bb6d998ccab Mon Sep 17 00:00:00 2001 From: mfo Date: Thu, 14 Nov 2024 14:04:33 +0100 Subject: [PATCH] feat(export.datetime): ensure to cast date and time --- app/models/concerns/columns_concern.rb | 2 +- app/services/dossier_filter_service.rb | 2 +- .../procedure_export_service_tabular_spec.rb | 29 ++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/models/concerns/columns_concern.rb b/app/models/concerns/columns_concern.rb index c52b52353..0a4a98299 100644 --- a/app/models/concerns/columns_concern.rb +++ b/app/models/concerns/columns_concern.rb @@ -109,7 +109,7 @@ module ColumnsConcern def dossier_dates_columns ['created_at', 'updated_at', 'last_champ_updated_at', 'depose_at', 'en_construction_at', 'en_instruction_at', 'processed_at'] - .map { |column| dossier_col(table: 'self', column:, type: :date) } + .map { |column| dossier_col(table: 'self', column:, type: :datetime) } end def email_column diff --git a/app/services/dossier_filter_service.rb b/app/services/dossier_filter_service.rb index a322dc8dd..ce32fefc2 100644 --- a/app/services/dossier_filter_service.rb +++ b/app/services/dossier_filter_service.rb @@ -81,7 +81,7 @@ class DossierFilterService else case table when 'self' - if filtered_column.type == :date + if filtered_column.type == :date || filtered_column.type == :datetime dates = values .filter_map { |v| Time.zone.parse(v).beginning_of_day rescue nil } diff --git a/spec/services/procedure_export_service_tabular_spec.rb b/spec/services/procedure_export_service_tabular_spec.rb index 1ebeb64f0..3bff4c42d 100644 --- a/spec/services/procedure_export_service_tabular_spec.rb +++ b/spec/services/procedure_export_service_tabular_spec.rb @@ -135,12 +135,39 @@ describe ProcedureExportService do it { expect(dossiers_sheet.data.last.last).to eq 42.0 } end - context 'with having TypesDeChamp::LinkedDropDownListTypeDeChamp' do + context 'with TypesDeChamp::LinkedDropDownListTypeDeChamp' do let(:types_de_champ_public) { [{ type: :linked_drop_down_list, libelle: "linked_drop_down_list", mandatory: true }] } let(:exported_columns) { [ExportedColumn.new(libelle: 'linked_drop_down_list', column: procedure.find_column(label: 'linked_drop_down_list'))] } before { create(:dossier, :with_populated_champs, procedure:) } it { expect(dossiers_sheet.data.last.last).to eq "primary / secondary" } end + + context 'with TypesDeChamp::DateTimeTypeDeChamp' do + let(:types_de_champ_public) { [{ type: :datetime, libelle: "datetime", mandatory: true }] } + let(:exported_columns) { [ExportedColumn.new(libelle: 'datetime', column: procedure.find_column(label: 'datetime'))] } + let(:dossier) { create(:dossier, :with_populated_champs, procedure:) } + before { dossier } + it do + champ_value = Time.zone.parse(dossier.champs.first.value) + offset = champ_value.utc_offset + sheet_value = Time.zone.at(dossiers_sheet.data.last.last - offset.seconds) + expect(sheet_value).to eq(champ_value.round) + end + end + + context 'with TypesDeChamp::Date' do + let(:types_de_champ_public) { [{ type: :date, libelle: "date", mandatory: true }] } + let(:exported_columns) { [ExportedColumn.new(libelle: 'date', column: procedure.find_column(label: 'date'))] } + before { create(:dossier, :with_populated_champs, procedure:) } + it { expect(dossiers_sheet.data.last.last).to be_an_instance_of(Date) } + end + + context 'with DossierColumn as datetime' do + let(:types_de_champ_public) { [] } + let(:exported_columns) { [ExportedColumn.new(libelle: 'Date de passage en instruction', column: procedure.find_column(label: 'Date de passage en instruction'))] } + before { create(:dossier, :en_instruction, :with_populated_champs, procedure:) } + it { expect(dossiers_sheet.data.last.last).to be_an_instance_of(Time) } + end end describe 'Etablissement sheet' do