Skip to content

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

✨ Add sheets/slots/bookings tRPC router + signupSheets flag (RAL-1190) (#2392)

Luke Vellaf8b52c26d8c8
diff --git a/apps/web/src/features/sheets/constants.ts b/apps/web/src/features/sheets/constants.ts
new file mode 100644
index 000000000..7705197df
--- /dev/null
+++ b/apps/web/src/features/sheets/constants.ts
@@ -0,0 +1 @@
+export const isSignupSheetsEnabled = process.env.NODE_ENV === "development";
diff --git a/apps/web/src/features/sheets/data.ts b/apps/web/src/features/sheets/data.ts
new file mode 100644
index 000000000..c80d9248c
--- /dev/null
+++ b/apps/web/src/features/sheets/data.ts
@@ -0,0 +1,139 @@
+import type { EventType, Sheet, SheetSlot } from "@rallly/database";
+import { prisma } from "@rallly/database";
+import { createLogger } from "@rallly/logger";
+import type {
+ SheetDetailDTO,
+ SheetDTO,
+ SheetSlotDTO,
+} from "@/features/sheets/types";
+import { locationSchema } from "@/lib/location";
+
+const logger = createLogger("sheets/data");
+
+type EventTypeForSlot = Pick<
+ EventType,
+ "id" | "name" | "duration" | "description" | "location" | "capacity"
+>;
+
+type SlotWithRelations = SheetSlot & {
+ eventType: EventTypeForSlot;
+ scheduledEvent: {
+ capacity: number | null;
+ _count: { invites: number };
+ } | null;
+};
+
+function parseLocation(
+ raw: EventTypeForSlot["location"],
+ context: { eventTypeId: string },
+) {
+ if (raw === null) {
+ return null;
+ }
+ const parsed = locationSchema.safeParse(raw);
+ if (!parsed.success) {
+ logger.warn(
+ { eventTypeId: context.eventTypeId, value: raw },
+ "Failed to parse event type location",
+ );
+ return null;
+ }
+ return parsed.data;
+}
+
+export function createSheetSlotDTO(slot: SlotWithRelations): SheetSlotDTO {
+ return {
+ id: slot.id,
+ startTime: slot.startTime,
+ eventType: {
+ id: slot.eventType.id,
+ name: slot.eventType.name,
+ duration: slot.eventType.duration,
+ description: slot.eventType.description,
+ location: parseLocation(slot.eventType.location, {
+ eventTypeId: slot.eventType.id,
+ }),
+ capacity: slot.eventType.capacity,
+ },
+ bookingCount: slot.scheduledEvent?._count.invites ?? 0,
+ capacity: slot.scheduledEvent?.capacity ?? slot.eventType.capacity,
+ };
+}
+
+export function createSheetDTO(sheet: Sheet): SheetDTO {
+ return {
+ id: sheet.id,
+ title: sheet.title,
+ description: sheet.description,
+ urlId: sheet.urlId,
+ createdAt: sheet.createdAt,
+ updatedAt: sheet.updatedAt,
+ };
+}
+
+export function createSheetDetailDTO(
+ sheet: Sheet & { slots: SlotWithRelations[] },
+): SheetDetailDTO {
+ return {
+ ...createSheetDTO(sheet),
+ slots: sheet.slots.map(createSheetSlotDTO),
+ };
+}
+
+const slotInclude = {
+ eventType: {
+ select: {
+ id: true,
+ name: true,
+ duration: true,
+ description: true,
+ location: true,
+ capacity: true,
+ },
+ },
+ scheduledEvent: {
+ select: {
+ capacity: true,
+ _count: { select: { invites: true } },
+ },
+ },
+} as const;
+
+export async function getSheets(spaceId: string): Promise<SheetDTO[]> {
+ const rows = await prisma.sheet.findMany({
+ where: { spaceId, deleted: false },
+ orderBy: { updatedAt: "desc" },
+ });
+ return rows.map(createSheetDTO);
+}
+
+export async function getSheetById(
+ id: string,
+ spaceId: string,
+): Promise<SheetDetailDTO | null> {
+ const sheet = await prisma.sheet.findFirst({
+ where: { id, spaceId, deleted: false },
+ include: {
+ slots: {
+ orderBy: { startTime: "asc" },
+ include: slotInclude,
+ },
+ },
+ });
+ return sheet ? createSheetDetailDTO(sheet) : null;
+}
+
+export async function getSheetByUrlId(
+ urlId: string,
+): Promise<SheetDetailDTO | null> {
+ const sheet = await prisma.sheet.findFirst({
+ where: { urlId, deleted: false },
+ include: {
+ slots: {
+ orderBy: { startTime: "asc" },
+ include: slotInclude,
+ },
+ },
+ });
+ return sheet ? createSheetDetailDTO(sheet) : null;
+}
diff --git a/apps/web/src/features/sheets/schema.ts b/apps/web/src/features/sheets/schema.ts
new file mode 100644
index 000000000..ab56eecd5
--- /dev/null
+++ b/apps/web/src/features/sheets/schema.ts
@@ -0,0 +1,31 @@
+import * as z from "zod";
+
+export const createSheetInputSchema = z.object({
+ title: z.string().min(1).max(255),
+});
+
+export type CreateSheetInput = z.infer<typeof createSheetInputSchema>;
+
+export const updateSheetInputSchema = z.object({
+ id: z.string(),
+ title: z.string().min(1).max(255),
+});
+
+export type UpdateSheetInput = z.infer<typeof updateSheetInputSchema>;
+
+export const createSlotInputSchema = z.object({
+ sheetId: z.string(),
+ eventTypeId: z.string(),
+ startTime: z.date(),
+});
+
+export type CreateSlotInput = z.infer<typeof createSlotInputSchema>;
+
+export const createBookingInputSchema = z.object({
+ slotId: z.string(),
+ name: z.string().min(1).max(100),
+ email: z.email(),
+ timeZone: z.string().min(1).max(64),
+});
+
+export type CreateBookingInput = z.infer<typeof createBookingInputSchema>;
diff --git a/apps/web/src/features/sheets/types.ts b/apps/web/src/features/sheets/types.ts
new file mode 100644
index 000000000..e0679b63a
--- /dev/null
+++ b/apps/web/src/features/sheets/types.ts
@@ -0,0 +1,29 @@
+import type { Location } from "@/lib/location";
+
+export interface SheetDTO {
+ id: string;
+ title: string;
+ description: string | null;
+ urlId: string;
+ createdAt: Date;
+ updatedAt: Date;
+}
+
+export interface SheetSlotDTO {
+ id: string;
+ startTime: Date;
+ eventType: {
+ id: string;
+ name: string;
+ duration: number;
+ description: string | null;
+ location: Location | null;
+ capacity: number | null;
+ };
+ bookingCount: number;
+ capacity: number | null;
+}
+
+export interface SheetDetailDTO extends SheetDTO {
+ slots: SheetSlotDTO[];
+}
diff --git a/apps/web/src/lib/feature-flags/config.ts b/apps/web/src/lib/feature-flags/config.ts
index a4f9fd3fb..da6bb1348 100644
--- a/apps/web/src/lib/feature-flags/config.ts
+++ b/apps/web/src/lib/feature-flags/config.ts
@@ -4,6 +4,7 @@ import { isBillingEnabled } from "@/features/billing/constants";
import { isCalendarsEnabled } from "@/features/calendars/constants";
import { isEventTypesEnabled } from "@/features/event-types/constants";
import { isFeedbackEnabled } from "@/features/feedback/constants";
+import { isSignupSheetsEnabled } from "@/features/sheets/constants";
import type { FeatureFlagConfig } from "@/lib/feature-flags/types";
import { isStorageEnabled } from "@/lib/storage";
@@ -18,4 +19,5 @@ export const featureFlagConfig: FeatureFlagConfig = {
registration: isEmailLoginEnabled && isRegistrationEnabled,
calendars: isCalendarsEnabled,
eventTypes: isEventTypesEnabled,
+ signupSheets: isSignupSheetsEnabled,
};
diff --git a/apps/web/src/lib/feature-flags/types.ts b/apps/web/src/lib/feature-flags/types.ts
index d939c7a19..7ba5bf201 100644
--- a/apps/web/src/lib/feature-flags/types.ts
+++ b/apps/web/src/lib/feature-flags/types.ts
@@ -6,6 +6,7 @@ export interface FeatureFlagConfig {
registration: boolean;
calendars: boolean;
eventTypes: boolean;
+ signupSheets: boolean;
}
export type Feature = keyof FeatureFlagConfig;
diff --git a/apps/web/src/trpc/routers/index.ts b/apps/web/src/trpc/routers/index.ts
index cffc0f31a..f254d4900 100644
--- a/apps/web/src/trpc/routers/index.ts
+++ b/apps/web/src/trpc/routers/index.ts
@@ -9,6 +9,7 @@ import { dashboard } from "./dashboard";
import { eventTypes } from "./event-types";
import { events } from "./events";
import { polls } from "./polls";
+import { sheets } from "./sheets";
import { spaces } from "./spaces";
import { system } from "./system";
import { user } from "./user";
@@ -22,6 +23,7 @@ export const appRouter = mergeRouters(
eventTypes,
events,
polls,
+ sheets,
spaces,
system,
user,
diff --git a/apps/web/src/trpc/routers/sheets.ts b/apps/web/src/trpc/routers/sheets.ts
new file mode 100644
index 000000000..ccd889820
--- /dev/null
+++ b/apps/web/src/trpc/routers/sheets.ts
@@ -0,0 +1,355 @@
+import { prisma } from "@rallly/database";
+import { nanoid } from "@rallly/utils/nanoid";
+import { TRPCError } from "@trpc/server";
+import * as z from "zod";
+import { isSignupSheetsEnabled } from "@/features/sheets/constants";
+import {
+ createSheetDTO,
+ getSheetById,
+ getSheetByUrlId,
+ getSheets,
+} from "@/features/sheets/data";
+import {
+ createBookingInputSchema,
+ createSheetInputSchema,
+ createSlotInputSchema,
+ updateSheetInputSchema,
+} from "@/features/sheets/schema";
+import type { Location } from "@/lib/location";
+import { locationSchema } from "@/lib/location";
+import {
+ mergeRouters,
+ middleware,
+ publicProcedure,
+ router,
+ spaceProcedure,
+} from "../trpc";
+
+const requireSignupSheetsEnabled = middleware(async ({ next }) => {
+ if (!isSignupSheetsEnabled) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Sign-up sheets are not enabled",
+ });
+ }
+ return next();
+});
+
+const sheetSpaceProcedure = spaceProcedure.use(requireSignupSheetsEnabled);
+const sheetPublicProcedure = publicProcedure.use(requireSignupSheetsEnabled);
+
+function locationSnapshot(value: unknown): string | null {
+ if (value === null || value === undefined) {
+ return null;
+ }
+ const parsed = locationSchema.safeParse(value);
+ if (!parsed.success) {
+ return null;
+ }
+ const location: Location = parsed.data;
+ if (location.type === "in_person") {
+ return location.address;
+ }
+ return location.text ?? location.url;
+}
+
+const sheetsRoot = router({
+ list: sheetSpaceProcedure.query(async ({ ctx }) => {
+ const sheets = await getSheets(ctx.space.id);
+ return { sheets };
+ }),
+
+ get: sheetSpaceProcedure
+ .input(z.object({ id: z.string() }))
+ .query(async ({ ctx, input }) => {
+ const sheet = await getSheetById(input.id, ctx.space.id);
+ if (!sheet) {
+ throw new TRPCError({ code: "NOT_FOUND", message: "Sheet not found" });
+ }
+ return { sheet };
+ }),
+
+ getByUrlId: sheetPublicProcedure
+ .input(z.object({ urlId: z.string() }))
+ .query(async ({ input }) => {
+ const sheet = await getSheetByUrlId(input.urlId);
+ if (!sheet) {
+ throw new TRPCError({ code: "NOT_FOUND", message: "Sheet not found" });
+ }
+ return { sheet };
+ }),
+
+ create: sheetSpaceProcedure
+ .input(createSheetInputSchema)
+ .mutation(async ({ ctx, input }) => {
+ const created = await prisma.sheet.create({
+ data: {
+ spaceId: ctx.space.id,
+ hostId: ctx.user.id,
+ title: input.title,
+ urlId: nanoid(),
+ },
+ });
+ return { sheet: createSheetDTO(created) };
+ }),
+
+ update: sheetSpaceProcedure
+ .input(updateSheetInputSchema)
+ .mutation(async ({ ctx, input }) => {
+ const updated = await prisma.sheet.updateMany({
+ where: {
+ id: input.id,
+ spaceId: ctx.space.id,
+ deleted: false,
+ },
+ data: {
+ title: input.title,
+ },
+ });
+ if (updated.count === 0) {
+ throw new TRPCError({ code: "NOT_FOUND", message: "Sheet not found" });
+ }
+ return { success: true };
+ }),
+
+ softDelete: sheetSpaceProcedure
+ .input(z.object({ id: z.string() }))
+ .mutation(async ({ ctx, input }) => {
+ const deleted = await prisma.sheet.updateMany({
+ where: {
+ id: input.id,
+ spaceId: ctx.space.id,
+ deleted: false,
+ },
+ data: {
+ deleted: true,
+ deletedAt: new Date(),
+ },
+ });
+ if (deleted.count === 0) {
+ throw new TRPCError({ code: "NOT_FOUND", message: "Sheet not found" });
+ }
+ return { success: true };
+ }),
+});
+
+const slots = router({
+ create: sheetSpaceProcedure
+ .input(createSlotInputSchema)
+ .mutation(async ({ ctx, input }) => {
+ const sheet = await prisma.sheet.findFirst({
+ where: {
+ id: input.sheetId,
+ spaceId: ctx.space.id,
+ deleted: false,
+ },
+ select: { id: true },
+ });
+ if (!sheet) {
+ throw new TRPCError({ code: "NOT_FOUND", message: "Sheet not found" });
+ }
+
+ const eventType = await prisma.eventType.findFirst({
+ where: {
+ id: input.eventTypeId,
+ spaceId: ctx.space.id,
+ deleted: false,
+ },
+ select: { id: true },
+ });
+ if (!eventType) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Event type not found in this space",
+ });
+ }
+
+ const slot = await prisma.sheetSlot.create({
+ data: {
+ sheetId: input.sheetId,
+ eventTypeId: input.eventTypeId,
+ startTime: input.startTime,
+ },
+ });
+
+ return {
+ slot: {
+ id: slot.id,
+ sheetId: slot.sheetId,
+ eventTypeId: slot.eventTypeId,
+ startTime: slot.startTime,
+ createdAt: slot.createdAt,
+ updatedAt: slot.updatedAt,
+ },
+ };
+ }),
+
+ delete: sheetSpaceProcedure
+ .input(z.object({ id: z.string() }))
+ .mutation(async ({ ctx, input }) => {
+ const slot = await prisma.sheetSlot.findFirst({
+ where: {
+ id: input.id,
+ sheet: { spaceId: ctx.space.id, deleted: false },
+ },
+ select: {
+ id: true,
+ scheduledEvent: { select: { id: true } },
+ },
+ });
+ if (!slot) {
+ throw new TRPCError({ code: "NOT_FOUND", message: "Slot not found" });
+ }
+ if (slot.scheduledEvent) {
+ throw new TRPCError({
+ code: "CONFLICT",
+ message:
+ "Cannot delete a slot with bookings. Cancel the booking first.",
+ });
+ }
+ await prisma.sheetSlot.delete({ where: { id: input.id } });
+ return { success: true };
+ }),
+});
+
+const bookings = router({
+ create: sheetPublicProcedure
+ .input(createBookingInputSchema)
+ .mutation(async ({ input }) => {
+ return await prisma.$transaction(async (tx) => {
+ // Lock the slot row to serialize concurrent bookings on the same slot
+ await tx.$queryRaw`SELECT id FROM "sheet_slots" WHERE id = ${input.slotId} FOR UPDATE`;
+
+ const slot = await tx.sheetSlot.findFirst({
+ where: {
+ id: input.slotId,
+ sheet: { deleted: false },
+ },
+ include: {
+ sheet: { select: { spaceId: true } },
+ eventType: {
+ select: {
+ id: true,
+ name: true,
+ description: true,
+ location: true,
+ duration: true,
+ capacity: true,
+ hostId: true,
+ },
+ },
+ scheduledEvent: {
+ select: {
+ id: true,
+ capacity: true,
+ _count: { select: { invites: true } },
+ },
+ },
+ },
+ });
+
+ if (!slot) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Slot not found",
+ });
+ }
+
+ const capacity =
+ slot.scheduledEvent?.capacity ?? slot.eventType.capacity;
+ const currentCount = slot.scheduledEvent?._count.invites ?? 0;
+
+ if (capacity !== null && currentCount >= capacity) {
+ throw new TRPCError({
+ code: "CONFLICT",
+ message: "This slot is full",
+ });
+ }
+
+ if (slot.scheduledEvent) {
+ const existing = await tx.scheduledEventInvite.findFirst({
+ where: {
+ scheduledEventId: slot.scheduledEvent.id,
+ inviteeEmail: input.email,
+ },
+ select: { id: true },
+ });
+ if (existing) {
+ throw new TRPCError({
+ code: "CONFLICT",
+ message: "You've already booked this slot",
+ });
+ }
+ }
+
+ const inviteUid = nanoid();
+ const endTime = new Date(
+ slot.startTime.getTime() + slot.eventType.duration * 60_000,
+ );
+
+ if (slot.scheduledEvent) {
+ await tx.scheduledEventInvite.create({
+ data: {
+ uid: inviteUid,
+ scheduledEventId: slot.scheduledEvent.id,
+ inviteeName: input.name,
+ inviteeEmail: input.email,
+ inviteeTimeZone: input.timeZone,
+ status: "accepted",
+ },
+ });
+ return {
+ scheduledEventId: slot.scheduledEvent.id,
+ attendeeUid: inviteUid,
+ };
+ }
+
+ const host = await tx.user.findUnique({
+ where: { id: slot.eventType.hostId },
+ select: { timeZone: true },
+ });
+
+ const eventId = nanoid();
+ const created = await tx.scheduledEvent.create({
+ data: {
+ id: eventId,
+ uid: `${eventId}@rallly.co`,
+ sheetSlotId: slot.id,
+ eventTypeId: slot.eventType.id,
+ userId: slot.eventType.hostId,
+ spaceId: slot.sheet.spaceId,
+ title: slot.eventType.name,
+ description: slot.eventType.description,
+ location: locationSnapshot(slot.eventType.location),
+ capacity: slot.eventType.capacity,
+ start: slot.startTime,
+ end: endTime,
+ timeZone: host?.timeZone ?? null,
+ status: "confirmed",
+ invites: {
+ create: {
+ uid: inviteUid,
+ inviteeName: input.name,
+ inviteeEmail: input.email,
+ inviteeTimeZone: input.timeZone,
+ status: "accepted",
+ },
+ },
+ },
+ });
+
+ return {
+ scheduledEventId: created.id,
+ attendeeUid: inviteUid,
+ };
+ });
+ }),
+});
+
+export const sheets = mergeRouters(
+ sheetsRoot,
+ router({
+ slots,
+ bookings,
+ }),
+);
diff --git a/packages/database/prisma/migrations/20260519111112_invitee_email_citext/migration.sql b/packages/database/prisma/migrations/20260519111112_invitee_email_citext/migration.sql
new file mode 100644
index 000000000..64660b573
--- /dev/null
+++ b/packages/database/prisma/migrations/20260519111112_invitee_email_citext/migration.sql
@@ -0,0 +1,2 @@
+-- AlterTable
+ALTER TABLE "scheduled_event_invites" ALTER COLUMN "invitee_email" SET DATA TYPE CITEXT;
diff --git a/packages/database/prisma/models/event.prisma b/packages/database/prisma/models/event.prisma
index 936009e51..3904b20a7 100644
--- a/packages/database/prisma/models/event.prisma
+++ b/packages/database/prisma/models/event.prisma
@@ -69,7 +69,7 @@ model ScheduledEventInvite {
uid String @unique
scheduledEventId String @map("scheduled_event_id")
inviteeName String @map("invitee_name")
- inviteeEmail String @map("invitee_email")
+ inviteeEmail String @map("invitee_email") @db.Citext
inviteeId String? @map("invitee_id")
inviteeTimeZone String? @map("invitee_time_zone")
inviteeLocale String? @map("invitee_locale")