1- import { auth } from "@/app/(auth)/auth" ;
2- import { getQuerier } from "@/lib/database" ;
1+ import Client from "@blink.so/api" ;
32import * as convert from "@blink.so/database/convert" ;
43import { notFound , redirect } from "next/navigation" ;
54import { cache } from "react" ;
5+ import { auth , getSessionToken } from "@/app/(auth)/auth" ;
6+ import { getQuerier } from "@/lib/database" ;
67
78export default async function OrganizationLayout ( {
89 children,
@@ -29,7 +30,7 @@ export const getOrganization = cache(
2930 return notFound ( ) ;
3031 }
3132 const baseURL = new URL (
32- process . env . NEXT_PUBLIC_BASE_URL ! ?? "http://localhost:3000 "
33+ process . env . NEXT_PUBLIC_BASE_URL ! ?? "http://localhost:3005 "
3334 ) ;
3435 return convert . organization ( baseURL , organization ) ;
3536 }
@@ -46,7 +47,7 @@ export const getOrganizationByID = cache(
4647 return notFound ( ) ;
4748 }
4849 const baseURL = new URL (
49- process . env . NEXT_PUBLIC_BASE_URL ! ?? "http://localhost:3000 "
50+ process . env . NEXT_PUBLIC_BASE_URL ! ?? "http://localhost:3005 "
5051 ) ;
5152 return convert . organization ( baseURL , organization ) ;
5253 }
@@ -74,51 +75,26 @@ export const getAgent = cache(
7475export const getAgentOrNull = cache (
7576 async ( organizationName : string , agentName : string ) => {
7677 const session = await auth ( ) ;
78+ const token = await getSessionToken ( ) ;
79+ const baseURL = process . env . NEXT_PUBLIC_BASE_URL ?? "http://localhost:3005" ;
80+ const client = new Client ( {
81+ baseURL,
82+ authToken : token ,
83+ } ) ;
7784 const userID = session ?. user ?. id ;
7885 const db = await getQuerier ( ) ;
79- const agent = await db . selectAgentByNameForUser ( {
86+ const agentFromDB = await db . selectAgentByNameForUser ( {
8087 organizationName,
8188 agentName,
8289 userID,
8390 } ) ;
84- if ( ! agent ) {
91+ if ( ! agentFromDB ) {
8592 return null ;
8693 }
87- // Get the production deployment target's request_id
88- const productionTarget = await db . selectAgentDeploymentTargetByName (
89- agent . id ,
90- "production"
91- ) ;
92- const requestURL = productionTarget ?. request_id
93- ? new URL ( `https://${ productionTarget . request_id } .blink.host/` )
94- : undefined ;
95-
96- // Get user permission for the agent
97- let userPermission : "read" | "write" | "admin" | undefined ;
98- if ( userID ) {
99- const org = await db . selectOrganizationForUser ( {
100- organizationID : agent . organization_id ,
101- userID,
102- } ) ;
103- // Org owners and admins get admin permission
104- if (
105- org ?. membership &&
106- ( org . membership . role === "owner" || org . membership . role === "admin" )
107- ) {
108- userPermission = "admin" ;
109- } else {
110- userPermission = await db . getAgentPermissionForUser ( {
111- agentId : agent . id ,
112- userId : userID ,
113- orgRole : org ?. membership ?. role ,
114- } ) ;
115- // If permission is undefined, user doesn't have access
116- if ( userPermission === undefined ) {
117- return null ;
118- }
119- }
94+ const agent = await client . agents . get ( agentFromDB . id ) ;
95+ if ( ! agent ) {
96+ return null ;
12097 }
121-
122- return convert . agent ( agent , requestURL , userPermission ) ;
98+ return agent ;
12399 }
124100) ;
0 commit comments