1
0
Fork 0
polaris-task-force/src/lib/tickets/voteState.ts
Z8MB1E 4594ff3d7c feat(tickets): add ticket mutation services
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-09-01 17:48:37 -04:00

42 lines
1.1 KiB
TypeScript

import type { Payload } from "payload";
import type { Ticket, User } from "@/payload-types";
export interface TicketVoteSummary {
readonly ticketId: number;
readonly voteCount: number;
readonly voted: boolean;
}
function relationId(relation: number | Ticket | User): number {
return typeof relation === "object" ? relation.id : relation;
}
export async function getTicketVoteSummaries(
payload: Payload,
userId: number,
ticketIds: readonly number[],
): Promise<readonly TicketVoteSummary[]> {
if (ticketIds.length === 0) return [];
const votes = await payload.find({
collection: "ticket-votes",
where: { ticket: { in: ticketIds } },
limit: 10000,
depth: 0,
overrideAccess: true,
});
const counts = new Map<number, number>();
const voted = new Set<number>();
for (const vote of votes.docs) {
const ticketId = relationId(vote.ticket);
counts.set(ticketId, (counts.get(ticketId) ?? 0) + 1);
if (relationId(vote.user) === userId) voted.add(ticketId);
}
return ticketIds.map((ticketId) => ({
ticketId,
voteCount: counts.get(ticketId) ?? 0,
voted: voted.has(ticketId),
}));
}