Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tungleduyxyz committed Sep 11, 2024
1 parent cea13f0 commit 6d4e3d3
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 35 deletions.
19 changes: 16 additions & 3 deletions app/controllers/kaui/account_timelines_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,21 @@ def show

def download
timeline = Kaui::AccountTimeline.find_by_account_id(params.require(:account_id), 'FULL', options_for_klient)
start_date = params[:startDate]
end_date = params[:endDate]
start_date = begin
Date.parse(start_date)
rescue StandardError
nil
end
end_date = begin
Date.parse(end_date)
rescue StandardError
nil
end
start_date = params[:startDate].present? ? Date.parse(params[:startDate]) : nil
end_date = params[:endDate].present? ? Date.parse(params[:endDate]) : nil

event_type = params[:eventType]
@account = timeline.account
@bundles = timeline.bundles
Expand Down Expand Up @@ -66,14 +79,14 @@ def download
csv << ['Effective Date', 'Bundles', 'Even Type', 'Details', 'Reason Code/ Comments']
if %w[INVOICE ALL].include?(event_type)
@invoices.each do |invoice_stub|
invoice = invoice_stub.invoice_id.present? && @invoices_by_id.has_key?(invoice_stub.invoice_id) ? @invoices_by_id[invoice_stub.invoice_id] : invoice_stub
invoice = invoice_stub.invoice_id.present? && @invoices_by_id.key?(invoice_stub.invoice_id) ? @invoices_by_id[invoice_stub.invoice_id] : invoice_stub
target_date = invoice.target_date.present? ? invoice.target_date : '[unknown]'
bundle_keys = invoice_stub.bundle_keys.present? ? invoice_stub.bundle_keys.split(',').map { |bundle_key| @bundle_names[bundle_key] }.join(', ') : ''
invoice_details = []
invoice_details << "Amount: #{invoice.amount_to_money} (#{@account.currency})"
invoice_details << "Balance: #{invoice.balance_to_money} (#{@account.currency})"
invoice_details << "Credit adjustment: #{invoice.credit_adjustment_to_money} (#{@account.currency})" if invoice.credit_adj.present? && invoice.credit_adj > 0
invoice_details << "Refund adjustment: #{invoice.refund_adjustment_to_money} (#{@account.currency})" if invoice.refund_adj.present? && invoice.refund_adj < 0
invoice_details << "Credit adjustment: #{invoice.credit_adjustment_to_money} (#{@account.currency})" if invoice.credit_adj.present? && invoice.credit_adj.positive?
invoice_details << "Refund adjustment: #{invoice.refund_adjustment_to_money} (#{@account.currency})" if invoice.refund_adj.present? && invoice.refund_adj.negative?
invoice_details << "Invoice #: #{invoice.invoice_number}"
audit_logs = invoice_stub.audit_logs.present? ? invoice_stub.audit_logs.map { |entry| Kaui::AuditLog.description(entry) }.join(', ') : ''
csv << [target_date, bundle_keys, 'INVOICE', invoice_details.join('; '), audit_logs] if filter_date(target_date, start_date, end_date)
Expand Down
24 changes: 19 additions & 5 deletions app/controllers/kaui/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ def index
return
end

@dropdown_default = default_columns(Kaui.account_search_columns.call()[2], Kaui::Account::SENSIVITE_DATA_FIELDS)
@dropdown_default = default_columns(Kaui.account_search_columns.call[2], Kaui::Account::SENSIVITE_DATA_FIELDS)

@ordering = params[:ordering] || (@search_query.blank? ? 'desc' : 'asc')
@offset = params[:offset] || 0
@limit = params[:limit] || 4
@limit = params[:limit] || 50

@max_nb_records = @search_query.blank? ? Kaui::Account.list_or_search(nil, 0, 0, options_for_klient).pagination_max_nb_records : 0
end

def pagination
cached_options_for_klient = options_for_klient

searcher = lambda do |search_key, offset, limit|
Kaui::Account.list_or_search(search_key, offset, limit, cached_options_for_klient)
end
Expand All @@ -46,19 +45,34 @@ def pagination
Kaui.account_search_columns.call(account, view_context)[1]
end

paginate searcher, data_extractor, formatter, default_columns(Kaui.account_search_columns.call()[2], Kaui::Account::SENSIVITE_DATA_FIELDS)
paginate searcher, data_extractor, formatter, default_columns(Kaui.account_search_columns.call[2], Kaui::Account::SENSIVITE_DATA_FIELDS)
end

def download
columns = params.require(:columnsString).split(',').map { |attr| attr.split.join('_').downcase }
accounts = Kaui::Account.list_or_search(nil, 0, 1000, options_for_klient)
start_date = params[:startDate]
end_date = params[:endDate]
start_date = begin
Date.parse(start_date)
rescue StandardError
nil
end
end_date = begin
Date.parse(end_date)
rescue StandardError
nil
end
accounts = Kaui::Account.list_or_search(nil, 0, MAXIMUM_NUMBER_OF_RECORDS_DOWNLOAD, options_for_klient)

csv_string = CSV.generate(headers: true) do |csv|
csv << columns
accounts.each do |account|
change_date = Date.parse(account.reference_time)
data = columns.map do |attr|
account&.send(attr.downcase)
end
next if start_date && end_date && change_date && (change_date < start_date || change_date > end_date)

csv << data
end
end
Expand Down
24 changes: 18 additions & 6 deletions app/controllers/kaui/audit_logs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'csv'

module Kaui
Expand Down Expand Up @@ -47,19 +48,30 @@ def download
account_id = params.require(:account_id)
start_date = params[:startDate]
end_date = params[:endDate]
start_date = Date.parse(start_date) rescue nil
end_date = Date.parse(end_date) rescue nil
start_date = begin
Date.parse(start_date)
rescue StandardError
nil
end
end_date = begin
Date.parse(end_date)
rescue StandardError
nil
end

account = Kaui::Account.find_by_id_or_key(account_id, false, false, options_for_klient)
audit_logs = account.audit(options_for_klient)

csv_file = CSV.generate do |csv|
csv << Kaui.account_audit_logs_columns.call()[0]
csv << Kaui.account_audit_logs_columns.call[0]
audit_logs.each do |log|
change_date = Date.parse(log.change_date) rescue nil
if start_date && end_date && change_date
next unless change_date > start_date && change_date < end_date
change_date = begin
Date.parse(log.change_date)
rescue StandardError
nil
end
next if start_date && end_date && change_date && !(change_date > start_date && change_date < end_date)

csv << [log.change_date, log.object_id, log.object_type, log.change_type, log.changed_by, log.reason_code, log.comments, log.user_token]
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/kaui/engine_controller_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Kaui
module EngineControllerUtil
# See DefaultPaginationSqlDaoHelper.java
SIMPLE_PAGINATION_THRESHOLD = 20_000
MAXIMUM_NUMBER_OF_RECORDS_DOWNLOAD = 1000

protected

Expand Down Expand Up @@ -154,7 +155,7 @@ def json_response
end

def default_columns(fields, sensivite_fields)
fields.map { |field| { "data": fields.index(field), "visible": !(sensivite_fields.include? field) } }
fields.map { |field| { data: fields.index(field), visible: !(sensivite_fields.include? field) } }
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/kaui/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def download
account = Kaui::Account.find_by_id_or_key(account_id, false, false, options_for_klient)
invoices = account.invoices(options_for_klient.merge(params: kb_params))
else
invoices = Kaui::Invoice.list_or_search(nil, 0, 0, options_for_klient.merge(params: kb_params))
invoices = Kaui::Invoice.list_or_search(nil, 0, MAXIMUM_NUMBER_OF_RECORDS_DOWNLOAD, options_for_klient.merge(params: kb_params))
end

csv_string = CSV.generate(headers: true) do |csv|
Expand Down Expand Up @@ -65,7 +65,7 @@ def pagination
end
formatter = lambda do |invoice|
row = [view_context.link_to(invoice.invoice_number, view_context.url_for(controller: :invoices, action: :show, account_id: invoice.account_id, id: invoice.invoice_id))]
row += Kaui.invoice_search_columns.call(invoice, view_context)[1]
row += Kaui.invoice_search_columns.call(invoice, view_context, cached_options_for_klient)[1]
row
end
else
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/kaui/payments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'csv'

module Kaui
Expand All @@ -25,7 +26,7 @@ def download
account = Kaui::Account.find_by_id_or_key(account_id, false, false, options_for_klient)
payments = account.payments(options_for_klient).map! { |payment| Kaui::Payment.build_from_raw_payment(payment) }
else
payments = Kaui::Payment.list_or_search(nil, 0, 0, options_for_klient.merge(params: kb_params))
payments = Kaui::Payment.list_or_search(nil, 0, MAXIMUM_NUMBER_OF_RECORDS_DOWNLOAD, options_for_klient.merge(params: kb_params))
end
payments.each do |payment|
created_date = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ $(document).ready(function() {
$('#modalDownloadButton').click(function() {
$('#downloadCsvModal').modal('show');
});
$('#startDate, #endDate').datepicker();
$('#startDate, #endDate').datepicker({
dateFormat: 'yy-mm-dd'
});

$('#downloadCsvModal').on('show.bs.modal', function (e) {
$('#customDate').prop('checked', true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<strong>Edit Columns</strong>
</button>
<ul class="dropdown-menu" id="column-visibility" aria-labelledby="v">
<% Kaui.account_search_columns.call()[0].each_with_index do |title, index| %>
<% Kaui.account_search_columns.call[0].each_with_index do |title, index| %>
<li class="list-group-item-manual" data-id="<%= index %>">
<label class="label-group-item-manual">
<input type="checkbox" class="column-toggle" draggable="true" data-column="<%= index %>" <%= 'checked' if @dropdown_default[index][:visible] %> > <%= title %>
Expand Down Expand Up @@ -50,23 +50,23 @@
<div class="col-md-6">
<div class="form-check">
<div>
<input disabled type="radio" id="customDate" name="download_option" value="customDate">
<input type="radio" id="customDate" name="download_option" value="customDate">
<label for="customDate">Custom date</label>
</div>
<div>
<input type="radio" id="allAccounts" name="download_option" value="all">
<label for="allAccounts">All accounts</label>
</div>
<div>
<input disabled type="radio" id="thisWeek" name="download_option" value="thisWeek">
<input type="radio" id="thisWeek" name="download_option" value="thisWeek">
<label for="thisWeek">This week</label>
</div>
<div>
<input disabled type="radio" id="thisMonth" name="download_option" value="thisMonth">
<input type="radio" id="thisMonth" name="download_option" value="thisMonth">
<label for="thisMonth">This month</label>
</div>
<div>
<input disabled type="radio" id="thisYear" name="download_option" value="thisYear">
<input type="radio" id="thisYear" name="download_option" value="thisYear">
<label for="thisYear">This year</label>
</div>
</div>
Expand Down Expand Up @@ -140,7 +140,9 @@ $(document).ready(function() {
$('#modalDownloadButton').click(function() {
$('#downloadCsvModal').modal('show');
});
$('#startDate, #endDate').datepicker();
$('#startDate, #endDate').datepicker({
dateFormat: 'yy-mm-dd'
});

$('#downloadCsvModal').on('show.bs.modal', function (e) {
$('#allAccounts').prop('checked', true);
Expand Down
4 changes: 2 additions & 2 deletions app/views/kaui/accounts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<% else %>
<h1>Showing all accounts</h1>
<% end %>
<%= render :partial => 'edit_columns' %>
<%= render :partial => 'multi_functions_bar' %>

<table id="accounts-table" class="table table-condensed mobile-data" style="width:100%">
<thead>
<tr>
<% Kaui.account_search_columns.call()[0].each do |title| %>
<% Kaui.account_search_columns.call[0].each do |title| %>
<th><%= title %></th>
<% end %>
</tr>
Expand Down
4 changes: 3 additions & 1 deletion app/views/kaui/audit_logs/_multi_functions_bar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ $(document).ready(function() {
$('#modalDownloadButton').click(function() {
$('#downloadCsvModal').modal('show');
});
$('#startDate, #endDate').datepicker();
$('#startDate, #endDate').datepicker({
dateFormat: 'yy-mm-dd'
});

$('#downloadCsvModal').on('show.bs.modal', function (e) {
$('#customDate').prop('checked', true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ $(document).ready(function() {
$('#modalDownloadButton').click(function() {
$('#downloadCsvModal').modal('show');
});
$('#startDate, #endDate').datepicker();
$('#startDate, #endDate').datepicker({
dateFormat: 'yy-mm-dd'
});

$('#downloadCsvModal').on('show.bs.modal', function (e) {
$('#customDate').prop('checked', true);
Expand Down
2 changes: 1 addition & 1 deletion app/views/kaui/invoices/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<h1>Invoices</h1>

<%= render :partial => 'edit_columns' %>
<%= render :partial => 'multi_functions_bar' %>

<table id="invoices-table" class="table table-condensed mobile-data" style="width:100%">
<thead>
Expand Down
4 changes: 3 additions & 1 deletion app/views/kaui/payments/_multi_functions_bar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ $(document).ready(function() {
$('#modalDownloadButton').click(function() {
$('#downloadCsvModal').modal('show');
});
$('#startDate, #endDate').datepicker();
$('#startDate, #endDate').datepicker({
dateFormat: 'yy-mm-dd'
});

$('#downloadCsvModal').on('show.bs.modal', function (e) {
$('#customDate').prop('checked', true);
Expand Down
8 changes: 4 additions & 4 deletions lib/kaui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ module Kaui
[headers, values, fields]
end

self.invoice_search_columns = lambda do |invoice = nil, view_context = nil|
fields = ['invoice_number', 'invoice_date', 'status']
self.invoice_search_columns = lambda do |invoice = nil, view_context = nil, _cached_options_for_klient = nil|
fields = %w[invoice_number invoice_date status]
headers = fields.map { |attr| attr.split('_').join(' ').capitalize }
values = fields.map do |attr|
case attr
Expand Down Expand Up @@ -134,8 +134,8 @@ module Kaui
self.account_payments_columns = lambda do |account = nil, payment = nil, view_context = nil|
fields = KillBillClient::Model::PaymentAttributes.instance_variable_get('@json_attributes')
# Change the order if needed
fields = ['payment_date', 'total_authed_amount_to_money', 'paid_amount_to_money', 'returned_amount_to_money'] + fields
fields -= ['payment_number', 'transactions', 'audit_logs']
fields = %w[payment_date total_authed_amount_to_money paid_amount_to_money returned_amount_to_money] + fields
fields -= %w[payment_number transactions audit_logs]
fields.unshift('status')
fields.unshift('payment_number')

Expand Down

0 comments on commit 6d4e3d3

Please sign in to comment.