mirror of
https://github.com/internetee/registry.git
synced 2025-06-12 23:54:44 +02:00
Merge pull request #1821 from internetee/1819-improve-credit-account-balance-overview
Registrar / REPP: Preserve balance after billable action
This commit is contained in:
commit
98bbc52c71
6 changed files with 68 additions and 3 deletions
|
@ -4,8 +4,28 @@ module Repp
|
||||||
def balance
|
def balance
|
||||||
resp = { balance: current_user.registrar.cash_account.balance,
|
resp = { balance: current_user.registrar.cash_account.balance,
|
||||||
currency: current_user.registrar.cash_account.currency }
|
currency: current_user.registrar.cash_account.currency }
|
||||||
|
resp[:transactions] = activities if params[:detailed] == 'true'
|
||||||
render_success(data: resp)
|
render_success(data: resp)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def activities
|
||||||
|
arr = []
|
||||||
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,6 +13,9 @@ class AccountActivity < ApplicationRecord
|
||||||
def update_balance
|
def update_balance
|
||||||
account.balance += sum
|
account.balance += sum
|
||||||
account.save
|
account.save
|
||||||
|
|
||||||
|
self.new_balance = account.balance
|
||||||
|
save
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
<table class="table table-hover table-condensed">
|
<table class="table table-hover table-condensed">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th class="col-xs-5">
|
<th class="col-xs-3">
|
||||||
<%= sort_link(@q, 'description') %>
|
<%= sort_link(@q, 'description') %>
|
||||||
</th>
|
</th>
|
||||||
<th class="col-xs-2">
|
<th class="col-xs-2">
|
||||||
|
@ -26,6 +26,9 @@
|
||||||
<th class="col-xs-2">
|
<th class="col-xs-2">
|
||||||
<%= sort_link(@q, 'sum') %>
|
<%= sort_link(@q, 'sum') %>
|
||||||
</th>
|
</th>
|
||||||
|
<th class="col-xs-2">
|
||||||
|
<%= sort_link(@q, 'new_balance', 'New balance') %>
|
||||||
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
@ -45,6 +48,9 @@
|
||||||
<td class="<%= c %>">
|
<td class="<%= c %>">
|
||||||
<%= s %>
|
<%= s %>
|
||||||
</td>
|
</td>
|
||||||
|
<td>
|
||||||
|
<%= x.new_balance.present? ? "#{currency(x.new_balance)} EUR" : 'N/A' %>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
|
@ -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
|
|
@ -226,7 +226,8 @@ CREATE TABLE public.account_activities (
|
||||||
creator_str character varying,
|
creator_str character varying,
|
||||||
updator_str character varying,
|
updator_str character varying,
|
||||||
activity_type 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'),
|
('20200908131554'),
|
||||||
('20200910085157'),
|
('20200910085157'),
|
||||||
('20200910102028'),
|
('20200910102028'),
|
||||||
('20200916125326');
|
('20200916125326'),
|
||||||
|
('20210215101019');
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ require 'test_helper'
|
||||||
|
|
||||||
class ReppV1BalanceTest < ActionDispatch::IntegrationTest
|
class ReppV1BalanceTest < ActionDispatch::IntegrationTest
|
||||||
def setup
|
def setup
|
||||||
|
travel_to Time.zone.parse('2010-07-05')
|
||||||
@registrar = users(:api_bestnames)
|
@registrar = users(:api_bestnames)
|
||||||
token = Base64.encode64("#{@registrar.username}:#{@registrar.plain_text_password}")
|
token = Base64.encode64("#{@registrar.username}:#{@registrar.plain_text_password}")
|
||||||
token = "Basic #{token}"
|
token = "Basic #{token}"
|
||||||
|
@ -19,4 +20,32 @@ class ReppV1BalanceTest < ActionDispatch::IntegrationTest
|
||||||
assert_equal @registrar.registrar.cash_account.balance.to_s, json[:data][:balance]
|
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.currency, json[:data][:currency]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_can_query_balance_with_details
|
||||||
|
# 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", 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]
|
||||||
|
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][: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
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue