Class: ForestLiana::ActionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/forest_liana/actions_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_user_from_jwt, #deactivate_count_response, #forest_user, #internal_server_error, papertrail?, #serialize_model, #serialize_models

Methods inherited from BaseController

#route_not_found

Instance Method Details

#changeObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/controllers/forest_liana/actions_controller.rb', line 112

def change
  change_request = get_smart_action_hook_request

  action = get_action(change_request[:collection_name])

  if !action
    return render status: 500, json: {error: 'Error in smart action change hook: cannot retrieve action from collection'}
  elsif change_request[:fields].nil?
    return render status: 500, json: {error: 'Error in smart action change hook: fields params is mandatory'}
  elsif !change_request[:fields].is_a?(Array)
    return render status: 500, json: {error: 'Error in smart action change hook: fields params must be an array'}
  end

  # Get the smart action hook change context
  context = get_smart_action_change_ctx(change_request[:fields], change_request[:changed_field])

  field_changed_hook = context[:field_changed][:hook]

  # Call the user-defined change hook.
  result = action.hooks[:change][field_changed_hook].(context)

  handle_result(result, action)
end

#get_action(collection_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/forest_liana/actions_controller.rb', line 20

def get_action(collection_name)
  collection = get_collection(collection_name)
  begin
    collection.actions.find {|action| ActiveSupport::Inflector.parameterize(action.name) == params[:action_name]}
  rescue => error
    FOREST_REPORTER.report error
    FOREST_LOGGER.error "Smart Action get action retrieval error: #{error}"
    nil
  end
end

#get_collection(collection_name) ⇒ Object



16
17
18
# File 'app/controllers/forest_liana/actions_controller.rb', line 16

def get_collection(collection_name)
  ForestLiana.apimap.find { |collection| collection.name.to_s == collection_name }
end

#get_smart_action_change_ctx(fields, field_changed) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'app/controllers/forest_liana/actions_controller.rb', line 40

def get_smart_action_change_ctx(fields, field_changed)
  found_field_changed = fields.find{|field| field[:field] == field_changed}
  fields = fields.map do |field|
    field = field.permit!.to_h.symbolize_keys
    ForestLiana::WidgetsHelper.set_field_widget(field)
    field
  end
  {:field_changed => found_field_changed, :fields => fields, :params => params, :user => forest_user}
end

#get_smart_action_hook_requestObject



4
5
6
7
8
9
10
11
12
13
14
# File 'app/controllers/forest_liana/actions_controller.rb', line 4

def get_smart_action_hook_request
  if params[:data] && params[:data][:attributes] && params[:data][:attributes][:collection_name]
    params[:data][:attributes]
  else
    error = 'parameters data attributes missing'
    FOREST_REPORTER.report error
    FOREST_LOGGER.error "Smart Action hook request error: #{error}"

    raise ForestLiana::Errors::HTTP422Error.new("Error in smart action load hook: cannot retrieve action from collection")
  end
end

#get_smart_action_load_ctx(fields) ⇒ Object



31
32
33
34
35
36
37
38
# File 'app/controllers/forest_liana/actions_controller.rb', line 31

def get_smart_action_load_ctx(fields)
  fields = fields.map do |field|
    ForestLiana::WidgetsHelper.set_field_widget(field)
    field[:value] = nil unless field[:value]
    field
  end
  {:fields => fields, :params => params, :user => forest_user}
end

#handle_result(result, action) ⇒ Object



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/forest_liana/actions_controller.rb', line 50

def handle_result(result, action)
  if result.nil? || !result.is_a?(Array)
    return render status: 500, json: { error: 'Error in smart action load hook: hook must return an array of fields' }
  end

  # Validate that the fields are well formed.
  begin
    # action.hooks[:change] is a hashmap here
    # to do the validation, only the hook names are require
    change_hooks_name = action.hooks[:change].nil? ? nil : action.hooks[:change].keys
    ForestLiana::SmartActionFieldValidator.validate_smart_action_fields(result, action.name, change_hooks_name)
  rescue ForestLiana::Errors::SmartActionInvalidFieldError => invalid_field_error
    FOREST_LOGGER.warn invalid_field_error.message
  rescue ForestLiana::Errors::SmartActionInvalidFieldHookError => invalid_hook_error
    FOREST_REPORTER.report invalid_hook_error
    FOREST_LOGGER.error invalid_hook_error.message
    return render status: 500, json: { error: invalid_hook_error.message }
  end

  # Apply result on fields (transform the object back to an array), preserve order.
  fields = result.map do |field|
    updated_field = result.find{|f| f[:field] == field[:field]}

    # Reset `value` when not present in `enums` (which means `enums` has changed).
    if updated_field[:enums].is_a?(Array)
      # `value` can be an array if the type of fields is `[x]`
      if updated_field[:type].is_a?(Array) && updated_field[:value].is_a?(Array) && !(updated_field[:value] - updated_field[:enums]).empty?
        updated_field[:value] = nil
      end

      # `value` can be any other value
      if !updated_field[:type].is_a?(Array) && !updated_field[:enums].include?(updated_field[:value])
        updated_field[:value] = nil
      end
    end

    # Response of load hook is not JSONAPI serialized
    # so we need to transform snake_case properties back to camelCase
    updated_field.transform_keys { |key| key.to_s.camelize(:lower) }
  end

  render serializer: nil, json: { fields: fields }, status: :ok
end

#loadObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/forest_liana/actions_controller.rb', line 94

def load
  load_request = get_smart_action_hook_request

  action = get_action(load_request[:collection_name])

  if !action
    render status: 500, json: {error: 'Error in smart action load hook: cannot retrieve action from collection'}
  else
    # Get the smart action hook load context
    context = get_smart_action_load_ctx(action.fields)

    # Call the user-defined load hook.
    result = action.hooks[:load].(context)

    handle_result(result, action)
  end
end