Spaces:
Sleeping
Sleeping
Ticket Comments - Implementation Summary
โ What Was Implemented
1. Service Layer (ticket_comment_service.py)
- โ
create_comment()- Create comments with validation - โ
update_comment()- Edit comments (author only) - โ
delete_comment()- Soft delete (author or PM) - โ
get_comment()- Get single comment - โ
list_comments()- Paginated list with filtering - โ
get_comment_replies()- Load threaded replies - โ
_to_response()- Convert model to response schema
2. API Endpoints (ticket_comments.py)
- โ
POST /tickets/{ticket_id}/comments- Create comment - โ
PUT /comments/{comment_id}- Update comment - โ
DELETE /comments/{comment_id}- Delete comment - โ
GET /comments/{comment_id}- Get comment - โ
GET /tickets/{ticket_id}/comments- List comments - โ
GET /comments/{comment_id}/replies- Get replies
3. Features
- โ
Threading - Reply to comments via
parent_comment_id - โ
Mentions - Tag users via
mentioned_user_ids - โ
Attachments - Link documents via
attachment_document_ids - โ Edit Tracking - Track who edited and when
- โ Soft Delete - Preserve data integrity
- โ Pagination - Handle large comment lists
- โ Filtering - By type, internal/external, parent-only
- โ Authorization - Role-based access control
4. Validation
- โ Ticket exists before creating comment
- โ Parent comment exists for replies
- โ Mentioned users exist
- โ Only author can edit their comments
- โ Author or PM can delete comments
- โ Comment text not empty
5. Documentation
- โ API documentation with examples
- โ Testing guide with curl commands
- โ Error handling documentation
- โ Best practices guide
๐ง How It Works
Threading (Replies)
Comment A (parent_comment_id: null)
โโโ Reply B (parent_comment_id: A)
โ โโโ Reply C (parent_comment_id: B)
โโโ Reply D (parent_comment_id: A)
Implementation:
- Each comment has optional
parent_comment_id reply_countcomputed in_to_response()- Replies loaded separately via
/comments/{id}/replies - Replies sorted by
created_at ASC(oldest first)
Edit Tracking
When comment is edited:
comment_textupdatedis_editedset totrueedited_atset to current timestampedited_by_user_idset to editor's IDupdated_atupdated
Original text NOT preserved (consider version history if needed)
Soft Delete
When comment is deleted:
deleted_atset to current timestamp- Comment excluded from all queries via
deleted_at IS NULL - Data preserved for audit trail
- Can be restored by setting
deleted_at = NULL
๐ฏ Usage Examples
Field Agent: Add Note
POST /tickets/{ticket_id}/comments
{
"comment_text": "Customer not home, will retry tomorrow",
"is_internal": true,
"comment_type": "update"
}
Dispatcher: Ask Question
POST /tickets/{ticket_id}/comments
{
"comment_text": "@john Can you check the customer location?",
"mentioned_user_ids": ["john-uuid"],
"is_internal": true,
"comment_type": "question"
}
PM: Reply to Question
POST /tickets/{ticket_id}/comments
{
"comment_text": "Location verified, proceed with installation",
"parent_comment_id": "question-uuid",
"is_internal": true,
"comment_type": "resolution"
}
Load Threaded Conversation
# 1. Get top-level comments
GET /tickets/{ticket_id}/comments?parent_only=true
# 2. For each comment with reply_count > 0:
GET /comments/{comment_id}/replies
๐ Security
Authorization Matrix
| Action | Field Agent | Dispatcher | PM | Platform Admin |
|---|---|---|---|---|
| Create comment | โ | โ | โ | โ |
| View comments | โ | โ | โ | โ |
| Edit own comment | โ | โ | โ | โ |
| Edit others' comment | โ | โ | โ | โ |
| Delete own comment | โ | โ | โ | โ |
| Delete others' comment | โ | โ | โ | โ |
Validation Checks
- โ Ticket exists
- โ Parent comment exists (for replies)
- โ Mentioned users exist
- โ User owns comment (for edit)
- โ User is author or PM (for delete)
- โ Comment text not empty
๐ Database Impact
Indexes (Already in schema.sql)
CREATE INDEX idx_ticket_comments_ticket ON ticket_comments(ticket_id, deleted_at);
CREATE INDEX idx_ticket_comments_parent ON ticket_comments(parent_comment_id);
CREATE INDEX idx_ticket_comments_user ON ticket_comments(user_id);
CREATE INDEX idx_ticket_comments_created ON ticket_comments(created_at DESC);
Storage Estimate
- Average comment: ~200 bytes
- 1000 comments: ~200 KB
- 1 million comments: ~200 MB
๐ Performance
Query Optimization
- โ
Use
joinedload()for relationships - โ
Filter
deleted_at IS NULLin all queries - โ Paginate large result sets
- โ
Index on
ticket_id,parent_comment_id,user_id
Expected Response Times
- Create comment: < 100ms
- List comments (50 items): < 200ms
- Get replies: < 100ms
- Update comment: < 100ms
- Delete comment: < 100ms
๐ฎ Future Enhancements
Phase 2: Notifications
- Notify mentioned users
- Notify ticket assignees on external comments
- Real-time updates via SSE
Phase 3: Rich Features
- Markdown support
- Code blocks
- File attachments (not just links)
- Emoji reactions
Phase 4: Advanced
- Full-text search
- Comment version history
- Bulk operations
- Comment templates
๐ Known Limitations
- No version history - Edits overwrite original text
- No notifications - Mentions don't trigger notifications yet
- No rich text - Plain text only
- No reactions - Can't like/emoji comments
- No search - Must paginate to find comments
๐ Testing Checklist
- Create comment on valid ticket
- Create reply to comment
- Update own comment
- Delete own comment
- List comments with pagination
- Get comment replies
- Filter by is_internal
- Filter by comment_type
- Filter parent_only
- Validate ticket exists
- Validate parent comment exists
- Validate mentioned users exist
- Prevent editing others' comments
- Allow PM to delete any comment
- Soft delete preserves data
- Reply count accurate
- Edit tracking works
๐ Best Practices
For Developers
- Always filter
deleted_at IS NULL - Use
joinedload()for relationships - Validate foreign keys before creating
- Use transactions for data consistency
- Log all operations for debugging
For Users
- Keep comments concise and actionable
- Use appropriate comment types
- Mark sensitive info as internal
- Reply to comments for context
- Edit instead of delete when possible
๐ Support
Common Issues
Q: Comments not appearing?
A: Check deleted_at IS NULL filter
Q: Can't edit comment? A: Only author can edit their own comments
Q: Reply count wrong?
A: Ensure subquery counts deleted_at IS NULL
Q: Mentions not working? A: Notifications not implemented yet (Phase 2)
Contact
- Backend issues: Check logs in
src/app/services/ticket_comment_service.py - API issues: Check
src/app/api/v1/ticket_comments.py - Database issues: Check indexes and constraints
๐จโ๐ป Frontend Team Guide
See FRONTEND.md for:
- 6 endpoints with exact request/response formats
- UI patterns (simple list, threaded view, pagination)
- React component examples
- Common use cases
- Error handling
- Performance tips
- Quick reference (no fluff)