-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logscontroller.js
169 lines (161 loc) · 7.82 KB
/
Logscontroller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());
var User = require('./User');
var Logs = require('./Logs');
var OpLogs = require('./OperationsLogs');
var jwt = require('jsonwebtoken');
var config = require('./config');
const time = new Date();
////multer as middleware to being able to interact with the body.formdata
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, callback) {
if(config.testingData){
(file) ? console.log('Destination:::::::File::::::',file) : console.log('No file from client');
}
callback(null, __dirname + '/uploads')
},
filename: function (req, file, callback) {
if(config.testingData){
(file) ? console.log('Filename:::::::File::::::',file) : console.log('No file from client');
}
callback(null, file.fieldname + '_' + Date.now() + "_" + file.originalname);
}
});
const upload = multer({ storage: storage }).single("file");
//get logs using a filter
router.get('/getLogs',function(req,res){
const token = req.headers['x-access-token'];
const filter = req.headers['filter'];
if(!filter) return res.status(404),send({ status: 'failed', message: 'I cannot query no filter'});
const parsedFilter = JSON.parse(filter);
if(!token) return res.status(404).send({ auth: false, message: 'No token provided!' });
jwt.verify(token, config.secret, function(err, decoded){
if(err) return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });
if(decoded){
Logs.find(parsedFilter,function(err,founds){
if(err) return res.status(500).send({ status: 'failed', message: err});
return res.status(200).send({ status: 'sucess', result: founds});
});
}else{
return res.status(404).send({ auth: false, message: 'Failed to decode.' });
}
});
});
//get user's logs
router.get('/alllogs', function(req, res){
const time = new Date();
// TODO verify token present on headers
var token = req.headers['x-access-token'];
if(!token) return res.status(404).send({ auth: false, message: 'No token provided!' });
jwt.verify(token, config.secret, function(err, decoded){
if(err) return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });
if(decoded){
//if decoded properly, we get the username of the "admin". Then we check in DB if that user is really usertype = admin.
User.findOne({ username: decoded.usernameHive }, function(err, user){
if(err) return res.status(500).send("There was a problem finding the user -xx.");
if(user){
//it exists at least. now check if his usertype === admin
if(user.usertype === "admin"){
//the user is an admin so we may process finding all the data he asked
//now we may search all users and find the result
Logs.find({}, function(err, logs){
if(err) return res.status(500).send('There was a problem finding the logs.');
if(config.testingData){
console.log('Hi there Admin!');
console.log('Admin looked Up all users logs in DB.', time);
};
res.status(200).send(logs);
});
}else{
//no admin so send error
if(config.testingData){
console.log('F.O you are not an admin! Go code.');
}
return res.status(500).send({ auth: false, message: 'Error authenticating "admin" user.' });
}
}else{
//
return res.status(500).send({ auth: false, message: 'Error no user found on that credentials.' });
}
});
}else{
return res.status(500).send({ auth: false, message: 'Error authenticating token GET logs.' });
}
});
});
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
/////////////Add OP logs
///from now on we use the logs as main log handler and schema.
router.post('/addOp',function(req, res){
const time = new Date();
// TODO verify token present on headers
var token = req.headers['x-access-token'];
if(!token) return res.status(500).send({ auth: false, message: 'No token provided!' });
jwt.verify(token, config.secret, function(err, decoded){
if(err) return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });
if(decoded){
upload(req, res, function(err){
if(err) return res.status(500).send({ status: 'failed', message: err });
Logs.create(req.body,function(err, newLog){
if(err){
console.log('Error trying to add new Log on DB!',err);
return res.status(500).send({ status: 'failed', message: err });
}
if(newLog){
return res.status(200).send({ result: "sucess", result: `Log added. ${newLog._id}`});
}
});
});
}else{
return res.status(404).send({ status: 'failed', message: 'Failed to decode token.'});
}
});
})
/////////////END - Add OP logs
///////////////////////////////////////////////////////////////////////
// get a single user from DB
// router.get('/:id', function(req, res){
// if(checkId(req.params.id.toString())){
// User.findById(req.params.id, function(err, user){
// if(err) return res.status(500).send("There was a problem finding the user." + "\n" + err);
// if (!user) return res.status(404).send("No user found.");
// res.status(200).send(user);
// console.log(`Searched User on DB. \n name:${user.name} \n id:${user.id}`);
// });
// }else {
// console.log("A wrong ID formatted query was trying to reach the server's DB");
// return res.status(404).send("Id format is not as required. Please use correct one!");
// }
// })
// deletes a user from DB
// router.delete('/:id', function(req, res){
// if(checkId(req.params.id.toString())){
// User.findByIdAndRemove(req.params.id, function(err, user){
// if(err) return res.status(500).send("There was a problem deleting the user");
// res.status(200).send("User " + user.name + " was deleted.");
// console.log(`Deleted User on DB. \n name:${user.name} \n id:${user.id}`);
// });
// }else {
// console.log("A wrong ID formatted query was trying to reach the server's DB");
// return res.status(404).send("Id format is not as required. Please use correct one!");
// }
// });
// updates a single user in DB
// router.put('/:id', function(req, res){
// if(checkId(req.params.id.toString())){
// User.findByIdAndUpdate(req.params.id, req.body, {new: true}, function(err, user){
// if(err) return res.status(500).send("There was a problem updating the user.");
// res.status(200).send(user);
// console.log(`Updated User on DB. \n name:${user.name} \n id:${user.id}`);
// });
// }else {
// console.log("A wrong ID formatted query was trying to reach the server's DB");
// return res.status(404).send("Id format is not as required. Please use correct one!");
// }
// });
module.exports = router;