111 lines
3.5 KiB
JavaScript
111 lines
3.5 KiB
JavaScript
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) {
|
|
console.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) {
|
|
console.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) {
|
|
console.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) {
|
|
console.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 });
|
|
console.log('System updated:', result.modifiedCount);
|
|
return result.modifiedCount;
|
|
} catch (error) {
|
|
console.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 });
|
|
console.log('System deleted:', result.deletedCount);
|
|
return result.deletedCount;
|
|
} catch (error) {
|
|
console.error('Error deleting system by name:', error);
|
|
throw error;
|
|
} finally {
|
|
// Close the connection
|
|
await db.close();
|
|
}
|
|
}; |