Method: Kaui::AccountsController#show

Defined in:
app/controllers/kaui/accounts_controller.rb

#showObject



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/kaui/accounts_controller.rb', line 78

def show
  # Go to the database once
  cached_options_for_klient = options_for_klient

  # Re-fetch the account with balance and CBA
  @account = Kaui::Account::find_by_id_or_key(params.require(:account_id), true, true, cached_options_for_klient)

  fetch_children = promise { @account.children(false, false, 'NONE',cached_options_for_klient)}
  fetch_parent = promise (!@account..nil?){ Kaui::Account::find_by_id(@account.,false,false,cached_options_for_klient)}
  fetch_overdue_state = promise { @account.overdue(cached_options_for_klient) }
   = promise { @account.tags(false, 'NONE', cached_options_for_klient).sort { |tag_a, tag_b| tag_a <=> tag_b } }
   = promise { @account.custom_fields('NONE', cached_options_for_klient).sort { |cf_a, cf_b| cf_a.name.downcase <=> cf_b.name.downcase } }
   = promise { Kaui::AccountEmail.(@account., 'NONE', cached_options_for_klient) }
  fetch_payments = promise { @account.payments(cached_options_for_klient).map! { |payment| Kaui::Payment.build_from_raw_payment(payment) } }
  fetch_payment_methods = promise(false) { Kaui::PaymentMethod.(@account., false, cached_options_for_klient) }

  # is email notification plugin available
  is_email_notifications_plugin_available = Kenui::EmailNotificationService.email_notification_plugin_available?(cached_options_for_klient).first
  fetch_email_notification_configuration = promise(is_email_notifications_plugin_available) do
    Kenui::EmailNotificationService.(params.require(:account_id),cached_options_for_klient)
  end.then do |configuration|
    if configuration.first.is_a?(FalseClass)
      Rails.logger.warn(configuration[1])
      configuration = []
    end
    configuration
  end

  fetch_payment_methods_with_details = fetch_payment_methods.then do |pms|
    ops = []
    pms.each do |pm|
      ops << promise(false) {
        begin
          Kaui::PaymentMethod.find_by_id(pm.payment_method_id, true, cached_options_for_klient)
        rescue => e
          # Maybe the plugin is not registered or the plugin threw an exception
          Rails.logger.warn(e)
          nil
        end
      }
    end
    ops
  end
  fetch_available_tags = promise { Kaui::TagDefinition.(cached_options_for_klient) }

  @overdue_state = wait(fetch_overdue_state)
  @tags = wait()
  @custom_fields = wait()
  @account_emails = wait()
  wait(fetch_payment_methods)
  @payment_methods = wait(fetch_payment_methods_with_details).map { |pm_f| pm_f.execute }.map { |pm_f| wait(pm_f) }.reject { |pm| pm.nil? }
  @available_tags = wait(fetch_available_tags)
  @children = wait(fetch_children)
  @account_parent = @account..nil? ? nil : wait(fetch_parent)
  @email_notification_configuration = wait(fetch_email_notification_configuration) if is_email_notifications_plugin_available

  @last_transaction_by_payment_method_id = {}
  wait(fetch_payments).each do |payment|
    transaction = payment.transactions.last
    transaction_date = Date.parse(transaction.effective_date)

    last_seen_transaction_date = @last_transaction_by_payment_method_id[payment.payment_method_id]
    if last_seen_transaction_date.nil? || Date.parse(last_seen_transaction_date.effective_date) < transaction_date
      @last_transaction_by_payment_method_id[payment.payment_method_id] = transaction
    end
  end

  params.permit!
end