class Family::Accounting::TransactionsController < Family::Controller
  include DateRangeHelper

  def index
    render_success :ok, json: transactions.map { |t| transaction_props(t) }
  end

  def show
    render_success :ok, json: transaction_props(transaction)
  end

  private
    def transactions
      current_family.accounting_transactions
        .references(:students, subcategories: :category)
        .includes(:students, subcategories: :category)
        .without_void(true)
        .with_date_range(date_range(params[:dates]))
        .with_category(params[:category_id])
        .with_student(params[:student_id])
        .ordered
        .distinct
    end

    def transaction
      @transaction ||= transactions.find_by(id: params[:id])
    end

    def transaction_props(transaction)
      {
        id: transaction.id,
        date: transaction.decorate.posted_date,
        memo: transaction.memo,
        amount: transaction.decorate.amount,
        type: transaction.class.name.demodulize,
        action: transaction.action.humanize,
        void: transaction.void,
        category: transaction.category_name,
        students: transaction.decorate.student_name
      }
    end
end
