Skip to content

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

⚡️ Improve participant create query performance (#2117)

Luke Vella0a51a08a39cb
diff --git a/apps/web/src/trpc/routers/polls/participants.ts b/apps/web/src/trpc/routers/polls/participants.ts
index 4f048205d..38bcff6d4 100644
--- a/apps/web/src/trpc/routers/polls/participants.ts
+++ b/apps/web/src/trpc/routers/polls/participants.ts
@@ -213,74 +213,68 @@ export const participants = router({
)
.mutation(
async ({ ctx, input: { pollId, votes, name, email, timeZone } }) => {
- const { participant, totalResponses } = await prisma.$transaction(
- async (prisma) => {
- const participantCount = await prisma.participant.count({
- where: {
- pollId,
- deleted: false,
- },
- });
+ 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`,
- });
- }
+ 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,
- ...(ctx.user.isLegacyGuest
- ? { guestId: ctx.user.id }
- : { userId: ctx.user.id }),
- locale: ctx.user.locale ?? undefined,
- },
- include: {
- poll: {
- select: {
- id: true,
- title: true,
- userId: true,
- guestId: true,
- },
- },
- },
- });
+ const options = await prisma.option.findMany({
+ where: {
+ pollId,
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ const existingOptionIds = new Set(options.map((option) => option.id));
+
+ const validVotes = votes.filter(({ optionId }) =>
+ existingOptionIds.has(optionId),
+ );
- const options = await prisma.option.findMany({
- where: {
- pollId,
+ const participant = await prisma.participant.create({
+ data: {
+ pollId: pollId,
+ name: name,
+ email,
+ timeZone,
+ ...(ctx.user.isLegacyGuest
+ ? { guestId: ctx.user.id }
+ : { userId: ctx.user.id }),
+ locale: ctx.user.locale ?? undefined,
+ votes: {
+ createMany: {
+ data: validVotes.map(({ optionId, type }) => ({
+ pollId,
+ optionId,
+ type,
+ })),
},
+ },
+ },
+ include: {
+ poll: {
select: {
id: true,
+ title: true,
+ userId: true,
+ guestId: true,
},
- });
-
- const existingOptionIds = new Set(
- options.map((option) => option.id),
- );
-
- const validVotes = votes.filter(({ optionId }) =>
- existingOptionIds.has(optionId),
- );
-
- await prisma.vote.createMany({
- data: validVotes.map(({ optionId, type }) => ({
- optionId,
- type,
- pollId,
- participantId: participant.id,
- })),
- });
-
- return { participant, totalResponses: participantCount + 1 };
+ },
},
- );
+ });
+
+ const totalResponses = participantCount + 1;
if (email) {
const token = await createParticipantEditToken(ctx.user.id);