partially completed api.js
parent
225047e24f
commit
de4f013bb4
|
@ -175,6 +175,10 @@ async function getDrives(dongleId, includeDeleted, includeMeta) {
|
|||
return orm.models.drives.findAll(query);
|
||||
}
|
||||
|
||||
async function getDriveFromidentifier(dongleId, identifier) {
|
||||
return orm.models.drives.findAll({ where: { dongle_id: dongleId, identifier } });
|
||||
}
|
||||
|
||||
/*
|
||||
TODO: ADD AUTHENTICATION TO ENDPOINTS
|
||||
*/
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
/* eslint-disable */
|
||||
const router = require('express').Router();
|
||||
const bodyParser = require('body-parser');
|
||||
const crypto = require('crypto');
|
||||
const config = require('../config');
|
||||
const deviceController = require('./../controllers/devices')
|
||||
const userController = require('./../controllers/users');
|
||||
const authenticationController = require('./../controllers/authentication')
|
||||
|
||||
function runAsyncWrapper(callback) {
|
||||
return function wrapper(req, res, next) {
|
||||
|
@ -59,7 +63,10 @@ router.get('/v1.1/devices/:dongleId/', runAsyncWrapper(async (req, res) => {
|
|||
const { dongleId } = req.params;
|
||||
logger.info(`HTTP.DEVICES called for ${req.params.dongleId}`);
|
||||
|
||||
const device = await models.drivesModel.getDevice(dongleId);
|
||||
|
||||
|
||||
|
||||
const device = deviceController.getDeviceFromDongle(dongleId);
|
||||
|
||||
if (!device) {
|
||||
logger.info(`HTTP.DEVICES device ${dongleId} not found`);
|
||||
|
@ -99,7 +106,7 @@ router.get('/v1.1/devices/:dongleId/stats', runAsyncWrapper(async (req, res) =>
|
|||
},
|
||||
};
|
||||
|
||||
const device = await models.drivesModel.getDevice(dongleId);
|
||||
const device = await deviceController.getDeviceFromDongle(dongleId);
|
||||
if (!device) {
|
||||
logger.info(`HTTP.STATS device ${dongleId} not found`);
|
||||
return res.status(404).json('Not found.');
|
||||
|
@ -114,7 +121,9 @@ router.get('/v1.1/devices/:dongleId/stats', runAsyncWrapper(async (req, res) =>
|
|||
return res.status(400).send('Unauthorized.');
|
||||
}
|
||||
|
||||
const statresult = await models.__db.get('SELECT COUNT(*) as routes, ROUND(SUM(distance_meters)/1609.34) as distance, ROUND(SUM(duration)/60) as duration FROM drives WHERE dongle_id=?', device.dongle_id);
|
||||
// TODO reimplement weekly stats
|
||||
|
||||
/*const statresult = await models.__db.get('SELECT COUNT(*) as routes, ROUND(SUM(distance_meters)/1609.34) as distance, ROUND(SUM(duration)/60) as duration FROM drives WHERE dongle_id=?', device.dongle_id);
|
||||
if (statresult != null && statresult.routes != null) {
|
||||
stats.all.routes = statresult.routes;
|
||||
stats.all.distance = statresult.distance != null ? statresult.distance : 0;
|
||||
|
@ -133,7 +142,7 @@ router.get('/v1.1/devices/:dongleId/stats', runAsyncWrapper(async (req, res) =>
|
|||
stats.week.routes = statresultweek.routes;
|
||||
stats.week.distance = statresultweek.distance != null ? statresultweek.distance : 0;
|
||||
stats.week.minutes = statresultweek.duration != null ? statresultweek.duration : 0;
|
||||
}
|
||||
}*/
|
||||
|
||||
logger.info(`HTTP.STATS for ${req.params.dongleId} returning: ${JSON.stringify(stats)}`);
|
||||
return res.status(200).json(stats);
|
||||
|
@ -144,7 +153,7 @@ router.get('/v1/devices/:dongleId/owner', runAsyncWrapper(async (req, res) => {
|
|||
const { dongleId } = req.params;
|
||||
logger.info(`HTTP.OWNER called for ${req.params.dongleId}`);
|
||||
|
||||
const device = await models.drivesModel.getDevice(dongleId);
|
||||
const device = await deviceController.getDeviceFromDongle(dongleId);
|
||||
|
||||
if (!device) {
|
||||
logger.info(`HTTP.OWNER device ${dongleId} not found`);
|
||||
|
@ -163,13 +172,15 @@ router.get('/v1/devices/:dongleId/owner', runAsyncWrapper(async (req, res) => {
|
|||
let owner = '';
|
||||
let points = 0;
|
||||
|
||||
const account = await models.__db.get('SELECT * FROM accounts WHERE id = ?', device.account_id);
|
||||
if (account != null) {
|
||||
let account = await userController.getAccountFromId(device.account_id);
|
||||
if (account != null && account.dataValues != null) {
|
||||
account = account.dataValues
|
||||
[owner] = account.email.split('@');
|
||||
const stats = await models.__db.all('SELECT SUM(distance_meters) as points FROM drives WHERE dongle_id IN (SELECT dongle_id FROM devices WHERE account_id=?)', account.id);
|
||||
// TODO reimplement "points"
|
||||
/*const stats = await models.__db.all('SELECT SUM(distance_meters) as points FROM drives WHERE dongle_id IN (SELECT dongle_id FROM devices WHERE account_id=?)', account.id);
|
||||
if (stats != null && stats.points != null) {
|
||||
points = stats.points;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
const response = { username: owner, points };
|
||||
|
@ -184,14 +195,14 @@ async function upload(req, res) {
|
|||
const auth = req.headers.authorization;
|
||||
logger.info(`HTTP.UPLOAD_URL called for ${req.params.dongleId} and file ${path}: ${JSON.stringify(req.headers)}`);
|
||||
|
||||
const device = await models.drivesModel.getDevice(dongleId);
|
||||
const device = await deviceController.getDeviceFromDongle(dongleId);
|
||||
if (!device) {
|
||||
logger.info(`HTTP.UPLOAD_URL device ${dongleId} not found or not linked to an account / refusing uploads`);
|
||||
return res.send('Unauthorized.').status(400);
|
||||
}
|
||||
|
||||
const decoded = device.public_key
|
||||
? await controllers.authentication.validateJWT(req.headers.authorization, device.public_key)
|
||||
? await authenticationController.validateJWT(req.headers.authorization, device.public_key)
|
||||
: null;
|
||||
|
||||
if ((!decoded || decoded.identity !== req.params.dongleId)) {
|
||||
|
@ -199,7 +210,7 @@ async function upload(req, res) {
|
|||
return res.send('Unauthorized.').status(400);
|
||||
}
|
||||
|
||||
await models.drivesModel.deviceCheckIn(dongleId);
|
||||
deviceController.updateLastPing(dongleId);
|
||||
|
||||
let responseUrl = null;
|
||||
const ts = Date.now(); // we use this to make sure old URLs cannot be reused (timeout after 60min)
|
||||
|
@ -256,7 +267,7 @@ async function upload(req, res) {
|
|||
responseUrl = `${config.baseUploadUrl}?file=${filename}&dir=${directory}&dongleId=${dongleId}&ts=${ts}&token=${token}`;
|
||||
logger.info(`HTTP.UPLOAD_URL matched 'drive' file upload, constructed responseUrl: ${responseUrl}`);
|
||||
|
||||
const drive = await models.__db.get('SELECT * FROM drives WHERE identifier = ? AND dongle_id = ?', driveName, dongleId);
|
||||
const drive = await deviceController.getDriveFromidentifier(dongleId, driveName);
|
||||
|
||||
if (drive == null) {
|
||||
// create a new drive
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
/* eslint-disable global-require */
|
||||
module.exports = (_models, _controllers, _logger) => ({
|
||||
module.exports = {
|
||||
useradmin: require('./useradmin')(_models, _controllers, _logger),
|
||||
api: require('./api')(_models, _controllers, _logger),
|
||||
api: require('./api'),
|
||||
useradminapi: require('./userAdminApi'),
|
||||
admin: require('./administration/adminApi'),
|
||||
realtime: require('./api/realtime'),
|
||||
|
||||
deviceApi: require('./api/devices'),
|
||||
authenticationApi: require('./api/authentication'),
|
||||
});
|
||||
};
|
||||
|
|
|
@ -4,6 +4,7 @@ const bodyParser = require('body-parser');
|
|||
const cookieParser = require('cookie-parser');
|
||||
const config = require('../config');
|
||||
const controllers = require('../controllers');
|
||||
const deviceController = require('../controllers/devices');
|
||||
|
||||
// TODO Remove this, pending on removing all auth logic from routes
|
||||
router.use(cookieParser());
|
||||
|
@ -189,20 +190,18 @@ router.get('/retropilot/0/unpair_device/:dongleId', runAsyncWrapper(async (req,
|
|||
if (account == null) {
|
||||
return res.json({ success: false, data: { session: false } }).status(403);
|
||||
}
|
||||
|
||||
const device = await models.__db.get('SELECT * FROM devices WHERE account_id = ? AND dongle_id = ?', account.id, req.params.dongleId);
|
||||
|
||||
if (device == null) {
|
||||
return res.json({ success: false }).status(400);
|
||||
}
|
||||
|
||||
await models.__db.run(
|
||||
'UPDATE devices SET account_id = ? WHERE dongle_id = ?',
|
||||
0,
|
||||
req.params.dongleId,
|
||||
);
|
||||
const pairDeviceToAccountId = await deviceController.pairDeviceToAccountId(req.prams.dongleId, 0);
|
||||
|
||||
return res.json({ success: true, data: { unlink: true } });
|
||||
if (pairDeviceToAccountId.success && pairDeviceToAccountId.paired) {
|
||||
return res.json({ success: true, data: { unlink: true } });
|
||||
}
|
||||
return res.json({ success: true, data: { unlink: false } });
|
||||
}));
|
||||
|
||||
router.post('/retropilot/0/pair_device', bodyParser.urlencoded({ extended: true }), runAsyncWrapper(async (req, res) => {
|
||||
|
@ -506,10 +505,4 @@ router.get('/useradmin/drive/:dongleId/:driveIdentifier', runAsyncWrapper(async
|
|||
}))
|
||||
*/
|
||||
|
||||
module.exports = (_models, _controllers, _logger) => {
|
||||
models = _models;
|
||||
controllers = _controllers;
|
||||
logger = _logger;
|
||||
|
||||
return router;
|
||||
};
|
||||
module.exports = router;
|
||||
|
|
Loading…
Reference in New Issue