Files
kandev/apps/backend/internal/office/service/comments.go
T
2026-05-16 20:07:57 +01:00

56 lines
1.7 KiB
Go

package service
import (
"context"
"fmt"
"go.uber.org/zap"
"github.com/kandev/kandev/internal/events"
"github.com/kandev/kandev/internal/events/bus"
"github.com/kandev/kandev/internal/office/models"
)
// CreateComment persists a task comment and emits a creation event so
// subscribers (e.g. channel relay) can react.
func (s *Service) CreateComment(ctx context.Context, comment *models.TaskComment) error {
if err := s.repo.CreateTaskComment(ctx, comment); err != nil {
return fmt.Errorf("create comment: %w", err)
}
s.publishCommentCreated(ctx, comment)
return nil
}
// ListComments returns all comments for a task.
func (s *Service) ListComments(ctx context.Context, taskID string) ([]*models.TaskComment, error) {
return s.repo.ListTaskComments(ctx, taskID)
}
// DeleteComment deletes a task comment by ID.
func (s *Service) DeleteComment(ctx context.Context, id string) error {
return s.repo.DeleteTaskComment(ctx, id)
}
// SetRelay replaces the channel relay instance (for testing with custom HTTP clients).
func (s *Service) SetRelay(r *ChannelRelay) {
s.relay = r
}
// publishCommentCreated emits an OfficeCommentCreated event.
// If no event bus is configured the call is silently skipped.
func (s *Service) publishCommentCreated(ctx context.Context, comment *models.TaskComment) {
if s.eb == nil {
return
}
data := CommentPostedData{
TaskID: comment.TaskID,
CommentID: comment.ID,
AuthorID: comment.AuthorID,
AuthorType: comment.AuthorType,
}
event := bus.NewEvent(events.OfficeCommentCreated, "office-service", data)
if err := s.eb.Publish(ctx, events.OfficeCommentCreated, event); err != nil {
s.logger.Error("publish comment created event failed", zap.Error(err))
}
}