<commit_before>class DebtsController < ApplicationController
  before_action :authenticate_user! ## for DEVISE
  
  ## Resource Actions
  def new
    assign_current_user
    @debtor = Debtor.find_by_id(params[:debtor_id])#something w/ params
    @debt = Debt.new
  end
  
  def edit
    assign_current_user
    @debt = Debt.find_by_id(params[:id]) #something w/ params
    @debtor = Debtor.find_by_id(@debt.debtor_id)
  end
  
  def show
    assign_current_user
    @debt = Debt.find_by_id(params[:id])
    @debtor = Debtor.find_by_id @debt.debtor_id
  end
  
  def index
    assign_current_user
    @debts_all = Debt.paginate(page: params[:page], per_page: 10)
    
    respond_to do |format|
      format.html
      format.csv { send_data @debts_all.to_csv}
      format.xls
    end
  end
  
  def create
    assign_current_user
    @debtor = Debtor.find_by_id(params[:debtor_id])
    fail if @debtor.nil?
    params[:debt][:debtor_id] = @debtor.id
    @debt = Debt.new(debt_params)
    @debt.permit_infraction_number = strip_hyphens(@debt.permit_infraction_number)
    @debt.originating_debt_amount = @debt.amount_owed_pending_balance
    if @debt.save
      flash[:success] = "Nueva Factura Creada"
      redirect_to @debt
    else
      flash[:error] = "Factura No Gravada"
      render 'new'
    end
  end
  
  def update
    assign_current_user
    @debt = Debt.find_by_id(params[:id]) #something w/ params
    @debtor = Debtor.find_by_id(@debt.debtor_id)
    @debt.permit_infraction_number = strip_hyphens(@debt.permit_infraction_number)
    if @debt.update_attributes(update_debt_params) && @debt.valid?
      flash[:success] = "Factura Actualizada"
      redirect_to @debt
    else
      flash[:error] = "Factura No Actualizada"
      render 'edit'
    end  
  end
  
  def preview_email   
    @debt = Debt.find_by_id(mailer_params[:id])
    @user = current_user
    @mailer = mailer_params[:mailer].to_sym
    @date_first_email_sent = date_first_email_sent(@debt)
    @print = (mailer_params[:print] == 'true') ? true : false
    prepare_email(@debt, @user, @mailer, preview: true, 
      date_first_email_sent: @date_first_email_sent, 
      display_attachments: false)

    render layout: false 
  end
  
  def send_email #should be Post only
    @debt = Debt.find_by_id(mailer_params[:id])
    @user = current_user
    @mailer = mailer_params[:mailer].to_sym
    @date_first_email_sent = date_first_email_sent(@debt)
    prepare_email(@debt, @user, @mailer, send: true,
      date_first_email_sent: @date_first_email_sent, 
      display_attachments: true)
    
    redirect_to @debt
  end
  
  private
  ## Mail Methods (Should be in elsewhere)
  def prepare_email(debt, user, mailer, options={})  
    date_first_email_sent = options.fetch :date_first_email_sent, "fecha de primer aviso no encontrada"
    display_attachments = options.fetch :display_attachments, true
    
    if guard_mailer(mailer) 
      @mail_preview = NotificationsMailer.public_send(mailer, debt, user, 
        date_first_email_sent: date_first_email_sent, display_attachments: display_attachments)
      if options.fetch :send, false 
        deliver_mail(debt, user, mailer, @mail_preview)
      end
    else 
      flash[:error] = "Email No Encontrado"
    end
  end
  
  def deliver_mail(debt, user, mailer, mail_preview, options={})
    require 'thread'
    msg = Queue.new
    Thread.new {
      if mail_preview.deliver #guard_mailer(mailer) && 
        # Log Mail
        log_email(mail_preview, debt, user, mailer_name: mailer)
        # flash[:success] = "Email: #{mail_preview.subject} Enviado"
        msg.push({:success => "Email: #{mail_preview.subject} Enviado"})
      else 
        # flash[:error] = "Email No Enviado"
        msg.push({:error => "Email No Enviado"})
      end
    }.join()
    flash_msg = msg.length.zero? ? {notice: 'thread error'} : msg.pop
    flash[flash_msg.keys.first] = flash_msg.values.first
  end
  
  def guard_mailer(mailer)
    [:first,:second,:third].include?(mailer) && NotificationsMailer.respond_to?(mailer)
  end
  
  def date_first_email_sent(debt)
    date_first_email_sent_raw = debt.mail_logs.find_by_mailer_name(:first)
    if date_first_email_sent_raw
      I18n.l (date_first_email_sent_raw.datetime_sent.to_date), :format => '%d de %B de %Y'
    else
      "fecha de primer aviso no encontrada"
    end
  end
  
  ## Controller Private Methods
  def assign_current_user
    @user = current_user<commit_msg>removes thead queue from mail deliver using join works<commit_after>class DebtsController < ApplicationController
  before_action :authenticate_user! ## for DEVISE
  
  ## Resource Actions
  def new
    assign_current_user
    @debtor = Debtor.find_by_id(params[:debtor_id])#something w/ params
    @debt = Debt.new
  end
  
  def edit
    assign_current_user
    @debt = Debt.find_by_id(params[:id]) #something w/ params
    @debtor = Debtor.find_by_id(@debt.debtor_id)
  end
  
  def show
    assign_current_user
    @debt = Debt.find_by_id(params[:id])
    @debtor = Debtor.find_by_id @debt.debtor_id
  end
  
  def index
    assign_current_user
    @debts_all = Debt.paginate(page: params[:page], per_page: 10)
    
    respond_to do |format|
      format.html
      format.csv { send_data @debts_all.to_csv}
      format.xls
    end
  end
  
  def create
    assign_current_user
    @debtor = Debtor.find_by_id(params[:debtor_id])
    fail if @debtor.nil?
    params[:debt][:debtor_id] = @debtor.id
    @debt = Debt.new(debt_params)
    @debt.permit_infraction_number = strip_hyphens(@debt.permit_infraction_number)
    @debt.originating_debt_amount = @debt.amount_owed_pending_balance
    if @debt.save
      flash[:success] = "Nueva Factura Creada"
      redirect_to @debt
    else
      flash[:error] = "Factura No Gravada"
      render 'new'
    end
  end
  
  def update
    assign_current_user
    @debt = Debt.find_by_id(params[:id]) #something w/ params
    @debtor = Debtor.find_by_id(@debt.debtor_id)
    @debt.permit_infraction_number = strip_hyphens(@debt.permit_infraction_number)
    if @debt.update_attributes(update_debt_params) && @debt.valid?
      flash[:success] = "Factura Actualizada"
      redirect_to @debt
    else
      flash[:error] = "Factura No Actualizada"
      render 'edit'
    end  
  end
  
  def preview_email   
    @debt = Debt.find_by_id(mailer_params[:id])
    @user = current_user
    @mailer = mailer_params[:mailer].to_sym
    @date_first_email_sent = date_first_email_sent(@debt)
    @print = (mailer_params[:print] == 'true') ? true : false
    prepare_email(@debt, @user, @mailer, preview: true, 
      date_first_email_sent: @date_first_email_sent, 
      display_attachments: false)

    render layout: false 
  end
  
  def send_email #should be Post only
    @debt = Debt.find_by_id(mailer_params[:id])
    @user = current_user
    @mailer = mailer_params[:mailer].to_sym
    @date_first_email_sent = date_first_email_sent(@debt)
    prepare_email(@debt, @user, @mailer, send: true,
      date_first_email_sent: @date_first_email_sent, 
      display_attachments: true)
    
    redirect_to @debt
  end
  
  private
  ## Mail Methods (Should be in elsewhere)
  def prepare_email(debt, user, mailer, options={})  
    date_first_email_sent = options.fetch :date_first_email_sent, "fecha de primer aviso no encontrada"
    display_attachments = options.fetch :display_attachments, true
    
    if guard_mailer(mailer) 
      @mail_preview = NotificationsMailer.public_send(mailer, debt, user, 
        date_first_email_sent: date_first_email_sent, display_attachments: display_attachments)
      if options.fetch :send, false 
        deliver_mail(debt, user, mailer, @mail_preview)
      end
    else 
      flash[:error] = "Email No Encontrado"
    end
  end
  
  def deliver_mail(debt, user, mailer, mail_preview, options={})
    require 'thread'
    Thread.new {
      if mail_preview.deliver #guard_mailer(mailer) && 
        # Log Mail
        log_email(mail_preview, debt, user, mailer_name: mailer)
        flash[:success] = "Email: #{mail_preview.subject} Enviado"
      else 
        flash[:error] = "Email No Enviado"
      end
    }.join()
  end
  
  def guard_mailer(mailer)
    [:first,:second,:third].include?(mailer) && NotificationsMailer.respond_to?(mailer)
  end
  
  def date_first_email_sent(debt)
    date_first_email_sent_raw = debt.mail_logs.find_by_mailer_name(:first)
    if date_first_email_sent_raw
      I18n.l (date_first_email_sent_raw.datetime_sent.to_date), :format => '%d de %B de %Y'
    else
      "fecha de primer aviso no encontrada"
    end
  end
  
  ## Controller Private Methods
  def assign_current_user
    @user = current_user
{'fraction_non_alphanumeric': 0.09882515549412578, 'fraction_numerical': 0.0009214466712739, 'mean_word_length': 3.4278429372768997, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5306175', 'n_tokens_mistral': 3142, 'n_tokens_neox': 2981, 'n_words': 605}