Skip to content

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

🗃️ Enforce one invite per email on a scheduled event (#2442)

Luke Vellaff63555db7f7
diff --git a/apps/web/src/features/scheduled-event/mutations.ts b/apps/web/src/features/scheduled-event/mutations.ts
index 58c65eb99..c0c894284 100644
--- a/apps/web/src/features/scheduled-event/mutations.ts
+++ b/apps/web/src/features/scheduled-event/mutations.ts
@@ -1,5 +1,5 @@
import type { ScheduledEventInviteStatus } from "@rallly/database";
-import { prisma } from "@rallly/database";
+import { Prisma, prisma } from "@rallly/database";
import { nanoid } from "@rallly/utils/nanoid";
import { updateTag } from "next/cache";
import { scheduledEventTag } from "@/features/scheduled-event/constants";
@@ -18,27 +18,32 @@ export async function createRsvp({
// Links the registration to a user account when the registrant is logged in.
inviteeId?: string;
}) {
- const existing = await prisma.scheduledEventInvite.findFirst({
- where: { scheduledEventId: eventId, inviteeEmail: email },
- select: { id: true },
- });
-
- if (existing) {
- return { ok: false, reason: "already_responded" } as const;
+ let invite: { uid: string };
+ try {
+ // The unique (scheduledEventId, inviteeEmail) index makes this atomic: a
+ // concurrent duplicate fails with P2002 instead of racing past a prior
+ // existence check.
+ invite = await prisma.scheduledEventInvite.create({
+ data: {
+ uid: nanoid(),
+ scheduledEventId: eventId,
+ inviteeName: name,
+ inviteeEmail: email,
+ inviteeId,
+ status,
+ },
+ select: { uid: true },
+ });
+ } catch (e) {
+ if (
+ e instanceof Prisma.PrismaClientKnownRequestError &&
+ e.code === "P2002"
+ ) {
+ return { ok: false, reason: "already_responded" } as const;
+ }
+ throw e;
}
- const invite = await prisma.scheduledEventInvite.create({
- data: {
- uid: nanoid(),
- scheduledEventId: eventId,
- inviteeName: name,
- inviteeEmail: email,
- inviteeId,
- status,
- },
- select: { uid: true },
- });
-
updateTag(scheduledEventTag(eventId));
return { ok: true, inviteUid: invite.uid } as const;
diff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.ts
index b63141fa2..616ba7658 100644
--- a/apps/web/src/trpc/routers/polls.ts
+++ b/apps/web/src/trpc/routers/polls.ts
@@ -829,6 +829,50 @@ export const polls = router({
},
});
+ // A poll can have several participants sharing an email; an event holds at
+ // most one invite per email, so collapse them, keeping the most committal
+ // response (accepted > tentative > declined) so a stale "no" duplicate
+ // can't bury an "accepted".
+ const inviteStatusByVote = {
+ yes: "accepted",
+ ifNeedBe: "tentative",
+ no: "declined",
+ } as const;
+ const inviteStatusRank = { accepted: 0, tentative: 1, declined: 2 };
+ const invitesByEmail = new Map<
+ string,
+ {
+ uid: string;
+ inviteeName: string;
+ inviteeEmail: string;
+ inviteeTimeZone: string | null | undefined;
+ status: (typeof inviteStatusByVote)[keyof typeof inviteStatusByVote];
+ }
+ >();
+ for (const p of poll.participants) {
+ if (!p.email) continue;
+ const status =
+ inviteStatusByVote[
+ p.votes.find((v) => v.optionId === input.optionId)?.type ?? "no"
+ ];
+ const key = p.email.trim().toLowerCase();
+ const existing = invitesByEmail.get(key);
+ if (
+ existing &&
+ inviteStatusRank[existing.status] <= inviteStatusRank[status]
+ ) {
+ continue;
+ }
+ invitesByEmail.set(key, {
+ uid: nanoid(),
+ inviteeName: p.name,
+ inviteeEmail: p.email,
+ inviteeTimeZone: p.user?.timeZone ?? p.timeZone ?? poll.timeZone,
+ status,
+ });
+ }
+ const inviteData = Array.from(invitesByEmail.values());
+
const scheduledEvent = await prisma.$transaction(async (tx) => {
// create scheduled event
const event = await tx.scheduledEvent.create({
@@ -853,25 +897,7 @@ export const polls = router({
hideAttendees: poll.hideParticipants,
invites: {
createMany: {
- data: poll.participants
- .filter((p) => !!p.email)
- .map((p) => ({
- uid: nanoid(),
- inviteeName: p.name,
- inviteeEmail: p.email as string,
- inviteeTimeZone:
- p.user?.timeZone ?? p.timeZone ?? poll.timeZone,
- status: (
- {
- yes: "accepted",
- ifNeedBe: "tentative",
- no: "declined",
- } as const
- )[
- p.votes.find((v) => v.optionId === input.optionId)
- ?.type ?? "no"
- ],
- })),
+ data: inviteData,
},
},
},
diff --git a/packages/database/prisma/migrations/20260616131914_unique_scheduled_event_invite_email/migration.sql b/packages/database/prisma/migrations/20260616131914_unique_scheduled_event_invite_email/migration.sql
new file mode 100644
index 000000000..dcc233606
--- /dev/null
+++ b/packages/database/prisma/migrations/20260616131914_unique_scheduled_event_invite_email/migration.sql
@@ -0,0 +1,35 @@
+-- Collapse duplicate invites before adding the unique constraint. A poll can
+-- have multiple participants with the same email, and finalizing it created one
+-- invite per participant, so an event can hold several invites for the same
+-- email. Keep one per (event, email): prefer an account-linked invite, then the
+-- most committal status, then the most recent activity.
+DELETE FROM "scheduled_event_invites"
+WHERE "id" IN (
+ SELECT "id"
+ FROM (
+ SELECT
+ "id",
+ row_number() OVER (
+ PARTITION BY "scheduled_event_id", "invitee_email"
+ ORDER BY
+ ("invitee_id" IS NOT NULL) DESC,
+ CASE "status"
+ WHEN 'accepted' THEN 0
+ WHEN 'tentative' THEN 1
+ WHEN 'declined' THEN 2
+ WHEN 'pending' THEN 3
+ END,
+ "updated_at" DESC NULLS LAST,
+ "created_at" DESC,
+ "id"
+ ) AS rn
+ FROM "scheduled_event_invites"
+ ) ranked
+ WHERE rn > 1
+);
+
+-- DropIndex
+DROP INDEX "scheduled_event_invites_scheduled_event_id_idx";
+
+-- CreateIndex
+CREATE UNIQUE INDEX "scheduled_event_invites_scheduled_event_id_invitee_email_key" ON "scheduled_event_invites"("scheduled_event_id", "invitee_email");
diff --git a/packages/database/prisma/models/event.prisma b/packages/database/prisma/models/event.prisma
index 1c5d4bdfc..e8a57809c 100644
--- a/packages/database/prisma/models/event.prisma
+++ b/packages/database/prisma/models/event.prisma
@@ -83,7 +83,7 @@ model ScheduledEventInvite {
scheduledEvent ScheduledEvent @relation(fields: [scheduledEventId], references: [id], onDelete: Cascade)
user User? @relation(fields: [inviteeId], references: [id], onDelete: SetNull) // Optional relation to User model
- @@index([scheduledEventId])
+ @@unique([scheduledEventId, inviteeEmail])
@@index([inviteeId])
@@index([inviteeEmail])
@@map("scheduled_event_invites")