Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
⚡️ Count poll list participants for the page only
Luke Vella1b456e2cb005
diff --git a/apps/web/src/features/poll/data.test.ts b/apps/web/src/features/poll/data.test.tsindex 6a4f27404..70002b8f9 100644--- a/apps/web/src/features/poll/data.test.ts+++ b/apps/web/src/features/poll/data.test.ts@@ -5,6 +5,7 @@ vi.mock("server-only", () => ({}));const mockFindMany = vi.fn();const mockCount = vi.fn();const mockOptionGroupBy = vi.fn();+const mockParticipantGroupBy = vi.fn();vi.mock("@rallly/database", () => ({prisma: {@@ -15,6 +16,9 @@ vi.mock("@rallly/database", () => ({option: {groupBy: (...args: unknown[]) => mockOptionGroupBy(...args),},+ participant: {+ groupBy: (...args: unknown[]) => mockParticipantGroupBy(...args),+ },$transaction: (operations: Promise<unknown>[]) => Promise.all(operations),},}));@@ -116,7 +120,7 @@ describe("getPolls", () => {vi.clearAllMocks();});- const makeListedPoll = (id: string, participants: number) => ({+ const makeListedPoll = (id: string) => ({id,title: `Poll ${id}`,status: "open",@@ -125,7 +129,6 @@ describe("getPolls", () => {createdAt: new Date("2025-01-10T12:00:00Z"),updatedAt: new Date("2025-01-11T12:00:00Z"),user: null,- _count: { participants },});it("returns each poll's participant count and option date range", async () => {@@ -133,12 +136,16 @@ describe("getPolls", () => {const end = new Date("2025-02-07T15:00:00Z");mockCount.mockResolvedValue(2);mockFindMany.mockResolvedValue([- makeListedPoll("p1", 3),- makeListedPoll("p2", 0),+ makeListedPoll("p1"),+ makeListedPoll("p2"),]);mockOptionGroupBy.mockResolvedValue([{ pollId: "p1", _min: { startTime: start }, _max: { startTime: end } },]);+ // A poll with no participants has no group+ mockParticipantGroupBy.mockResolvedValue([+ { pollId: "p1", _count: { _all: 3 } },+ ]);const result = await getPolls({ scope: { spaceId } });@@ -148,6 +155,12 @@ describe("getPolls", () => {_min: { startTime: true },_max: { startTime: true },});+ expect(mockParticipantGroupBy).toHaveBeenCalledWith({+ by: ["pollId"],+ where: { pollId: { in: ["p1", "p2"] } },+ _count: { _all: true },+ });+ expect(mockFindMany.mock.calls[0]?.[0].select).not.toHaveProperty("_count");expect(result.polls[0]).toMatchObject({id: "p1",participantCount: 3,diff --git a/apps/web/src/features/poll/data.ts b/apps/web/src/features/poll/data.tsindex 7613ec3dc..c71fc0a21 100644--- a/apps/web/src/features/poll/data.ts+++ b/apps/web/src/features/poll/data.ts@@ -372,11 +372,6 @@ export const getPolls = async ({image: true,},},- _count: {- select: {- participants: true,- },- },},orderBy: [{ updatedAt: "desc" }, { id: "desc" }],skip: (page - 1) * pageSize,@@ -384,15 +379,28 @@ export const getPolls = async ({}),]);- const optionRanges = await prisma.option.groupBy({- by: ["pollId"],- where: { pollId: { in: polls.map((poll) => poll.id) } },- _min: { startTime: true },- _max: { startTime: true },- });+ const pollIds = polls.map((poll) => poll.id);+ // Counted per page: a relation _count compiles to a GROUP BY over the+ // whole participants table, joined after the fact+ const [optionRanges, participantCounts] = await Promise.all([+ prisma.option.groupBy({+ by: ["pollId"],+ where: { pollId: { in: pollIds } },+ _min: { startTime: true },+ _max: { startTime: true },+ }),+ prisma.participant.groupBy({+ by: ["pollId"],+ where: { pollId: { in: pollIds } },+ _count: { _all: true },+ }),+ ]);const rangeByPollId = new Map(optionRanges.map((range) => [range.pollId, range]),);+ const participantCountByPollId = new Map(+ participantCounts.map((row) => [row.pollId, row._count._all]),+ );const transformedPolls = polls.map((poll) => {const range = rangeByPollId.get(poll.id);@@ -412,7 +420,7 @@ export const getPolls = async ({image: poll.user.image,}: null,- participantCount: poll._count.participants,+ participantCount: participantCountByPollId.get(poll.id) ?? 0,dateRange:range?._min.startTime && range._max.startTime? { start: range._min.startTime, end: range._max.startTime }