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 2, 2023
1 parent 17a71eb commit 650be8b
Show file tree
Hide file tree
Showing 9 changed files with 64 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
9 changes: 9 additions & 0 deletions db/migrate/20231017100652_add_browser_traitements_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddBrowserTraitementsIndex < ActiveRecord::Migration[7.0]
disable_ddl_transaction!

def change
add_index :traitements, :browser_name, algorithm: :concurrently
add_index :traitements, :browser_version, algorithm: :concurrently
add_index :traitements, :browser_supported, algorithm: :concurrently
end
end
8 changes: 7 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,13 +979,19 @@
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"
t.boolean "process_expired"
t.boolean "process_expired_migrated", default: false
t.datetime "processed_at", precision: 6
t.string "state"
t.index ["browser_name"], name: "index_traitements_on_browser_name"
t.index ["browser_supported"], name: "index_traitements_on_browser_supported"
t.index ["browser_version"], name: "index_traitements_on_browser_version"
t.index ["dossier_id"], name: "index_traitements_on_dossier_id"
t.index ["process_expired"], name: "index_traitements_on_process_expired"
end
Expand Down

0 comments on commit 650be8b

Please sign in to comment.