<commit_before>class AccessionsController < ApplicationController
  before_action :set_recent_patients_list, except: [:destroy]

  def index
    @pending_accessions = Accession.includes(:drawer, :patient, results: [:lab_test, :lab_test_value]).queued.pending.page(params[:pending_page])
    @reported_accessions = Accession.includes(:patient, :reporter).recently.reported.page(params[:page])
  end

  def show
    @accession = Accession.find(params[:id])
    @patient = @accession.patient
    @results = @accession.results.includes({ accession: { notes: [:department] } }, { lab_test: [:department] }, :lab_test_value, :reference_ranges, :unit).order('lab_tests.position').group_by(&:department)
  rescue ActiveRecord::RecordNotFound
    flash[:error] = t('flash.accession.accession_not_found')
    redirect_to accessions_url
  end

  def new
    @patient = Patient.find(params[:patient_id])
    @accession = @patient.accessions.build
    @accession.drawn_at = Time.current
    @accession.drawer_id = current_user.id
    @accession.received_at = Time.current
    @accession.receiver_id = current_user.id
    @panels = Panel.sorted.includes(:lab_tests)
    @departments = Department.all.includes(:lab_tests)
    @users = User.sorted
  end

  def create
    @patient = Patient.find(params[:patient_id])
    @panels = Panel.sorted.includes(:lab_tests)
    @departments = Department.all.includes(:lab_tests)
    @accession = @patient.accessions.build(accession_params)
    @users = User.sorted
    if @accession.save
      flash[:notice] = t('flash.accession.create')
      redirect_to accession_url(@accession)
    else
      render action: 'new'
    end
  end

  def edit
    @accession = Accession.find(params[:id])
    @patient = @accession.patient
    @departments = Department.all.includes(:lab_tests)
    @panels = Panel.sorted.includes(:lab_tests)
    @users = User.sorted
    $update_action = 'edit'
  end

  def update
    @accession = Accession.find(params[:id])
    @patient = @accession.patient
    @departments = Department.all.includes(:lab_tests)
    @panels = Panel.sorted.includes(:lab_tests)
    @users = User.sorted
    if @accession.update(accession_params)
      unless current_user.admin?
        @accession.update(reporter_id: current_user.id, reported_at: Time.current) if @accession.reported_at
      end
      flash[:notice] = t('flash.accession.update')
      redirect_to accession_url(@accession)
    else
      render action: $update_action
    end
  end

  def destroy
    @accession = Accession.find(params[:id])
    @accession.destroy
    flash[:notice] = t('flash.accession.destroy')
    redirect_to patient_url(@accession.patient_id)
  end

  def edit_results
    @accession = Accession.find(params[:id])
    @patient = @accession.patient
    @results = @accession.results.includes({ accession: { notes: [:department] } }, { lab_test: [:department] }, :lab_test_value, :reference_ranges, :unit).order('lab_tests.position').group_by(&:department)
    # TODO: Missing per department blank validation.
    # It will only check first
    @results.each do |department, _result|
      @accession.notes.build(department_id: department.id) unless @accession.try(:notes).find_by_department_id(department.id)
    end
    $update_action = 'edit_results'
  end

  def report
    @accession = Accession.find(params[:id])
    if @accession.complete?
      @accession.reporter_id = current_user.id
      @accession.reported_at = Time.current
      @accession.save
      redirect_to accession_results_url(@accession, format: 'pdf')
    else
      flash[:error] = t('flash.accession.report_error')
      redirect_to accession_url(@accession)
    end
  end

  def email
    @accession = Accession.find(params[:id])
    @patient = @accession.patient
    @results = @accession.results.includes({ accession: { notes: [:department] } }, { lab_test: [:department] }, :lab_test_value, :reference_ranges, :unit).order('lab_tests.position').group_by(&:department)

    pdf = LabReport.new(@patient, @accession, @results, view_context)

    if @patient.email.present?
      ResultsMailer.email(@accession, pdf).deliver_now
      redirect_to accession_url(@accession), notice: t('flash.accession.email_success')
    else
      redirect_to accession_url(@accession), error: t('flash.accession.email_error')
    end
  end

  def email_doctor
    @accession = Accession.find(params[:id])
    @patient = @accession.patient
    @results = @accession.results.includes({ accession: { notes: [:department] } }, { lab_test: [:department] }, :lab_test_value, :reference_ranges, :unit).order('lab_tests.position').group_by(&:department)

    pdf = LabReport.new(@patient, @accession, @results, view_context)

    if true # @accession.doctor.email.present?
      DoctorsMailer.email(@accession, pdf).deliver_now
      redirect_to accession_url(@accession), notice: t('flash.accession.email_success')
    else
      redirect_to accession_url(@accession), error: t('flash.accession.email_error')
    end
  end

  protected

  def set_recent_patients_list
    @recent ||= Patient.cached_recent
  end

  private

  def accession_params
    params.require(:accession).permit(:drawn_at, :drawer_id, :received_at, :receiver_id, :doctor_name, :icd9, { lab_test_ids: [] }, :reporter_id, :reported_at, { results_attributes: [:id, :lab_test_id, :lab_test_value_id, :value] }, { panel_ids: [] }, { notes_attributes: [:id, :content, :department_id] })
  end
end<commit_msg>Fix email to doctor fail test case<commit_after>class AccessionsController < ApplicationController
  before_action :set_recent_patients_list, except: [:destroy]

  def index
    @pending_accessions = Accession.includes(:drawer, :patient, results: [:lab_test, :lab_test_value]).queued.pending.page(params[:pending_page])
    @reported_accessions = Accession.includes(:patient, :reporter).recently.reported.page(params[:page])
  end

  def show
    @accession = Accession.find(params[:id])
    @patient = @accession.patient
    @results = @accession.results.includes({ accession: { notes: [:department] } }, { lab_test: [:department] }, :lab_test_value, :reference_ranges, :unit).order('lab_tests.position').group_by(&:department)
  rescue ActiveRecord::RecordNotFound
    flash[:error] = t('flash.accession.accession_not_found')
    redirect_to accessions_url
  end

  def new
    @patient = Patient.find(params[:patient_id])
    @accession = @patient.accessions.build
    @accession.drawn_at = Time.current
    @accession.drawer_id = current_user.id
    @accession.received_at = Time.current
    @accession.receiver_id = current_user.id
    @panels = Panel.sorted.includes(:lab_tests)
    @departments = Department.all.includes(:lab_tests)
    @users = User.sorted
  end

  def create
    @patient = Patient.find(params[:patient_id])
    @panels = Panel.sorted.includes(:lab_tests)
    @departments = Department.all.includes(:lab_tests)
    @accession = @patient.accessions.build(accession_params)
    @users = User.sorted
    if @accession.save
      flash[:notice] = t('flash.accession.create')
      redirect_to accession_url(@accession)
    else
      render action: 'new'
    end
  end

  def edit
    @accession = Accession.find(params[:id])
    @patient = @accession.patient
    @departments = Department.all.includes(:lab_tests)
    @panels = Panel.sorted.includes(:lab_tests)
    @users = User.sorted
    $update_action = 'edit'
  end

  def update
    @accession = Accession.find(params[:id])
    @patient = @accession.patient
    @departments = Department.all.includes(:lab_tests)
    @panels = Panel.sorted.includes(:lab_tests)
    @users = User.sorted
    if @accession.update(accession_params)
      unless current_user.admin?
        @accession.update(reporter_id: current_user.id, reported_at: Time.current) if @accession.reported_at
      end
      flash[:notice] = t('flash.accession.update')
      redirect_to accession_url(@accession)
    else
      render action: $update_action
    end
  end

  def destroy
    @accession = Accession.find(params[:id])
    @accession.destroy
    flash[:notice] = t('flash.accession.destroy')
    redirect_to patient_url(@accession.patient_id)
  end

  def edit_results
    @accession = Accession.find(params[:id])
    @patient = @accession.patient
    @results = @accession.results.includes({ accession: { notes: [:department] } }, { lab_test: [:department] }, :lab_test_value, :reference_ranges, :unit).order('lab_tests.position').group_by(&:department)
    # TODO: Missing per department blank validation.
    # It will only check first
    @results.each do |department, _result|
      @accession.notes.build(department_id: department.id) unless @accession.try(:notes).find_by_department_id(department.id)
    end
    $update_action = 'edit_results'
  end

  def report
    @accession = Accession.find(params[:id])
    if @accession.complete?
      @accession.reporter_id = current_user.id
      @accession.reported_at = Time.current
      @accession.save
      redirect_to accession_results_url(@accession, format: 'pdf')
    else
      flash[:error] = t('flash.accession.report_error')
      redirect_to accession_url(@accession)
    end
  end

  def email
    @accession = Accession.find(params[:id])
    @patient = @accession.patient
    @results = @accession.results.includes({ accession: { notes: [:department] } }, { lab_test: [:department] }, :lab_test_value, :reference_ranges, :unit).order('lab_tests.position').group_by(&:department)

    pdf = LabReport.new(@patient, @accession, @results, view_context)

    if @patient.email.present?
      ResultsMailer.email(@accession, pdf).deliver_now
      redirect_to accession_url(@accession), notice: t('flash.accession.email_success')
    else
      redirect_to accession_url(@accession), error: t('flash.accession.email_error')
    end
  end

  def email_doctor
    @accession = Accession.find(params[:id])
    @patient = @accession.patient
    @results = @accession.results.includes({ accession: { notes: [:department] } }, { lab_test: [:department] }, :lab_test_value, :reference_ranges, :unit).order('lab_tests.position').group_by(&:department)

    pdf = LabReport.new(@patient, @accession, @results, view_context)

    if @accession.doctor.email.present?
      DoctorsMailer.email(@accession, pdf).deliver_now
      redirect_to accession_url(@accession), notice: t('flash.accession.email_success')
    else
      redirect_to accession_url(@accession), error: t('flash.accession.email_error')
    end
  end

  protected

  def set_recent_patients_list
    @recent ||= Patient.cached_recent
  end

  private

  def accession_params
    params.require(:accession).permit(:drawn_at, :drawer_id, :received_at, :receiver_id, :doctor_name, :icd9, { lab_test_ids: [] }, :reporter_id, :reported_at, { results_attributes: [:id, :lab_test_id, :lab_test_value_id, :value] }, { panel_ids: [] }, { notes_attributes: [:id, :content, :department_id] })
  end
end
{'fraction_non_alphanumeric': 0.12852693294246978, 'fraction_numerical': 0.00018321729571271528, 'mean_word_length': 4.348848603625673, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7473626', 'n_tokens_mistral': 3635, 'n_tokens_neox': 3484, 'n_words': 622}