From 2321c3e5b4766f5819a95e66037c2355fdd1841e Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Wed, 27 Jan 2021 15:59:17 +0200 Subject: [PATCH 1/5] added test for check balance activity in account balance overview --- .../repp/v1/accounts/balance_test.rb | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/integration/repp/v1/accounts/balance_test.rb b/test/integration/repp/v1/accounts/balance_test.rb index 785e0aee8..38b793020 100644 --- a/test/integration/repp/v1/accounts/balance_test.rb +++ b/test/integration/repp/v1/accounts/balance_test.rb @@ -13,10 +13,39 @@ class ReppV1BalanceTest < ActionDispatch::IntegrationTest get '/repp/v1/accounts/balance', headers: @auth_headers json = JSON.parse(response.body, symbolize_names: true) + puts response.body + puts @registrar.registrar.cash_account.balance + puts @registrar.registrar.cash_account.currency + assert_response :ok assert_equal 1000, json[:code] assert_equal 'Command completed successfully', json[:message] assert_equal @registrar.registrar.cash_account.balance.to_s, json[:data][:balance] assert_equal @registrar.registrar.cash_account.currency, json[:data][:currency] end + + def test_can_query_balance_with_details + started_from = "2020-01-01" + end_to = DateTime.current.to_date.to_s(:db) + + get "/repp/v1/accounts/balance?detailed=true&started_from=#{started_from}&end_to=#{end_to}", headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal 1000, json[:code] + assert_equal 'Command completed successfully', json[:message] + assert_equal @registrar.registrar.cash_account.balance.to_s, json[:data][:balance] + assert_equal @registrar.registrar.cash_account.currency, json[:data][:currency] + assert_equal @registrar.registrar.cash_account.account_activities[0].created_at, json[:data][:transactions][0][:created_at] + assert_equal @registrar.registrar.cash_account.account_activities[0].description, json[:data][:transactions][0][:description] + assert_equal @registrar.registrar.cash_account.account_activities[0].activity_type, json[:data][:transactions][0][:action] + assert_equal @registrar.registrar.cash_account.account_activities[0].sum.to_s, json[:data][:transactions][0][:price] + assert_equal @registrar.registrar.cash_account.account_activities[0].new_balance.to_s, json[:data][:transactions][0][:new_balance] + + json[:data][:transaction].map do |trans| + assert trans[:created_at].to_date.to_s(:db) >= started_from + assert trans[:created_at].to_date.to_s(:db) >= end_to + end + + end end 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 2/5] 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 From 27e441bc249098ec3c0229a7d2ab4368378b1434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 15 Feb 2021 14:41:50 +0200 Subject: [PATCH 3/5] Track account balance dynamically in AccountActivity --- app/models/account_activity.rb | 3 +++ app/views/registrar/account_activities/index.html.erb | 8 +++++++- .../20210215101019_add_new_balance_to_account_activity.rb | 5 +++++ db/structure.sql | 6 ++++-- 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20210215101019_add_new_balance_to_account_activity.rb diff --git a/app/models/account_activity.rb b/app/models/account_activity.rb index d8e6be4d4..9df64209a 100644 --- a/app/models/account_activity.rb +++ b/app/models/account_activity.rb @@ -13,6 +13,9 @@ class AccountActivity < ApplicationRecord def update_balance account.balance += sum account.save + + self.new_balance = account.balance + save end class << self diff --git a/app/views/registrar/account_activities/index.html.erb b/app/views/registrar/account_activities/index.html.erb index c5eaf2063..aefcdd47f 100644 --- a/app/views/registrar/account_activities/index.html.erb +++ b/app/views/registrar/account_activities/index.html.erb @@ -14,7 +14,7 @@ - + @@ -45,6 +48,9 @@ + <% end %> diff --git a/db/migrate/20210215101019_add_new_balance_to_account_activity.rb b/db/migrate/20210215101019_add_new_balance_to_account_activity.rb new file mode 100644 index 000000000..64c829833 --- /dev/null +++ b/db/migrate/20210215101019_add_new_balance_to_account_activity.rb @@ -0,0 +1,5 @@ +class AddNewBalanceToAccountActivity < ActiveRecord::Migration[6.0] + def change + add_column :account_activities, :new_balance, :decimal, precision: 10, scale: 2, null: true + end +end diff --git a/db/structure.sql b/db/structure.sql index acf134a55..de2243597 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -226,7 +226,8 @@ CREATE TABLE public.account_activities ( creator_str character varying, updator_str character varying, activity_type character varying, - price_id integer + price_id integer, + new_balance numeric(10,2) ); @@ -4959,6 +4960,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20200908131554'), ('20200910085157'), ('20200910102028'), -('20200916125326'); +('20200916125326'), +('20210215101019'); From 7b4214def11e3f72ec582337ec6192637c4d1af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 15 Feb 2021 16:51:50 +0200 Subject: [PATCH 4/5] Add from/until sorting to balance API --- app/controllers/repp/v1/accounts_controller.rb | 16 +++++++--------- .../integration/repp/v1/accounts/balance_test.rb | 6 +----- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/app/controllers/repp/v1/accounts_controller.rb b/app/controllers/repp/v1/accounts_controller.rb index 012a36dc5..f9550b6b2 100644 --- a/app/controllers/repp/v1/accounts_controller.rb +++ b/app/controllers/repp/v1/accounts_controller.rb @@ -2,10 +2,9 @@ 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 } + resp[:activities] = activities if params[:detailed] == 'true' render_success(data: resp) end @@ -17,22 +16,21 @@ module Repp end def activities - bal = current_user.registrar.cash_account.balance - act = [] activities = current_user.registrar.cash_account.activities.order(created_at: :desc) + activities = activities.where('created_at >= ?', params[:from]) if params[:from] + activities = activities.where('created_at <= ?', params[:until]) if params[:until] + arr = [] activities.each do |a| - act << { + arr << { created_at: a.created_at, description: a.description, type: a.activity_type == 'add_credit' ? 'credit' : 'debit', sum: a.sum, - balance: bal, + balance: a.new_balance, } - - bal = a.activity_type == 'add_credit' ? bal = bal + a.sum : bal - a.sum end - act + arr end end end diff --git a/test/integration/repp/v1/accounts/balance_test.rb b/test/integration/repp/v1/accounts/balance_test.rb index 38b793020..d2b2624fa 100644 --- a/test/integration/repp/v1/accounts/balance_test.rb +++ b/test/integration/repp/v1/accounts/balance_test.rb @@ -13,10 +13,6 @@ class ReppV1BalanceTest < ActionDispatch::IntegrationTest get '/repp/v1/accounts/balance', headers: @auth_headers json = JSON.parse(response.body, symbolize_names: true) - puts response.body - puts @registrar.registrar.cash_account.balance - puts @registrar.registrar.cash_account.currency - assert_response :ok assert_equal 1000, json[:code] assert_equal 'Command completed successfully', json[:message] @@ -28,7 +24,7 @@ class ReppV1BalanceTest < ActionDispatch::IntegrationTest started_from = "2020-01-01" end_to = DateTime.current.to_date.to_s(:db) - get "/repp/v1/accounts/balance?detailed=true&started_from=#{started_from}&end_to=#{end_to}", headers: @auth_headers + get "/repp/v1/accounts/balance?detailed=true&from=#{started_from}&until=#{end_to}", headers: @auth_headers json = JSON.parse(response.body, symbolize_names: true) assert_response :ok From 47cd7cc4f7f779fbaf46c2d176fe452e712b4bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 18 Feb 2021 11:22:59 +0200 Subject: [PATCH 5/5] Fix CC issues --- .../repp/v1/accounts_controller.rb | 32 ++++++++----------- .../repp/v1/accounts/balance_test.rb | 22 +++++++------ 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/app/controllers/repp/v1/accounts_controller.rb b/app/controllers/repp/v1/accounts_controller.rb index f9550b6b2..1e0fde466 100644 --- a/app/controllers/repp/v1/accounts_controller.rb +++ b/app/controllers/repp/v1/accounts_controller.rb @@ -4,34 +4,28 @@ module Repp def balance resp = { balance: current_user.registrar.cash_account.balance, currency: current_user.registrar.cash_account.currency } - resp[:activities] = activities if params[:detailed] == 'true' - 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 + resp[:transactions] = activities if params[:detailed] == 'true' render_success(data: resp) end def activities - activities = current_user.registrar.cash_account.activities.order(created_at: :desc) - activities = activities.where('created_at >= ?', params[:from]) if params[:from] - activities = activities.where('created_at <= ?', params[:until]) if params[:until] arr = [] - activities.each do |a| - arr << { - created_at: a.created_at, - description: a.description, - type: a.activity_type == 'add_credit' ? 'credit' : 'debit', - sum: a.sum, - balance: a.new_balance, - } + registrar_activities.each do |a| + arr << { created_at: a.created_at, description: a.description, + type: a.activity_type == 'add_credit' ? 'credit' : 'debit', + sum: a.sum, balance: a.new_balance } end arr end + + def registrar_activities + activities = current_user.registrar.cash_account.activities.order(created_at: :desc) + activities = activities.where('created_at >= ?', params[:from]) if params[:from] + activities = activities.where('created_at <= ?', params[:until]) if params[:until] + + activities + end end end end diff --git a/test/integration/repp/v1/accounts/balance_test.rb b/test/integration/repp/v1/accounts/balance_test.rb index d2b2624fa..4b711bd05 100644 --- a/test/integration/repp/v1/accounts/balance_test.rb +++ b/test/integration/repp/v1/accounts/balance_test.rb @@ -2,6 +2,7 @@ require 'test_helper' class ReppV1BalanceTest < ActionDispatch::IntegrationTest def setup + travel_to Time.zone.parse('2010-07-05') @registrar = users(:api_bestnames) token = Base64.encode64("#{@registrar.username}:#{@registrar.plain_text_password}") token = "Basic #{token}" @@ -21,10 +22,13 @@ class ReppV1BalanceTest < ActionDispatch::IntegrationTest end def test_can_query_balance_with_details - started_from = "2020-01-01" + # Create new billable action to get activity + post "/repp/v1/domains/renew/bulk", headers: @auth_headers, params: { domains: ['shop.test'], renew_period: '1y' } + + started_from = "2010-07-05" end_to = DateTime.current.to_date.to_s(:db) - get "/repp/v1/accounts/balance?detailed=true&from=#{started_from}&until=#{end_to}", headers: @auth_headers + get "/repp/v1/accounts/balance?detailed=true", headers: @auth_headers json = JSON.parse(response.body, symbolize_names: true) assert_response :ok @@ -32,16 +36,16 @@ class ReppV1BalanceTest < ActionDispatch::IntegrationTest assert_equal 'Command completed successfully', json[:message] assert_equal @registrar.registrar.cash_account.balance.to_s, json[:data][:balance] assert_equal @registrar.registrar.cash_account.currency, json[:data][:currency] - assert_equal @registrar.registrar.cash_account.account_activities[0].created_at, json[:data][:transactions][0][:created_at] - assert_equal @registrar.registrar.cash_account.account_activities[0].description, json[:data][:transactions][0][:description] - assert_equal @registrar.registrar.cash_account.account_activities[0].activity_type, json[:data][:transactions][0][:action] - assert_equal @registrar.registrar.cash_account.account_activities[0].sum.to_s, json[:data][:transactions][0][:price] - assert_equal @registrar.registrar.cash_account.account_activities[0].new_balance.to_s, json[:data][:transactions][0][:new_balance] + entry = json[:data][:transactions].last + assert_equal @registrar.registrar.cash_account.account_activities.last.created_at, entry[:created_at] + assert_equal @registrar.registrar.cash_account.account_activities.last.description, entry[:description] + assert_equal 'debit', entry[:type] + assert_equal @registrar.registrar.cash_account.account_activities.last.sum.to_s, entry[:sum] + assert_equal @registrar.registrar.cash_account.account_activities.last.new_balance.to_s, entry[:balance] - json[:data][:transaction].map do |trans| + json[:data][:transactions].map do |trans| assert trans[:created_at].to_date.to_s(:db) >= started_from assert trans[:created_at].to_date.to_s(:db) >= end_to end - end end
+ <%= sort_link(@q, 'description') %> @@ -26,6 +26,9 @@ <%= sort_link(@q, 'sum') %> + <%= sort_link(@q, 'new_balance', 'New balance') %> +
<%= s %> + <%= x.new_balance.present? ? "#{currency(x.new_balance)} EUR" : 'N/A' %> +