Class: Reports::Importer

Inherits:
Object
  • Object
show all
Includes:
HasHelpers::Attributes
Defined in:
app/commands/reports/importer.rb

Constant Summary collapse

AuditError =
Class.new(::StandardError)
RESOURCE_TYPE_MISMATCH_ERR_MSG =
"When initializing HasReports::Importer, :resource must be an instance of HasReports::Resource"
AUDIT_EXTRA_RECORD_LOG_MSG =
"%{option_type} name: %{name}, resource: %{resource} was flagged during an audit. The record may need to be migrated to match a new name or removed as the case may require."
AUDIT_EXTRA_EXPANSION_LOG_MSG =
"Resource: %{resource}, Expanded Value: \"%{expanded_value}\", Expanded Column: \"%{expanded_column_name}\", Dynamic: \"%{is_dynamic}\", Node: \"%{node}\", was flagged during an audit. The record may need to be migrated to match a new name or removed as the case may require."

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasHelpers::Attributes

included

Constructor Details

#initialize(resource_id, resource_name: nil, **_options) ⇒ Importer

Returns a new instance of Importer.



21
22
23
24
# File 'app/commands/reports/importer.rb', line 21

def initialize(resource_id, resource_name: nil, **_options)
  @resource_id = resource_id
  @resource_name = resource_name
end

Instance Attribute Details

#resource_idObject

Returns the value of attribute resource_id.



19
20
21
# File 'app/commands/reports/importer.rb', line 19

def resource_id
  @resource_id
end

#resource_nameObject

Returns the value of attribute resource_name.



19
20
21
# File 'app/commands/reports/importer.rb', line 19

def resource_name
  @resource_name
end

Instance Method Details

#build_filter_option(attrs, resource_id) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/commands/reports/importer.rb', line 98

def build_filter_option(attrs, resource_id)
  attrs[:resourceId] = resource_id.to_i if resource_id.present?
  results = Reports::ExternalGraphqlClient.create_resource(mutation_name: "createHasReportsFilterOption", input_name: "HasReportsFilterOptionInput", attributes: attrs) if attrs.present?
  create_data = results&.dig("data", "createHasReportsFilterOption")
  if create_data && create_data["errors"].blank?
    {
      success: true,
      message: "FilterOption created successfully with ID #{create_data["resource"]["id"]}",
      errors: nil
    }
  else
    errors =
      if create_data.present?
        create_data["errors"] || []
      elsif results&.dig("errors").present?
        results["errors"].map do |e|
          {
            message: e["message"],
            path: e.dig("extensions", "problems")&.flat_map { |p| p["path"] } || [],
            explanation: e.dig("extensions", "problems")&.map { |p| p["explanation"] }&.join("; ")
          }
        end
      else
        []
      end

    {
      success: false,
      message: "Failed to create FilterOption '#{@resource_name}'->'#{attrs[:name]}'",
      errors: errors
    }
  end
end

#build_select_option(attrs, resource_id) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/commands/reports/importer.rb', line 45

def build_select_option(attrs, resource_id)
  attrs[:resourceId] = resource_id.to_i if resource_id.present?
  results = Reports::ExternalGraphqlClient.create_resource(mutation_name: "createHasReportsSelectOption", input_name: "HasReportsSelectOptionInput", attributes: attrs) if attrs.present?
  create_data = results&.dig("data", "createHasReportsSelectOption")
  if create_data && create_data["errors"].blank?
    {
      success: true,
      message: "SelectOption created successfully with ID #{create_data["resource"]["id"]}",
      errors: nil
    }
  else
    errors =
      if create_data.present?
        create_data["errors"] || []
      elsif results&.dig("errors").present?
        results["errors"].map do |e|
          {
            message: e["message"],
            path: e.dig("extensions", "problems")&.flat_map { |p| p["path"] } || [],
            explanation: e.dig("extensions", "problems")&.map { |p| p["explanation"] }&.join("; ")
          }
        end
      else
        []
      end

    {
      success: false,
      message: "Failed to create SelectOption '#{@resource_name}'->'#{attrs[:name]}'",
      errors: errors
    }
  end
end

#import_filter_options(values, resource_id, verbose: false) ⇒ Object

def import_filter_options(values, resource_id, audit: true, verbose: false)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/commands/reports/importer.rb', line 80

def import_filter_options(values, resource_id, verbose: false)
  values = Array.wrap(values)
  [].tap do |results|
    values.each do |attrs|
      puts "--> FilterOption: #{ attrs.slice(:name, :where_statement).inspect }" if verbose
      result = build_filter_option(attrs, resource_id)
      results << result
    rescue => e
      puts "Error importing FilterOption: #{ attrs.slice(:name, :where_statement).inspect } - #{ e.message }" if verbose
      results << {
        success: false,
        message: "Exception during import: #{e.message}",
        backtrace: e.backtrace
      }
    end
  end
end

#import_select_options(values, resource_id, verbose: false) ⇒ Object

def import_select_options(values, resource_id, audit: true, verbose: false)



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/commands/reports/importer.rb', line 27

def import_select_options(values, resource_id, verbose: false)
  values = Array.wrap(values)
  [].tap do |results|
    values.each do |attrs|
      puts "--> SelectOption: #{ attrs.slice(:name, :select_statement).inspect }" if verbose
      result = build_select_option(attrs, resource_id)
      results << result
    rescue => e
      puts "Error importing SelectOption: #{ attrs.slice(:name, :select_statement).inspect } - #{ e.message }" if verbose
      results << {
        success: false,
        message: "Exception during import: #{e.message}",
        backtrace: e.backtrace
      }
    end
  end
end