Skip to content

Commit

Permalink
debugging systems list
Browse files Browse the repository at this point in the history
  • Loading branch information
robotastic committed Aug 26, 2024
1 parent d46c8b0 commit 221ee0d
Showing 1 changed file with 81 additions and 81 deletions.
162 changes: 81 additions & 81 deletions backend/controllers/systems.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const mailjet = new Mailjet({
var systemList = [];
var systemListTime = Date.now();

exports.contact_system = async function(req, res) {
exports.contact_system = async function (req, res) {
var system = await System.findOne({
shortName: req.params.shortName.toLowerCase()
}).catch(err => {
Expand Down Expand Up @@ -59,7 +59,7 @@ exports.contact_system = async function(req, res) {
let message = "Thank you for contributing a feed to OpenMHz. A user has sent in a message about this feed:\n\n-------------------------------\n"
message = message + "User Name: " + req.body.name + "\n";
message = message + "User Email: " + req.body.email + "\n-------------------------------\n";
message = message + "Message:\n" + req.body.message + "\n-------------------------------\n\n";
message = message + "Message:\n" + req.body.message + "\n-------------------------------\n\n";
message = message + "If you wish to stop receiving User Messages for this System:\n - goto the Admin for " + site_name + ": " + admin_server + "\n - find this System\n - Update it and turn off this Allow Contact option."
const request = mailjet.post("send", {
version: "v3.1"
Expand All @@ -72,20 +72,20 @@ exports.contact_system = async function(req, res) {
To: [{
Email: user.email,
Name: user.firstName + " " + user.lastName
},{
}, {
Email: admin_email,
Name: site_name + " Admin"
}],
Subject: site_name + " - " + system.shortName + " - " + " User Comment",
TextPart: message
}]
}]
});
request.then(result => {
console.log(`User Comment sent to: ${user.email} for shortName: ${system.shortName}`)
res.json({
success: true
});
})
console.log(`User Comment sent to: ${user.email} for shortName: ${system.shortName}`)
res.json({
success: true
});
})
.catch(err => {
console.error(err.statusCode);
console.error(err);
Expand All @@ -102,91 +102,91 @@ exports.contact_system = async function(req, res) {

async function load_systems(systemClients) {
let fromDate = new Date(Date.now() - 60 * 60 * 24 * 30 * 1000);
const results = await System.find({lastActive: {$gte: fromDate}}).populate('userId', "screenName").catch( err => {
//const results = await System.find({active: true}).populate('userId', "screenName").catch( err => {
//const results = await System.find({active: true}).catch( err => { // super simple query
console.error("Error - get_systems: " + err.message);
const results = await System.find({ lastActive: { $gte: fromDate } }).populate('userId', "screenName").catch(err => {
//const results = await System.find({active: true}).populate('userId', "screenName").catch( err => {
//const results = await System.find({active: true}).catch( err => { // super simple query
console.error("Error - get_systems: " + err.message);
});
systemList = [];
for (var result in results) {

var clientCount = 0;
if (systemClients.hasOwnProperty(results[result].shortName)) {
clientCount = systemClients[results[result].shortName];
}
var system = {
name: results[result].name,
shortName: results[result].shortName,
systemType: results[result].systemType,
county: results[result].county,
country: results[result].country,
city: results[result].city,
state: results[result].state,
active: results[result].active,
lastActive: results[result].lastActive,
callAvg: results[result].callAvg,
description: results[result].description,
status: results[result].status,
allowContact: results[result].allowContact,
clientCount: clientCount
}
/*
if (results[result].showScreenName && results[result].userId) {
system.screenName = results[result].userId.screenName
} else {
system.screenName = null;
}*/
system.screenName = null;
systemList.push(system);
}
systemList = [];
for (var result in results) {

var clientCount = 0;
if (systemClients.hasOwnProperty(results[result].shortName)) {
clientCount = systemClients[results[result].shortName];
}
var system = {
name: results[result].name,
shortName: results[result].shortName,
systemType: results[result].systemType,
county: results[result].county,
country: results[result].country,
city: results[result].city,
state: results[result].state,
active: results[result].active,
lastActive: results[result].lastActive,
callAvg: results[result].callAvg,
description: results[result].description,
status: results[result].status,
allowContact: results[result].allowContact,
clientCount: clientCount
}
/*
if (results[result].showScreenName && results[result].userId) {
system.screenName = results[result].userId.screenName
} else {
system.screenName = null;
}*/
system.screenName = null;
systemList.push(system);
}
console.log("Loaded Systems: " + systemList.length + " that have been active since: " + fromDate);
}


exports.get_systems = async function (req, res) {
if ((systemList.length == 0) || (systemListTime < (Date.now() - 60 * 1000 * 15))) {
if ((systemList.length == 0) || (systemListTime < (Date.now() - 60 * 1000 * 2))) {
await load_systems(req.systemClients);
systemListTime = Date.now();
}

res.contentType('json');
res.send(JSON.stringify({
success: true,
systems: systemList
}));
}
res.contentType('json');
res.send(JSON.stringify({
success: true,
systems: systemList
}));
}

exports.authorize_system = async function (req, res) {
exports.authorize_system = async function (req, res) {

var shortName = req.params.shortName.toLowerCase();
var apiKey = req.body.api_key;
let item = null;

try {
item = await System.findOne({ 'shortName': shortName }, ["key"]);
} catch (err) {
console.warn("[" + req.params.shortName + "] Error /:shortName/authorize - Error: " + err);
res.status(500);
res.send("Invalid System Name\n");
return;
}

if (!item) {
console.info("[" + req.params.shortName + "] Error /:shortName/authorize ShortName does not exist");
res.status(500);
res.send("Invalid System Name\n");
return;
}
var shortName = req.params.shortName.toLowerCase();
var apiKey = req.body.api_key;
let item = null;

if (apiKey != item.key) {
console.warn("[" + req.params.shortName + "] Error /:shortName/authorize API Key Mismatch - Provided key: " + apiKey);
res.status(403);
res.send("Invalid API Key\n");
return;
} else {
// System shortName exists and the API Key is valid.
res.status(200).end();
}
try {
item = await System.findOne({ 'shortName': shortName }, ["key"]);
} catch (err) {
console.warn("[" + req.params.shortName + "] Error /:shortName/authorize - Error: " + err);
res.status(500);
res.send("Invalid System Name\n");
return;
}

if (!item) {
console.info("[" + req.params.shortName + "] Error /:shortName/authorize ShortName does not exist");
res.status(500);
res.send("Invalid System Name\n");
return;
}

if (apiKey != item.key) {
console.warn("[" + req.params.shortName + "] Error /:shortName/authorize API Key Mismatch - Provided key: " + apiKey);
res.status(403);
res.send("Invalid API Key\n");
return;
} else {
// System shortName exists and the API Key is valid.
res.status(200).end();
}

}

0 comments on commit 221ee0d

Please sign in to comment.