Skip to content

Commit

Permalink
Add Download button to Audit logs table
Browse files Browse the repository at this point in the history
  • Loading branch information
tungleduyxyz committed Sep 8, 2024
1 parent 2145638 commit eb25dfa
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/kaui/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def pagination
paginate searcher, data_extractor, formatter, default_columns(Kaui.account_search_columns.call()[2], Kaui::Account::SENSIVITE_DATA_FIELDS)
end

def download_accounts
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)

Expand Down
25 changes: 25 additions & 0 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
class AuditLogsController < Kaui::EngineController
Expand Down Expand Up @@ -42,6 +43,30 @@ def index
@audit_logs_json = @audit_logs_json.to_json
end

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

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]
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
end
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

send_data csv_file, type: 'text/csv', filename: "audit_logs_#{account_id}.csv"
end

def history
json_response do
account_id = params.require(:account_id)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/kaui/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index
@max_nb_records = @search_query.blank? ? Kaui::Invoice.list_or_search(nil, 0, 0, options_for_klient).pagination_max_nb_records : 0
end

def download_invoices
def download
account_id = params[:account_id]
start_date = params[:startDate]
end_date = params[:endDate]
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/kaui/payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index
@max_nb_records = @search_query.blank? ? Kaui::Payment.list_or_search(nil, 0, 0, options_for_klient).pagination_max_nb_records : 0
end

def download_payments
def download
account_id = params[:account_id]
start_date = params[:startDate]
end_date = params[:endDate]
Expand Down
205 changes: 205 additions & 0 deletions app/views/kaui/audit_logs/_multi_functions_bar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<div class="dropdown-container">
<button class="btn btn-default download-button-right" type="button" id="modalDownloadButton">
<i class="glyphicon glyphicon-download-alt"></i>
<strong>Download CSV</strong>
</button>
</div>

<div class="modal fade" id="downloadCsvModal" tabindex="-1" role="dialog" aria-labelledby="downloadCsvModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="downloadCsvModalLabel">Download</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form id="downloadCsvForm">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="startDate">Start Date:</label>
<input type="text" class="form-control" id="startDate" name="startDate">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="endDate">End Date:</label>
<input type="text" class="form-control" id="endDate" name="endDate">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-check">
<div>
<input type="radio" id="customDate" name="download_option" value="customDate">
<label for="customDate">Custom date</label>
</div>
<div>
<input type="radio" id="allData" name="download_option" value="all">
<label for="allData">All logs</label>
</div>
<div>
<input type="radio" id="thisWeek" name="download_option" value="thisWeek">
<label for="thisWeek">This week</label>
</div>
<div>
<input type="radio" id="thisMonth" name="download_option" value="thisMonth">
<label for="thisMonth">This month</label>
</div>
<div>
<input type="radio" id="thisYear" name="download_option" value="thisYear">
<label for="thisYear">This year</label>
</div>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="downloadButton">Download</button>
</div>
</div>
</div>
</div>

<style>
.dropdown-menu#column-visibility {
max-height: 300px;
width: 220px;
overflow-y: auto;
}

.dropdown-menu {
padding: 5px;
}
.toggle-button-right {
float: right;
margin-bottom: 10px;
margin-left: 10px;
background-color: white;
color: black;
text-transform: none;
border: 1px solid #ccc;
padding: 8px 15px;
}

.download-button-right {
float: right;
margin-bottom: 10px;
margin-left: 10px;
background-color: white;
color: black;
text-transform: none;
border: 1px solid #ccc;
padding: 8px 15px;
}

.icon-drag {
float: right;
padding: 5px;
}

.dropdown-container {
display: flex;
justify-content: flex-end;
}

.label-group-item-manual {
margin: 5px;
width: -webkit-fill-available;
cursor: grab;
}
.label-group-item-manual:active {
cursor: grabbing;
}
</style>

<%= javascript_tag do %>
$(document).ready(function() {
$('#modalDownloadButton').click(function() {
$('#downloadCsvModal').modal('show');
});
$('#startDate, #endDate').datepicker();

$('#downloadCsvModal').on('show.bs.modal', function (e) {
$('#customDate').prop('checked', true);
$('#startDate, #endDate').prop('disabled', false);
$('#startDate').val(null);
$('#endDate').val(null);
});

$('#allData').change(function() {
var isChecked = $(this).is(':checked');
$('#startDate, #endDate').prop('disabled', true);
});

function setDateRange(option) {
var currentDate = new Date();
var startDate, endDate;

if (option === "week") {
startDate = new Date(currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 1));
currentDate = new Date();
endDate = new Date(currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 7));
} else if (option === "month") {
startDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
endDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0);
} else if (option === "year") {
startDate = new Date(currentDate.getFullYear(), 0, 1);
endDate = new Date(currentDate.getFullYear(), 11, 31);
}

var startDateFormatted = startDate.toISOString().split('T')[0];
var endDateFormatted = endDate.toISOString().split('T')[0];

$('#startDate').val(startDateFormatted);
$('#endDate').val(endDateFormatted);
$('#startDate, #endDate').prop('disabled', true);
}

$('#thisWeek').change(function() {
if ($(this).is(':checked')) {
setDateRange("week");
}
});

$('#thisMonth').change(function() {
if ($(this).is(':checked')) {
setDateRange("month");
}
});

$('#thisYear').change(function() {
if ($(this).is(':checked')) {
setDateRange("year");
}
});

$('#customDate').change(function() {
var isChecked = $(this).is(':checked');
$('#startDate, #endDate').prop('disabled', false);
$('#startDate').val(null);
$('#endDate').val(null);
});

var downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
event.preventDefault();
var startDate = $('#startDate').val();
var endDate = $('#endDate').val();
var downloadAll = $('#allData').is(':checked');

if (downloadAll) {
window.open("<%= download_audit_logs_path %>?account_id=<%=@account.account_id%>", '_blank');
} else {
window.open("<%= download_audit_logs_path %>?account_id=<%=@account.account_id%>&startDate="+startDate+"&endDate="+endDate, '_blank');
}
});
}
});
<% end %>
1 change: 1 addition & 0 deletions app/views/kaui/audit_logs/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<div class="column-block">

<h1>Audit logs</h1>
<%= render :partial => 'multi_functions_bar' %>
<input type="hidden" id="audit-logs" value="<%= @audit_logs_json %>">
<table id="audit-logs-table" class="table table-condensed table-colored-rows mobile-data">
<thead>
Expand Down
7 changes: 4 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def nested_param
scope '/accounts' do
match '/pagination' => 'accounts#pagination', :via => :get, :as => 'accounts_pagination'
match '/validate_external_key' => 'accounts#validate_external_key', :via => :get, :as => 'accounts_validate_external_key'
match '/download_accounts' => 'accounts#download_accounts', :via => :get, :as => 'download_accounts'
match '/download' => 'accounts#download', :via => :get, :as => 'download_accounts'
get '/export/:account_id', to: 'accounts#export_account', as: 'export_account'

scope '/email_notifications' do
Expand Down Expand Up @@ -66,6 +66,7 @@ def nested_param
end
scope '/audit_logs' do
match '/history' => 'audit_logs#history', :via => :get, :as => 'audit_logs_history'
match '/download' => 'audit_logs#download', :via => :get, :as => 'download_audit_logs'
end
end
end
Expand Down Expand Up @@ -100,7 +101,7 @@ def nested_param

scope '/invoices' do
match '/pagination' => 'invoices#pagination', :via => :get, :as => 'invoices_pagination'
match '/download_invoices' => 'invoices#download_invoices', :via => :get, :as => 'download_invoices'
match '/download' => 'invoices#download', :via => :get, :as => 'download_invoices'
match '/:id/show_html' => 'invoices#show_html', :via => :get, :as => 'show_html_invoice'
match '/:number' => 'invoices#restful_show_by_number', :via => :get, :constraints => { number: /\d+/ }
match '/:id' => 'invoices#restful_show', :via => :get, :as => 'invoice'
Expand All @@ -116,7 +117,7 @@ def nested_param

scope '/payments' do
match '/pagination' => 'payments#pagination', :via => :get, :as => 'payments_pagination'
match '/download_payments' => 'payments#download_payments', :via => :get, :as => 'download_payments'
match '/download' => 'payments#download', :via => :get, :as => 'download_payments'
match '/:id' => 'payments#restful_show', :via => :get, :as => 'payment'
match '/:id/cancel_scheduled_payment' => 'payments#cancel_scheduled_payment', :via => :delete, :as => 'payment_cancel_scheduled_payment'
end
Expand Down
6 changes: 6 additions & 0 deletions lib/kaui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Kaui
mattr_accessor :invoice_search_columns
mattr_accessor :account_invoices_columns
mattr_accessor :account_payments_columns
mattr_accessor :account_audit_logs_columns
mattr_accessor :refund_invoice_description

mattr_accessor :customer_invoice_link
Expand Down Expand Up @@ -164,6 +165,11 @@ module Kaui
[headers, values]
end

self.account_audit_logs_columns = lambda do
headers = %w[CreatedDate ObjectID ObjectType ChangeType Username Reason Comment UserToken]
[headers, []]
end

self.refund_invoice_description = lambda { |index, ii, bundle_result|
"Item #{index + 1} : #{ii.description} #{"(bundle #{bundle_result.external_key})" unless bundle_result.nil?}"
}
Expand Down

0 comments on commit eb25dfa

Please sign in to comment.