From f2efb2bfabc54d9b1d17e18523d617df4ff2e852 Mon Sep 17 00:00:00 2001 From: luciajanikova <19lucia99@gmail.com> Date: Sat, 16 Nov 2024 12:02:06 +0100 Subject: [PATCH 1/4] Autoload FS boxes --- app/jobs/fs/boxify_all_api_connections_job.rb | 9 +++++++++ app/jobs/fs/boxify_api_connection_job.rb | 9 +++++++++ app/models/fs/api_connection.rb | 1 - app/models/fs/box.rb | 2 ++ app/models/tenant.rb | 2 +- config/application.rb | 6 ++++++ 6 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 app/jobs/fs/boxify_all_api_connections_job.rb create mode 100644 app/jobs/fs/boxify_api_connection_job.rb diff --git a/app/jobs/fs/boxify_all_api_connections_job.rb b/app/jobs/fs/boxify_all_api_connections_job.rb new file mode 100644 index 00000000..3b0bdbda --- /dev/null +++ b/app/jobs/fs/boxify_all_api_connections_job.rb @@ -0,0 +1,9 @@ +module Fs + class BoxifyAllApiConnectionsJob < ApplicationJob + def perform + Fs::ApiConnection.find_each do |api_connection| + Fs::BoxifyApiConnectionJob.perform_later(api_connection) + end + end + end +end diff --git a/app/jobs/fs/boxify_api_connection_job.rb b/app/jobs/fs/boxify_api_connection_job.rb new file mode 100644 index 00000000..50201c2b --- /dev/null +++ b/app/jobs/fs/boxify_api_connection_job.rb @@ -0,0 +1,9 @@ +module Fs + class BoxifyApiConnectionJob < ApplicationJob + def perform(api_connection) + Box.transaction do + api_connection.boxify + end + end + end +end diff --git a/app/models/fs/api_connection.rb b/app/models/fs/api_connection.rb index cf63a8fd..65996adf 100644 --- a/app/models/fs/api_connection.rb +++ b/app/models/fs/api_connection.rb @@ -63,7 +63,6 @@ def boxify box.name = "FS " + subject["name"] box.short_name ||= generate_short_name_from_name(subject["name"]) box.uri = "dic://sk/#{subject['dic']}" - box.syncable = false end count += 1 if box.new_record? && box.save diff --git a/app/models/fs/box.rb b/app/models/fs/box.rb index 40a0c4b7..b1b30568 100644 --- a/app/models/fs/box.rb +++ b/app/models/fs/box.rb @@ -18,6 +18,8 @@ class Fs::Box < Box DISABLED_MESSAGE_DRAFTS_IMPORT_KEYWORDS = ['(oblasť SPD)'] + after_create { update(syncable: tenant.feature_enabled?(:fs_sync)) } + validates_uniqueness_of :name, :short_name, scope: :tenant_id def self.policy_class diff --git a/app/models/tenant.rb b/app/models/tenant.rb index 1b33c4dd..55b22f53 100644 --- a/app/models/tenant.rb +++ b/app/models/tenant.rb @@ -44,7 +44,7 @@ class Tenant < ApplicationRecord validates_presence_of :name - AVAILABLE_FEATURE_FLAGS = [:audit_log, :archive, :api, :message_draft_import, :fs_api] + AVAILABLE_FEATURE_FLAGS = [:audit_log, :archive, :api, :message_draft_import, :fs_api, :fs_sync] def draft_tag! draft_tag || raise(ActiveRecord::RecordNotFound, "`DraftTag` not found in tenant: #{id}") diff --git a/config/application.rb b/config/application.rb index 1a61347b..1e828702 100644 --- a/config/application.rb +++ b/config/application.rb @@ -52,6 +52,12 @@ class Application < Rails::Application } end + config.good_job.cron['autoload_fs_boxes'] = { + cron: "00 6 * * *", # run every day at 6:00 am + class: "Fs::BoxifyAllApiConnectionsJob", + description: "Regular job to autoload FS boxes" + } + config.good_job.cron['check_messages_mapping'] = { cron: "30 7 * * *", # run every day at 7:30 am class: "Govbox::CheckMessagesMappingJob", From 7e94555497b00f74134011e1fbfcaf161ecaebf1 Mon Sep 17 00:00:00 2001 From: luciajanikova <19lucia99@gmail.com> Date: Sat, 16 Nov 2024 12:11:40 +0100 Subject: [PATCH 2/4] Add test --- test/models/fs/box_test.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 test/models/fs/box_test.rb diff --git a/test/models/fs/box_test.rb b/test/models/fs/box_test.rb new file mode 100644 index 00000000..edf8b861 --- /dev/null +++ b/test/models/fs/box_test.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require "test_helper" + +class Fs::BoxTest < ActiveSupport::TestCase + test "#after_create callback sets syncable value to true if tenant fs_sync feature is enabled" do + tenant = tenants(:accountants) + tenant.enable_feature(:fs_sync) + + box = tenant.boxes.create( + name: 'Test box', + short_name: 'FS TB', + uri: 'dic://sk/0246802', + type: 'Fs::Box' + ) + + assert box.reload.syncable + end + + test "#after_create callback sets syncable value to false if tenant fs_sync feature is disabled" do + tenant = tenants(:ssd) + tenant.disable_feature(:fs_sync) + + box = tenant.boxes.create( + name: 'Test box', + short_name: 'FS TB', + uri: 'dic://sk/0246802', + type: 'Fs::Box' + ) + + assert_not box.reload.syncable + end +end From e01b26c93c28aa6c02c5b8060e8b0224c59841e3 Mon Sep 17 00:00:00 2001 From: luciajanikova <19lucia99@gmail.com> Date: Sat, 16 Nov 2024 14:34:58 +0100 Subject: [PATCH 3/4] Update tests --- test/models/fs/box_test.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/models/fs/box_test.rb b/test/models/fs/box_test.rb index edf8b861..c9221af3 100644 --- a/test/models/fs/box_test.rb +++ b/test/models/fs/box_test.rb @@ -11,21 +11,22 @@ class Fs::BoxTest < ActiveSupport::TestCase name: 'Test box', short_name: 'FS TB', uri: 'dic://sk/0246802', - type: 'Fs::Box' + type: 'Fs::Box', + api_connection: api_connections(:fs_api_connection1) ) assert box.reload.syncable end test "#after_create callback sets syncable value to false if tenant fs_sync feature is disabled" do - tenant = tenants(:ssd) - tenant.disable_feature(:fs_sync) + tenant = tenants(:solver) box = tenant.boxes.create( name: 'Test box', short_name: 'FS TB', uri: 'dic://sk/0246802', - type: 'Fs::Box' + type: 'Fs::Box', + api_connection: api_connections(:fs_api_connection2) ) assert_not box.reload.syncable From 80eab2a0707dd79bb20ebdbc8adc2f32fe29a3dc Mon Sep 17 00:00:00 2001 From: luciajanikova <19lucia99@gmail.com> Date: Mon, 18 Nov 2024 15:12:01 +0100 Subject: [PATCH 4/4] Update callback on Fs::Box --- app/models/fs/box.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/fs/box.rb b/app/models/fs/box.rb index b1b30568..9cde9beb 100644 --- a/app/models/fs/box.rb +++ b/app/models/fs/box.rb @@ -18,7 +18,7 @@ class Fs::Box < Box DISABLED_MESSAGE_DRAFTS_IMPORT_KEYWORDS = ['(oblasť SPD)'] - after_create { update(syncable: tenant.feature_enabled?(:fs_sync)) } + before_create { self.syncable = tenant.feature_enabled?(:fs_sync) } validates_uniqueness_of :name, :short_name, scope: :tenant_id