fix: endpoints

This commit is contained in:
charles-gauthereau
2026-05-27 22:02:22 +02:00
parent 56517226b6
commit c1b71e7772
18 changed files with 941 additions and 457 deletions
+34
View File
@@ -0,0 +1,34 @@
import {withApiKey} from "@/lib/api-v1/middleware";
import {ApiKeyContext} from "@/lib/api-v1/types";
import {NextResponse} from "next/server";
import {getAgent, resolveAgentAccess} from "@/lib/api-v1/services/agents";
import {logger} from "@/lib/logger";
import {generateEdgeKey} from "@/utils/edge_key";
import {getServerUrl} from "@/utils/get-server-url";
const log = logger.child({ module: "api/v1/agents/[id]/key" });
export const GET = withApiKey(
async (_req: Request, ctx: ApiKeyContext, params?: Record<string, string>) => {
try {
const id = params?.id;
if (!id) return NextResponse.json({ error: "Not found" }, { status: 404 });
const access = await resolveAgentAccess(id, ctx.user);
if (access === "forbidden") return NextResponse.json({ error: "Forbidden" }, { status: 403 });
if (access === "not_found") return NextResponse.json({ error: "Not found" }, { status: 404 });
const agent = await getAgent(id);
if (!agent) return NextResponse.json({ error: "Agent not found" }, { status: 404 });
const edgeKey = await generateEdgeKey(getServerUrl(), agent.id);
return NextResponse.json({ data: edgeKey });
} catch (error) {
log.error({ error }, "Error in GET /api/v1/agents/[id]");
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
}
}
);
+55 -66
View File
@@ -1,82 +1,71 @@
import { NextResponse } from "next/server";
import { withApiKey, ApiKeyContext } from "@/lib/api-v1/middleware";
import { getAccessibleAgentIds } from "@/lib/api-v1/acl";
import { db } from "@/db";
import {NextResponse} from "next/server";
import {withApiKey} from "@/lib/api-v1/middleware";
import {db} from "@/db";
import * as drizzleDb from "@/db";
import { eq, and, or, isNull } from "drizzle-orm";
import { withUpdatedAt } from "@/db/utils";
import { v4 as uuidv4 } from "uuid";
import { logger } from "@/lib/logger";
import {eq, and, or, isNull} from "drizzle-orm";
import {withUpdatedAt} from "@/db/utils";
import {v4 as uuidv4} from "uuid";
import {logger} from "@/lib/logger";
import {ApiKeyContext} from "@/lib/api-v1/types";
import {getAgent, resolveAgentAccess} from "@/lib/api-v1/services/agents";
import {deleteAgentService} from "@/features/agents/agent-delete.action";
const log = logger.child({ module: "api/v1/agents/[id]" });
async function resolveAgentAccess(id: string, userId: string) {
const accessibleAgentIds = await getAccessibleAgentIds(userId);
if (accessibleAgentIds.includes(id)) return "ok";
const exists = await db.query.agent.findFirst({
where: and(
eq(drizzleDb.schemas.agent.id, id),
or(
eq(drizzleDb.schemas.agent.isArchived, false),
isNull(drizzleDb.schemas.agent.isArchived)
)
),
columns: { id: true },
});
return exists ? "forbidden" : "not_found";
}
const log = logger.child({module: "api/v1/agents/[id]"});
export const GET = withApiKey(
async (_req: Request, ctx: ApiKeyContext, params?: Record<string, string>) => {
try {
const id = params?.id;
if (!id) return NextResponse.json({ error: "Not found" }, { status: 404 });
async (_req: Request, ctx: ApiKeyContext, params?: Record<string, string>) => {
try {
const id = params?.id;
if (!id) return NextResponse.json({error: "Not found"}, {status: 404});
const access = await resolveAgentAccess(id, ctx.userId);
if (access === "forbidden") return NextResponse.json({ error: "Forbidden" }, { status: 403 });
if (access === "not_found") return NextResponse.json({ error: "Not found" }, { status: 404 });
const access = await resolveAgentAccess(id, ctx.user);
const agent = await db.query.agent.findFirst({
where: and(
eq(drizzleDb.schemas.agent.id, id),
or(
eq(drizzleDb.schemas.agent.isArchived, false),
isNull(drizzleDb.schemas.agent.isArchived)
)
),
with: { databases: true },
});
if (access === "forbidden") return NextResponse.json({error: "Forbidden"}, {status: 403});
if (access === "not_found") return NextResponse.json({error: "Not found"}, {status: 404});
if (!agent) return NextResponse.json({ error: "Not found" }, { status: 404 });
return NextResponse.json({ data: agent });
} catch (error) {
log.error({ error }, "Error in GET /api/v1/agents/[id]");
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
const agent = await getAgent(id, {
includeDatabases: true,
includeOrganizations: false,
});
if (!agent) return NextResponse.json({error: "Not found"}, {status: 404});
return NextResponse.json({data: agent});
} catch (error) {
log.error({error}, "Error in GET /api/v1/agents/[id]");
return NextResponse.json({error: "Internal server error"}, {status: 500});
}
}
}
);
export const DELETE = withApiKey(
async (_req: Request, ctx: ApiKeyContext, params?: Record<string, string>) => {
try {
const id = params?.id;
if (!id) return NextResponse.json({ error: "Not found" }, { status: 404 });
async (_req: Request, ctx: ApiKeyContext, params?: Record<string, string>) => {
try {
const id = params?.id;
if (!id) return NextResponse.json({error: "Not found"}, {status: 404});
const access = await resolveAgentAccess(id, ctx.userId);
if (access === "forbidden") return NextResponse.json({ error: "Forbidden" }, { status: 403 });
if (access === "not_found") return NextResponse.json({ error: "Not found" }, { status: 404 });
const access = await resolveAgentAccess(id, ctx.user);
if (access === "forbidden") return NextResponse.json({error: "Forbidden"}, {status: 403});
if (access === "not_found") return NextResponse.json({error: "Not found"}, {status: 404});
await db
.update(drizzleDb.schemas.agent)
.set(withUpdatedAt({ isArchived: true, slug: uuidv4(), deletedAt: new Date() }))
.where(eq(drizzleDb.schemas.agent.id, id));
const agent = await getAgent(id, {
includeDatabases: false,
includeOrganizations: true,
});
return new Response(null, { status: 204 });
} catch (error) {
log.error({ error }, "Error in DELETE /api/v1/agents/[id]");
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
if (!agent) return NextResponse.json({error: "Agent no found"}, {status: 404});
const organizationIds = agent.organizations.map(org => org.organizationId)
await deleteAgentService({
agentId: agent.id,
organizationId: agent.organizationId ?? undefined,
organizationIds: organizationIds
});
return new Response(null, {status: 204});
} catch (error) {
log.error({error}, "Error in DELETE /api/v1/agents/[id]");
return NextResponse.json({error: "Internal server error"}, {status: 500});
}
}
}
);