import express from 'express'; import globals from '../globals'; import RootValidate from './validate/root'; import res400 from './validate/res400'; import ChangelogModel from '../models/changelog'; import mongoose from 'mongoose'; import _ from 'lodash'; const router = express.Router(); router.get('/', (req, res) => { res.json({status: 'API server up and running!'}); }); router.get('/authorized', (req, res) => { if (!req.auth(res, globals.levels)) return; res.json({ status: 'Authorization successful', method: req.authDetails.method, level: req.authDetails.level, user_id: req.authDetails.id }); }); // TODO: evaluate exact changelog functionality (restoring, deleting after time, etc.) router.get('/changelog/:timestamp/:page?/:pagesize?', (req, res, next) => { if (!req.auth(res, ['dev', 'admin'], 'basic')) return; const {error, value: options} = RootValidate.changelogParams({ timestamp: req.params.timestamp, page: req.params.page, pagesize: req.params.pagesize }); if (error) return res400(error, res); const id = new mongoose.Types .ObjectId(Math.floor(new Date(options.timestamp).getTime() / 1000).toString(16) + '0000000000000000'); ChangelogModel.find({_id: {$lte: id}}).sort({_id: -1}).skip(options.page * options.pagesize).limit(options.pagesize) .lean().exec((err, data) => { if (err) return next(err); // validate all and filter null values from validation errors res.json(_.compact(data.map(e => RootValidate.changelogOutput(e)))); }); }); module.exports = router;