Skip to content

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

✨ Add eventTypes tRPC router + feature flag (#2380)

Luke Vellacf77bc1bc771
diff --git a/apps/web/src/features/event-types/constants.ts b/apps/web/src/features/event-types/constants.ts
new file mode 100644
index 000000000..a000ab7e3
--- /dev/null
+++ b/apps/web/src/features/event-types/constants.ts
@@ -0,0 +1 @@
+export const isEventTypesEnabled = process.env.NODE_ENV === "development";
diff --git a/apps/web/src/features/event-types/data.ts b/apps/web/src/features/event-types/data.ts
new file mode 100644
index 000000000..d06575500
--- /dev/null
+++ b/apps/web/src/features/event-types/data.ts
@@ -0,0 +1,46 @@
+import type { EventType } from "@rallly/database";
+import { prisma } from "@rallly/database";
+import { createLogger } from "@rallly/logger";
+import type { EventTypeDTO } from "@/features/event-types/types";
+import type { Location } from "@/lib/location";
+import { locationSchema } from "@/lib/location";
+
+const logger = createLogger("event-types/data");
+
+export function createEventTypeDTO(eventType: EventType): EventTypeDTO {
+ let location: Location | null = null;
+ if (eventType.location !== null) {
+ try {
+ location = locationSchema.parse(eventType.location);
+ } catch (error) {
+ logger.warn(
+ { error, eventTypeId: eventType.id, value: eventType.location },
+ "Failed to parse event type location",
+ );
+ }
+ }
+ return {
+ id: eventType.id,
+ name: eventType.name,
+ duration: eventType.duration,
+ capacity: eventType.capacity,
+ description: eventType.description,
+ location,
+ hostId: eventType.hostId,
+ spaceId: eventType.spaceId,
+ createdAt: eventType.createdAt,
+ updatedAt: eventType.updatedAt,
+ };
+}
+
+export async function getEventTypes(spaceId: string): Promise<EventTypeDTO[]> {
+ const rows = await prisma.eventType.findMany({
+ where: {
+ spaceId,
+ deleted: false,
+ },
+ orderBy: { updatedAt: "desc" },
+ });
+
+ return rows.map(createEventTypeDTO);
+}
diff --git a/apps/web/src/features/event-types/hooks.ts b/apps/web/src/features/event-types/hooks.ts
new file mode 100644
index 000000000..a12ad4c90
--- /dev/null
+++ b/apps/web/src/features/event-types/hooks.ts
@@ -0,0 +1,7 @@
+"use client";
+
+import { useFeatureFlag } from "@/lib/feature-flags/client";
+
+export function useEventTypesEnabled() {
+ return useFeatureFlag("eventTypes");
+}
diff --git a/apps/web/src/features/event-types/schema.ts b/apps/web/src/features/event-types/schema.ts
new file mode 100644
index 000000000..462c19600
--- /dev/null
+++ b/apps/web/src/features/event-types/schema.ts
@@ -0,0 +1,18 @@
+import { z } from "zod";
+import { locationSchema } from "@/lib/location";
+
+export const eventTypeInputSchema = z.object({
+ name: z.string().min(1).max(255),
+ duration: z.number().int().positive(),
+ capacity: z.number().int().positive().nullable().optional(),
+ description: z.string().max(2000).optional(),
+ location: locationSchema.optional(),
+});
+
+export type EventTypeInput = z.infer<typeof eventTypeInputSchema>;
+
+export const updateEventTypeInputSchema = eventTypeInputSchema.extend({
+ id: z.string(),
+});
+
+export type UpdateEventTypeInput = z.infer<typeof updateEventTypeInputSchema>;
diff --git a/apps/web/src/features/event-types/types.ts b/apps/web/src/features/event-types/types.ts
new file mode 100644
index 000000000..8d4fd4da5
--- /dev/null
+++ b/apps/web/src/features/event-types/types.ts
@@ -0,0 +1,14 @@
+import type { Location } from "@/lib/location";
+
+export type EventTypeDTO = {
+ id: string;
+ name: string;
+ duration: number;
+ capacity: number | null;
+ description: string | null;
+ location: Location | null;
+ hostId: string;
+ spaceId: string;
+ createdAt: Date;
+ updatedAt: Date;
+};
diff --git a/apps/web/src/lib/feature-flags/config.ts b/apps/web/src/lib/feature-flags/config.ts
index 89b60f32d..a4f9fd3fb 100644
--- a/apps/web/src/lib/feature-flags/config.ts
+++ b/apps/web/src/lib/feature-flags/config.ts
@@ -2,6 +2,7 @@ import "server-only";
import { env } from "@/env";
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 type { FeatureFlagConfig } from "@/lib/feature-flags/types";
import { isStorageEnabled } from "@/lib/storage";
@@ -16,4 +17,5 @@ export const featureFlagConfig: FeatureFlagConfig = {
emailLogin: isEmailLoginEnabled,
registration: isEmailLoginEnabled && isRegistrationEnabled,
calendars: isCalendarsEnabled,
+ eventTypes: isEventTypesEnabled,
};
diff --git a/apps/web/src/lib/feature-flags/types.ts b/apps/web/src/lib/feature-flags/types.ts
index c3ce3f8b5..d939c7a19 100644
--- a/apps/web/src/lib/feature-flags/types.ts
+++ b/apps/web/src/lib/feature-flags/types.ts
@@ -5,6 +5,7 @@ export interface FeatureFlagConfig {
emailLogin: boolean;
registration: boolean;
calendars: boolean;
+ eventTypes: boolean;
}
export type Feature = keyof FeatureFlagConfig;
diff --git a/apps/web/src/trpc/routers/event-types.ts b/apps/web/src/trpc/routers/event-types.ts
new file mode 100644
index 000000000..77805a289
--- /dev/null
+++ b/apps/web/src/trpc/routers/event-types.ts
@@ -0,0 +1,92 @@
+import { prisma } from "@rallly/database";
+import { TRPCError } from "@trpc/server";
+import { z } from "zod";
+import { isEventTypesEnabled } from "@/features/event-types/constants";
+import { createEventTypeDTO, getEventTypes } from "@/features/event-types/data";
+import {
+ eventTypeInputSchema,
+ updateEventTypeInputSchema,
+} from "@/features/event-types/schema";
+import { middleware, router, spaceProcedure } from "../trpc";
+
+const requireEventTypesEnabled = middleware(async ({ next }) => {
+ if (!isEventTypesEnabled) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Event types are not enabled",
+ });
+ }
+ return next();
+});
+
+const eventTypeProcedure = spaceProcedure.use(requireEventTypesEnabled);
+
+export const eventTypes = router({
+ list: eventTypeProcedure.query(async ({ ctx }) => {
+ const eventTypes = await getEventTypes(ctx.space.id);
+ return { eventTypes };
+ }),
+
+ create: eventTypeProcedure
+ .input(eventTypeInputSchema)
+ .mutation(async ({ ctx, input }) => {
+ const created = await prisma.eventType.create({
+ data: {
+ spaceId: ctx.space.id,
+ hostId: ctx.user.id,
+ name: input.name,
+ duration: input.duration,
+ capacity: input.capacity,
+ description: input.description,
+ location: input.location,
+ },
+ });
+ return createEventTypeDTO(created);
+ }),
+
+ update: eventTypeProcedure
+ .input(updateEventTypeInputSchema)
+ .mutation(async ({ ctx, input }) => {
+ const updated = await prisma.eventType.update({
+ where: {
+ id: input.id,
+ spaceId: ctx.space.id,
+ deleted: false,
+ },
+ data: {
+ name: input.name,
+ duration: input.duration,
+ capacity: input.capacity,
+ description: input.description,
+ location: input.location,
+ },
+ });
+
+ return createEventTypeDTO(updated);
+ }),
+
+ softDelete: eventTypeProcedure
+ .input(z.object({ id: z.string() }))
+ .mutation(async ({ ctx, input }) => {
+ const deleted = await prisma.eventType.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: "Event type not found",
+ });
+ }
+
+ return { success: true };
+ }),
+});
diff --git a/apps/web/src/trpc/routers/index.ts b/apps/web/src/trpc/routers/index.ts
index a150b69ca..cffc0f31a 100644
--- a/apps/web/src/trpc/routers/index.ts
+++ b/apps/web/src/trpc/routers/index.ts
@@ -6,6 +6,7 @@ import { auth } from "./auth";
import { billing } from "./billing";
import { calendars } from "./calendars";
import { dashboard } from "./dashboard";
+import { eventTypes } from "./event-types";
import { events } from "./events";
import { polls } from "./polls";
import { spaces } from "./spaces";
@@ -18,6 +19,7 @@ export const appRouter = mergeRouters(
auth,
billing,
dashboard,
+ eventTypes,
events,
polls,
spaces,