mirror of
https://github.com/openmhz/trunk-server.git
synced 2026-11-12 08:47:40 -05:00
Gettign going
This commit is contained in:
@@ -2,16 +2,15 @@ const path = require("path");
|
||||
const express = require("express");
|
||||
const session = require("express-session");
|
||||
const bodyParser = require("body-parser");
|
||||
const connectMongo = require("connect-mongo");
|
||||
const MongoStore = require("connect-mongo");
|
||||
const secrets = require("./secrets");
|
||||
|
||||
const MongoStore = connectMongo(session)
|
||||
var cookie_domain = process.env['REACT_APP_COOKIE_DOMAIN'] != null ? process.env['REACT_APP_COOKIE_DOMAIN'] : '.openmhz.com'; //'https://s3.amazonaws.com/robotastic';
|
||||
var backend_server = process.env['REACT_APP_BACKEND_SERVER'] != null ? process.env['REACT_APP_BACKEND_SERVER'] : 'https://api.openmhz.com';
|
||||
var frontend_server = process.env['REACT_APP_FRONTEND_SERVER'] != null ? process.env['REACT_APP_FRONTEND_SERVER'] : 'https://openmhz.com';
|
||||
var socket_server = process.env['REACT_APP_SOCKET_SERVER'] != null ? process.env['REACT_APP_SOCKET_SERVER'] : 'wss://socket.openmhz.com'; //'https://s3.amazonaws.com/robotastic';
|
||||
var account_server = process.env['REACT_APP_ACCOUNT_SERVER'] != null ? process.env['REACT_APP_ACCOUNT_SERVER'] : 'https://account.openmhz.com'; //'https://s3.amazonaws.com/robotastic';
|
||||
var dev_server = frontend_server + ":3000"
|
||||
var dev_server = "http://admin.openmhz.test:3000"
|
||||
|
||||
module.exports = function(app, passport) {
|
||||
app.set("port", 3008)
|
||||
@@ -36,9 +35,12 @@ module.exports = function(app, passport) {
|
||||
secure: false,
|
||||
domain: cookie_domain
|
||||
},
|
||||
store: new MongoStore({
|
||||
url: secrets.db,
|
||||
autoReconnect: true
|
||||
store: MongoStore.create({
|
||||
mongoUrl: secrets.db,
|
||||
autoReconnect: true,
|
||||
autoRemove: 'interval',
|
||||
autoRemoveInterval: 240,
|
||||
touchAfter: 24 * 3600 // time period in seconds
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -14,10 +14,13 @@ module.exports = function (app, passport) {
|
||||
done(null, user.id)
|
||||
})
|
||||
|
||||
passport.deserializeUser((id, done) => {
|
||||
User.findById(id, (err, user) => {
|
||||
done(err, user)
|
||||
})
|
||||
passport.deserializeUser(async (id, done) => {
|
||||
const user = await User.findById(id).exec();
|
||||
if (user) {
|
||||
done(null,user);
|
||||
} else {
|
||||
done("User not found", null);
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
||||
@@ -4,286 +4,241 @@ const mongoose = require("mongoose");
|
||||
|
||||
//router.get('/system/:shortName/group/:groupId?', isLoggedIn, function(req, res) {
|
||||
|
||||
exports.getGroups = function(req, res, next) {
|
||||
process.nextTick(function() {
|
||||
System.findOne(
|
||||
{
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
function(err, system) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.json({ success: false, message: err });
|
||||
return;
|
||||
}
|
||||
if (!system) {
|
||||
console.error(err);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "That Short Name does not exist."
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!system.userId.equals(req.user._id)) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: "You are not the user associated with this system."
|
||||
});
|
||||
return;
|
||||
}
|
||||
exports.getGroups = async function (req, res, next) {
|
||||
process.nextTick(async function () {
|
||||
const system = await System.findOne({ shortName: req.params.shortName.toLowerCase() });
|
||||
if (!system) {
|
||||
res.status(404);
|
||||
res.json({ success: false });
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.params.groupId) {
|
||||
var groupId = req.params.groupId.toLowerCase();
|
||||
if (!system.userId.equals(req.user._id)) {
|
||||
res.status(401)
|
||||
res.json({
|
||||
success: false,
|
||||
message: "You are not the user associated with this system."
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
Group.findOne(
|
||||
{
|
||||
_id: new mongoose.Types.ObjectId(groupId),
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
{ sort: { position: -1 } },
|
||||
function(err, group) {
|
||||
if (err) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: err
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
res.json({
|
||||
success: true,
|
||||
group: group
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
} else {
|
||||
Group.find(
|
||||
{
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
}).sort("position").exec(
|
||||
function(err, groups) {
|
||||
if (err) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: err
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
res.json({
|
||||
success: true,
|
||||
groups: groups
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
if (req.params.groupId) {
|
||||
var groupId = req.params.groupId.toLowerCase();
|
||||
|
||||
const group = await Group.findOne(
|
||||
{
|
||||
_id: new mongoose.Types.ObjectId(groupId),
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
{ sort: { position: -1 } });
|
||||
if (!group) {
|
||||
res.status(404);
|
||||
res.json({ success: false });
|
||||
return;
|
||||
|
||||
} else {
|
||||
res.json(group);
|
||||
return;
|
||||
}
|
||||
);
|
||||
});
|
||||
} else {
|
||||
const group = await Group.findOne(
|
||||
{
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
}).sort("position").exec();
|
||||
if (!group) {
|
||||
res.status(404);
|
||||
res.json({ success: false });
|
||||
return;
|
||||
|
||||
} else {
|
||||
let result = group;
|
||||
if (!Array.isArray(group)) {
|
||||
result = [group]
|
||||
}
|
||||
res.json(result);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
exports.deleteGroup = function(req, res, next) {
|
||||
//router.get('/system/:shortName/remove_group/:groupId', isLoggedIn, function(req, res) {
|
||||
System.findOne({
|
||||
'shortName': req.params.shortName.toLowerCase()
|
||||
}, function(err, system) {
|
||||
exports.deleteGroup = function (req, res, next) {
|
||||
//router.get('/system/:shortName/remove_group/:groupId', isLoggedIn, function(req, res) {
|
||||
System.findOne({
|
||||
'shortName': req.params.shortName.toLowerCase()
|
||||
}, function (err, system) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.json({ success: false, message: err });
|
||||
return;
|
||||
}
|
||||
if (!system) {
|
||||
console.error(err);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "That Short Name does not exist."
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!system.userId.equals(req.user._id)) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: "You are not the user associated with this system."
|
||||
});
|
||||
return;
|
||||
}
|
||||
Group.findOneAndRemove({
|
||||
shortName: req.params.shortName.toLowerCase(),
|
||||
'_id': new mongoose.Types.ObjectId(req.params.groupId)
|
||||
}, function (err) {
|
||||
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.json({ success: false, message: err });
|
||||
return;
|
||||
}
|
||||
if (!system) {
|
||||
console.error(err);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "That Short Name does not exist."
|
||||
message: err
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!system.userId.equals(req.user._id)) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: "You are not the user associated with this system."
|
||||
});
|
||||
return;
|
||||
}
|
||||
Group.findOneAndRemove({
|
||||
shortName: req.params.shortName.toLowerCase(),
|
||||
'_id': new mongoose.Types.ObjectId(req.params.groupId)
|
||||
}, function(err) {
|
||||
|
||||
if (err) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: err
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
res.json({
|
||||
success: true
|
||||
});
|
||||
return;
|
||||
}
|
||||
// removed!
|
||||
});
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
exports.reorderGroups = function(req, res, next) {
|
||||
//router.post('/system/:shortName/group_order', isLoggedIn, function(req, res) {
|
||||
process.nextTick(function() {
|
||||
System.findOne(
|
||||
{
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
function(err, system) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.json({ success: false, message: err });
|
||||
return;
|
||||
}
|
||||
if (!system) {
|
||||
console.error(err);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "That Short Name does not exist."
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!system.userId.equals(req.user._id)) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: "You are not the user associated with this system."
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
var groupOrder = JSON.parse(req.body.groupOrder);
|
||||
for (var i = 0; i < groupOrder.length; i++) {
|
||||
|
||||
console.log("in group: " + groupOrder[i] + " position: " + i);
|
||||
Group.findOneAndUpdate(
|
||||
{
|
||||
_id: new mongoose.Types.ObjectId(groupOrder[i]),
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
{
|
||||
position: i
|
||||
},
|
||||
{
|
||||
new: true,
|
||||
upsert: false
|
||||
},
|
||||
function(err, group) {
|
||||
console.log("Save group: " + group._id + " position: " + group.position);
|
||||
if (err) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: err
|
||||
});
|
||||
|
||||
console.log(
|
||||
"Error: Group Order - " +
|
||||
err +
|
||||
" Shortname: " +
|
||||
req.params.shortName.toLowerCase()
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
} else {
|
||||
res.json({
|
||||
success: true
|
||||
});
|
||||
return;
|
||||
}
|
||||
);
|
||||
// removed!
|
||||
});
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
exports.upsertGroup = function(req, res, next) {
|
||||
exports.reorderGroups = function (req, res, next) {
|
||||
//router.post('/system/:shortName/group_order', isLoggedIn, function(req, res) {
|
||||
process.nextTick(async function () {
|
||||
const system = await System.findOne({ shortName: req.params.shortName.toLowerCase() });
|
||||
|
||||
if (!system) {
|
||||
res.status(404);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "That Short Name does not exist."
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!system.userId.equals(req.user._id)) {
|
||||
res.status(401);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "You are not the user associated with this system."
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
var groupOrder = JSON.parse(req.body.groupOrder);
|
||||
for (var i = 0; i < groupOrder.length; i++) {
|
||||
|
||||
console.log("in group: " + groupOrder[i] + " position: " + i);
|
||||
const group = await Group.findOneAndUpdate(
|
||||
{
|
||||
_id: new mongoose.Types.ObjectId(groupOrder[i]),
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
{
|
||||
position: i
|
||||
},
|
||||
{
|
||||
new: true,
|
||||
upsert: false
|
||||
});
|
||||
if (!group) {
|
||||
res.status(404)
|
||||
res.json({
|
||||
success: false,
|
||||
message: err
|
||||
});
|
||||
|
||||
console.log(
|
||||
"Error: Group Order - Shortname: " +
|
||||
req.params.shortName.toLowerCase()
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
res.json({
|
||||
success: true
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
exports.upsertGroup = function (req, res, next) {
|
||||
//router.post('/system/:shortName/group/:groupId?', isLoggedIn, function(req, res) {
|
||||
process.nextTick(function() {
|
||||
System.findOne(
|
||||
process.nextTick(async function () {
|
||||
let system = System.findOne({ shortName: req.params.shortName.toLowerCase() });
|
||||
|
||||
if (!system) {
|
||||
res.status(404);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "That Short Name does not exist."
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!system.userId.equals(req.user._id)) {
|
||||
res.status(401);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "You are not the user associated with this system."
|
||||
});
|
||||
return;
|
||||
}
|
||||
var message = "";
|
||||
var groupName = req.body.groupName.replace(/[^\w\s]/g, "");
|
||||
var talkgroups = JSON.parse(req.body.talkgroups);
|
||||
|
||||
if (!talkgroups.length || !Array.isArray(talkgroups)) {
|
||||
message = "Talkgroups need to be selected, group can not be empty ";
|
||||
}
|
||||
if (!groupName.length) {
|
||||
message = message + "Group Name is required";
|
||||
}
|
||||
|
||||
if (message.length) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: message
|
||||
});
|
||||
return;
|
||||
}
|
||||
// if we got here, that means that everything is good for updating/inserting
|
||||
|
||||
var groupId = req.params.groupId;
|
||||
|
||||
let group = Group.findOneAndUpdate(
|
||||
{
|
||||
_id: new mongoose.Types.ObjectId(groupId),
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
function(err, system) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.json({ success: false, message: err });
|
||||
return;
|
||||
}
|
||||
if (!system) {
|
||||
console.error(err);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "That Short Name does not exist."
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!system.userId.equals(req.user._id)) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: "You are not the user associated with this system."
|
||||
});
|
||||
return;
|
||||
}
|
||||
var message = "";
|
||||
var groupName = req.body.groupName.replace(/[^\w\s]/g, "");
|
||||
var talkgroups = JSON.parse(req.body.talkgroups);
|
||||
{
|
||||
groupName: groupName,
|
||||
talkgroups: talkgroups
|
||||
},
|
||||
{
|
||||
new: true,
|
||||
upsert: true
|
||||
});
|
||||
|
||||
if (!talkgroups.length || !Array.isArray(talkgroups)) {
|
||||
message = "Talkgroups need to be selected, group can not be empty ";
|
||||
}
|
||||
if (!groupName.length) {
|
||||
message = message + "Group Name is required";
|
||||
}
|
||||
|
||||
if (message.length) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: message
|
||||
});
|
||||
return;
|
||||
}
|
||||
// if we got here, that means that everything is good for updating/inserting
|
||||
|
||||
var groupId = req.params.groupId;
|
||||
|
||||
Group.findOneAndUpdate(
|
||||
{
|
||||
_id: new mongoose.Types.ObjectId(groupId),
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
{
|
||||
groupName: groupName,
|
||||
talkgroups: talkgroups
|
||||
},
|
||||
{
|
||||
new: true,
|
||||
upsert: true
|
||||
},
|
||||
function(err, group) {
|
||||
if (err) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: err
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
res.json({
|
||||
success: true,
|
||||
group: group
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
||||
if (!group) {
|
||||
res.status(500);
|
||||
res.json({
|
||||
success: false,
|
||||
message: err
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
res.json(group);
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -12,91 +12,76 @@ exports.isLoggedIn = function (req, res, next) {
|
||||
|
||||
// -------------------------------------------
|
||||
|
||||
exports.listSystems = function (req, res, next) {
|
||||
exports.listSystems = async function (req, res, next) {
|
||||
console.log("Listing Systems for: " + req.user._id);
|
||||
System.find({
|
||||
userId: mongoose.Types.ObjectId(req.user._id)
|
||||
},
|
||||
function (err, systems) {
|
||||
if (err) {
|
||||
console.error("Error - ListSystems: " + err);
|
||||
res.json({
|
||||
success: false,
|
||||
message: err
|
||||
});
|
||||
return;
|
||||
}
|
||||
const userId = new mongoose.Types.ObjectId(req.user._id);
|
||||
const systems = await System.find({ userId: userId }).exec();
|
||||
if (systems == null) {
|
||||
res.status(400);
|
||||
res.json({ success: false, message: err });
|
||||
return;
|
||||
}
|
||||
|
||||
var shortNames = new Array();
|
||||
for (var i = 0; i < systems.length; i++) {
|
||||
shortNames.push(systems[i].shortName);
|
||||
}
|
||||
let shortNames = new Array();
|
||||
for (var i = 0; i < systems.length; i++) {
|
||||
shortNames.push(systems[i].shortName);
|
||||
}
|
||||
|
||||
SystemStat.find({
|
||||
shortName: {
|
||||
$in: shortNames
|
||||
}
|
||||
},
|
||||
function (err, stats) {
|
||||
var returnSys = systems.map(obj => {
|
||||
var rObj = (({
|
||||
name,
|
||||
shortName,
|
||||
description,
|
||||
systemType,
|
||||
city,
|
||||
state,
|
||||
county,
|
||||
country,
|
||||
userId,
|
||||
key,
|
||||
showScreenName,
|
||||
ignoreUnknownTalkgroup
|
||||
}) => ({
|
||||
name,
|
||||
shortName,
|
||||
description,
|
||||
systemType,
|
||||
city,
|
||||
state,
|
||||
county,
|
||||
country,
|
||||
userId,
|
||||
key,
|
||||
showScreenName,
|
||||
ignoreUnknownTalkgroup
|
||||
}))(obj);
|
||||
if (obj.showScreenName) {
|
||||
rObj.screenName = req.user.screenName;
|
||||
} else {
|
||||
rObj.screenName = null;
|
||||
}
|
||||
rObj.id = obj._id;
|
||||
return rObj;
|
||||
});
|
||||
var sys_stats = {}
|
||||
var MS_PER_MINUTE = 60000;
|
||||
var now = new Date();
|
||||
for (var i = 0; i < stats.length; i++) {
|
||||
sys_stats[stats[i].shortName] = {};
|
||||
const stats = await SystemStat.find({ shortName: { $in: shortNames } }).exec();
|
||||
|
||||
sys_stats[stats[i].shortName].callTotals = stats[i].callTotals;
|
||||
sys_stats[stats[i].shortName].errorTotals = stats[i].errorTotals;
|
||||
sys_stats[stats[i].shortName].errorStats = stats[i].errorStats;
|
||||
sys_stats[stats[i].shortName].usageBytes = stats[i].usageBytes;
|
||||
sys_stats[stats[i].shortName].usageMb = stats[i].usageMb;
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
systems: returnSys,
|
||||
stats: sys_stats
|
||||
});
|
||||
return;
|
||||
}
|
||||
);
|
||||
let returnSys = systems.map(obj => {
|
||||
var rObj = (({
|
||||
name,
|
||||
shortName,
|
||||
description,
|
||||
systemType,
|
||||
city,
|
||||
state,
|
||||
county,
|
||||
country,
|
||||
userId,
|
||||
key,
|
||||
showScreenName,
|
||||
ignoreUnknownTalkgroup
|
||||
}) => ({
|
||||
name,
|
||||
shortName,
|
||||
description,
|
||||
systemType,
|
||||
city,
|
||||
state,
|
||||
county,
|
||||
country,
|
||||
userId,
|
||||
key,
|
||||
showScreenName,
|
||||
ignoreUnknownTalkgroup
|
||||
}))(obj);
|
||||
if (obj.showScreenName) {
|
||||
rObj.screenName = req.user.screenName;
|
||||
} else {
|
||||
rObj.screenName = null;
|
||||
}
|
||||
);
|
||||
rObj.id = obj._id;
|
||||
return rObj;
|
||||
});
|
||||
var sys_stats = {}
|
||||
for (var i = 0; i < stats.length; i++) {
|
||||
sys_stats[stats[i].shortName] = {};
|
||||
|
||||
sys_stats[stats[i].shortName].callTotals = stats[i].callTotals;
|
||||
sys_stats[stats[i].shortName].errorTotals = stats[i].errorTotals;
|
||||
sys_stats[stats[i].shortName].errorStats = stats[i].errorStats;
|
||||
sys_stats[stats[i].shortName].usageBytes = stats[i].usageBytes;
|
||||
sys_stats[stats[i].shortName].usageMb = stats[i].usageMb;
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
systems: returnSys,
|
||||
stats: sys_stats
|
||||
});
|
||||
return;
|
||||
};
|
||||
|
||||
function remove_system(shortName) {
|
||||
@@ -115,8 +100,8 @@ function remove_system(shortName) {
|
||||
});
|
||||
*/
|
||||
System.remove({
|
||||
shortName: shortName
|
||||
},
|
||||
shortName: shortName
|
||||
},
|
||||
function (err) {
|
||||
if (err) return console.error(err);
|
||||
// removed!
|
||||
@@ -124,8 +109,8 @@ function remove_system(shortName) {
|
||||
);
|
||||
|
||||
Talkgroup.remove({
|
||||
shortName: shortName
|
||||
},
|
||||
shortName: shortName
|
||||
},
|
||||
function (err) {
|
||||
if (err) return console.error(err);
|
||||
// removed!
|
||||
@@ -133,8 +118,8 @@ function remove_system(shortName) {
|
||||
);
|
||||
|
||||
Group.remove({
|
||||
shortName: shortName
|
||||
},
|
||||
shortName: shortName
|
||||
},
|
||||
function (err) {
|
||||
if (err) return console.error(err);
|
||||
// removed!
|
||||
@@ -145,8 +130,8 @@ function remove_system(shortName) {
|
||||
exports.deleteSystem = function (req, res, next) {
|
||||
//router.get('/delete_system/:shortName', isLoggedIn, function(req, res) {
|
||||
System.findOne({
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
function (err, system) {
|
||||
if (err) {
|
||||
res.json({
|
||||
@@ -224,7 +209,7 @@ exports.uniqueShortName = async function (req, res, next) {
|
||||
|
||||
var shortName = res.locals.shortName;
|
||||
if (typeof shortName == 'undefined') {
|
||||
console.error("locals: " + res.locals.shortName + " body: " + req.body.shortName )
|
||||
console.error("locals: " + res.locals.shortName + " body: " + req.body.shortName)
|
||||
console.error("ShortName is not in res.local, pulling from req.body instead")
|
||||
shortName = req.body.shortName;
|
||||
}
|
||||
@@ -299,7 +284,7 @@ exports.updateSystem = async function (req, res, next) {
|
||||
userId,
|
||||
key,
|
||||
showScreenName,
|
||||
ignoreUnknownTalkgroup
|
||||
ignoreUnknownTalkgroup
|
||||
}) => ({
|
||||
name,
|
||||
shortName,
|
||||
@@ -312,7 +297,7 @@ exports.updateSystem = async function (req, res, next) {
|
||||
userId,
|
||||
key,
|
||||
showScreenName,
|
||||
ignoreUnknownTalkgroup
|
||||
ignoreUnknownTalkgroup
|
||||
}))(res.locals.system);
|
||||
returnSys.id = res.locals.system._id;
|
||||
res.json({
|
||||
@@ -329,7 +314,7 @@ exports.updateSystem = async function (req, res, next) {
|
||||
exports.validateSystem = async function (req, res, next) {
|
||||
try {
|
||||
res.locals.showScreenName = req.body.showScreenName;
|
||||
res.locals.ignoreUnknownTalkgroup = req.body.ignoreUnknownTalkgroup ;
|
||||
res.locals.ignoreUnknownTalkgroup = req.body.ignoreUnknownTalkgroup;
|
||||
if (!req.body.name || (req.body.name.length < 2)) {
|
||||
console.error("ERROR: Validate System - req.body.name");
|
||||
res.json({
|
||||
@@ -457,7 +442,7 @@ exports.createSystem = async function (req, res, next) {
|
||||
county,
|
||||
country,
|
||||
showScreenName,
|
||||
ignoreUnknownTalkgroup
|
||||
ignoreUnknownTalkgroup
|
||||
}) => ({
|
||||
name,
|
||||
shortName,
|
||||
@@ -468,10 +453,10 @@ exports.createSystem = async function (req, res, next) {
|
||||
county,
|
||||
country,
|
||||
showScreenName,
|
||||
ignoreUnknownTalkgroup
|
||||
ignoreUnknownTalkgroup
|
||||
}))(res.locals);
|
||||
system.key = key;
|
||||
system.userId = mongoose.Types.ObjectId(req.user._id);
|
||||
system.userId = new mongoose.Types.ObjectId(req.user._id);
|
||||
System.create(system, function (err, newSys) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
@@ -492,7 +477,7 @@ exports.createSystem = async function (req, res, next) {
|
||||
country,
|
||||
userId,
|
||||
showScreenName,
|
||||
ignoreUnknownTalkgroup ,
|
||||
ignoreUnknownTalkgroup,
|
||||
key
|
||||
}) => ({
|
||||
name,
|
||||
@@ -505,7 +490,7 @@ exports.createSystem = async function (req, res, next) {
|
||||
country,
|
||||
userId,
|
||||
showScreenName,
|
||||
ignoreUnknownTalkgroup ,
|
||||
ignoreUnknownTalkgroup,
|
||||
key
|
||||
}))(newSys);
|
||||
returnSys.id = newSys._id;
|
||||
|
||||
@@ -5,251 +5,217 @@ const fs = require('fs-extra');
|
||||
const crypto = require("crypto");
|
||||
var parse = require('csv-parse');
|
||||
|
||||
exports.isLoggedIn = function(req, res, next) {
|
||||
exports.isLoggedIn = function (req, res, next) {
|
||||
if (req.isAuthenticated()) return next();
|
||||
res.redirect(account_server + "/login");
|
||||
};
|
||||
|
||||
//router.get("/tg_export/:shortName", isLoggedIn, function(req, res) {
|
||||
exports.exportTalkgroups = function(req, res, next) {
|
||||
process.nextTick(function() {
|
||||
System.findOne(
|
||||
{
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
function(err, system) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.json({ success: false, message: err });
|
||||
return;
|
||||
}
|
||||
if (!system) {
|
||||
console.error(err);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "That Short Name does not exist."
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!system.userId.equals(req.user._id)) {
|
||||
res.json({
|
||||
success: false,
|
||||
message: "You are not the user associated with this system."
|
||||
});
|
||||
return;
|
||||
}
|
||||
Talkgroup.find(
|
||||
{
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
function(err, talkgroups) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.json({ success: false, message: err });
|
||||
return;
|
||||
}
|
||||
if (!talkgroups || !Array.isArray(talkgroups)) {
|
||||
console.error(err);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "Talkgroups were not found."
|
||||
});
|
||||
return;
|
||||
}
|
||||
res.statusCode = 200;
|
||||
res.setHeader("Content-Type", "text/csv");
|
||||
talkgroups.forEach(function(item) {
|
||||
/* res.write('"' + item.num + '", "'
|
||||
+ item.mode + '", "'
|
||||
+ item.alpha + '", "'
|
||||
+ item.description + '", "'
|
||||
+ item.tag + '", "'
|
||||
+ item.group + '", "'
|
||||
+ item.priority+ '"\r\n');*/
|
||||
if (item) {
|
||||
var sending = "";
|
||||
sending = item.num + ", ";
|
||||
if (item.mode) {
|
||||
sending = sending + item.mode.toString().replace(/\,/g, "") + ", ";
|
||||
} else {
|
||||
sending = sending + " ,";
|
||||
}
|
||||
if (item.alpa) {
|
||||
sending = sending + item.alpha.toString().replace(/\,/g, "") + ", ";
|
||||
} else {
|
||||
sending = sending + " ,";
|
||||
}
|
||||
if (item.description) {
|
||||
sending = sending + item.description.toString().replace(/\,/g, "") + ", ";
|
||||
} else {
|
||||
sending = sending + " ,";
|
||||
}
|
||||
if (item.tag) {
|
||||
sending = sending + item.tag.toString().replace(/\,/g, "") + ", ";
|
||||
} else {
|
||||
sending = sending + " ,";
|
||||
}
|
||||
if (item.group) {
|
||||
sending = sending + item.group.toString().replace(/\,/g, "") + ", ";
|
||||
} else {
|
||||
sending = sending + " ,";
|
||||
}
|
||||
if (item.priority) {
|
||||
sending = sending + item.priority.toString().replace(/\,/g, "") + "\r\n";
|
||||
} else {
|
||||
sending = sending + "\r\n";
|
||||
}
|
||||
res.write(sending);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
res.end();
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
function csv_import(shortName, filename, callback) {
|
||||
var talkgroups = [];
|
||||
Talkgroup.remove(
|
||||
{
|
||||
shortName: shortName
|
||||
},
|
||||
function(err) {
|
||||
if (err) {
|
||||
console.error("Error: csv import: " + err);
|
||||
callback(err, null);
|
||||
} else {
|
||||
var parser = parse(
|
||||
{
|
||||
columns: [
|
||||
"num",
|
||||
"mode",
|
||||
"alpha",
|
||||
"description",
|
||||
"tag",
|
||||
"group",
|
||||
"priority"
|
||||
]
|
||||
},
|
||||
async function(err, data) {
|
||||
if (err) {
|
||||
console.error("Error: csv import 2: " + err);
|
||||
callback(err, null);
|
||||
} else {
|
||||
var response = [];
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
var row = data[i];
|
||||
row.num = parseInt(row.num);
|
||||
row.tag = row.tag.toLowerCase();
|
||||
row.group = row.group.toLowerCase();
|
||||
var newTg = new Talkgroup();
|
||||
newTg.num = row.num;
|
||||
newTg.alpha = row.alpha;
|
||||
newTg.mode = row.mode;
|
||||
newTg.description = row.description;
|
||||
newTg.tag = row.tag;
|
||||
newTg.group = row.group;
|
||||
newTg.priority = row.priority;
|
||||
newTg.shortName = shortName;
|
||||
|
||||
await newTg.save(function(err) {
|
||||
if (err) {
|
||||
console.log("Error: CSV import: " + err);
|
||||
callback(err, null);
|
||||
}
|
||||
});
|
||||
|
||||
response.push(row);
|
||||
}
|
||||
callback(null, response);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
fs.createReadStream(filename).pipe(parser);
|
||||
// removed!
|
||||
exports.exportTalkgroups = function (req, res, next) {
|
||||
process.nextTick(async function () {
|
||||
const system = await System.findOne({ shortName: req.params.shortName.toLowerCase() });
|
||||
if (!system) {
|
||||
res.status(404);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "That Short Name does not exist."
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!system.userId.equals(req.user._id)) {
|
||||
res.status(401);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "You are not the user associated with this system."
|
||||
});
|
||||
return;
|
||||
}
|
||||
const talkgroups = await Talkgroup.find({ shortName: req.params.shortName.toLowerCase() })
|
||||
|
||||
if (!talkgroups || !Array.isArray(talkgroups)) {
|
||||
console.error(err);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "Talkgroups were not found."
|
||||
});
|
||||
return;
|
||||
}
|
||||
res.statusCode = 200;
|
||||
res.setHeader("Content-Type", "text/csv");
|
||||
talkgroups.forEach(function (item) {
|
||||
/* res.write('"' + item.num + '", "'
|
||||
+ item.mode + '", "'
|
||||
+ item.alpha + '", "'
|
||||
+ item.description + '", "'
|
||||
+ item.tag + '", "'
|
||||
+ item.group + '", "'
|
||||
+ item.priority+ '"\r\n');*/
|
||||
if (item) {
|
||||
var sending = "";
|
||||
sending = item.num + ", ";
|
||||
if (item.mode) {
|
||||
sending = sending + item.mode.toString().replace(/\,/g, "") + ", ";
|
||||
} else {
|
||||
sending = sending + " ,";
|
||||
}
|
||||
if (item.alpa) {
|
||||
sending = sending + item.alpha.toString().replace(/\,/g, "") + ", ";
|
||||
} else {
|
||||
sending = sending + " ,";
|
||||
}
|
||||
if (item.description) {
|
||||
sending = sending + item.description.toString().replace(/\,/g, "") + ", ";
|
||||
} else {
|
||||
sending = sending + " ,";
|
||||
}
|
||||
if (item.tag) {
|
||||
sending = sending + item.tag.toString().replace(/\,/g, "") + ", ";
|
||||
} else {
|
||||
sending = sending + " ,";
|
||||
}
|
||||
if (item.group) {
|
||||
sending = sending + item.group.toString().replace(/\,/g, "") + ", ";
|
||||
} else {
|
||||
sending = sending + " ,";
|
||||
}
|
||||
if (item.priority) {
|
||||
sending = sending + item.priority.toString().replace(/\,/g, "") + "\r\n";
|
||||
} else {
|
||||
sending = sending + "\r\n";
|
||||
}
|
||||
res.write(sending);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
res.end();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
//router.post("/tg_import/:shortName", isLoggedIn, upload.single('file'), function(req, res, next) {
|
||||
exports.importTalkgroups = function(req, res, next) {
|
||||
process.nextTick(function() {
|
||||
System.findOne(
|
||||
{
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
function(err, system) {
|
||||
if (err) {
|
||||
console.error("Error - importTalkgroups: " + err);
|
||||
res.json({ success: false, message: err });
|
||||
return;
|
||||
}
|
||||
if (!system) {
|
||||
console.error("Error - importTalkgroups: System not found - " + req.params.shortName.toLowerCase());
|
||||
res.json({
|
||||
success: false,
|
||||
message: "That Short Name does not exist."
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!system.userId.equals(req.user._id)) {
|
||||
console.error("Error - importTalkgroups: you are not the user for hte system: " + req.user._id);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "You are not the user associated with this system."
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!req.file || req.file.size === 0) {
|
||||
res.json({ success: false, message: "Please select a file." });
|
||||
return;
|
||||
}
|
||||
async function csv_import(shortName, filename, callback) {
|
||||
var talkgroups = [];
|
||||
const count = await Talkgroup.deleteMany({ shortName: shortName });
|
||||
|
||||
fs.exists(req.file.path, function(exists) {
|
||||
if (exists) {
|
||||
csv_import(system.shortName, req.file.path, function(err, data) {
|
||||
if (err) {
|
||||
console.error("Erorr - CSV import, callback: " + err);
|
||||
res.json({ success: false, message: err });
|
||||
res.end();
|
||||
return;
|
||||
} else {
|
||||
res.json({ success: true, talkgroups: data });
|
||||
return;
|
||||
}
|
||||
});
|
||||
var parser = parse(
|
||||
{
|
||||
columns: [
|
||||
"num",
|
||||
"mode",
|
||||
"alpha",
|
||||
"description",
|
||||
"tag",
|
||||
"group",
|
||||
"priority"
|
||||
]
|
||||
},
|
||||
async function (err, data) {
|
||||
if (err) {
|
||||
console.error("Error: csv import 2: " + err);
|
||||
callback(err, null);
|
||||
} else {
|
||||
var response = [];
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
var row = data[i];
|
||||
row.num = parseInt(row.num);
|
||||
row.tag = row.tag.toLowerCase();
|
||||
row.group = row.group.toLowerCase();
|
||||
var newTg = new Talkgroup();
|
||||
newTg.num = row.num;
|
||||
newTg.alpha = row.alpha;
|
||||
newTg.mode = row.mode;
|
||||
newTg.description = row.description;
|
||||
newTg.tag = row.tag;
|
||||
newTg.group = row.group;
|
||||
newTg.priority = row.priority;
|
||||
newTg.shortName = shortName;
|
||||
|
||||
await newTg.save().catch(err => {
|
||||
console.log("Error: CSV import: " + err);
|
||||
callback(err, null);
|
||||
});
|
||||
|
||||
response.push(row);
|
||||
}
|
||||
callback(null, response);
|
||||
}
|
||||
}
|
||||
);
|
||||
fs.createReadStream(filename).pipe(parser);
|
||||
}
|
||||
|
||||
|
||||
//router.post("/tg_import/:shortName", isLoggedIn, upload.single('file'), function(req, res, next) {
|
||||
exports.importTalkgroups = function (req, res, next) {
|
||||
process.nextTick(async function () {
|
||||
const system = System.findOne({ shortName: req.params.shortName.toLowerCase() }).catch(err => {
|
||||
res.status(500);
|
||||
console.error("Error - importTalkgroups: " + err);
|
||||
res.json({ success: false, message: err });
|
||||
return;
|
||||
});
|
||||
|
||||
if (!system) {
|
||||
console.error("Error - importTalkgroups: System not found - " + req.params.shortName.toLowerCase());
|
||||
res.status(404);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "That Short Name does not exist."
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!system.userId.equals(req.user._id)) {
|
||||
console.error("Error - importTalkgroups: you are not the user for hte system: " + req.user._id);
|
||||
res.status(401);
|
||||
res.json({
|
||||
success: false,
|
||||
message: "You are not the user associated with this system."
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!req.file || req.file.size === 0) {
|
||||
res.status(500);
|
||||
res.json({ success: false, message: "Please select a file." });
|
||||
return;
|
||||
}
|
||||
|
||||
fs.exists(req.file.path, function (exists) {
|
||||
if (exists) {
|
||||
csv_import(system.shortName, req.file.path, function (err, data) {
|
||||
if (err) {
|
||||
console.error("Erorr - CSV import, callback: " + err);
|
||||
res.status(500);
|
||||
res.json({ success: false, message: err });
|
||||
res.end();
|
||||
return;
|
||||
} else {
|
||||
console.error("Error - importTalkgroups: no file");
|
||||
res.end(
|
||||
"Well, there is no magic for those who don’t believe in it!"
|
||||
);
|
||||
res.json( data );
|
||||
return;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
res.status(500);
|
||||
console.error("Error - importTalkgroups: no file");
|
||||
res.end(
|
||||
"Well, there is no magic for those who don’t believe in it!"
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
// -------------------------------------------
|
||||
|
||||
exports.fetchTalkgroups = function(req, res, next) {
|
||||
Talkgroup.find(
|
||||
{
|
||||
shortName: req.params.shortName.toLowerCase()
|
||||
},
|
||||
function(err, talkgroups) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.json({ success: false, message: err });
|
||||
return;
|
||||
}
|
||||
res.json({ success: true, talkgroups: talkgroups });
|
||||
return;
|
||||
}
|
||||
);
|
||||
};
|
||||
exports.fetchTalkgroups = async function (req, res, next) {
|
||||
const talkgroups = await Talkgroup.find({ shortName: req.params.shortName.toLowerCase() }).catch(err => {
|
||||
console.error(err);
|
||||
res.status(500);
|
||||
res.json({ success: false, message: err });
|
||||
return;
|
||||
});
|
||||
|
||||
res.json(talkgroups );
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -42,13 +42,7 @@ async function connect() {
|
||||
|
||||
// Connect to a MongoDB server running on 'localhost:27017' and use the
|
||||
// 'test' database.
|
||||
await mongoose.connect(secrets.db, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true }, (err, res) => {
|
||||
if (err) {
|
||||
console.log(`Mongoose - Error connecting to ${secrets.db}. ${err}`)
|
||||
} else {
|
||||
console.log(`Mongoose Successfully connected to ${secrets.db}.`)
|
||||
}
|
||||
})
|
||||
await mongoose.connect(secrets.db, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
console.log("All Done");
|
||||
}
|
||||
connect();
|
||||
|
||||
Reference in New Issue
Block a user