Skip to content

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

✨ Allow create polls on behalf of all space members (#2115)

Luke Vellae531c8fc379d
diff --git a/apps/web/src/app/api/private/[...route]/route.test.ts b/apps/web/src/app/api/private/[...route]/route.test.ts
index 3fec06940..49a4422f9 100644
--- a/apps/web/src/app/api/private/[...route]/route.test.ts
+++ b/apps/web/src/app/api/private/[...route]/route.test.ts
@@ -70,7 +70,6 @@ describe("Private API - /polls", () => {
vi.mocked(prisma.spaceApiKey.update).mockResolvedValue({} as never);
vi.mocked(prisma.user.findUnique).mockResolvedValue({
id: "test-user-id",
- timeZone: "Europe/London",
} as never);
vi.mocked(prisma.spaceMember.findFirst).mockResolvedValue(null);
vi.mocked(prisma.poll.create).mockResolvedValue({
@@ -302,7 +301,7 @@ describe("Private API - /polls", () => {
);
});
- it("should use user timezone when not provided in request", async () => {
+ it("should create poll without timezone when not provided in request", async () => {
const res = await app.request("/api/private/polls", {
method: "POST",
headers: {
@@ -322,37 +321,12 @@ describe("Private API - /polls", () => {
expect(prisma.poll.create).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
- timeZone: "Europe/London",
+ timeZone: undefined,
}),
}),
);
});
- it("should return error when timezone is missing and user has no timezone", async () => {
- vi.mocked(prisma.user.findUnique).mockResolvedValue({
- timeZone: null,
- } as never);
-
- const res = await app.request("/api/private/polls", {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- Authorization: `Bearer ${testApiKey}`,
- },
- body: JSON.stringify({
- title: "Team sync",
- slots: {
- duration: 30,
- times: ["2025-01-15T09:00:00Z"],
- },
- }),
- });
-
- expect(res.status).toBe(400);
- const json = await res.json();
- expect(json.error.code).toBe("TIMEZONE_REQUIRED");
- });
-
it("should return error for invalid timezone", async () => {
const res = await app.request("/api/private/polls", {
method: "POST",
diff --git a/apps/web/src/app/api/private/[...route]/route.ts b/apps/web/src/app/api/private/[...route]/route.ts
index 9fd019172..9524f5486 100644
--- a/apps/web/src/app/api/private/[...route]/route.ts
+++ b/apps/web/src/app/api/private/[...route]/route.ts
@@ -112,13 +112,35 @@ app.post(
const input = c.req.valid("json");
const { spaceId, spaceOwnerId } = c.get("apiAuth");
- const user = await prisma.user.findUnique({
- where: { id: spaceOwnerId },
- select: { id: true, timeZone: true },
- });
+ // Determine the organizer userId
+ let organizerUserId = spaceOwnerId;
- if (!user) {
- return c.json(apiError("UNAUTHORIZED", "User not found"), 401);
+ if (input.organizer) {
+ const spaceMember = await prisma.spaceMember.findFirst({
+ where: {
+ spaceId,
+ user: {
+ email: input.organizer.email,
+ },
+ },
+ include: {
+ user: {
+ select: { id: true, email: true },
+ },
+ },
+ });
+
+ if (!spaceMember) {
+ return c.json(
+ apiError(
+ "ORGANIZER_NOT_MEMBER",
+ "The specified organizer is not a member of this space.",
+ ),
+ 400,
+ );
+ }
+
+ organizerUserId = spaceMember.user.id;
}
// Process dates (all-day options)
@@ -151,7 +173,7 @@ app.post(
}));
const poll = await createPoll({
- userId: user.id,
+ userId: organizerUserId,
title: input.title,
description: input.description,
location: input.location,
@@ -161,7 +183,6 @@ app.post(
disableComments: input.disableComments,
options,
spaceId,
- spaceOwnerId,
});
return c.json({
@@ -182,19 +203,9 @@ app.post(
}
const slots = input.slots;
- const timeZone = slots.timezone ?? user?.timeZone;
-
- if (!timeZone) {
- return c.json(
- apiError(
- "TIMEZONE_REQUIRED",
- "Timezone is required. Either provide a timezone in slots or set one in your profile.",
- ),
- 400,
- );
- }
+ const timeZone = slots.timezone;
- if (!isSupportedTimeZone(timeZone)) {
+ if (timeZone && !isSupportedTimeZone(timeZone)) {
return c.json(
apiError(
"INVALID_TIMEZONE",
@@ -245,7 +256,7 @@ app.post(
}
const poll = await createPoll({
- userId: spaceOwnerId,
+ userId: organizerUserId,
title: input.title,
description: input.description,
location: input.location,
@@ -256,7 +267,6 @@ app.post(
disableComments: input.disableComments,
options,
spaceId,
- spaceOwnerId,
});
return c.json({
diff --git a/apps/web/src/app/api/private/schemas.ts b/apps/web/src/app/api/private/schemas.ts
index 9142c9ceb..be265d61e 100644
--- a/apps/web/src/app/api/private/schemas.ts
+++ b/apps/web/src/app/api/private/schemas.ts
@@ -47,7 +47,7 @@ const slotsInputSchema = z
.optional()
.openapi({
description:
- "IANA timezone. Defaults to user's timezone if not provided.",
+ "IANA timezone. If not provided, times will be stored as UTC and no timezone will be set on the poll (indicating floating times).",
example: "Europe/London",
}),
times: z
@@ -103,6 +103,18 @@ export const createPollInputSchema = z
"ID of the space to create the poll in. Defaults to user's most recently used space.",
example: "space_abc123",
}),
+ organizer: z
+ .object({
+ email: z.email().openapi({
+ description: "Email address of the organizer",
+ example: "[email protected]",
+ }),
+ })
+ .optional()
+ .openapi({
+ description:
+ "Organizer of the poll. Defaults to the space owner if not provided. The organizer must be a member of the space.",
+ }),
dates: datesInputSchema.optional(),
slots: slotsInputSchema.optional(),
})
diff --git a/apps/web/src/app/api/private/utils/poll.ts b/apps/web/src/app/api/private/utils/poll.ts
index 7a174647a..351c626a8 100644
--- a/apps/web/src/app/api/private/utils/poll.ts
+++ b/apps/web/src/app/api/private/utils/poll.ts
@@ -18,8 +18,7 @@ export type CreatePollParams = {
hideScores?: boolean;
disableComments?: boolean;
options: PollOption[];
- spaceId?: string;
- spaceOwnerId?: string;
+ spaceId: string;
};
export const createPoll = async ({
@@ -34,21 +33,7 @@ export const createPoll = async ({
disableComments,
options,
spaceId,
- spaceOwnerId,
}: CreatePollParams) => {
- // If spaceId and spaceOwnerId are provided, use them directly
- // Otherwise, fall back to looking up the user's most recent space
- const finalUserId = spaceOwnerId ?? userId;
- const finalSpaceId =
- spaceId ??
- (
- await prisma.spaceMember.findFirst({
- where: { userId },
- orderBy: { lastSelectedAt: "desc" },
- select: { spaceId: true },
- })
- )?.spaceId;
-
const poll = await prisma.poll.create({
data: {
id: nanoid(),
@@ -62,10 +47,10 @@ export const createPoll = async ({
disableComments,
adminUrlId: nanoid(),
participantUrlId: nanoid(),
- userId: finalUserId,
- watchers: { create: { userId: finalUserId } },
+ userId,
+ watchers: { create: { userId } },
+ spaceId,
options: { createMany: { data: options } },
- spaceId: finalSpaceId,
},
select: { id: true },
});
diff --git a/apps/web/src/app/api/private/utils/time-slots.test.ts b/apps/web/src/app/api/private/utils/time-slots.test.ts
index 254c0e4f3..731a54b5a 100644
--- a/apps/web/src/app/api/private/utils/time-slots.test.ts
+++ b/apps/web/src/app/api/private/utils/time-slots.test.ts
@@ -48,6 +48,20 @@ describe("time-slots utilities", () => {
expect(result.startTime).toEqual(new Date("2025-01-15T17:00:00Z"));
});
+
+ it("should parse datetime without offset as UTC when timezone is undefined", () => {
+ const result = parseStartTime("2025-01-15T09:00:00", undefined, 30);
+
+ expect(result.startTime).toEqual(new Date("2025-01-15T09:00:00Z"));
+ expect(result.duration).toBe(30);
+ });
+
+ it("should parse ISO datetime with timezone offset as-is when timezone is undefined", () => {
+ const result = parseStartTime("2025-01-15T09:00:00Z", undefined, 30);
+
+ expect(result.startTime).toEqual(new Date("2025-01-15T09:00:00Z"));
+ expect(result.duration).toBe(30);
+ });
});
describe("dedupeTimeSlots", () => {
@@ -221,5 +235,24 @@ describe("time-slots utilities", () => {
const uniqueTimes = new Set(result.map((s) => s.startTime.toISOString()));
expect(result.length).toBe(uniqueTimes.size);
});
+
+ it("should generate slots as UTC when timezone is undefined", () => {
+ const result = generateTimeSlots(
+ {
+ startDate: "2025-01-15",
+ endDate: "2025-01-15",
+ daysOfWeek: ["wed"],
+ fromTime: "09:00",
+ toTime: "10:00",
+ },
+ undefined,
+ 30,
+ );
+
+ expect(result).toHaveLength(2);
+ expect(result[0].startTime).toEqual(new Date("2025-01-15T09:00:00Z"));
+ expect(result[1].startTime).toEqual(new Date("2025-01-15T09:30:00Z"));
+ expect(result[0].duration).toBe(30);
+ });
});
});
diff --git a/apps/web/src/app/api/private/utils/time-slots.ts b/apps/web/src/app/api/private/utils/time-slots.ts
index d36628511..be8d3b8a2 100644
--- a/apps/web/src/app/api/private/utils/time-slots.ts
+++ b/apps/web/src/app/api/private/utils/time-slots.ts
@@ -32,18 +32,25 @@ export const dedupeTimeSlots = (slots: Array<TimeSlot>) => {
const hasTzOffset = (value: string) =>
/[zZ]$|[+-]\d{2}:\d{2}$|[+-]\d{4}$/.test(value);
-const parseDateTimeInTimeZone = (value: string, timeZone: string) =>
- hasTzOffset(value) ? dayjs(value) : dayjs(value).tz(timeZone, true);
+const parseDateTimeInTimeZone = (value: string, timeZone?: string) =>
+ hasTzOffset(value)
+ ? dayjs(value)
+ : timeZone
+ ? dayjs(value).tz(timeZone, true)
+ : dayjs(value).utc(true);
const parseLocalDateTimeInTimeZone = (
date: string,
time: string,
- timeZone: string,
-) => dayjs(`${date}T${time}`).tz(timeZone, true);
+ timeZone: string | undefined,
+) =>
+ timeZone
+ ? dayjs(`${date}T${time}`).tz(timeZone, true)
+ : dayjs(`${date}T${time}`).utc(true);
export const parseStartTime = (
startTime: string,
- timeZone: string,
+ timeZone: string | undefined,
duration: number,
): TimeSlot => ({
startTime: parseDateTimeInTimeZone(startTime, timeZone).toDate(),
@@ -52,7 +59,7 @@ export const parseStartTime = (
export const generateTimeSlots = (
generator: SlotGeneratorInput,
- timeZone: string,
+ timeZone: string | undefined,
durationMinutes: number,
): Array<TimeSlot> => {
const dayMap: Record<SlotGeneratorInput["daysOfWeek"][number], number> = {
@@ -67,8 +74,12 @@ export const generateTimeSlots = (
const allowed = new Set(generator.daysOfWeek.map((d) => dayMap[d]));
- const startDay = dayjs(generator.startDate).tz(timeZone, true).startOf("day");
- const endDay = dayjs(generator.endDate).tz(timeZone, true).startOf("day");
+ const startDay = timeZone
+ ? dayjs(generator.startDate).tz(timeZone, true).startOf("day")
+ : dayjs(generator.startDate).utc(true).startOf("day");
+ const endDay = timeZone
+ ? dayjs(generator.endDate).tz(timeZone, true).startOf("day")
+ : dayjs(generator.endDate).utc(true).startOf("day");
const from = parseLocalDateTimeInTimeZone(
generator.startDate,