Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

📈 Track total responses (#1903)

Luke Vella084df75086d0
diff --git a/apps/web/src/features/poll/analytics.ts b/apps/web/src/features/poll/analytics.ts
index 8e9ab13ba..e1cf1e57e 100644
--- a/apps/web/src/features/poll/analytics.ts
+++ b/apps/web/src/features/poll/analytics.ts
@@ -127,6 +127,7 @@ export interface PollResponseSubmitEvent extends BaseEventData {
properties: {
participantId: string;
hasEmail: boolean;
+ totalResponses: number;
};
}
diff --git a/apps/web/src/trpc/routers/polls/participants.ts b/apps/web/src/trpc/routers/polls/participants.ts
index 872954657..bf2331836 100644
--- a/apps/web/src/trpc/routers/polls/participants.ts
+++ b/apps/web/src/trpc/routers/polls/participants.ts
@@ -154,66 +154,70 @@ export const participants = router({
async ({ ctx, input: { pollId, votes, name, email, timeZone } }) => {
const { user } = ctx;
- const participant = await prisma.$transaction(async (prisma) => {
- const participantCount = await prisma.participant.count({
- where: {
- pollId,
- deleted: false,
- },
- });
-
- if (participantCount >= MAX_PARTICIPANTS) {
- throw new TRPCError({
- code: "BAD_REQUEST",
- message: `This poll has reached its maximum limit of ${MAX_PARTICIPANTS} participants`,
+ const { participant, totalResponses } = await prisma.$transaction(
+ async (prisma) => {
+ const participantCount = await prisma.participant.count({
+ where: {
+ pollId,
+ deleted: false,
+ },
});
- }
- const participant = await prisma.participant.create({
- data: {
- pollId: pollId,
- name: name,
- email,
- timeZone,
- ...(user.isGuest ? { guestId: user.id } : { userId: user.id }),
- locale: user.locale ?? undefined,
- },
- include: {
- poll: {
- select: {
- id: true,
- title: true,
+ if (participantCount >= MAX_PARTICIPANTS) {
+ throw new TRPCError({
+ code: "BAD_REQUEST",
+ message: `This poll has reached its maximum limit of ${MAX_PARTICIPANTS} participants`,
+ });
+ }
+
+ const participant = await prisma.participant.create({
+ data: {
+ pollId: pollId,
+ name: name,
+ email,
+ timeZone,
+ ...(user.isGuest ? { guestId: user.id } : { userId: user.id }),
+ locale: user.locale ?? undefined,
+ },
+ include: {
+ poll: {
+ select: {
+ id: true,
+ title: true,
+ },
},
},
- },
- });
+ });
- const options = await prisma.option.findMany({
- where: {
- pollId,
- },
- select: {
- id: true,
- },
- });
+ const options = await prisma.option.findMany({
+ where: {
+ pollId,
+ },
+ select: {
+ id: true,
+ },
+ });
- const existingOptionIds = new Set(options.map((option) => option.id));
+ const existingOptionIds = new Set(
+ options.map((option) => option.id),
+ );
- const validVotes = votes.filter(({ optionId }) =>
- existingOptionIds.has(optionId),
- );
+ const validVotes = votes.filter(({ optionId }) =>
+ existingOptionIds.has(optionId),
+ );
- await prisma.vote.createMany({
- data: validVotes.map(({ optionId, type }) => ({
- optionId,
- type,
- pollId,
- participantId: participant.id,
- })),
- });
+ await prisma.vote.createMany({
+ data: validVotes.map(({ optionId, type }) => ({
+ optionId,
+ type,
+ pollId,
+ participantId: participant.id,
+ })),
+ });
- return participant;
- });
+ return { participant, totalResponses: participantCount + 1 };
+ },
+ );
if (email) {
const token = await createToken(
@@ -253,6 +257,7 @@ export const participants = router({
properties: {
participantId: participant.id,
hasEmail: !!email,
+ totalResponses,
},
});