Class: ForestAdminAgent::Routes::Resources::Related::CsvRelated

Inherits:
AbstractRelatedRoute show all
Includes:
Utils, ForestAdminDatasourceToolkit::Components::Query::ConditionTree, ForestAdminDatasourceToolkit::Utils
Defined in:
lib/forest_admin_agent/routes/resources/related/csv_related.rb

Instance Method Summary collapse

Methods inherited from AbstractRelatedRoute

#build

Methods inherited from AbstractAuthenticatedRoute

#build, #format_attributes, #redacted_full_projection, #redacted_projection_with_pks

Methods inherited from AbstractRoute

#add_route, #build, #initialize, #routes

Constructor Details

This class inherits a constructor from ForestAdminAgent::Routes::AbstractRoute

Instance Method Details

#handle_request(args = {}) ⇒ Object



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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/forest_admin_agent/routes/resources/related/csv_related.rb', line 22

def handle_request(args = {})
  context = build(args)
  context.permissions.can?(:browse, context.child_collection)
  context.permissions.can?(:export, context.child_collection)
  condition_tree = ForestAdminAgent::Utils::QueryStringParser.parse_condition_tree(
    context.child_collection, args
  )
  context.permissions.assert_can_read_query_fields(
    context.child_collection, condition_tree: condition_tree
  )

  filter = ForestAdminDatasourceToolkit::Components::Query::Filter.new(
    condition_tree: ConditionTreeFactory.intersect(
      [context.permissions.get_scope(context.child_collection), condition_tree]
    )
  )
  requested = ForestAdminAgent::Utils::QueryStringParser.parse_requested_projection(
    context.child_collection, args
  )
  projection = context.permissions.redact_projection(
    context.child_collection,
    requested[:projection],
    named_by_caller: requested[:named_by_caller]
  )

  # Get the parent record primary keys
  primary_key_values = Utils::Id.unpack_id(context.collection, args[:params]['id'], with_key: true)
  relation_name = args[:params]['relation_name']

  # Generate timestamp for filename
  now = Time.now.strftime('%Y%m%d_%H%M%S')
  collection_name = args.dig(:params, 'collection_name')
  header = ForestAdminAgent::Utils::CsvGenerator.filter_header(
    args.dig(:params, 'header'), requested[:projection], projection
  )
  filename_with_timestamp = "#{collection_name}_#{relation_name}_export_#{now}.csv"

  # Create a callable to fetch related records
  list_records = lambda do |batch_filter|
    ForestAdminDatasourceToolkit::Utils::Collection.list_relation(
      context.collection,
      primary_key_values,
      relation_name,
      context.caller,
      batch_filter,
      projection
    )
  end

  # For related exports, use list_relation to fetch records
  {
    content: {
      type: 'Stream',
      enumerator: ForestAdminAgent::Utils::CsvGeneratorStream.stream(
        header,
        filter,
        projection,
        list_records,
        Facades::Container.config_from_cache[:limit_export_size]
      ),
      headers: {
        'Content-Type' => 'text/csv; charset=utf-8',
        'Content-Disposition' => "attachment; filename=\"#{filename_with_timestamp}\"",
        'Cache-Control' => 'no-cache',
        'X-Accel-Buffering' => 'no'
      }
    },
    status: 200
  }
end

#setup_routesObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/forest_admin_agent/routes/resources/related/csv_related.rb', line 10

def setup_routes
  add_route(
    'forest_related_list_csv',
    'get',
    '/:collection_name/:id/relationships/:relation_name.:format',
    ->(args) { handle_request(args) },
    'csv'
  )

  self
end