From ad75a79135db8179fc3a05c55db58710a0cbae09 Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Mon, 10 Apr 2023 21:07:01 +0530 Subject: [PATCH] feat: add pagination support for audit logs API (#6843) Add pagination support for audit logs API --- .gitignore | 2 -- .../api/v1/accounts/audit_logs_controller.rb | 10 +++++++++- .../api/v1/accounts/audit_logs_controller_spec.rb | 10 ++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 4182a0ac7..bf9a98fa5 100644 --- a/.gitignore +++ b/.gitignore @@ -62,7 +62,5 @@ test/cypress/videos/* /config/*.enc .vscode/settings.json - # yalc for local testing .yalc -yalc.lock \ No newline at end of file diff --git a/enterprise/app/controllers/api/v1/accounts/audit_logs_controller.rb b/enterprise/app/controllers/api/v1/accounts/audit_logs_controller.rb index 02db5156b..1c4dc6647 100644 --- a/enterprise/app/controllers/api/v1/accounts/audit_logs_controller.rb +++ b/enterprise/app/controllers/api/v1/accounts/audit_logs_controller.rb @@ -3,8 +3,16 @@ class Api::V1::Accounts::AuditLogsController < Api::V1::Accounts::BaseController before_action :check_admin_authorization? before_action :fetch_audit + RESULTS_PER_PAGE = 15 + def show - render json: @audit_logs + @audit_logs = @audit_logs.page(params[:page]).per(RESULTS_PER_PAGE) + render json: { + audit_logs: @audit_logs, + current_page: @audit_logs.current_page, + per_page: RESULTS_PER_PAGE, + total_entries: @audit_logs.total_count + } end private diff --git a/spec/enterprise/controllers/api/v1/accounts/audit_logs_controller_spec.rb b/spec/enterprise/controllers/api/v1/accounts/audit_logs_controller_spec.rb index 3d7b2e0e4..88ca5891e 100644 --- a/spec/enterprise/controllers/api/v1/accounts/audit_logs_controller_spec.rb +++ b/spec/enterprise/controllers/api/v1/accounts/audit_logs_controller_spec.rb @@ -34,10 +34,12 @@ RSpec.describe 'Enterprise Audit API', type: :request do expect(response).to have_http_status(:success) json_response = JSON.parse(response.body) - expect(json_response[0]['auditable_type']).to eql('Inbox') - expect(json_response[0]['action']).to eql('create') - expect(json_response[0]['audited_changes']['name']).to eql(inbox.name) - expect(json_response[0]['associated_id']).to eql(account.id) + expect(json_response['audit_logs'][0]['auditable_type']).to eql('Inbox') + expect(json_response['audit_logs'][0]['action']).to eql('create') + expect(json_response['audit_logs'][0]['audited_changes']['name']).to eql(inbox.name) + expect(json_response['audit_logs'][0]['associated_id']).to eql(account.id) + expect(json_response['current_page']).to be(1) + expect(json_response['total_entries']).to be(1) end end end