Updated nodesController to properly update nodes
This commit is contained in:
@@ -65,25 +65,48 @@ exports.getNodeInfo = async (req, res) => {
|
||||
exports.nodeCheckIn = async (req, res) => {
|
||||
if (!req.body.id) return res.status(400).json("No id specified");
|
||||
getNodeInfoFromId(req.body.id, (nodeInfo) => {
|
||||
let checkInObject = new nodeObject();
|
||||
// Convert the DB systems buffer to a JSON object to be worked with
|
||||
nodeInfo.nearbySystems = utils.BufferToJson(nodeInfo.nearbySystems)
|
||||
let checkInObject = new nodeObject({});
|
||||
// Convert the online status to a boolean to be worked with
|
||||
nodeInfo.online = nodeInfo.online !== 0;
|
||||
|
||||
if (req.body.name && req.body.name !== nodeInfo.name) checkInObject.name = req.body.name
|
||||
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.location && req.body.location !== nodeInfo.location) checkInObject.location = req.body.location
|
||||
if (req.body.nearbySystems && JSON.stringify(req.body.nearbySystems) !== JSON.stringify(nodeInfo.nearbySystems)) checkInObject.nearbySystems = req.body.nearbySystems
|
||||
if (req.body.online && req.body.online !== nodeInfo.online) checkInObject.online = req.body.online
|
||||
var isObjectUpdated = false;
|
||||
|
||||
if (req.body.name && req.body.name != nodeInfo.name) {
|
||||
checkInObject.name = req.body.name;
|
||||
isObjectUpdated = true;
|
||||
}
|
||||
|
||||
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 (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);
|
||||
// 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, () => {
|
||||
return res.status(202).json({"updatedKeys": checkInObject});
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user