From baafe71f65aa93c72b1fac99bc36047d8150f32d Mon Sep 17 00:00:00 2001 From: Lewis Carhart Date: Sat, 5 Apr 2025 22:55:14 -0400 Subject: [PATCH] fix: organization setup redirect if it didn't create correctly --- .../getAllFrameworkInstancesWithControls.ts | 45 +++++---- .../getSingleFrameworkInstanceWithControls.ts | 51 +++++----- .../(dashboard)/[orgId]/frameworks/page.tsx | 42 ++++---- .../(app)/(dashboard)/[orgId]/layout.tsx | 98 +++++++++++-------- .../app/src/app/[locale]/(app)/setup/page.tsx | 82 ++++++++-------- 5 files changed, 169 insertions(+), 149 deletions(-) diff --git a/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/frameworks/data/getAllFrameworkInstancesWithControls.ts b/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/frameworks/data/getAllFrameworkInstancesWithControls.ts index 6e416b7f6a..7b3e3a9952 100644 --- a/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/frameworks/data/getAllFrameworkInstancesWithControls.ts +++ b/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/frameworks/data/getAllFrameworkInstancesWithControls.ts @@ -1,31 +1,34 @@ "use server"; +import { cache } from "react"; import { db } from "@comp/db"; import type { FrameworkInstanceWithControls } from "../types"; -export async function getAllFrameworkInstancesWithControls({ - organizationId, -}: { - organizationId: string; -}): Promise { - const frameworksWithControls = await db.frameworkInstance.findMany({ - where: { - organizationId, - }, - include: { - controls: { - include: { - artifacts: { - include: { - policy: true, - evidence: true, +export const getAllFrameworkInstancesWithControls = cache( + async ({ + organizationId, + }: { + organizationId: string; + }): Promise => { + const frameworksWithControls = await db.frameworkInstance.findMany({ + where: { + organizationId, + }, + include: { + controls: { + include: { + artifacts: { + include: { + policy: true, + evidence: true, + }, }, + requirementsMapped: true, }, - requirementsMapped: true, }, }, - }, - }); + }); - return frameworksWithControls; -} + return frameworksWithControls; + }, +); diff --git a/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/frameworks/data/getSingleFrameworkInstanceWithControls.ts b/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/frameworks/data/getSingleFrameworkInstanceWithControls.ts index e1de65ecc6..7cff9c4bdb 100644 --- a/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/frameworks/data/getSingleFrameworkInstanceWithControls.ts +++ b/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/frameworks/data/getSingleFrameworkInstanceWithControls.ts @@ -1,34 +1,37 @@ "use server"; +import { cache } from "react"; import { db } from "@comp/db"; import type { FrameworkInstanceWithControls } from "../types"; -export async function getSingleFrameworkInstanceWithControls({ - organizationId, - frameworkInstanceId, -}: { - organizationId: string; - frameworkInstanceId: string; -}): Promise { - const frameworkInstance = await db.frameworkInstance.findUnique({ - where: { - organizationId, - id: frameworkInstanceId, - }, - include: { - controls: { - include: { - artifacts: { - include: { - policy: true, - evidence: true, +export const getSingleFrameworkInstanceWithControls = cache( + async ({ + organizationId, + frameworkInstanceId, + }: { + organizationId: string; + frameworkInstanceId: string; + }): Promise => { + const frameworkInstance = await db.frameworkInstance.findUnique({ + where: { + organizationId, + id: frameworkInstanceId, + }, + include: { + controls: { + include: { + artifacts: { + include: { + policy: true, + evidence: true, + }, }, + requirementsMapped: true, }, - requirementsMapped: true, }, }, - }, - }); + }); - return frameworkInstance; -} + return frameworkInstance; + }, +); diff --git a/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/frameworks/page.tsx b/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/frameworks/page.tsx index 6b0427b12f..98f63676ee 100644 --- a/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/frameworks/page.tsx +++ b/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/frameworks/page.tsx @@ -2,37 +2,37 @@ import { getI18n } from "@/locales/server"; import { auth } from "@/utils/auth"; import { headers } from "next/headers"; import { redirect } from "next/navigation"; -import PageWithBreadcrumb from "../../../../../../components/pages/PageWithBreadcrumb"; +import PageWithBreadcrumb from "@/components/pages/PageWithBreadcrumb"; import { FrameworksOverview } from "./components/FrameworksOverview"; import { getAllFrameworkInstancesWithControls } from "./data/getAllFrameworkInstancesWithControls"; export async function generateMetadata() { - const t = await getI18n(); + const t = await getI18n(); - return { - title: t("sidebar.frameworks"), - }; + return { + title: t("sidebar.frameworks"), + }; } export default async function DashboardPage() { - const session = await auth.api.getSession({ - headers: await headers(), - }); + const session = await auth.api.getSession({ + headers: await headers(), + }); - const organizationId = session?.session.activeOrganizationId; + const organizationId = session?.session.activeOrganizationId; - if (!organizationId) { - console.log("Redirect on page.tsx"); - redirect("/"); - } + if (!organizationId) { + console.log("Redirect on page.tsx"); + redirect("/"); + } - const frameworksWithControls = await getAllFrameworkInstancesWithControls({ - organizationId, - }); + const frameworksWithControls = await getAllFrameworkInstancesWithControls({ + organizationId, + }); - return ( - - - - ); + return ( + + + + ); } diff --git a/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/layout.tsx b/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/layout.tsx index 02cc353aa8..b3df82c599 100644 --- a/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/layout.tsx +++ b/apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/layout.tsx @@ -4,54 +4,72 @@ import { AssistantSheet } from "@/components/sheets/assistant-sheet"; import { Sidebar } from "@/components/sidebar"; import { SidebarProvider } from "@/context/sidebar-context"; import { auth } from "@/utils/auth"; +import { db } from "@comp/db"; import dynamic from "next/dynamic"; import { cookies, headers } from "next/headers"; import { redirect } from "next/navigation"; +import { cache } from "react"; const HotKeys = dynamic( - () => import("@/components/hot-keys").then((mod) => mod.HotKeys), - { - ssr: true, - }, + () => import("@/components/hot-keys").then((mod) => mod.HotKeys), + { + ssr: true, + }, ); export default async function Layout({ - children, - params, + children, + params, }: { - children: React.ReactNode; - params: Promise<{ orgId: string }>; + children: React.ReactNode; + params: Promise<{ orgId: string }>; }) { - const session = await auth.api.getSession({ - headers: await headers(), - }); - - const orgId = (await params).orgId; - const cookieStore = await cookies(); - const isCollapsed = cookieStore.get("sidebar-collapsed")?.value === "true"; - - if (!session) { - redirect("/auth"); - } - - if (!orgId) { - redirect("/"); - } - - if (!session?.session.activeOrganizationId) { - redirect("/"); - } - - return ( - - } isCollapsed={isCollapsed}> -
-
-
{children}
-
- -
- -
- ); + const session = await auth.api.getSession({ + headers: await headers(), + }); + + const orgId = (await params).orgId; + await getOrganization(orgId); + const cookieStore = await cookies(); + const isCollapsed = cookieStore.get("sidebar-collapsed")?.value === "true"; + + if (!session) { + redirect("/auth"); + } + + if (!session?.session.activeOrganizationId) { + redirect("/"); + } + + return ( + + } isCollapsed={isCollapsed}> +
+
+
{children}
+
+ +
+ +
+ ); } + +const getOrganization = cache(async (orgId: string) => { + try { + const organization = await db.organization.findUnique({ + where: { + id: orgId, + }, + }); + + if (!organization) { + redirect("/setup"); + } + + return organization; + } catch (error) { + console.error(error); + redirect("/setup"); + } +}); diff --git a/apps/app/src/app/[locale]/(app)/setup/page.tsx b/apps/app/src/app/[locale]/(app)/setup/page.tsx index 10abdd1d00..7396853b85 100644 --- a/apps/app/src/app/[locale]/(app)/setup/page.tsx +++ b/apps/app/src/app/[locale]/(app)/setup/page.tsx @@ -7,50 +7,46 @@ import { redirect } from "next/navigation"; import { AcceptInvite } from "./components/accept-invite"; export const metadata: Metadata = { - title: "Organization Setup | Comp AI", + title: "Organization Setup | Comp AI", }; export default async function Page() { - const headersList = await headers(); - - const session = await auth.api.getSession({ - headers: headersList, - }); - - if (session?.session?.activeOrganizationId) { - redirect(`/${session.session.activeOrganizationId}/frameworks`); - } - - const organization = await db.organization.findFirst({ - where: { - members: { - some: { - userId: session?.user.id, - }, - }, - }, - }); - - if (!session?.session?.activeOrganizationId && organization?.id) { - await auth.api.setActiveOrganization({ - headers: headersList, - body: { - organizationId: organization.id, - }, - }); - redirect(`/${organization.id}/frameworks`); - } - - const hasInvite = await db.invitation.findFirst({ - where: { - email: session?.user.email, - status: "pending", - }, - }); - - if (hasInvite) { - return ; - } - - return ; + const headersList = await headers(); + + const session = await auth.api.getSession({ + headers: headersList, + }); + + const organization = await db.organization.findFirst({ + where: { + members: { + some: { + userId: session?.user.id, + }, + }, + }, + }); + + if (!session?.session?.activeOrganizationId && organization?.id) { + await auth.api.setActiveOrganization({ + headers: headersList, + body: { + organizationId: organization.id, + }, + }); + redirect(`/${organization.id}/frameworks`); + } + + const hasInvite = await db.invitation.findFirst({ + where: { + email: session?.user.email, + status: "pending", + }, + }); + + if (hasInvite) { + return ; + } + + return ; }