From 416390a3c41b91313c166a02b6fe11542458737a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Fri, 12 Feb 2021 15:33:34 +0200 Subject: [PATCH] Add detailed balance to REPP --- .../repp/v1/accounts_controller.rb | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/app/controllers/repp/v1/accounts_controller.rb b/app/controllers/repp/v1/accounts_controller.rb index 89c14808f..012a36dc5 100644 --- a/app/controllers/repp/v1/accounts_controller.rb +++ b/app/controllers/repp/v1/accounts_controller.rb @@ -2,10 +2,38 @@ module Repp module V1 class AccountsController < BaseController def balance + return activity if params[:detailed] == 'true' + resp = { balance: current_user.registrar.cash_account.balance, currency: current_user.registrar.cash_account.currency } render_success(data: resp) end + + def activity + resp = { balance: current_user.registrar.cash_account.balance, + currency: current_user.registrar.cash_account.currency } + resp[:activities] = activities + render_success(data: resp) + end + + def activities + bal = current_user.registrar.cash_account.balance + act = [] + activities = current_user.registrar.cash_account.activities.order(created_at: :desc) + activities.each do |a| + act << { + created_at: a.created_at, + description: a.description, + type: a.activity_type == 'add_credit' ? 'credit' : 'debit', + sum: a.sum, + balance: bal, + } + + bal = a.activity_type == 'add_credit' ? bal = bal + a.sum : bal - a.sum + end + + act + end end end end