summaryrefslogtreecommitdiff
path: root/lib/bulk_imports/importers/groups_importer.rb
blob: 8641577ff4711db3479fa06fc02db992500007e3 (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
# frozen_string_literal: true

module BulkImports
  module Importers
    class GroupsImporter
      def initialize(bulk_import_id)
        @bulk_import = BulkImport.find(bulk_import_id)
      end

      def execute
        bulk_import.start! unless bulk_import.started?

        if entities_to_import.empty?
          bulk_import.finish!
        else
          entities_to_import.each do |entity|
            BulkImports::Importers::GroupImporter.new(entity).execute
          end

          # A new BulkImportWorker job is enqueued to either
          #   - Process the new BulkImports::Entity created for the subgroups
          #   - Or to mark the `bulk_import` as finished.
          BulkImportWorker.perform_async(bulk_import.id)
        end
      end

      private

      attr_reader :bulk_import

      def entities_to_import
        @entities_to_import ||= bulk_import.entities.with_status(:created)
      end
    end
  end
end