<commit_before>class AccountController < ApplicationController
  def search
    render json: Account.search(params[:term]).map { |a| { label: a.autocomplete_text, value: a.id, full_name: a.full_name } }
  end

  def show
    @account = Account.find(params[:id]) if params[:id] =~ /^\d+$/
    @account ||= Account.find_by(trigramme: params[:id].upcase) if params[:id].size == 3
    return redirect_to controller: :application, action: :index unless @account
    @transactions = Transaction.where(id: @account.id)
      .includes(:receiver)
      .includes(:administrator)
      .order(date: :desc)
      .paginate(page: params[:page])
  end

  def exists
    render json: Account.where(trigramme: params[:trigramme].upcase).exists?
  end

  def details
    account = Account.find_by(trigramme: params[:trigramme].upcase)
    return render json: nil if account.nil?
    render json: account.as_json.merge!(display_text: account.autocomplete_text(with_trigramme: false))
  end

  def log
    @account = Account.find(params[:id])
    amount = params[:amount].to_f
    fail TdbException, 'Amount must be positive!' if amount < 0
    require_admin!(:log_eleve) if amount > 20 || (!@account.x_platal? && @account.budget < amount)
    Transaction.log(@account, @bank, amount, comment: params[:comment], admin: @admin)
    render_redirect_to
  end

  def credit
    @account = Account.find(params[:id])
    amount = params[:amount].to_f
    fail TdbException, 'Amount must be positive!' if amount < 0
    require_admin!(:credit)
    comment = params[:commit]
    comment += " - #{params[:comment]}" if params[:comment] && !params[:comment].empty?
    Transaction.log(@account, @bank, -amount, comment: comment, admin: @admin)
    render_redirect_to
  end

  def clopes
    @account = Account.find(params[:id])
    clope = Clope.find(params[:clope_id])
    quantity = params[:quantity].to_i
    total_price = quantity * clope.euro_price # Clopes price is in cents!
    require_admin!(:log_eleve) if total_price || (!@account.x_platal? && @account.budget < total_price)
    clope.sell(@account, quantity, admin: @admin)
    render_redirect_to
  end

  def update
    @account = Account.find(params[:id])
    require_admin!(:modifier_tri)
    to_update = {}
    if params[:picture]
      ext = File.extname(params[:picture].tempfile)<commit_msg>Keep only one account image<commit_after>class AccountController < ApplicationController
  def search
    render json: Account.search(params[:term]).map { |a| { label: a.autocomplete_text, value: a.id, full_name: a.full_name } }
  end

  def show
    @account = Account.find(params[:id]) if params[:id] =~ /^\d+$/
    @account ||= Account.find_by(trigramme: params[:id].upcase) if params[:id].size == 3
    return redirect_to controller: :application, action: :index unless @account
    @transactions = Transaction.where(id: @account.id)
      .includes(:receiver)
      .includes(:administrator)
      .order(date: :desc)
      .paginate(page: params[:page])
  end

  def exists
    render json: Account.where(trigramme: params[:trigramme].upcase).exists?
  end

  def details
    account = Account.find_by(trigramme: params[:trigramme].upcase)
    return render json: nil if account.nil?
    render json: account.as_json.merge!(display_text: account.autocomplete_text(with_trigramme: false))
  end

  def log
    @account = Account.find(params[:id])
    amount = params[:amount].to_f
    fail TdbException, 'Amount must be positive!' if amount < 0
    require_admin!(:log_eleve) if amount > 20 || (!@account.x_platal? && @account.budget < amount)
    Transaction.log(@account, @bank, amount, comment: params[:comment], admin: @admin)
    render_redirect_to
  end

  def credit
    @account = Account.find(params[:id])
    amount = params[:amount].to_f
    fail TdbException, 'Amount must be positive!' if amount < 0
    require_admin!(:credit)
    comment = params[:commit]
    comment += " - #{params[:comment]}" if params[:comment] && !params[:comment].empty?
    Transaction.log(@account, @bank, -amount, comment: comment, admin: @admin)
    render_redirect_to
  end

  def clopes
    @account = Account.find(params[:id])
    clope = Clope.find(params[:clope_id])
    quantity = params[:quantity].to_i
    total_price = quantity * clope.euro_price # Clopes price is in cents!
    require_admin!(:log_eleve) if total_price || (!@account.x_platal? && @account.budget < total_price)
    clope.sell(@account, quantity, admin: @admin)
    render_redirect_to
  end

  def update
    @account = Account.find(params[:id])
    require_admin!(:modifier_tri)
    to_update = {}
    if params[:picture]
      ext = File.extname(params[:picture].tempfile)
      Dir[Rails.root.join('app', 'assets', 'images', 'accounts', "#{@account.id}.*")].each { |f| File.delete f }
{'fraction_non_alphanumeric': 0.1399958272480701, 'fraction_numerical': 0.002086375964948884, 'mean_word_length': 4.051633298208641, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, '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': '1065485', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1510, 'n_words': 355}