1# frozen_string_literal: true
2
3module API
4  module Entities
5    class Note < Grape::Entity
6      # Only Issue and MergeRequest have iid
7      NOTEABLE_TYPES_WITH_IID = %w(Issue MergeRequest).freeze
8
9      expose :id
10      expose :type
11      expose :note, as: :body
12      expose :attachment_identifier, as: :attachment
13      expose :author, using: Entities::UserBasic
14      expose :created_at, :updated_at
15      expose :system?, as: :system
16      expose :noteable_id, :noteable_type
17      expose :commit_id, if: ->(note, options) { note.noteable_type == "MergeRequest" && note.is_a?(DiffNote) }
18
19      expose :position, if: ->(note, options) { note.is_a?(DiffNote) } do |note|
20        note.position.to_h
21      end
22
23      expose :resolvable?, as: :resolvable
24      expose :resolved?, as: :resolved, if: ->(note, options) { note.resolvable? }
25      expose :resolved_by, using: Entities::UserBasic, if: ->(note, options) { note.resolvable? }
26      expose :resolved_at, if: ->(note, options) { note.resolvable? }
27
28      expose :confidential?, as: :confidential
29
30      # Avoid N+1 queries as much as possible
31      expose(:noteable_iid) { |note| note.noteable.iid if NOTEABLE_TYPES_WITH_IID.include?(note.noteable_type) }
32
33      expose(:commands_changes) { |note| note.commands_changes || {} }
34    end
35
36    # To be returned if the note was command-only
37    class NoteCommands < Grape::Entity
38      expose(:commands_changes) { |note| note.commands_changes || {} }
39      expose(:summary) { |note| note.errors[:commands_only] }
40    end
41  end
42end
43