From 1678e3aacc4d11597ae8c3dc720c9b496080237c Mon Sep 17 00:00:00 2001 From: Kyle Le Date: Wed, 24 Apr 2024 10:19:44 +0700 Subject: [PATCH 1/2] Issue 347 Add validate catalog feature --- .../kaui/admin_tenants_controller.rb | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/app/controllers/kaui/admin_tenants_controller.rb b/app/controllers/kaui/admin_tenants_controller.rb index 18e4789f..2a0d4408 100644 --- a/app/controllers/kaui/admin_tenants_controller.rb +++ b/app/controllers/kaui/admin_tenants_controller.rb @@ -130,9 +130,23 @@ def upload_catalog uploaded_catalog = params.require(:catalog) catalog_xml = uploaded_catalog.read - Kaui::AdminTenant.upload_catalog(catalog_xml, options[:username], nil, comment, options) - - redirect_to admin_tenant_path(current_tenant.id), notice: I18n.translate('flashes.notices.catalog_uploaded_successfully') + validate_response = Kaui::Catalog.validate_catalog(catalog_xml, options[:username], nil, comment, options) + catalog_validation_errors = begin + JSON.parse(validate_response.response.body)['catalogValidationErrors'] + rescue StandardError + nil + end + if catalog_validation_errors.blank? + Kaui::AdminTenant.upload_catalog(catalog_xml, options[:username], nil, comment, options) + redirect_to admin_tenant_path(current_tenant.id), notice: I18n.translate('flashes.notices.catalog_uploaded_successfully') + else + errors = '' + catalog_validation_errors.each do |validation_error| + errors += (validation_error['errorDescription']) + end + flash[:error] = errors + redirect_to admin_tenant_new_catalog_path(id: current_tenant.id) + end end def new_catalog From 3b7d4eaa623f2f5276ee98691cf7f73360792ace Mon Sep 17 00:00:00 2001 From: Kyle Le Date: Wed, 24 Apr 2024 11:04:52 +0700 Subject: [PATCH 2/2] Fix Rubocop --- app/controllers/kaui/accounts_controller.rb | 5 ++--- app/controllers/kaui/admin_tenants_controller.rb | 5 ++--- lib/tasks/kaui_tasks.rake | 1 + test/unit/kaui/admin_tenant_test.rb | 6 ++---- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/app/controllers/kaui/accounts_controller.rb b/app/controllers/kaui/accounts_controller.rb index d0583385..44a035a8 100644 --- a/app/controllers/kaui/accounts_controller.rb +++ b/app/controllers/kaui/accounts_controller.rb @@ -115,9 +115,8 @@ def show end fetch_payment_methods_with_details = fetch_payment_methods.then do |pms| - ops = [] - pms.each do |pm| - ops << promise do + ops = pms.map do |pm| + promise do Kaui::PaymentMethod.find_by_id(pm.payment_method_id, true, cached_options_for_klient) rescue StandardError => e # Maybe the plugin is not registered or the plugin threw an exception diff --git a/app/controllers/kaui/admin_tenants_controller.rb b/app/controllers/kaui/admin_tenants_controller.rb index 2a0d4408..6f12ed8a 100644 --- a/app/controllers/kaui/admin_tenants_controller.rb +++ b/app/controllers/kaui/admin_tenants_controller.rb @@ -455,9 +455,8 @@ def catalog_by_effective_date # convert result to a full hash since dynamic attributes of a class are ignored when converting to json result.each do |data| - plans = [] - data[:plans].each do |plan| - plans << plan.instance_variables.each_with_object({}) { |var, hash_plan| hash_plan[var.to_s.delete('@')] = plan.instance_variable_get(var) } + plans = data[:plans].map do |plan| + plan.instance_variables.each_with_object({}) { |var, hash_plan| hash_plan[var.to_s.delete('@')] = plan.instance_variable_get(var) } end catalog << { version_date: data[:version_date], diff --git a/lib/tasks/kaui_tasks.rake b/lib/tasks/kaui_tasks.rake index 8ced242a..44059961 100644 --- a/lib/tasks/kaui_tasks.rake +++ b/lib/tasks/kaui_tasks.rake @@ -1,4 +1,5 @@ # frozen_string_literal: true + # desc "Explaining what the task does" # task :kaui do # # Task goes here diff --git a/test/unit/kaui/admin_tenant_test.rb b/test/unit/kaui/admin_tenant_test.rb index 1c4e9430..e4658f67 100644 --- a/test/unit/kaui/admin_tenant_test.rb +++ b/test/unit/kaui/admin_tenant_test.rb @@ -108,17 +108,15 @@ class AdminTenantTest < ActiveSupport::TestCase private def plugins_repo - plugins_info = [] hash_plugin_info = JSON.parse(PLUGIN_REPO) - hash_plugin_info.each do |plugin| - plugins_info << { + hash_plugin_info.map do |plugin| + { plugin_key: plugin['plugin_key'], plugin_name: plugin['plugin_name'], plugin_type: nil, installed: true } end - plugins_info end def assert_split(splitted)