Class: ExportCsvDocumentDistributionsService

Inherits:
Object
  • Object
show all
Defined in:
app/services/export_csv_document_distributions_service.rb

Overview

ExportCsvDocumentDistributionsService

This service is responsible for exporting document distributions to a CSV format. It broadcasts the progress of the export process via ActionCable.

Class Method Summary collapse

Class Method Details

.call(document_ids) ⇒ Array

Initiates the CSV export process for the given document IDs.

This method broadcasts the progress of the export process to the “export_channel”. It processes the document IDs in slices and handles any NoMethodError exceptions that occur during the export process.

Parameters:

  • document_ids (Array)

    an array of document IDs to export.

Returns:

  • (Array)

    the generated CSV file content as an array of rows.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/services/export_csv_document_distributions_service.rb', line 29

def self.call(document_ids)
  ActionCable.server.broadcast("export_channel", {progress: 0})

  document_ids = document_ids.flatten
  total = document_ids.size
  count = 0
  slice_count = 100
  csv_file = []

  Rails.logger.debug { "\n\nExportCsvDocumentDistributionsService: #{document_ids.inspect}\n\n" }

  CSV.generate(headers: true) do |_csv|
    csv_file << DocumentDistribution.csv_column_names
    document_ids.each_slice(slice_count) do |slice|
      # Broadcast progress percentage
      count += slice_count
      progress = ((count.to_f / total) * 100).round
      progress = 100 if progress > 100

      ActionCable.server.broadcast("export_channel", {progress: progress})
      slice.each do |doc_id|
        doc = Document.find_by(friendlier_id: doc_id)

        Rails.logger.debug { "\n\nDocDistributions: #{doc.distributions_csv.size}\n\n" }

        doc.distributions_csv.each do |distribution|
          csv_file << distribution
        end
      rescue NoMethodError
        Rails.logger.debug { "\n\nExport Failed: #{doc_id.inspect}\n\n" }
      end
    end
  end

  csv_file
end

.include_distributions?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'app/services/export_csv_document_distributions_service.rb', line 17

def self.include_distributions?
  false
end

.short_nameString

Returns a short name for the service.

Returns:

  • (String)

    the short name of the service.



13
14
15
# File 'app/services/export_csv_document_distributions_service.rb', line 13

def self.short_name
  "Distributions"
end