kaui 4.0.11 → 4.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/kaui/account_timelines_controller.rb +3 -3
  3. data/app/controllers/kaui/accounts_controller.rb +51 -51
  4. data/app/controllers/kaui/admin_allowed_users_controller.rb +29 -29
  5. data/app/controllers/kaui/admin_controller.rb +2 -2
  6. data/app/controllers/kaui/admin_tenants_controller.rb +80 -80
  7. data/app/controllers/kaui/audit_logs_controller.rb +1 -1
  8. data/app/controllers/kaui/chargebacks_controller.rb +1 -1
  9. data/app/controllers/kaui/charges_controller.rb +5 -2
  10. data/app/controllers/kaui/credits_controller.rb +1 -1
  11. data/app/controllers/kaui/custom_fields_controller.rb +11 -11
  12. data/app/controllers/kaui/engine_controller_util.rb +3 -3
  13. data/app/controllers/kaui/home_controller.rb +6 -2
  14. data/app/controllers/kaui/invoices_controller.rb +5 -3
  15. data/app/controllers/kaui/payment_methods_controller.rb +5 -5
  16. data/app/controllers/kaui/payments_controller.rb +19 -19
  17. data/app/controllers/kaui/queues_controller.rb +6 -6
  18. data/app/controllers/kaui/registrations_controller.rb +1 -1
  19. data/app/controllers/kaui/role_definitions_controller.rb +2 -2
  20. data/app/controllers/kaui/sessions_controller.rb +1 -1
  21. data/app/controllers/kaui/subscriptions_controller.rb +15 -14
  22. data/app/controllers/kaui/tag_definitions_controller.rb +1 -1
  23. data/app/controllers/kaui/tenants_controller.rb +2 -2
  24. data/app/controllers/kaui/transactions_controller.rb +6 -6
  25. data/app/helpers/kaui/account_helper.rb +9 -7
  26. data/app/helpers/kaui/exception_helper.rb +7 -5
  27. data/app/helpers/kaui/payment_helper.rb +2 -9
  28. data/app/helpers/kaui/subscription_helper.rb +28 -30
  29. data/app/helpers/kaui/uuid_helper.rb +1 -1
  30. data/app/models/kaui/account.rb +4 -3
  31. data/app/models/kaui/admin_tenant.rb +2 -2
  32. data/app/models/kaui/allowed_user.rb +3 -1
  33. data/app/models/kaui/allowed_user_tenant.rb +2 -2
  34. data/app/models/kaui/audit_log.rb +1 -1
  35. data/app/models/kaui/bundle.rb +5 -5
  36. data/app/models/kaui/invoice.rb +1 -1
  37. data/app/models/kaui/invoice_payment.rb +2 -2
  38. data/app/models/kaui/killbill_authenticatable.rb +2 -39
  39. data/app/models/kaui/killbill_registerable.rb +3 -11
  40. data/app/models/kaui/payment.rb +1 -1
  41. data/app/models/kaui/payment_state.rb +3 -1
  42. data/app/models/kaui/rails_methods.rb +2 -2
  43. data/app/models/kaui/tag_definition.rb +2 -2
  44. data/app/models/kaui/tenant.rb +2 -1
  45. data/app/models/kaui/transaction.rb +1 -1
  46. data/app/services/dependencies/kenui.rb +1 -1
  47. data/app/views/kaui/components/search_input/_search_input.html.erb +39 -1
  48. data/app/views/kaui/subscriptions/_form.html.erb +7 -1
  49. data/app/views/kaui/subscriptions/_subscriptions_table.html.erb +11 -0
  50. data/config/routes.rb +79 -79
  51. data/lib/devise/models/killbill_authenticatable.rb +38 -0
  52. data/lib/devise/models/killbill_registerable.rb +9 -0
  53. data/lib/generators/kaui/install/install_generator.rb +1 -4
  54. data/lib/kaui/version.rb +1 -1
  55. data/lib/kaui.rb +7 -7
  56. metadata +4 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7328709e0c330b84f3e47b4041cf55aa61bf98c7ac80dbb9b2d199b71c3d1f7a
4
- data.tar.gz: 27b7f69cdb1e950cd327ac7c31141a76ae6c07e7e063d5ccd6d66c020a4fe612
3
+ metadata.gz: a7cae94ec25bd96299f58f2bd783dabf34441f203d38fb2846e0f92b59b97e2a
4
+ data.tar.gz: 04f9d02da92a77eb81160518cad11a5c88af312d20b1c65cfc0d3ad141814801
5
5
  SHA512:
6
- metadata.gz: e7e3008686fb14493dca39a5adcda76d6032c0a8ba23cbf76971dac4829d59ae275a34779d3450aaf7babead74924a66a968cfc07579e74bcd6669763681a632
7
- data.tar.gz: 9195fcbb008a972b2a8e4b6dbee1b471662e5f30623910ea5b4abe290957f726b8c620c56c82df742ef43587b08f006a7c9d65825833938f01e98a666e3605fe
6
+ metadata.gz: b99fa2fe8751882f22e754e79a1b1eacc1a7933a039e572bbfa22cf880d1967b7e8c8156d04d98cc6511c9b2b6ae634f9ba3ff24102bc4341072b2422dd41e68
7
+ data.tar.gz: f494903ee370dd0a6bb10808bde638eedd769619cb7e0c70b321c74c9b246ea09ebc00f4d9976cfac8765c53a43fd724daace311ea59b15288d7adb070fb20cb
@@ -76,7 +76,7 @@ module Kaui
76
76
  if %w[INVOICE ALL].include?(event_type)
77
77
  @invoices.each do |invoice_stub|
78
78
  invoice = invoice_stub.invoice_id.present? && @invoices_by_id.key?(invoice_stub.invoice_id) ? @invoices_by_id[invoice_stub.invoice_id] : invoice_stub
79
- target_date = invoice.target_date.present? ? invoice.target_date : '[unknown]'
79
+ target_date = invoice.target_date.presence || '[unknown]'
80
80
  bundle_keys = invoice_stub.bundle_keys.present? ? invoice_stub.bundle_keys.split(',').map { |bundle_key| @bundle_names[bundle_key] }.join(', ') : ''
81
81
  invoice_details = []
82
82
  invoice_details << "Amount: #{invoice.amount_to_money} (#{@account.currency})"
@@ -133,7 +133,7 @@ module Kaui
133
133
  end
134
134
  end
135
135
 
136
- send_data csv_string, filename: "account-timelines-#{Date.today}.csv", type: 'text/csv'
136
+ send_data csv_string, filename: "account-timelines-#{Time.zone.today}.csv", type: 'text/csv'
137
137
  end
138
138
 
139
139
  private
@@ -154,7 +154,7 @@ module Kaui
154
154
  end
155
155
 
156
156
  def extract_invoices_by_id(all_invoices)
157
- return {} if all_invoices.nil? || all_invoices.empty?
157
+ return {} if all_invoices.blank?
158
158
 
159
159
  # Convert into Kaui::Invoice to benefit from additional methods xxx_to_money
160
160
  @invoices_by_id = all_invoices.to_h do |invoice|
@@ -57,7 +57,7 @@ module Kaui
57
57
  query_string = params[:search]
58
58
 
59
59
  if all_fields_checked
60
- columns = KillBillClient::Model::AccountAttributes.instance_variable_get('@json_attributes')
60
+ columns = KillBillClient::Model::AccountAttributes.instance_variable_get(:@json_attributes)
61
61
  csv_headers = columns.dup
62
62
  Kaui::Account::REMAPPING_FIELDS.each do |k, v|
63
63
  index = csv_headers.index(k)
@@ -97,36 +97,7 @@ module Kaui
97
97
  csv << data
98
98
  end
99
99
  end
100
- send_data csv_string, filename: "accounts-#{Date.today}.csv", type: 'text/csv'
101
- end
102
-
103
- def new
104
- @account = Kaui::Account.new
105
- end
106
-
107
- def create
108
- @account = Kaui::Account.new(params.require(:account).delete_if { |_key, value| value.blank? })
109
-
110
- @account.errors.add(:phone, :invalid_phone) if !@account.phone.nil? && !@account.check_account_details_phone?
111
-
112
- @account.errors.add(:check_account_details_bill_cycle_day_local, :invalid_bill_cycle_day_local) if !@account.bill_cycle_day_local.nil? && !@account.check_account_details_bill_cycle_day_local?
113
-
114
- unless @account.errors.empty?
115
- flash.now[:errors] = @account.errors.messages.values.flatten
116
- render action: :new and return
117
- end
118
-
119
- # Transform "1" into boolean
120
- @account.is_migrated = @account.is_migrated == '1'
121
-
122
- begin
123
- @account = @account.create(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
124
-
125
- redirect_to account_path(@account.account_id), notice: 'Account was successfully created'
126
- rescue StandardError => e
127
- flash.now[:error] = "Error while creating account: #{as_string(e)}"
128
- render action: :new
129
- end
100
+ send_data csv_string, filename: "accounts-#{Time.zone.today}.csv", type: 'text/csv'
130
101
  end
131
102
 
132
103
  # rubocop:disable Style/MultilineBlockChain
@@ -140,7 +111,7 @@ module Kaui
140
111
  fetch_children = promise { @account.children(false, false, 'NONE', cached_options_for_klient) }
141
112
  fetch_parent = @account.parent_account_id.nil? ? nil : promise { Kaui::Account.find_by_id(@account.parent_account_id, false, false, cached_options_for_klient) }
142
113
  fetch_overdue_state = promise { @account.overdue(cached_options_for_klient) }
143
- fetch_account_tags = promise { @account.tags(false, 'NONE', cached_options_for_klient).sort { |tag_a, tag_b| tag_a <=> tag_b } }
114
+ fetch_account_tags = promise { @account.tags(false, 'NONE', cached_options_for_klient).sort }
144
115
  fetch_account_fields = promise { @account.custom_fields('NONE', cached_options_for_klient).sort { |cf_a, cf_b| cf_a.name.downcase <=> cf_b.name.downcase } }
145
116
  fetch_account_emails = promise { Kaui::AccountEmail.find_all_sorted_by_account_id(@account.account_id, 'NONE', cached_options_for_klient) }
146
117
  fetch_payments = promise { @account.payments(cached_options_for_klient).map! { |payment| Kaui::Payment.build_from_raw_payment(payment) } }
@@ -180,7 +151,7 @@ module Kaui
180
151
  @custom_fields = wait(fetch_account_fields)
181
152
  @account_emails = wait(fetch_account_emails)
182
153
  wait(fetch_payment_methods)
183
- @payment_methods = wait(fetch_payment_methods_with_details).map { |pm_f| wait(pm_f) }.compact
154
+ @payment_methods = wait(fetch_payment_methods_with_details).filter_map { |pm_f| wait(pm_f) }
184
155
  @available_tags = wait(fetch_available_tags)
185
156
  @children = wait(fetch_children)
186
157
  @account_parent = @account.parent_account_id.nil? ? nil : wait(fetch_parent)
@@ -198,6 +169,52 @@ module Kaui
198
169
 
199
170
  params.permit!
200
171
  end
172
+
173
+ def new
174
+ @account = Kaui::Account.new
175
+ end
176
+
177
+ def edit; end
178
+
179
+ def create
180
+ @account = Kaui::Account.new(params.require(:account).permit!.to_h.compact_blank)
181
+
182
+ @account.errors.add(:phone, :invalid_phone) if !@account.phone.nil? && !@account.check_account_details_phone?
183
+
184
+ @account.errors.add(:check_account_details_bill_cycle_day_local, :invalid_bill_cycle_day_local) if !@account.bill_cycle_day_local.nil? && !@account.check_account_details_bill_cycle_day_local?
185
+
186
+ unless @account.errors.empty?
187
+ flash.now[:errors] = @account.errors.messages.values.flatten
188
+ render action: :new and return
189
+ end
190
+
191
+ # Transform "1" into boolean
192
+ @account.is_migrated = @account.is_migrated == '1'
193
+
194
+ begin
195
+ @account = @account.create(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
196
+
197
+ redirect_to account_path(@account.account_id), notice: 'Account was successfully created'
198
+ rescue StandardError => e
199
+ flash.now[:error] = "Error while creating account: #{as_string(e)}"
200
+ render action: :new
201
+ end
202
+ end
203
+
204
+ def update
205
+ @account = Kaui::Account.new(params.require(:account).permit!.to_h.compact_blank)
206
+ @account.account_id = params.require(:account_id)
207
+
208
+ # Transform "1" into boolean
209
+ @account.is_migrated = @account.is_migrated == '1'
210
+
211
+ @account.update(true, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
212
+
213
+ redirect_to account_path(@account.account_id), notice: 'Account successfully updated'
214
+ rescue StandardError => e
215
+ flash.now[:error] = "Error while updating account: #{as_string(e)}"
216
+ render action: :edit
217
+ end
201
218
  # rubocop:enable Style/MultilineBlockChain
202
219
 
203
220
  def destroy
@@ -261,23 +278,6 @@ module Kaui
261
278
  end
262
279
  end
263
280
 
264
- def edit; end
265
-
266
- def update
267
- @account = Kaui::Account.new(params.require(:account).delete_if { |_key, value| value.blank? })
268
- @account.account_id = params.require(:account_id)
269
-
270
- # Transform "1" into boolean
271
- @account.is_migrated = @account.is_migrated == '1'
272
-
273
- @account.update(true, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
274
-
275
- redirect_to account_path(@account.account_id), notice: 'Account successfully updated'
276
- rescue StandardError => e
277
- flash.now[:error] = "Error while updating account: #{as_string(e)}"
278
- render action: :edit
279
- end
280
-
281
281
  def set_default_payment_method
282
282
  account_id = params.require(:account_id)
283
283
  payment_method_id = params.require(:payment_method_id)
@@ -309,7 +309,7 @@ module Kaui
309
309
  end
310
310
 
311
311
  def link_to_parent
312
- @account = Kaui::Account.new(params.require(:account).delete_if { |_key, value| value.blank? })
312
+ @account = Kaui::Account.new(params.require(:account).permit!.to_h.compact_blank)
313
313
  @account.account_id = params.require(:account_id)
314
314
  @account.is_payment_delegated_to_parent = @account.is_payment_delegated_to_parent == '1'
315
315
 
@@ -12,26 +12,50 @@ module Kaui
12
12
  end
13
13
  end
14
14
 
15
+ def show
16
+ @allowed_user = Kaui::AllowedUser.find(params.require(:id))
17
+ raise ActiveRecord::RecordNotFound, "Could not find user #{@allowed_user.id}" unless current_user.root? || @allowed_user.kb_username == current_user.kb_username
18
+
19
+ @roles = roles_for_user(@allowed_user)
20
+
21
+ tenants_for_current_user = retrieve_tenants_for_current_user
22
+ @tenants = Kaui::Tenant.all.select { |tenant| tenants_for_current_user.include?(tenant.kb_tenant_id) }
23
+ end
24
+
15
25
  def new
16
26
  @allowed_user = Kaui::AllowedUser.new
17
27
  @is_killbill_managed = true
18
28
  @roles = []
19
29
 
20
30
  # Restore form state if returning from role creation
21
- return unless params[:user_context].present?
31
+ return if params[:user_context].blank?
22
32
 
23
33
  context = params[:user_context]
24
34
  @allowed_user.kb_username = context[:kb_username]
25
35
  @allowed_user.description = context[:description]
26
- @roles = context[:roles].to_s.split(',').reject(&:blank?)
36
+ @roles = context[:roles].to_s.split(',').compact_blank
27
37
  @external_checked = context[:external] == '1'
28
38
  end
29
39
 
40
+ def edit
41
+ @allowed_user = Kaui::AllowedUser.find(params.require(:id))
42
+ @is_killbill_managed = killbill_managed?(@allowed_user, options_for_klient)
43
+
44
+ # Use roles from context if returning from role creation, otherwise fetch from KB
45
+ if params[:user_context].present?
46
+ context = params[:user_context]
47
+ @roles = context[:roles].to_s.split(',').compact_blank
48
+ @allowed_user.description = context[:description] if context[:description].present?
49
+ else
50
+ @roles = roles_for_user(@allowed_user)
51
+ end
52
+ end
53
+
30
54
  def create
31
55
  @is_killbill_managed = nil
32
56
  @allowed_user = Kaui::AllowedUser.new(allowed_user_params)
33
57
 
34
- existing_user = Kaui::AllowedUser.find_by_kb_username(@allowed_user.kb_username)
58
+ existing_user = Kaui::AllowedUser.find_by(kb_username: @allowed_user.kb_username)
35
59
  if existing_user.blank?
36
60
  if params[:external] == '1'
37
61
  # Create locally only
@@ -53,37 +77,13 @@ module Kaui
53
77
  end
54
78
  end
55
79
 
56
- def show
57
- @allowed_user = Kaui::AllowedUser.find(params.require(:id))
58
- raise ActiveRecord::RecordNotFound, "Could not find user #{@allowed_user.id}" unless current_user.root? || @allowed_user.kb_username == current_user.kb_username
59
-
60
- @roles = roles_for_user(@allowed_user)
61
-
62
- tenants_for_current_user = retrieve_tenants_for_current_user
63
- @tenants = Kaui::Tenant.all.select { |tenant| tenants_for_current_user.include?(tenant.kb_tenant_id) }
64
- end
65
-
66
- def edit
67
- @allowed_user = Kaui::AllowedUser.find(params.require(:id))
68
- @is_killbill_managed = killbill_managed?(@allowed_user, options_for_klient)
69
-
70
- # Use roles from context if returning from role creation, otherwise fetch from KB
71
- if params[:user_context].present?
72
- context = params[:user_context]
73
- @roles = context[:roles].to_s.split(',').reject(&:blank?)
74
- @allowed_user.description = context[:description] if context[:description].present?
75
- else
76
- @roles = roles_for_user(@allowed_user)
77
- end
78
- end
79
-
80
80
  def update
81
81
  @allowed_user = Kaui::AllowedUser.find(params.require(:id))
82
82
 
83
83
  @allowed_user.description = params[:allowed_user][:description].presence
84
84
 
85
85
  @allowed_user.update_in_kb!(params[:password].presence,
86
- params[:roles].blank? ? nil : params[:roles].split(','),
86
+ params[:roles].presence&.split(','),
87
87
  current_user.kb_username,
88
88
  params[:reason],
89
89
  params[:comment],
@@ -149,7 +149,7 @@ module Kaui
149
149
  end
150
150
 
151
151
  def roles_for_user(allowed_user)
152
- Kaui::UserRole.find_roles_by_username(allowed_user.kb_username, options_for_klient).map(&:presence).compact
152
+ Kaui::UserRole.find_roles_by_username(allowed_user.kb_username, options_for_klient).filter_map(&:presence)
153
153
  rescue StandardError
154
154
  []
155
155
  end
@@ -23,10 +23,10 @@ module Kaui
23
23
  def set_clock
24
24
  if params[:commit] == 'Submit'
25
25
  date = Date.parse(params[:new_date]).strftime('%Y-%m-%d')
26
- msg = I18n.translate('flashes.notices.clock_updated_successfully', new_date: date)
26
+ msg = I18n.t('flashes.notices.clock_updated_successfully', new_date: date)
27
27
  else
28
28
  date = nil
29
- msg = I18n.translate('flashes.notices.clock_reset_successfully')
29
+ msg = I18n.t('flashes.notices.clock_reset_successfully')
30
30
  end
31
31
  begin
32
32
  Kaui::Admin.set_clock(date, nil, options_for_klient)
@@ -10,68 +10,6 @@ module Kaui
10
10
  @tenants = Kaui::Tenant.all.select { |tenant| tenants_for_current_user.include?(tenant.kb_tenant_id) }
11
11
  end
12
12
 
13
- def new
14
- @tenant = Kaui::Tenant.new
15
- end
16
-
17
- def create
18
- param_tenant = params[:tenant]
19
-
20
- old_tenant = Kaui::Tenant.find_by_name(param_tenant[:name]) || Kaui::Tenant.find_by_api_key(param_tenant[:api_key])
21
- if old_tenant
22
- old_tenant.kaui_allowed_users << Kaui::AllowedUser.where(kb_username: current_user.kb_username).first_or_create
23
- redirect_to admin_tenant_path(old_tenant[:id]), notice: 'Tenant was successfully configured' and return
24
- end
25
-
26
- begin
27
- options = tenant_options_for_client
28
- new_tenant = nil
29
-
30
- begin
31
- options[:api_key] = param_tenant[:api_key]
32
- options[:api_secret] = param_tenant[:api_secret]
33
- new_tenant = Kaui::AdminTenant.find_by_api_key(param_tenant[:api_key], options)
34
- rescue KillBillClient::API::Unauthorized, KillBillClient::API::NotFound, KillBillClient::API::InternalServerError => e
35
- # Create the tenant in Kill Bill
36
- if e.instance_of?(KillBillClient::API::InternalServerError) && !e.message&.include?('TenantCacheLoader cannot find value')
37
- flash[:error] = "Internal server error while retrieving tenant: #{as_string(e)}"
38
- redirect_to admin_tenants_path and return
39
- end
40
- new_tenant = Kaui::AdminTenant.new
41
- new_tenant.external_key = param_tenant[:name]
42
- new_tenant.api_key = param_tenant[:api_key]
43
- new_tenant.api_secret = param_tenant[:api_secret]
44
- new_tenant = new_tenant.create(false, options[:username], nil, comment, options)
45
- end
46
-
47
- # Transform object to Kaui model
48
- tenant_model = Kaui::Tenant.new
49
- tenant_model.name = param_tenant[:name]
50
- tenant_model.api_key = param_tenant[:api_key]
51
- tenant_model.api_secret = param_tenant[:api_secret]
52
- tenant_model.kb_tenant_id = new_tenant.tenant_id
53
-
54
- # Save in KAUI tables
55
- tenant_model.save!
56
- # Make sure at least the current user can access the tenant
57
- tenant_model.kaui_allowed_users << Kaui::AllowedUser.where(kb_username: current_user.kb_username).first_or_create
58
- rescue KillBillClient::API::Conflict => _e
59
- # tenant api_key was found but has a wrong api_secret
60
- flash[:error] = "Submitted credentials for #{param_tenant[:api_key]} did not match the expected credentials."
61
- redirect_to admin_tenants_path and return
62
- rescue StandardError => e
63
- flash[:error] = "Failed to create the tenant: #{as_string(e)}"
64
- redirect_to admin_tenants_path and return
65
- end
66
-
67
- # Select the tenant, see TenantsController
68
- session[:kb_tenant_id] = tenant_model.kb_tenant_id
69
- session[:kb_tenant_name] = tenant_model.name
70
- session[:tenant_id] = tenant_model.id
71
-
72
- redirect_to admin_tenant_path(tenant_model[:id]), notice: 'Tenant was successfully configured'
73
- end
74
-
75
13
  def show
76
14
  @tenant = safely_find_tenant_by_id(params[:id])
77
15
  @allowed_users = @tenant.kaui_allowed_users & retrieve_allowed_users_for_current_user
@@ -140,6 +78,68 @@ module Kaui
140
78
  end
141
79
  end
142
80
 
81
+ def new
82
+ @tenant = Kaui::Tenant.new
83
+ end
84
+
85
+ def create
86
+ param_tenant = params[:tenant]
87
+
88
+ old_tenant = Kaui::Tenant.find_by(name: param_tenant[:name]) || Kaui::Tenant.find_by(api_key: param_tenant[:api_key])
89
+ if old_tenant
90
+ old_tenant.kaui_allowed_users << Kaui::AllowedUser.where(kb_username: current_user.kb_username).first_or_create
91
+ redirect_to admin_tenant_path(old_tenant[:id]), notice: 'Tenant was successfully configured' and return
92
+ end
93
+
94
+ begin
95
+ options = tenant_options_for_client
96
+ new_tenant = nil
97
+
98
+ begin
99
+ options[:api_key] = param_tenant[:api_key]
100
+ options[:api_secret] = param_tenant[:api_secret]
101
+ new_tenant = Kaui::AdminTenant.find_by_api_key(param_tenant[:api_key], options)
102
+ rescue KillBillClient::API::Unauthorized, KillBillClient::API::NotFound, KillBillClient::API::InternalServerError => e
103
+ # Create the tenant in Kill Bill
104
+ if e.instance_of?(KillBillClient::API::InternalServerError) && !e.message&.include?('TenantCacheLoader cannot find value')
105
+ flash[:error] = "Internal server error while retrieving tenant: #{as_string(e)}"
106
+ redirect_to admin_tenants_path and return
107
+ end
108
+ new_tenant = Kaui::AdminTenant.new
109
+ new_tenant.external_key = param_tenant[:name]
110
+ new_tenant.api_key = param_tenant[:api_key]
111
+ new_tenant.api_secret = param_tenant[:api_secret]
112
+ new_tenant = new_tenant.create(false, options[:username], nil, comment, options)
113
+ end
114
+
115
+ # Transform object to Kaui model
116
+ tenant_model = Kaui::Tenant.new
117
+ tenant_model.name = param_tenant[:name]
118
+ tenant_model.api_key = param_tenant[:api_key]
119
+ tenant_model.api_secret = param_tenant[:api_secret]
120
+ tenant_model.kb_tenant_id = new_tenant.tenant_id
121
+
122
+ # Save in KAUI tables
123
+ tenant_model.save!
124
+ # Make sure at least the current user can access the tenant
125
+ tenant_model.kaui_allowed_users << Kaui::AllowedUser.where(kb_username: current_user.kb_username).first_or_create
126
+ rescue KillBillClient::API::Conflict => _e
127
+ # tenant api_key was found but has a wrong api_secret
128
+ flash[:error] = "Submitted credentials for #{param_tenant[:api_key]} did not match the expected credentials."
129
+ redirect_to admin_tenants_path and return
130
+ rescue StandardError => e
131
+ flash[:error] = "Failed to create the tenant: #{as_string(e)}"
132
+ redirect_to admin_tenants_path and return
133
+ end
134
+
135
+ # Select the tenant, see TenantsController
136
+ session[:kb_tenant_id] = tenant_model.kb_tenant_id
137
+ session[:kb_tenant_name] = tenant_model.name
138
+ session[:tenant_id] = tenant_model.id
139
+
140
+ redirect_to admin_tenant_path(tenant_model[:id]), notice: 'Tenant was successfully configured'
141
+ end
142
+
143
143
  def upload_catalog
144
144
  current_tenant = safely_find_tenant_by_id(params[:id])
145
145
 
@@ -158,7 +158,7 @@ module Kaui
158
158
  end
159
159
  if catalog_validation_errors.blank?
160
160
  Kaui::AdminTenant.upload_catalog(catalog_xml, options[:username], nil, comment, options)
161
- redirect_to admin_tenant_path(current_tenant.id), notice: I18n.translate('flashes.notices.catalog_uploaded_successfully')
161
+ redirect_to admin_tenant_path(current_tenant.id), notice: I18n.t('flashes.notices.catalog_uploaded_successfully')
162
162
  else
163
163
  errors = ''
164
164
  catalog_validation_errors.each do |validation_error|
@@ -234,7 +234,7 @@ module Kaui
234
234
  options = tenant_options_for_client
235
235
  fetch_state_for_new_catalog_screen(options)
236
236
 
237
- simple_plan = params.require(:simple_plan).delete_if { |_e, value| value.blank? }
237
+ simple_plan = params.require(:simple_plan).permit!.to_h.compact_blank
238
238
  # Fix issue in Rails where first entry in the multi-select array is an empty string
239
239
  simple_plan['available_base_products']&.reject!(&:blank?)
240
240
 
@@ -298,12 +298,12 @@ module Kaui
298
298
  options[:api_key] = current_tenant.api_key
299
299
  options[:api_secret] = current_tenant.api_secret
300
300
 
301
- view_form_model = params.require(:kill_bill_client_model_overdue).delete_if { |_e, value| value.blank? }
302
- view_form_model['states'] = view_form_model['states'].values unless view_form_model['states'].blank?
301
+ view_form_model = params.require(:kill_bill_client_model_overdue).permit!.to_h.compact_blank
302
+ view_form_model['states'] = view_form_model['states'].values if view_form_model['states'].present?
303
303
 
304
304
  overdue = Kaui::Overdue.from_overdue_form_model(view_form_model)
305
305
  Kaui::Overdue.upload_tenant_overdue_config_json(overdue.to_json, options[:username], nil, comment, options)
306
- redirect_to admin_tenant_path(current_tenant.id, active_tab: 'OverdueShow'), notice: I18n.translate('flashes.notices.overdue_added_successfully')
306
+ redirect_to admin_tenant_path(current_tenant.id, active_tab: 'OverdueShow'), notice: I18n.t('flashes.notices.overdue_added_successfully')
307
307
  end
308
308
 
309
309
  def upload_overdue_config
@@ -319,13 +319,13 @@ module Kaui
319
319
  begin
320
320
  Nokogiri::XML(overdue_config_xml, &:strict)
321
321
  rescue Nokogiri::XML::SyntaxError => e
322
- flash[:error] = I18n.translate('errors.messages.invalid_xml', error: e)
322
+ flash[:error] = I18n.t('errors.messages.invalid_xml', error: e)
323
323
  redirect_to admin_tenant_path(current_tenant.id) and return
324
324
  end
325
325
 
326
326
  Kaui::AdminTenant.upload_overdue_config(overdue_config_xml, options[:username], nil, comment, options)
327
327
 
328
- redirect_to admin_tenant_path(current_tenant.id, active_tab: 'OverdueShow'), notice: I18n.translate('flashes.notices.overdue_uploaded_successfully')
328
+ redirect_to admin_tenant_path(current_tenant.id, active_tab: 'OverdueShow'), notice: I18n.t('flashes.notices.overdue_uploaded_successfully')
329
329
  end
330
330
 
331
331
  def upload_invoice_template
@@ -341,7 +341,7 @@ module Kaui
341
341
 
342
342
  Kaui::AdminTenant.upload_invoice_template(invoice_template, is_manual_pay, true, options[:username], nil, comment, options)
343
343
 
344
- redirect_to admin_tenant_path(current_tenant.id), notice: I18n.translate('flashes.notices.invoice_template_uploaded_successfully')
344
+ redirect_to admin_tenant_path(current_tenant.id), notice: I18n.t('flashes.notices.invoice_template_uploaded_successfully')
345
345
  end
346
346
 
347
347
  def upload_invoice_translation
@@ -357,7 +357,7 @@ module Kaui
357
357
 
358
358
  Kaui::AdminTenant.upload_invoice_translation(invoice_translation, locale, true, options[:username], nil, comment, options)
359
359
 
360
- redirect_to admin_tenant_path(current_tenant.id), notice: I18n.translate('flashes.notices.invoice_translation_uploaded_successfully')
360
+ redirect_to admin_tenant_path(current_tenant.id), notice: I18n.t('flashes.notices.invoice_translation_uploaded_successfully')
361
361
  end
362
362
 
363
363
  def upload_catalog_translation
@@ -373,7 +373,7 @@ module Kaui
373
373
 
374
374
  Kaui::AdminTenant.upload_catalog_translation(catalog_translation, locale, true, options[:username], nil, comment, options)
375
375
 
376
- redirect_to admin_tenant_path(current_tenant.id), notice: I18n.translate('flashes.notices.catalog_translation_uploaded_successfully')
376
+ redirect_to admin_tenant_path(current_tenant.id), notice: I18n.t('flashes.notices.catalog_translation_uploaded_successfully')
377
377
  end
378
378
 
379
379
  def upload_plugin_config
@@ -407,18 +407,18 @@ module Kaui
407
407
  au = Kaui::AllowedUser.find(params.require(:allowed_user).require(:id))
408
408
 
409
409
  unless current_user.root?
410
- render json: { alert: 'Only the root user can remove users from tenants' }.to_json, status: 401
410
+ render json: { alert: 'Only the root user can remove users from tenants' }.to_json, status: :unauthorized
411
411
  return
412
412
  end
413
413
 
414
414
  # remove the association
415
415
  au.kaui_tenants.delete current_tenant
416
- render json: '{}', status: 200
416
+ render json: '{}', status: :ok
417
417
  end
418
418
 
419
419
  def add_allowed_user
420
420
  current_tenant = safely_find_tenant_by_id(params[:tenant_id])
421
- allowed_user = Kaui::AllowedUser.find_by_kb_username(params.require(:allowed_user).require(:kb_username))
421
+ allowed_user = Kaui::AllowedUser.find_by(kb_username: params.require(:allowed_user).require(:kb_username))
422
422
 
423
423
  unless current_user.root?
424
424
  flash[:error] = 'Only the root user can add users from tenants'
@@ -498,10 +498,10 @@ module Kaui
498
498
 
499
499
  if params[:commit] == 'Submit'
500
500
  date = Date.parse(params[:new_date]).strftime('%Y-%m-%d')
501
- msg = I18n.translate('flashes.notices.clock_updated_successfully', new_date: date)
501
+ msg = I18n.t('flashes.notices.clock_updated_successfully', new_date: date)
502
502
  else
503
503
  date = nil
504
- msg = I18n.translate('flashes.notices.clock_reset_successfully')
504
+ msg = I18n.t('flashes.notices.clock_reset_successfully')
505
505
  end
506
506
 
507
507
  begin
@@ -515,7 +515,7 @@ module Kaui
515
515
  end
516
516
 
517
517
  def switch_tenant
518
- tenant = Kaui::Tenant.find_by_kb_tenant_id(params.require(:kb_tenant_id))
518
+ tenant = Kaui::Tenant.find_by(kb_tenant_id: params.require(:kb_tenant_id))
519
519
 
520
520
  # Select the tenant, see TenantsController
521
521
  session[:kb_tenant_id] = tenant.kb_tenant_id
@@ -560,7 +560,7 @@ module Kaui
560
560
  end
561
561
 
562
562
  def safely_find_tenant_by_id(tenant_id)
563
- tenant = Kaui::Tenant.find_by_id(tenant_id)
563
+ tenant = Kaui::Tenant.find_by(id: tenant_id)
564
564
  raise ActiveRecord::RecordNotFound, "Could not find tenant #{tenant_id}" unless retrieve_tenants_for_current_user.include?(tenant.kb_tenant_id)
565
565
 
566
566
  tenant
@@ -606,7 +606,7 @@ module Kaui
606
606
  end
607
607
 
608
608
  catalog_xml = {}
609
- catalog_xml = response[0][:xml] unless response.blank?
609
+ catalog_xml = response[0][:xml] if response.present?
610
610
 
611
611
  catalog_xml
612
612
  end
@@ -76,7 +76,7 @@ module Kaui
76
76
  end
77
77
  end
78
78
 
79
- send_data csv_file, type: 'text/csv', filename: "audit-logs-#{Date.today}.csv"
79
+ send_data csv_file, type: 'text/csv', filename: "audit-logs-#{Time.zone.today}.csv"
80
80
  end
81
81
 
82
82
  def history
@@ -29,7 +29,7 @@ module Kaui
29
29
  account.bundles(cached_options_for_klient).each do |bundle|
30
30
  bundle.subscriptions.each do |subscription|
31
31
  # Already cancelled?
32
- next unless subscription.billing_end_date.blank?
32
+ next if subscription.billing_end_date.present?
33
33
 
34
34
  # Cancel the entitlement immediately but use the default billing policy
35
35
  entitlement = Kaui::Subscription.new(subscription_id: subscription.subscription_id)
@@ -19,13 +19,16 @@ module Kaui
19
19
  end
20
20
 
21
21
  def create
22
- charge = Kaui::InvoiceItem.new(params.require(:invoice_item).delete_if { |_key, value| value.blank? })
22
+ charge = Kaui::InvoiceItem.new(params.require(:invoice_item).permit!.to_h.compact_blank)
23
23
  charge.account_id ||= params.require(:account_id)
24
24
 
25
+ # Preserve the original invoice_id since the API may not return it
26
+ original_invoice_id = charge.invoice_id
27
+
25
28
  auto_commit = params[:auto_commit] == '1'
26
29
 
27
30
  charge = charge.create(auto_commit, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
28
- redirect_to kaui_engine.account_invoice_path(charge.account_id, charge.invoice_id), notice: 'Charge was successfully created'
31
+ redirect_to kaui_engine.account_invoice_path(charge.account_id, charge.invoice_id || original_invoice_id), notice: 'Charge was successfully created'
29
32
  end
30
33
  end
31
34
  end
@@ -19,7 +19,7 @@ module Kaui
19
19
  end
20
20
 
21
21
  def create
22
- credit = Kaui::Credit.new(params[:credit].delete_if { |_key, value| value.blank? })
22
+ credit = Kaui::Credit.new(params[:credit].permit!.to_h.compact_blank)
23
23
  credit.account_id ||= params.require(:account_id)
24
24
 
25
25
  # No need to show the newly created invoice for account level credits