Skip to content

Commit

Permalink
Merge pull request #500 from slovensko-digital/GO-150/do_automatic_bo…
Browse files Browse the repository at this point in the history
…xify_on_fs_api_connection

GO-150 Do automatic boxify on Fs::ApiConnections
  • Loading branch information
luciajanikova authored Nov 18, 2024
2 parents 5df6caf + 80eab2a commit cee4643
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/jobs/fs/boxify_all_api_connections_job.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions app/jobs/fs/boxify_api_connection_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Fs
class BoxifyApiConnectionJob < ApplicationJob
def perform(api_connection)
Box.transaction do
api_connection.boxify
end
end
end
end
1 change: 0 additions & 1 deletion app/models/fs/api_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/models/fs/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
6 changes: 6 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions test/models/fs/box_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cee4643

Please sign in to comment.