Feat: Add support for updating and deleting agents in local MongoDB

This commit is contained in:
ypratama committed 2026-09-02 16:57:49 +07:00
1 parent e7c9870e59
commit 3524588e42
1 file changed
+61 -2
+61 -2
View File
@@ -14,8 +14,67 @@ import { fetchAgentsFromMongo } from "./agentsMongo";
import { getAuthUser } from "./authHelper";
import mongoose from "mongoose";
// Re-export mutations for backwards compatibility
export { createAgent, updateAgent, deleteAgent };
// Override mutations to use MongoDB for On-Premise mode
export async function updateAgent(agentId: string, label: string) {
try {
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/backone_dpi';
if (mongoose.connection.readyState !== 1) {
await mongoose.connect(MONGODB_URI, { serverSelectionTimeoutMS: 5000 });
}
const db = mongoose.connection.db;
// Check if agent exists in agent_registry
const agent = await db.collection('agent_registry').findOne({ $or: [{ uuid: agentId }, { serial: agentId }] });
if (!agent) {
return { success: false, message: "Agent tidak ditemukan di database lokal." };
}
await db.collection('agent_registry').updateOne(
{ _id: agent._id },
{ $set: { label: label } }
);
// Also try to update the summaries if possible, so historical displays match
await db.collection('summaries').updateMany(
{ agent_uuid: agent.uuid },
{ $set: { agent_label: label } }
);
const { revalidatePath } = require('next/cache');
revalidatePath('/agents');
return { success: true, message: "Label berhasil diubah." };
} catch (err: any) {
return { success: false, message: err.message };
}
}
export async function deleteAgent(agentId: string) {
try {
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/backone_dpi';
if (mongoose.connection.readyState !== 1) {
await mongoose.connect(MONGODB_URI, { serverSelectionTimeoutMS: 5000 });
}
const db = mongoose.connection.db;
// Check if agent exists in agent_registry
const agent = await db.collection('agent_registry').findOne({ $or: [{ uuid: agentId }, { serial: agentId }] });
if (!agent) {
return { success: false, message: "Agent tidak ditemukan di database lokal." };
}
await db.collection('agent_registry').deleteOne({ _id: agent._id });
const { revalidatePath } = require('next/cache');
revalidatePath('/agents');
return { success: true, message: "Agent berhasil dihapus." };
} catch (err: any) {
return { success: false, message: err.message };
}
}
export { createAgent };
export async function getAgents(clientSiteUuid?: string): Promise<Agent[]> {
try {