Class: CommentsFiles

Inherits:
ApplicationRecord show all
Includes:
CommentFileProcedures
Defined in:
app/models/comments_files.rb

Constant Summary

Constants included from CommentFileProcedures

CommentFileProcedures::HEADERS

Class Method Summary collapse

Methods inherited from ApplicationRecord

define_decrypted_attribute, define_decrypted_attributes, #errors_in_bullet_points, primary_connection, special_connection

Class Method Details

.find_by_page(company_id:, reference:, reference_id:, display_length:, search_value: '', display_start_from_page_no: 1, grouping: 'comment_file_id desc') ⇒ Object



11
12
13
14
15
16
17
18
19
20
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
48
49
50
51
52
53
54
# File 'app/models/comments_files.rb', line 11

def find_by_page(
  company_id:,
  reference:,
  reference_id:,
  display_length:,
  search_value: '',
  display_start_from_page_no: 1,
  grouping: 'comment_file_id desc'
)
  # Fetch paginated parent comment files
  results = where_procedure(
    company_id: company_id,
    reference: reference,
    reference_id: reference_id,
    display_length: display_length,
    search_value: search_value,
    display_start_from_page_no: display_start_from_page_no,
    grouping: grouping
  )

  parent_files = results[:data]

  # Build final structured result
  structured_files = parent_files.map do |parent|
    parent_id = parent[:comment_file_id]
    children = find_procedure(parent_id) || []

    # Separate children into comments and files
    comments = children.select { |child| child[:transaction_type] == '01' }
    files = children.select { |child| child[:transaction_type] == '02' }

    # Merge children arrays into parent structure
    parent.merge(
      comments: comments,
      files: files
    )
  end

  # Return as { data, metadata }
  {
    data: structured_files,
    metadata: results[:metadata]
  }
end