Skip to content

Comments: store user_id as author_id for local_agent comments instead of api_token_id #29

@HamptonMakes

Description

@HamptonMakes

Problem

When a comment is created via the API, author_id is set to @api_token.id (the API token's ID), not the user's ID. This forces CommentsHelper#comment_author_name to do a raw SQL join through coplan_api_tokens to resolve the user's name, hardcoding the users table name in the process.

Current behavior

# API controller sets token ID as author:
author_id: @api_token.id

# Helper has to join through token to get user name:
CoPlan.user_class
  .joins("INNER JOIN coplan_api_tokens ON coplan_api_tokens.user_id = users.id")
  .where("coplan_api_tokens.id = ?", comment.author_id)
  .pick(:name)

Proposed fix

Store @api_token.user_id as author_id for local_agent comments. Then the helper can use CoPlan.user_class.find_by(id: comment.author_id) for both human and local_agent types — no join, no hardcoded table name.

The agent_name field (already required for local_agent comments) distinguishes which agent made the comment.

Files involved

  • engine/app/controllers/coplan/api/v1/comments_controller.rb — change author_id: @api_token.idauthor_id: @api_token.user_id
  • engine/app/helpers/coplan/comments_helper.rb — simplify local_agent branch to a direct user lookup
  • Backfill migration for any existing comments with token IDs as author_id

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions