framez-server/Server/API/Routers/UserRouter.js

71 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2018-04-28 14:52:36 +00:00
const express = require("express");
const DBUtils = require('../../Utils/DBUtil');
const AuthUtil = require('../../Utils/AuthUtil');
const Logger = require('../../Utils/Logger');
2018-04-28 14:52:36 +00:00
const router = express.Router();
const TAG = '[UserRouter]'
router.get('/framez', (req, res) => {
const token = req.get('token');
Logger.debug(TAG, 'GET: /framez');
AuthUtil.getAccountByToken(token)
.then(account => {
DBUtils.Models.Frame.find({admin:account._id}, (err, docs) => {
if (err) {
Logger.error(TAG, 'Failed to query DB. ERROR:', err);
res.status(500).json({
message: err.message
});
}
else{
Logger.debug(TAG, 'responding with frames');
let frames = docs.map(f=>{
let frame = f.toObject()
delete frame.viewerKeys
return frame
})
res.json({frames:frames});
}
});
})
.catch(reason => {
Logger.warn(TAG, 'Invalid token');
res.status(401).json({
message: reason
});
})
})
2018-04-28 14:52:36 +00:00
router.get('/:accountId', (req, res) => {
const token = req.get('token');
const accountId = req.params.accountId;
if(!accountId){
res.status(400).json({
message: 'Invalid account id'
});
return;
}
AuthUtil.getAccountByToken(token)
.then(account => {
AuthUtil.getUserByAccountId(accountId)
.then(user => {
res.json(user);
})
.catch(reason => {
res.status(400).json({
message: reason
});
})
})
.catch(reason => {
res.status(401).json({
message: reason
});
})
});
module.exports = router;