2021-01-07 15:45:02 +01:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: experts
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
class Expert < ApplicationRecord
|
|
|
|
has_one :user
|
2021-02-09 10:24:13 +01:00
|
|
|
has_many :experts_procedures
|
|
|
|
has_many :avis, through: :experts_procedures
|
|
|
|
has_many :dossiers, through: :avis
|
2021-10-04 15:26:11 +02:00
|
|
|
has_many :commentaires
|
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
|
|
|
|
COUNT(*) FILTER (where answer IS NULL) AS unanswered,
|
|
|
|
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
|
|
|
|
|
|
|
def merge(old_expert)
|
|
|
|
old_expert.experts_procedures.update_all(expert_id: id)
|
|
|
|
old_expert.commentaires.update_all(expert_id: id)
|
|
|
|
end
|
2021-01-07 15:45:02 +01:00
|
|
|
end
|