Files

54 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

import {NextResponse} from "next/server";
2026-04-10 18:37:35 +02:00
import {and, eq} from "drizzle-orm";
import {db} from "@/db";
2026-02-08 20:42:42 +01:00
import * as drizzleDb from "@/db";
import {logger} from "@/lib/logger";
const log = logger.child({module: "api/agent/backup/helpers"});
2026-02-08 20:42:42 +01:00
export function withAgentCheck(handler: Function) {
return async (request: Request, context: { params: Promise<{ agentId: string }> }) => {
try {
const agentId = (await context.params).agentId;
const agent = await db.query.agent.findFirst({
2026-04-10 18:37:35 +02:00
where: and(eq(drizzleDb.schemas.agent.id, agentId), eq(drizzleDb.schemas.agent.isArchived, false)),
2026-02-08 20:42:42 +01:00
});
if (!agent) {
return NextResponse.json(
{error: "Agent not found"},
{status: 404}
2026-02-08 20:42:42 +01:00
);
}
return handler(request, {...context, agent});
2026-02-08 20:42:42 +01:00
} catch (err) {
log.error({error: err, name: "withAgentCheck"}, "Error in agent middleware");
2026-02-08 20:42:42 +01:00
return NextResponse.json(
{error: "Internal server error"},
{status: 500}
2026-02-08 20:42:42 +01:00
);
}
};
}
2026-02-08 20:42:42 +01:00
export async function getDatabaseOrThrow(generatedId: string) {
const database = await db.query.database.findFirst({
where: eq(drizzleDb.schemas.database.agentDatabaseId, generatedId),
with: {
project: true,
alertPolicies: true,
storagePolicies: true
}
});
2026-02-08 20:42:42 +01:00
if (!database) {
throw NextResponse.json(
{error: "Database associated with generatedId not found"},
{status: 404}
2026-02-08 20:42:42 +01:00
);
}
2026-02-08 20:42:42 +01:00
return database;
}