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 {