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

82 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-12-01 11:11:27 +01:00
import {NextResponse} from "next/server";
2024-11-28 20:28:00 +01:00
import {getFileUrlPresignedLocal} from "@/features/upload/private/upload.action";
2024-11-29 12:54:34 +01:00
import {handleDatabases} from "./helpers";
import {eventEmitter} from "../../../events/route";
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";
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 = {
databases: databaseAgent[]
}
2024-11-28 20:28:00 +01:00
export async function GET(request: Request) {
const url = await getFileUrlPresignedLocal("d4a7fa35-2506-4d01-a612-a8ef2e2cc1c5.dump")
return Response.json({
message: url
})
}
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
const body: Body = await request.json();
2024-12-01 11:11:27 +01:00
const lastContact = new Date();
2025-07-17 09:28:57 +02:00
if (!isUuidv4(agentId)) {
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) {
return NextResponse.json({error: "Agent not found"}, {status: 404})
}
2024-11-29 12:39:39 +01:00
const databasesResponse = await handleDatabases(body, agent, lastContact)
2024-12-01 11:11:27 +01:00
2025-07-16 18:15:28 +02:00
await db
.update(drizzleDb.schemas.agent)
.set({ lastContact: lastContact })
.where(eq(drizzleDb.schemas.agent.id, agentId));
eventEmitter.emit('modification', {update: true});
const response = {
agent: {
id: agentId,
lastContact: lastContact
},
2024-11-29 12:39:39 +01:00
databases: databasesResponse
}
console.log(response)
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