Skip to content

Commit

Permalink
feat(traitement): add browser information
Browse files Browse the repository at this point in the history
  • Loading branch information
tchak committed Nov 7, 2023
1 parent 234c6e6 commit 1afd995
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 14 deletions.
4 changes: 4 additions & 0 deletions app/controllers/api/v2/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/current.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Current < ActiveSupport::CurrentAttributes
attribute :user, :request_id
attribute :user, :request_id, :browser
end
31 changes: 19 additions & 12 deletions app/models/dossier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions app/models/traitement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions db/migrate/20231017092437_add_browser_to_traitements.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -979,6 +979,9 @@
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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,9 @@ def cursor_for(item)
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
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/users/dossiers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@
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
Expand Down

0 comments on commit 1afd995

Please sign in to comment.