Class: Assistant::TechnicalArticleManagementToolBuilder::AddRelatedMaterialTool

Inherits:
ManagementTool
  • Object
show all
Defined in:
app/services/assistant/technical_article_management_tool_builder.rb

Overview

Adds one curated evidence or related-material link.

Instance Method Summary collapse

Methods inherited from ManagementTool

#initialize

Constructor Details

This class inherits a constructor from Assistant::TechnicalArticleManagementToolBuilder::ManagementTool

Instance Method Details

#execute(id:, edit_token:, target_type:, target_id:, context: ArticleRevision::NO_CONTEXT) ⇒ String

Returns JSON link result.

Parameters:

  • id (Integer)

    draft article ID

  • edit_token (String)

    current optimistic token

  • target_type (String)

    curated target kind

  • target_id (Integer)

    target record ID

  • context (String, nil) (defaults to: ArticleRevision::NO_CONTEXT)

    evidence explanation

Returns:

  • (String)

    JSON link result



706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'app/services/assistant/technical_article_management_tool_builder.rb', line 706

def execute(id:, edit_token:, target_type:, target_id:, context: ArticleRevision::NO_CONTEXT, **)
  safely('add related material') do
    target, link_type = TechnicalArticleManagementToolBuilder.resolve_target!(
      target_type,
      target_id
    )
    authorize!(:read, target)
    context_supplied = !context.equal?(ArticleRevision::NO_CONTEXT)
    reserved_context = context_supplied && ContentLink.reserved_workflow_context?(context)
    raise ToolError, 'That link context is reserved for the Technical Article consolidation workflow.' if reserved_context

    with_current_article_for_related_material(id, edit_token) do |article, revision|
      if revision
        require_actor!
        staging_attributes = {
          target:,
          link_type:,
          created_by_type: 'sunny'
        }
        staging_attributes[:context] = context if context_supplied
        entry = revision.stage_content_link!(**staging_attributes)
        related_materials = TechnicalArticleManagementToolBuilder.revision_link_rows(revision)
        related_material = related_materials.find { |row| row[:key] == entry['key'] }
        return revision_response(article, revision, { related_material: })
      end

      link = article.outbound_content_links.with_targets.detect do |candidate|
        candidate.target == target && candidate.link_type == link_type
      end
      raise ToolError, 'Consolidation source links are workflow-managed and cannot be changed directly.' if link&.workflow_managed?

      link ||= article.outbound_content_links.build(
        target: target,
        link_type: link_type,
        created_by_type: 'sunny',
        position: article.outbound_content_links.maximum(:position).to_i + 1
      )
      link.context = context.presence if context_supplied
      link.save!
      article.update!(updater_id: require_actor!.id, updated_at: Time.current)
      return response(
        article,
        related_material: TechnicalArticleManagementToolBuilder.link_row(link)
      )
    end
  end
end

#nameString

Returns stable tool name.

Returns:

  • (String)

    stable tool name



698
# File 'app/services/assistant/technical_article_management_tool_builder.rb', line 698

def name = 'add_technical_article_related_material'