aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/import/resource.rb
blob: 43690755d57a68d6be3d4aedc088f8a2239445e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Import::Resource < ApplicationModel
  self.table_name = :import_resources

  include IevInterfaces::Resource

  belongs_to :import, class_name: Import::Base
  belongs_to :referential
  has_many :messages, class_name: "Import::Message", foreign_key: :resource_id

  scope :main_resources, ->{ where(resource_type: "referential") }

  def root_import
    import = self.import
    import = import.parent while import.parent
    import
  end

  def next_step
    if root_import.class == Import::Workbench

      return unless netex_import&.successful?

      workbench.workgroup.import_compliance_control_sets.map do |key, label|
        next unless (control_set = workbench.compliance_control_set(key)).present?
        compliance_check_set = workbench_import_check_set key
        if compliance_check_set.nil?
          ComplianceControlSetCopyWorker.perform_async control_set.id, referential_id, root_import.class.name, root_import.id
        end
      end
    end
  end

  def workbench
    import.workbench
  end

  def workgroup
    workbench.workgroup
  end

  def netex_import
    return unless self.resource_type == "referential"
    import.children.where(name: self.reference).last
  end

  def workbench_import_check_set key
    return unless referential.present?
    control_set = referential.workbench.compliance_control_set(key)
    return unless control_set.present?
    referential.compliance_check_sets.where(compliance_control_set_id: control_set.id, referential_id: referential_id).last
  end
end