Class: Kaui::AuditLogsController

Inherits:
EngineController show all
Defined in:
app/controllers/kaui/audit_logs_controller.rb

Constant Summary collapse

OBJECT_WITH_HISTORY =
%w[ACCOUNT ACCOUNT_EMAIL BLOCKING_STATES BUNDLE CUSTOM_FIELD INVOICE INVOICE_ITEM PAYMENT_ATTEMPT PAYMENT PAYMENT_METHOD SUBSCRIPTION SUBSCRIPTION_EVENT TRANSACTION TAG TAG_DEFINITION].freeze

Constants included from EngineControllerUtil

EngineControllerUtil::SIMPLE_PAGINATION_THRESHOLD

Instance Method Summary collapse

Methods inherited from EngineController

#check_for_redirect_to_tenant_screen, #current_ability, #current_user, #options_for_klient, #populate_account_details, #retrieve_allowed_users_for_current_user, #retrieve_tenants_for_current_user

Instance Method Details

#historyObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/kaui/audit_logs_controller.rb', line 45

def history
  json_response do
     = params.require(:account_id)
    object_id = params.require(:object_id)
    object_type = params.require(:object_type)
    cached_options_for_klient = options_for_klient

    audit_logs_with_history = []
    error = nil

    begin
      case object_type
      when 'ACCOUNT'
         = Kaui::Account.find_by_id_or_key(object_id, false, false, cached_options_for_klient)
        audit_logs_with_history = .audit_logs_with_history(cached_options_for_klient)
      when 'ACCOUNT_EMAIL'
         = Kaui::Account.find_by_id_or_key(, false, false, cached_options_for_klient)
        audit_logs_with_history = .email_audit_logs_with_history(object_id, cached_options_for_klient)
      when 'BLOCKING_STATES'
        audit_logs_with_history = Kaui::Account.blocking_state_audit_logs_with_history(object_id, cached_options_for_klient)
      when 'BUNDLE'
        bundle = Kaui::Bundle.find_by_id(object_id, cached_options_for_klient)
        audit_logs_with_history = bundle.audit_logs_with_history(cached_options_for_klient)
      when 'CUSTOM_FIELD'
        audit_logs_with_history = Kaui::CustomField.new({ custom_field_id: object_id }).audit_logs_with_history(cached_options_for_klient)
      when 'INVOICE'
        invoice = Kaui::Invoice.find_by_id(object_id, 'NONE', cached_options_for_klient)
        audit_logs_with_history = invoice.audit_logs_with_history(cached_options_for_klient)
      when 'INVOICE_ITEM'
        invoice_item = Kaui::InvoiceItem.new
        invoice_item.invoice_item_id = object_id
        audit_logs_with_history = invoice_item.audit_logs_with_history(cached_options_for_klient)
      # See https://github.com/killbill/killbill/issues/1104
      #         elsif object_type == 'INVOICE_PAYMENT'
      #           invoice_payment = Kaui::InvoicePayment::find_by_id(object_id, false, false, cached_options_for_klient)
      #           audit_logs_with_history = invoice_payment.audit_logs_with_history(cached_options_for_klient)
      when 'PAYMENT_ATTEMPT'
        audit_logs_with_history = Kaui::Payment.attempt_audit_logs_with_history(object_id, cached_options_for_klient)
      when 'PAYMENT'
        payment = Kaui::Payment.find_by_id(object_id, false, false, cached_options_for_klient)
        audit_logs_with_history = payment.audit_logs_with_history(cached_options_for_klient)
      when 'PAYMENT_METHOD'
        payment_method = Kaui::PaymentMethod.find_by_id(object_id, false, cached_options_for_klient)
        audit_logs_with_history = payment_method.audit_logs_with_history(cached_options_for_klient)
      when 'SUBSCRIPTION'
        subscription = Kaui::Subscription.find_by_id(object_id, cached_options_for_klient)
        audit_logs_with_history = subscription.audit_logs_with_history(cached_options_for_klient)
      when 'SUBSCRIPTION_EVENT'
        audit_logs_with_history = Kaui::Subscription.event_audit_logs_with_history(object_id, cached_options_for_klient)
      when 'TRANSACTION'
        audit_logs_with_history = Kaui::Transaction.new({ transaction_id: object_id }).audit_logs_with_history(cached_options_for_klient)
      when 'TAG'
        audit_logs_with_history = Kaui::Tag.new({ tag_id: object_id }).audit_logs_with_history(cached_options_for_klient)
      when 'TAG_DEFINITION'
        audit_logs_with_history = Kaui::TagDefinition.new({ id: object_id }).audit_logs_with_history(cached_options_for_klient)
      else
        error = "Object [#{object_type}] history is not supported."
      end
    rescue StandardError => e
      error = e.message
    end

    { audits: audit_logs_with_history, error: }
  end
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/kaui/audit_logs_controller.rb', line 7

def index
  cached_options_for_klient = options_for_klient
  @account = Kaui::Account.find_by_id_or_key(params.require(:account_id), false, false, cached_options_for_klient)
  audit_logs = @account.audit(cached_options_for_klient)

  formatter = lambda do |log|
    object_id_text = view_context.object_id_popover(log.object_id)

    if object_with_history?(log.object_type)
      object_id_text = view_context.link_to(object_id_text, '#showHistoryModal',
                                            data: {
                                              toggle: 'modal',
                                              object_id: log.object_id,
                                              object_type: log.object_type,
                                              change_date: log.change_date,
                                              change_type: log.change_type,
                                              account_id: @account.
                                            })
    end

    [
      log.change_date,
      object_id_text,
      log.object_type,
      log.change_type,
      log.changed_by,
      log.reason_code,
      log.comments,
      view_context.object_id_popover(log.user_token, 'left')
    ]
  end

  @audit_logs_json = []
  audit_logs.each { |page| @audit_logs_json << formatter.call(page) }

  @audit_logs_json = @audit_logs_json.to_json
end