Updated nodesController to properly update nodes

This commit is contained in:
Logan Cusano
2023-06-19 00:02:37 -04:00
parent b180f90973
commit d96e6ad519

View File

@@ -65,25 +65,48 @@ exports.getNodeInfo = async (req, res) => {
exports.nodeCheckIn = async (req, res) => { exports.nodeCheckIn = async (req, res) => {
if (!req.body.id) return res.status(400).json("No id specified"); if (!req.body.id) return res.status(400).json("No id specified");
getNodeInfoFromId(req.body.id, (nodeInfo) => { getNodeInfoFromId(req.body.id, (nodeInfo) => {
let checkInObject = new nodeObject(); let checkInObject = new nodeObject({});
// Convert the DB systems buffer to a JSON object to be worked with
nodeInfo.nearbySystems = utils.BufferToJson(nodeInfo.nearbySystems)
// Convert the online status to a boolean to be worked with // Convert the online status to a boolean to be worked with
nodeInfo.online = nodeInfo.online !== 0; nodeInfo.online = nodeInfo.online !== 0;
if (req.body.name && req.body.name !== nodeInfo.name) checkInObject.name = req.body.name var isObjectUpdated = false;
if (req.body.ip && req.body.ip !== nodeInfo.ip) checkInObject.ip = req.body.ip
if (req.body.port && req.body.port !== nodeInfo.port) checkInObject.port = req.body.port if (req.body.name && req.body.name != nodeInfo.name) {
if (req.body.location && req.body.location !== nodeInfo.location) checkInObject.location = req.body.location checkInObject.name = req.body.name;
if (req.body.nearbySystems && JSON.stringify(req.body.nearbySystems) !== JSON.stringify(nodeInfo.nearbySystems)) checkInObject.nearbySystems = req.body.nearbySystems isObjectUpdated = true;
if (req.body.online && req.body.online !== nodeInfo.online) checkInObject.online = req.body.online }
if (req.body.ip && req.body.ip != nodeInfo.ip) {
checkInObject.ip = req.body.ip;
isObjectUpdated = true;
}
if (req.body.port && req.body.port != nodeInfo.port) {
checkInObject.port = req.body.port;
isObjectUpdated = true;
}
if (req.body.location && req.body.location != nodeInfo.location) {
checkInObject.location = req.body.location;
isObjectUpdated = true;
}
if (req.body.nearbySystems && JSON.stringify(req.body.nearbySystems) !== JSON.stringify(nodeInfo.nearbySystems)) {
checkInObject.nearbySystems = req.body.nearbySystems;
isObjectUpdated = true;
}
if (req.body.online && (req.body.online === "true") != nodeInfo.online) {
checkInObject.online = (req.body.online === "true");
isObjectUpdated = true;
}
// If no changes are made tell the client // If no changes are made tell the client
if (Object.keys(checkInObject).length === 0) return res.status(200).json("No keys updated"); if (!isObjectUpdated) return res.status(200).json("No keys updated");
log.INFO("Updating the following keys for ID: ", req.body.id, checkInObject); log.INFO("Updating the following keys for ID: ", req.body.id, checkInObject);
// Adding the ID key to the body so that the client can double-check their ID // Adding the ID key to the body so that the client can double-check their ID
checkInObject.id = req.body.id; checkInObject.id = nodeInfo.id;
updateNodeInfo(checkInObject, () => { updateNodeInfo(checkInObject, () => {
return res.status(202).json({"updatedKeys": checkInObject}); return res.status(202).json({"updatedKeys": checkInObject});
}) })