feat: quick polls in comments that end in a decision

«📊 Añadir una encuesta» adds 2 to 6 options to a new comment. Members,
viewers too, vote by clicking an option (one vote each, which they can
change or take back) and see each option's votes as a bar. Whoever
started it, or an admin, closes it with «Cerrar y decidir»: the most
voted wins (none on a tie) and a «Decidido: …» reply marked as a
decision shows above the ticket. Events of the comment (voted, closed).

API: addComment(…, poll), votePoll, closePoll, Comment.poll;
POST /api/comments/{id}/poll/vote and …/poll/close. MCP: add_comment
poll, vote_poll, close_poll.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-20 20:48:12 +02:00
parent 93d028e870
commit 3a46a236e4
41 changed files with 2144 additions and 77 deletions

View File

@@ -7,6 +7,10 @@ import {
import { CommentAddedEvent } from '../events/comment-added.event';
import { CommentDecisionMarkedEvent } from '../events/comment-decision-marked.event';
import { CommentThreadResolvedEvent } from '../events/comment-thread-resolved.event';
import {
CommentPollClosedEvent,
CommentPollVotedEvent,
} from '../events/comment-poll.events';
import { CommentDeletedEvent } from '../events/comment-deleted.event';
import { CommentEditedEvent } from '../events/comment-edited.event';
import { CommentReactedEvent } from '../events/comment-reacted.event';
@@ -30,6 +34,32 @@ export interface CommentProps {
deleted: boolean;
createdAt: string;
editedAt: string | null;
/** A quick poll's options (2–6); absent without one. */
poll?: string[] | null;
}
export const MIN_POLL_OPTIONS = 2;
export const MAX_POLL_OPTIONS = 6;
const MAX_POLL_OPTION = 80;
/** A poll's options, trimmed: 2 to 6, different, short. */
export function validPoll(options: string[]): string[] {
const clean = options
.map((o) => o.replace(/\s+/g, ' ').trim())
.filter(Boolean);
assertThat(
clean.length >= MIN_POLL_OPTIONS && clean.length <= MAX_POLL_OPTIONS,
`A poll takes ${MIN_POLL_OPTIONS} to ${MAX_POLL_OPTIONS} options`,
);
assertThat(
clean.every((o) => o.length <= MAX_POLL_OPTION),
`A poll option takes ${MAX_POLL_OPTION} characters at most`,
);
assertThat(
new Set(clean.map((o) => o.toLowerCase())).size === clean.length,
'Poll options must be different',
);
return clean;
}
const MAX_BODY = 20_000;
@@ -47,6 +77,11 @@ export class Comment extends VersionedAggregateRoot implements CommentProps {
public decision = false;
/** A top-level comment whose conversation was settled. */
public resolved = false;
/** A quick poll: its options, who voted what, and whether it closed. */
public poll: string[] | null = null;
public pollVotes: Record<string, number> = {};
public pollClosed = false;
public pollWinner: number | null = null;
public deleted = false;
public createdAt!: string;
public editedAt: string | null = null;
@@ -139,6 +174,63 @@ export class Comment extends VersionedAggregateRoot implements CommentProps {
);
}
/** Votes `option` in its poll (null takes the vote back); repeating it changes nothing. */
votePoll(actorId: string, option: number | null): void {
this.assertAlive();
if (!this.poll) throw new BusinessRuleError('This comment has no poll');
if (this.pollClosed) throw new BusinessRuleError('The poll is closed');
assertThat(
option === null ||
(Number.isInteger(option) && option >= 0 && option < this.poll.length),
'No such option',
);
if ((this.pollVotes[actorId] ?? null) === option) return;
const votes = { ...this.pollVotes };
if (option === null) delete votes[actorId];
else votes[actorId] = option;
this.apply(
new CommentPollVotedEvent(
actorId,
this.id,
this.projectId,
this.issueId,
votes,
),
);
}
/**
* Closes the poll: its author, or (`canModerate`) whoever manages the
* project. The winner is the most voted option; none on a tie.
*/
closePoll(actorId: string, canModerate: boolean): number | null {
this.assertAlive();
if (!this.poll) throw new BusinessRuleError('This comment has no poll');
if (this.pollClosed) throw new BusinessRuleError('The poll is closed');
if (actorId !== this.authorId && !canModerate)
throw new ForbiddenError(
'Only whoever started the poll or a project admin can close it',
);
const counts = this.poll.map(
(_, i) => Object.values(this.pollVotes).filter((v) => v === i).length,
);
const most = Math.max(...counts);
const winner =
most > 0 && counts.filter((c) => c === most).length === 1
? counts.indexOf(most)
: null;
this.apply(
new CommentPollClosedEvent(
actorId,
this.id,
this.projectId,
this.issueId,
winner,
),
);
return winner;
}
/** @param canModerate whether the actor manages the project (may delete others' comments). */
delete(actorId: string, canModerate: boolean): void {
this.assertAlive();
@@ -156,6 +248,16 @@ export class Comment extends VersionedAggregateRoot implements CommentProps {
onCommentAddedEvent({ comment }: CommentAddedEvent): void {
Object.assign(this, comment);
this.poll = comment.poll ?? null;
}
onCommentPollVotedEvent(event: CommentPollVotedEvent): void {
this.pollVotes = event.votes;
}
onCommentPollClosedEvent(event: CommentPollClosedEvent): void {
this.pollClosed = true;
this.pollWinner = event.winner;
}
onCommentEditedEvent(event: CommentEditedEvent): void {

View File

@@ -0,0 +1,31 @@
import { AutowiredEvent } from '@task-stream/api/shared/domain';
import { TrackerEvent } from './tracker.event';
/** Someone voted in a comment's poll, changed or took back their vote. */
@AutowiredEvent
export class CommentPollVotedEvent extends TrackerEvent {
constructor(
actorId: string,
commentId: string,
projectId: string,
readonly issueId: string,
/** Every vote after the change: user id → option index. */
readonly votes: Record<string, number>,
) {
super(actorId, commentId, projectId);
}
}
/** A comment's poll was closed; `winner` is null on a tie or no votes. */
@AutowiredEvent
export class CommentPollClosedEvent extends TrackerEvent {
constructor(
actorId: string,
commentId: string,
projectId: string,
readonly issueId: string,
readonly winner: number | null,
) {
super(actorId, commentId, projectId);
}
}

View File

@@ -18,6 +18,7 @@ export * from './comment-reacted.event';
export * from './comment-decision-marked.event';
export * from './comment-thread-resolved.event';
export * from './issue-review.events';
export * from './comment-poll.events';
export * from './comment-deleted.event';
export * from './release-created.event';
export * from './release-updated.event';

View File

@@ -1,6 +1,11 @@
import { Injectable } from '@nestjs/common';
import { newId } from '@task-stream/api/shared/domain';
import { Comment, CommentProps, validCommentBody } from '../comment/comment';
import {
Comment,
CommentProps,
validCommentBody,
validPoll,
} from '../comment/comment';
import { CommentAddedEvent } from '../events/comment-added.event';
@Injectable()
@@ -12,6 +17,8 @@ export class CommentFactory {
body: string;
/** Top-level comment it answers. */
replyTo?: string | null;
/** A quick poll's options. */
poll?: string[] | null;
}): Comment {
const data: CommentProps = {
id: newId(),
@@ -23,6 +30,7 @@ export class CommentFactory {
deleted: false,
createdAt: new Date().toISOString(),
editedAt: null,
...(props.poll?.length ? { poll: validPoll(props.poll) } : {}),
};
const comment = new Comment(data.id);
Object.assign(comment, { ...data });

View File

@@ -6,6 +6,14 @@ export class CommentReactionReadModel {
users!: UserRefReadModel[];
}
/** A quick poll: each option with who voted it. */
export class CommentPollReadModel {
options!: { text: string; voters: UserRefReadModel[] }[];
closed!: boolean;
/** Index of the winning option once closed (null on a tie). */
winner!: number | null;
}
export class CommentReadModel {
id!: string;
issueId!: string;
@@ -26,6 +34,8 @@ export class CommentReadModel {
/** Who marked it and when (null when it is not a decision). */
decidedBy!: UserRefReadModel | null;
decidedAt!: string | null;
/** A quick poll in the comment, if any. */
poll!: CommentPollReadModel | null;
/** A top-level comment whose conversation was settled. */
resolved!: boolean;
/** Who settled it and when (null while open). */