Files
portabase/app/api/agent/[agentId]/status/route.ts
T

89 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-12-01 11:11:27 +01:00
import {NextResponse} from "next/server";
2024-11-29 12:54:34 +01:00
import {handleDatabases} from "./helpers";
2025-07-16 18:15:28 +02:00
import * as drizzleDb from "@/db";
import {db} from "@/db";
import {EDbmsSchema} from "@/db/schema/types";
import {eq} from "drizzle-orm";
2025-07-17 09:28:57 +02:00
import {isUuidv4} from "@/utils/verify-uuid";
2025-12-21 11:25:33 +01:00
import {withUpdatedAt} from "@/db/utils";
2026-01-31 22:15:01 +01:00
import {eventEmitter} from "@/features/shared/event";
2026-02-12 20:18:26 +01:00
import {notFound} from "next/navigation";
2024-11-29 12:39:39 +01:00
export type databaseAgent = {
name: string,
2025-07-16 18:15:28 +02:00
dbms: EDbmsSchema,
generatedId: string
}
2024-11-29 12:39:39 +01:00
export type Body = {
2025-12-21 11:25:33 +01:00
version: string,
2024-11-29 12:39:39 +01:00
databases: databaseAgent[]
}
export async function POST(
2024-11-11 18:50:42 +01:00
request: Request,
{params}: { params: Promise<{ agentId: string }> }
) {
try {
const agentId = (await params).agentId
2026-02-08 20:42:42 +01:00
console.log(agentId)
const body: Body = await request.json();
2024-12-01 11:11:27 +01:00
const lastContact = new Date();
2025-09-26 09:04:18 +02:00
let message: string
2025-07-17 09:28:57 +02:00
if (!isUuidv4(agentId)) {
2025-09-26 09:04:18 +02:00
message = "agentId is not a valid uuid"
2026-02-08 20:42:42 +01:00
console.error(message)
2025-07-17 09:28:57 +02:00
return NextResponse.json(
{error: "agentId is not a valid uuid"},
{status: 500}
);
}
2025-07-16 18:15:28 +02:00
const agent = await db.query.agent.findFirst({
where: eq(drizzleDb.schemas.agent.id, agentId),
})
2025-07-16 18:15:28 +02:00
if (!agent) {
2025-09-26 09:04:18 +02:00
message = "Agent not found"
return NextResponse.json({error: message}, {status: 404})
}
2026-02-12 20:18:26 +01:00
const [settings] = await db.select().from(drizzleDb.schemas.setting).where(eq(drizzleDb.schemas.setting.name, "system")).limit(1);
if (!settings) {
return NextResponse.json({error: "An error occured"}, {status: 404})
}
const databasesResponse = await handleDatabases(body, agent, lastContact, settings)
2024-12-01 11:11:27 +01:00
2025-07-16 18:15:28 +02:00
await db
.update(drizzleDb.schemas.agent)
2025-12-21 11:25:33 +01:00
.set(withUpdatedAt({
version: body.version,
lastContact: lastContact
}))
2025-07-16 18:15:28 +02:00
.where(eq(drizzleDb.schemas.agent.id, agentId));
const response = {
agent: {
id: agentId,
lastContact: lastContact
},
2024-11-29 12:39:39 +01:00
databases: databasesResponse
}
2025-09-26 09:04:18 +02:00
2026-02-08 20:42:42 +01:00
2024-11-29 12:54:34 +01:00
return Response.json(response)
} catch (error) {
console.error('Error in POST handler:', error);
return NextResponse.json(
2024-11-29 12:39:39 +01:00
{error: 'Internal server error'},
{status: 500}
);
2024-11-11 18:50:42 +01:00
}
2024-11-29 12:39:39 +01:00
}
2024-11-11 18:50:42 +01:00