2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-01-07 15:45:02 +01:00
|
|
|
class Expert < ApplicationRecord
|
2022-03-15 17:28:22 +01:00
|
|
|
belongs_to :user
|
2021-02-09 10:24:13 +01:00
|
|
|
has_many :experts_procedures
|
2021-10-07 14:34:01 +02:00
|
|
|
has_many :procedures, through: :experts_procedures
|
2021-02-09 10:24:13 +01:00
|
|
|
has_many :avis, through: :experts_procedures
|
|
|
|
has_many :dossiers, through: :avis
|
2022-10-19 11:53:10 +02:00
|
|
|
has_many :commentaires, inverse_of: :expert, dependent: :nullify
|
2021-02-09 10:24:13 +01:00
|
|
|
|
|
|
|
default_scope { eager_load(:user) }
|
2021-01-25 13:01:13 +01:00
|
|
|
|
|
|
|
def email
|
|
|
|
user.email
|
|
|
|
end
|
2021-02-09 10:24:13 +01:00
|
|
|
|
|
|
|
def self.by_email(email)
|
|
|
|
Expert.eager_load(:user).find_by(users: { email: email })
|
|
|
|
end
|
2021-04-23 14:56:17 +02:00
|
|
|
|
|
|
|
def avis_summary
|
|
|
|
if @avis_summary.present?
|
|
|
|
@avis_summary
|
|
|
|
else
|
|
|
|
query = <<~EOF
|
2023-04-03 17:05:17 +02:00
|
|
|
COUNT(*) FILTER (where answer IS NULL AND dossiers.hidden_by_administration_at IS NULL AND dossiers.state not in ('accepte', 'refuse', 'sans_suite')) AS unanswered,
|
2021-04-23 14:56:17 +02:00
|
|
|
COUNT(*) AS total
|
|
|
|
EOF
|
|
|
|
result = avis.select(query)[0]
|
|
|
|
@avis_summary = { unanswered: result.unanswered, total: result.total }
|
|
|
|
end
|
|
|
|
end
|
2021-10-04 15:26:11 +02:00
|
|
|
|
2024-04-16 11:12:32 +02:00
|
|
|
def self.autocomplete_mails(procedure)
|
2024-04-16 15:47:13 +02:00
|
|
|
procedure_experts = Expert
|
2024-04-16 13:33:37 +02:00
|
|
|
.joins(:experts_procedures, :user)
|
|
|
|
.where(experts_procedures: { procedure: procedure })
|
2024-04-16 15:47:13 +02:00
|
|
|
|
|
|
|
suggested_expert = if procedure.experts_require_administrateur_invitation?
|
2024-04-19 13:03:09 +02:00
|
|
|
procedure_experts
|
2024-04-16 13:33:37 +02:00
|
|
|
.where(experts_procedures: { revoked_at: nil })
|
2024-04-16 15:47:13 +02:00
|
|
|
else
|
2024-04-19 13:03:09 +02:00
|
|
|
procedure_experts
|
|
|
|
.where.not(users: { last_sign_in_at: nil })
|
|
|
|
.or(procedure_experts.where(users: { created_at: 1.day.ago.. }))
|
2024-04-16 11:12:32 +02:00
|
|
|
end
|
2024-04-16 13:33:37 +02:00
|
|
|
|
2024-04-16 15:47:13 +02:00
|
|
|
suggested_expert
|
2024-04-16 13:33:37 +02:00
|
|
|
.pluck('users.email')
|
|
|
|
.sort
|
2024-04-16 11:12:32 +02:00
|
|
|
end
|
|
|
|
|
2021-10-04 15:26:11 +02:00
|
|
|
def merge(old_expert)
|
2021-10-12 11:04:06 +02:00
|
|
|
return if old_expert.nil?
|
|
|
|
|
2021-10-07 14:34:01 +02:00
|
|
|
procedure_with_new, procedure_without_new = old_expert
|
|
|
|
.procedures
|
2023-08-31 14:26:29 +02:00
|
|
|
.with_discarded
|
2021-10-07 14:34:01 +02:00
|
|
|
.partition { |p| p.experts.exists?(id) }
|
|
|
|
|
|
|
|
ExpertsProcedure
|
|
|
|
.where(expert_id: old_expert.id, procedure: procedure_without_new)
|
|
|
|
.update_all(expert_id: id)
|
|
|
|
|
|
|
|
ExpertsProcedure
|
|
|
|
.where(expert_id: old_expert.id, procedure: procedure_with_new)
|
|
|
|
.destroy_all
|
|
|
|
|
2021-10-04 15:26:11 +02:00
|
|
|
old_expert.commentaires.update_all(expert_id: id)
|
2021-10-07 14:34:01 +02:00
|
|
|
|
|
|
|
Avis
|
|
|
|
.where(claimant_id: old_expert.id, claimant_type: Expert.name)
|
|
|
|
.update_all(claimant_id: id)
|
2021-10-04 15:26:11 +02:00
|
|
|
end
|
2021-01-07 15:45:02 +01:00
|
|
|
end
|