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..9cde9beb 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)'] + before_create { self.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 da76974d..59a6fa1e 100644 --- a/config/application.rb +++ b/config/application.rb @@ -54,6 +54,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", diff --git a/test/models/fs/box_test.rb b/test/models/fs/box_test.rb new file mode 100644 index 00000000..c9221af3 --- /dev/null +++ b/test/models/fs/box_test.rb @@ -0,0 +1,34 @@ +# 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', + 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(:solver) + + box = tenant.boxes.create( + name: 'Test box', + short_name: 'FS TB', + uri: 'dic://sk/0246802', + type: 'Fs::Box', + api_connection: api_connections(:fs_api_connection2) + ) + + assert_not box.reload.syncable + end +end