120 lines
3.3 KiB
JavaScript
120 lines
3.3 KiB
JavaScript
import { DebugBuilder } from "../../modules/debugger.mjs";
|
|
const log = new DebugBuilder("server", "mongoSystemsWrappers");
|
|
import {
|
|
insertDocument,
|
|
getDocuments,
|
|
connectToDatabase,
|
|
} from "./mongoHandler.mjs";
|
|
|
|
const collectionName = "radio-systems";
|
|
|
|
// Local wrapper to remove any local files from radio systems
|
|
const removeLocalFilesFromsystem = async (system) => {
|
|
if (system.trunkFile) delete system.trunkFile;
|
|
if (system.whitelistFile) delete system.whitelistFile;
|
|
};
|
|
|
|
// Wrapper for inserting a system
|
|
export const createSystem = async (name, system, nuid) => {
|
|
try {
|
|
// Remove any local files
|
|
await removeLocalFilesFromsystem(system);
|
|
// Add the NUID of the node that created this system
|
|
system.nodes = [nuid];
|
|
// Add the name of the system
|
|
system.name = name;
|
|
const insertedId = await insertDocument(collectionName, system);
|
|
return insertedId;
|
|
} catch (error) {
|
|
log.ERROR("Error creating system:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Wrapper for retrieving all systems
|
|
export const getAllSystems = async () => {
|
|
try {
|
|
const systems = await getDocuments(collectionName);
|
|
return systems;
|
|
} catch (error) {
|
|
log.ERROR("Error getting all systems:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Wrapper for retrieving a system by name
|
|
export const getSystemByName = async (name) => {
|
|
const db = await connectToDatabase();
|
|
try {
|
|
const collection = db.db().collection(collectionName);
|
|
const system = await collection.findOne({ name });
|
|
return system;
|
|
} catch (error) {
|
|
log.ERROR("Error getting system by name:", error);
|
|
throw error;
|
|
} finally {
|
|
// Close the connection
|
|
await db.close();
|
|
}
|
|
};
|
|
|
|
// Wrapper to get all systems from a given node
|
|
export const getSystemsByNuid = async (nuid) => {
|
|
const db = await connectToDatabase();
|
|
try {
|
|
const collection = db.db().collection(collectionName);
|
|
|
|
// Query for documents where the 'nodes' array contains the given nodeID
|
|
const query = { nodes: nuid };
|
|
const systems = await collection.find(query).toArray();
|
|
|
|
return systems;
|
|
} catch (error) {
|
|
log.ERROR("Error finding entries:", error);
|
|
throw error;
|
|
} finally {
|
|
// Close the connection
|
|
await db.close();
|
|
}
|
|
};
|
|
|
|
// Wrapper for updating a system by name
|
|
export const updateSystemByName = async (name, updatedSystem) => {
|
|
// Remove any local files
|
|
await removeLocalFilesFromsystem(updatedSystem);
|
|
|
|
const db = await connectToDatabase();
|
|
try {
|
|
const collection = db.db().collection(collectionName);
|
|
const result = await collection.updateOne(
|
|
{ name },
|
|
{ $set: updatedSystem },
|
|
);
|
|
log.INFO("System updated:", result.modifiedCount);
|
|
return result.modifiedCount;
|
|
} catch (error) {
|
|
log.ERROR("Error updating system by name:", error);
|
|
throw error;
|
|
} finally {
|
|
// Close the connection
|
|
await db.close();
|
|
}
|
|
};
|
|
|
|
// Wrapper for deleting a system by name
|
|
export const deleteSystemByName = async (name) => {
|
|
const db = await connectToDatabase();
|
|
try {
|
|
const collection = db.db().collection(collectionName);
|
|
const result = await collection.deleteOne({ name });
|
|
log.INFO("System deleted:", result.deletedCount);
|
|
return result.deletedCount;
|
|
} catch (error) {
|
|
log.ERROR("Error deleting system by name:", error);
|
|
throw error;
|
|
} finally {
|
|
// Close the connection
|
|
await db.close();
|
|
}
|
|
};
|