demarches-normaliennes/app/models/traitement.rb
Paul Chavard df474b60cf WIP
2021-11-18 18:58:17 +03:00

45 lines
1.4 KiB
Ruby

# == Schema Information
#
# Table name: traitements
#
# id :bigint not null, primary key
# instructeur_email :string
# motivation :string
# process_expired :boolean
# processed_at :datetime
# state :string
# dossier_id :bigint
#
class Traitement < ApplicationRecord
belongs_to :dossier, optional: false
scope :en_construction, -> { where(state: Dossier.states.fetch(:en_construction)) }
scope :en_instruction, -> { where(state: Dossier.states.fetch(:en_instruction)) }
scope :termine, -> { where(state: Dossier::TERMINE) }
scope :for_traitement_time_stats, -> (procedure) do
includes(:dossier)
.termine
.where(dossier: procedure.dossiers)
.where.not('dossiers.en_construction_at' => nil, :processed_at => nil)
.order(:processed_at)
end
def self.count_dossiers_termines_by_month(groupe_instructeurs)
last_traitements_per_dossier = Traitement
.select('max(traitements.processed_at) as processed_at')
.termine
.where(dossier: Dossier.state_termine.where(groupe_instructeur: groupe_instructeurs))
.group(:dossier_id)
.to_sql
sql = <<~EOF
select date_trunc('month', r1.processed_at) as month, count(r1.processed_at)
from (#{last_traitements_per_dossier}) as r1
group by date_trunc('month', r1.processed_at)
order by month desc
EOF
ActiveRecord::Base.connection.execute(sql)
end
end