Class: ForestAdminAgent::Routes::Action::Actions

Inherits:
ForestAdminAgent::Routes::AbstractAuthenticatedRoute show all
Includes:
Builder, Utils, ForestAdminDatasourceCustomizer::Decorators::Action, ForestAdminDatasourceToolkit::Components::Query, ForestAdminDatasourceToolkit::Components::Query::ConditionTree
Defined in:
lib/forest_admin_agent/routes/action/actions.rb

Instance Method Summary collapse

Methods inherited from ForestAdminAgent::Routes::AbstractAuthenticatedRoute

#build, #format_attributes, #redacted_full_projection, #redacted_projection_with_pks

Methods inherited from ForestAdminAgent::Routes::AbstractRoute

#add_route, #build, #routes

Constructor Details

#initialize(collection, action) ⇒ Actions

Returns a new instance of Actions.



15
16
17
18
19
# File 'lib/forest_admin_agent/routes/action/actions.rb', line 15

def initialize(collection, action)
  @action_name = action
  @route_collection = collection
  super()
end

Instance Method Details

#handle_hook_request(args = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/forest_admin_agent/routes/action/actions.rb', line 83

def handle_hook_request(args = {})
  context = build(args)
  forest_fields = args.dig(:params, :data, :attributes, :fields)
  data = (if forest_fields
            Schema::ForestValueConverter.make_form_data_from_fields(context.datasource,
                                                                    forest_fields)
          end)
  filter = get_record_selection(args, context)
  search_values = {}
  forest_fields&.each { |field| search_values[field['field']] = field['searchValue'] }

  form = context.collection.get_form(
    context.caller,
    @action_name,
    data,
    filter,
    {
      change_field: args.dig(:params, :data, :attributes, :changed_field),
      search_field: args.dig(:params, :data, :attributes, :search_field),
      search_values: search_values,
      includeHiddenFields: false
    }
  )
  form_elements = Schema::GeneratorAction.extract_fields_and_layout(form)

  {
    content: {
      fields: form_elements[:fields].map do |f|
        Schema::GeneratorAction.build_field_schema(context.datasource, f)
      end,
      layout: Schema::GeneratorAction.build_layout(form_elements[:layout])
    }
  }
end

#handle_request(args = {}) ⇒ Object



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
78
79
80
81
# File 'lib/forest_admin_agent/routes/action/actions.rb', line 49

def handle_request(args = {})
  context = build(args)
  args = middleware_custom_action_approval_request_data(args)
  filter_for_caller = get_record_selection(args, context)
  get_record_selection(args, context, include_user_scope: false)

  context.permissions.can_smart_action?(args, context.collection, filter_for_caller)

  raw_data = args.dig(:params, :data, :attributes, :values)

  # As forms are dynamic, we don't have any way to ensure that we're parsing the data correctly
  # better send invalid data to the getForm() customer handler than to the execute() one.
  unsafe_data = Schema::ForestValueConverter.make_form_data_unsafe(raw_data)

  fields = context.collection.get_form(
    context.caller,
    @action_name,
    unsafe_data,
    filter_for_caller,
    { include_hidden_fields: true } # during execute, we need all possible fields
  )

  # Now that we have the field list, we can parse the data again.
  data = Schema::ForestValueConverter.make_form_data(
    context.datasource,
    raw_data,
    fields.reject { |field| field.type == 'Layout' }
  )

  result = execute_and_audit(context, args, data, filter_for_caller)

  { content: ForestAdminAgent::Utils::ActionResult.parse(result) }
end

#setup_routesObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/forest_admin_agent/routes/action/actions.rb', line 21

def setup_routes
  action_index = @route_collection.schema[:actions].keys.index(@action_name)
  slug = ForestAdminAgent::Utils::Schema::GeneratorAction.get_action_slug(@action_name)
  route_name = "forest_action_#{@route_collection.name}_#{action_index}_#{slug}"
  path = "/_actions/:collection_name/#{action_index}/#{slug}"

  add_route(route_name, 'post', path, proc { |args| handle_request(args) })
  add_route(
    "#{route_name}_load",
    'post',
    "#{path}/hooks/load",
    proc { |args| handle_hook_request(args) }
  )
  add_route(
    "#{route_name}_change",
    'post',
    "#{path}/hooks/change",
    proc { |args| handle_hook_request(args) }
  )
  add_route(
    "#{route_name}_search",
    'post',
    "#{path}/hooks/search",
    proc { |args| handle_hook_request(args) }
  )
  self
end