Class: Decidim::Proposals::ProposalAnswerType

Inherits:
Api::Types::BaseMutation
  • Object
show all
Defined in:
lib/decidim/api/mutations/proposal_answer_type.rb

Instance Method Summary collapse

Instance Method Details

#authorized?(attributes:) ⇒ Boolean



48
49
50
# File 'lib/decidim/api/mutations/proposal_answer_type.rb', line 48

def authorized?(attributes:)
  super && allowed_to?(:create, :proposal_answer, object, context, scope: :admin)
end

#current_userObject



52
53
54
# File 'lib/decidim/api/mutations/proposal_answer_type.rb', line 52

def current_user
  context[:current_user]
end

#resolve(attributes:) ⇒ Object



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
# File 'lib/decidim/api/mutations/proposal_answer_type.rb', line 13

def resolve(attributes:)
  answer_content = attributes.to_h.fetch(:answer_content, object.answer)
  internal_state = attributes.to_h.fetch(:state, object.internal_state)
  params = attributes.to_h.reverse_merge(
    internal_state:,
    answer: answer_content,
    cost: object.cost,
    cost_report: object.cost_report,
    execution_period: object.execution_period
  )

  form = Decidim::Proposals::Admin::ProposalAnswerForm.from_params(
    params
  ).with_context(
    current_component: object.component,
    current_user:,
    current_organization: current_user.organization
  )

  Admin::AnswerProposal.call(form, object) do
    on(:ok) do
      return object
    end
    on(:invalid) do
      return GraphQL::ExecutionError.new(
        form.errors.full_messages.join(", ")
      )
    end

    GraphQL::ExecutionError.new(
      I18n.t("decidim.proposals.admin.proposals.answer.invalid")
    )
  end
end