From d93c6241647cb18a14b788239bee1871e652e4a4 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 17 Oct 2023 12:00:16 +0200 Subject: [PATCH] feat(traitement): add browser information --- app/controllers/api/v2/base_controller.rb | 4 +++ app/controllers/api_controller.rb | 4 +++ app/controllers/application_controller.rb | 1 + app/models/current.rb | 2 +- app/models/dossier.rb | 31 ++++++++++++------- app/models/traitement.rb | 12 +++++++ ...231017092437_add_browser_to_traitements.rb | 7 +++++ db/schema.rb | 5 ++- .../graphql_controller_stored_queries_spec.rb | 3 ++ .../users/dossiers_controller_spec.rb | 1 + 10 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20231017092437_add_browser_to_traitements.rb diff --git a/app/controllers/api/v2/base_controller.rb b/app/controllers/api/v2/base_controller.rb index aafcc0aa3..294844c87 100644 --- a/app/controllers/api/v2/base_controller.rb +++ b/app/controllers/api/v2/base_controller.rb @@ -3,6 +3,10 @@ class API::V2::BaseController < ApplicationController skip_before_action :setup_tracking before_action :authenticate_from_token + before_action do + Current.browser = 'api' + end + private def context diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 928ad5106..cc5927194 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -2,6 +2,10 @@ class APIController < ApplicationController before_action :default_format_json before_action :authenticate_from_token + before_action do + Current.browser = 'api' + end + private def default_format_json diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 17600d1e0..bfc334d69 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -27,6 +27,7 @@ class ApplicationController < ActionController::Base before_action do Current.request_id = request.uuid Current.user = current_user + Current.browser = browser end def staging_authenticate diff --git a/app/models/current.rb b/app/models/current.rb index a6f27d411..c32b02f6d 100644 --- a/app/models/current.rb +++ b/app/models/current.rb @@ -1,3 +1,3 @@ class Current < ActiveSupport::CurrentAttributes - attribute :user, :request_id + attribute :user, :request_id, :browser end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index f01a109a4..ed858ddab 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -64,49 +64,56 @@ class Dossier < ApplicationRecord def passer_en_construction(instructeur: nil, processed_at: Time.zone.now) build(state: Dossier.states.fetch(:en_construction), instructeur_email: instructeur&.email, - processed_at: processed_at) + processed_at:, + browser: Current.browser) end def submit_en_construction(processed_at: Time.zone.now) - build(state: Dossier.states.fetch(:en_construction), processed_at:) + build(state: Dossier.states.fetch(:en_construction), + processed_at:, + browser: Current.browser) end def passer_en_instruction(instructeur: nil, processed_at: Time.zone.now) build(state: Dossier.states.fetch(:en_instruction), instructeur_email: instructeur&.email, - processed_at: processed_at) + processed_at:, + browser: Current.browser) end def accepter_automatiquement(processed_at: Time.zone.now) build(state: Dossier.states.fetch(:accepte), - processed_at: processed_at) + processed_at:) end def accepter(motivation: nil, instructeur: nil, processed_at: Time.zone.now) build(state: Dossier.states.fetch(:accepte), instructeur_email: instructeur&.email, - motivation: motivation, - processed_at: processed_at) + motivation:, + processed_at:, + browser: Current.browser) end def refuser(motivation: nil, instructeur: nil, processed_at: Time.zone.now) build(state: Dossier.states.fetch(:refuse), instructeur_email: instructeur&.email, - motivation: motivation, - processed_at: processed_at) + motivation:, + processed_at:, + browser: Current.browser) end def refuser_automatiquement(processed_at: Time.zone.now, motivation:) build(state: Dossier.states.fetch(:refuse), - motivation: motivation, - processed_at: processed_at) + motivation:, + processed_at:) end def classer_sans_suite(motivation: nil, instructeur: nil, processed_at: Time.zone.now) build(state: Dossier.states.fetch(:sans_suite), instructeur_email: instructeur&.email, - motivation: motivation, - processed_at: processed_at) + motivation:, + processed_at:, + browser: Current.browser) end end has_one :traitement, -> { order(processed_at: :desc) }, inverse_of: false diff --git a/app/models/traitement.rb b/app/models/traitement.rb index 6c407a154..efff0bbfd 100644 --- a/app/models/traitement.rb +++ b/app/models/traitement.rb @@ -13,4 +13,16 @@ class Traitement < ApplicationRecord .where.not(processed_at: nil) .order(:processed_at) end + + def browser=(browser) + if browser == 'api' + self.browser_name = browser + self.browser_version = 2 + self.browser_supported = true + elsif browser.present? + self.browser_name = browser.name + self.browser_version = browser.version + self.browser_supported = BrowserSupport.supported?(browser) + end + end end diff --git a/db/migrate/20231017092437_add_browser_to_traitements.rb b/db/migrate/20231017092437_add_browser_to_traitements.rb new file mode 100644 index 000000000..22997922b --- /dev/null +++ b/db/migrate/20231017092437_add_browser_to_traitements.rb @@ -0,0 +1,7 @@ +class AddBrowserToTraitements < ActiveRecord::Migration[7.0] + def change + add_column :traitements, :browser_name, :string, null: true + add_column :traitements, :browser_version, :integer, null: true + add_column :traitements, :browser_supported, :boolean, null: true # rubocop:disable Rails/ThreeStateBooleanColumn + end +end diff --git a/db/schema.rb b/db/schema.rb index c7a9948ac..20bae5558 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_10_25_161609) do +ActiveRecord::Schema[7.0].define(version: 2023_10_26_161609) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -979,6 +979,9 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_25_161609) do end create_table "traitements", force: :cascade do |t| + t.string "browser_name" + t.boolean "browser_supported" + t.integer "browser_version" t.bigint "dossier_id" t.string "instructeur_email" t.string "motivation" diff --git a/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb b/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb index 4d760601e..380007607 100644 --- a/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb +++ b/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb @@ -964,6 +964,9 @@ describe API::V2::GraphqlController do expect(gql_data[:dossierAccepter][:dossier][:state]).to eq('accepte') perform_enqueued_jobs expect(ActionMailer::Base.deliveries.size).to eq(1) + + expect(dossier.traitements.last.browser_name).to eq('api') + expect(dossier.traitements.last.browser_version).to eq(2) } context 'without notifications' do diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index e030fb3c9..29eb3fb49 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -481,6 +481,7 @@ describe Users::DossiersController, type: :controller do expect(dossier).to be_en_instruction expect(dossier.pending_correction?).to be_falsey expect(dossier.en_instruction_at).to within(5.seconds).of(Time.current) + expect(dossier.traitements.last.browser_name).to eq('Unknown Browser') end end end