Class: Bulkrax::StepperResponseFormatter

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

Overview

Formats validation data from CsvParser.validate_csv into the structure expected by the importers_stepper.js frontend component.

This service acts as a presentation layer, transforming raw validation data into a structured response with proper status messages, severity levels, and formatted issue lists that the JavaScript can render correctly.

rubocop:disable Metrics/ClassLength

Examples:

Basic usage

validation_data = CsvParser.validate_csv(csv_file: file, zip_file: zip)
formatted_response = StepperResponseFormatter.format(validation_data)
render json: formatted_response

Error response

error_response = StepperResponseFormatter.error(message: "Unable to process files")
render json: error_response, status: :ok

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ StepperResponseFormatter

Returns a new instance of StepperResponseFormatter.



72
73
74
# File 'app/services/bulkrax/stepper_response_formatter.rb', line 72

def initialize(data)
  @data = data
end

Class Method Details

.error(message: I18n.t('bulkrax.importer.guided_import.validation.unable_to_process'), summary: nil) ⇒ Hash

Generate an error response for validation failures

Parameters:

  • message (String) (defaults to: I18n.t('bulkrax.importer.guided_import.validation.unable_to_process'))

    Error message to display

  • summary (String) (defaults to: nil)

    Optional summary (defaults to standard message)

Returns:

  • (Hash)

    Error response structure



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/services/bulkrax/stepper_response_formatter.rb', line 50

def self.error(message: I18n.t('bulkrax.importer.guided_import.validation.unable_to_process'), summary: nil)
  {
    totalItems: 0,
    collections: [],
    works: [],
    fileSets: [],
    isValid: false,
    hasWarnings: false,
    messages: {
      validationStatus: {
        severity: 'error',
        icon: 'fa-times-circle',
        title: I18n.t('bulkrax.importer.guided_import.validation.failed'),
        summary: summary || message,
        details: I18n.t('bulkrax.importer.guided_import.validation.critical_errors'),
        defaultOpen: true
      },
      issues: []
    }
  }
end

.format(data) ⇒ Hash

Format validation data for the stepper frontend

Parameters:

  • data (Hash)

    Raw validation data from CsvParser.validate_csv containing:

    • headers: Array of CSV column names

    • missingRequired: Array of hashes of missing required fields by model (e.g. ‘GenericWork’, field: ‘source_identifier’)

    • unrecognized: Array of unrecognized column names

    • rowCount: Total number of data rows

    • isValid: Boolean indicating validation success

    • hasWarnings: Boolean indicating presence of warnings

    • collections: Array of collection items with id, title, type, parentIds (array), childIds (array)

    • works: Array of work items with id, title, type, parentIds (array), childIds (array)

    • fileSets: Array of file set items

    • totalItems: Total count of items

    • fileReferences: Count of file references

    • missingFiles: Array of missing file names

    • foundFiles: Count of found files

    • zipIncluded: Boolean indicating if zip was provided

Returns:

  • (Hash)

    Formatted response ready for JSON rendering



41
42
43
# File 'app/services/bulkrax/stepper_response_formatter.rb', line 41

def self.format(data)
  new(data).format
end

Instance Method Details

#formatHash

Format the validation data with messages structure If data already contains a messages structure, return it as-is

Returns:

  • (Hash)

    Complete formatted response



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/services/bulkrax/stepper_response_formatter.rb', line 80

def format
  # Check if data is already formatted (has messages structure)
  return @data if already_formatted?

  # Build formatted response with messages structure
  {
    headers: @data[:headers],
    missingRequired: @data[:missingRequired],
    unrecognized: @data[:unrecognized],
    rowCount: @data[:rowCount],
    isValid: @data[:isValid],
    hasWarnings: @data[:hasWarnings],
    rowErrors: @data[:rowErrors],
    collections: @data[:collections],
    works: @data[:works],
    fileSets: @data[:fileSets],
    totalItems: @data[:totalItems],
    fileReferences: @data[:fileReferences],
    missingFiles: @data[:missingFiles],
    foundFiles: @data[:foundFiles],
    zipIncluded: @data[:zipIncluded],
    messages: build_messages
  }
end