Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Make a poll's space active when its admin page is opened from another space (#3423)
Luke Vella90eca1e107c5
diff --git a/apps/web/src/features/poll/loaders.ts b/apps/web/src/features/poll/loaders.tsindex b0b59039e..2bc951ba3 100644--- a/apps/web/src/features/poll/loaders.ts+++ b/apps/web/src/features/poll/loaders.ts@@ -21,9 +21,12 @@ import {filterCommentsForViewer,maskParticipantsForViewer,} from "@/features/poll/utils";+import { getActiveSpaceForUser } from "@/features/space/data";import { loadActiveSpaceContentScope } from "@/features/space/loaders";+import { setActiveSpace } from "@/features/space/member/mutations";import { getUser } from "@/features/user/data";import { getSession } from "@/lib/auth";+import { getPathname } from "@/lib/pathname";export const loadPollStatusCounts = cache(async () => {const scope = await loadActiveSpaceContentScope();@@ -220,6 +223,27 @@ export const loadAdminPoll = cache(async (pollId: string) => {redirect(`/invite/${pollId}`);}+ // Access is proven against the poll's space, not the active one, so a deep+ // link can land a member of several spaces in a poll outside the space the+ // rest of the app is showing. Make the poll's space active, the same write+ // the space switcher does, then reload: the parent layout's tier and the+ // page's space resolve in parallel with this loader, so the only way every+ // reader sees the switch is a fresh request.+ if (poll.spaceId) {+ const activeSpace = await getActiveSpaceForUser(user.id);++ if (activeSpace?.id !== poll.spaceId) {+ const switched = await setActiveSpace({+ userId: user.id,+ spaceId: poll.spaceId,+ });++ if (switched) {+ redirect((await getPathname()) ?? `/poll/${pollId}`);+ }+ }+ }+const [participants, comments] = await Promise.all([listPollParticipants({ pollId }),listPollComments({ pollId }),diff --git a/apps/web/tests/poll-active-space.spec.ts b/apps/web/tests/poll-active-space.spec.tsnew file mode 100644index 000000000..dd6d99672--- /dev/null+++ b/apps/web/tests/poll-active-space.spec.ts@@ -0,0 +1,91 @@+import { expect, test } from "@playwright/test";+import { prisma } from "@rallly/database";+import dayjs from "dayjs";+import {+ createUserInDb,+ loginWithEmail,+ upgradeSpaceToPro,+} from "./test-utils";++// The active space is the membership selected most recently. A poll admin+// page authorizes against the poll's own space, so a member of two spaces+// can open a poll that belongs to the one that is not active. Opening it+// makes that space active, so the dashboard the viewer returns to is the+// poll's space and not the one they happened to have selected before.++const runId = Date.now().toString(36);+const createdUserIds: string[] = [];++function emailFor(name: string) {+ return `${name.toLowerCase().replace(/\s/g, "-")}-${runId}@example.com`;+}++test.afterAll(async () => {+ if (createdUserIds.length > 0) {+ await prisma.user.deleteMany({ where: { id: { in: createdUserIds } } });+ }+});++test("opening a poll from another space makes that space active", async ({+ page,+}) => {+ const owner = await createUserInDb({+ email: emailFor("Team Owner"),+ name: "Team Owner",+ });+ createdUserIds.push(owner.id);+ const team = await prisma.space.findFirstOrThrow({+ where: { ownerId: owner.id },+ });+ await prisma.space.update({+ where: { id: team.id },+ data: { name: `Marketing ${runId}` },+ });+ await upgradeSpaceToPro({ spaceId: team.id, userId: owner.id, seats: 2 });++ const email = emailFor("Jessie Smith");+ const member = await createUserInDb({ email, name: "Jessie Smith" });+ createdUserIds.push(member.id);++ // The member's own space stays the active one: selected now, the team+ // space long ago.+ await prisma.spaceMember.updateMany({+ where: { userId: member.id },+ data: { lastSelectedAt: new Date() },+ });+ await prisma.spaceMember.create({+ data: {+ spaceId: team.id,+ userId: member.id,+ role: "MEMBER",+ lastSelectedAt: new Date(0),+ },+ });++ const poll = await prisma.poll.create({+ data: {+ id: `active-space-${runId}`,+ title: "Team offsite",+ userId: member.id,+ spaceId: team.id,+ kind: "time",+ options: {+ create: {+ startTime: dayjs().add(1, "day").startOf("hour").toDate(),+ duration: 60,+ },+ },+ },+ });++ await loginWithEmail(page, { email });++ await page.goto(`/poll/${poll.id}`);+ await expect(page).toHaveURL(new RegExp(`/poll/${poll.id}$`));+ await expect(page.getByRole("button", { name: "Manage" })).toBeVisible();++ await page.goto("/polls");+ await expect(+ page.getByRole("button", { name: `Marketing ${runId}` }),+ ).toBeVisible();+});