Archived
2
This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
definma-api/src/routes/root.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-01-14 13:25:13 +01:00
import express from 'express';
2020-04-23 13:59:45 +02:00
import globals from '../globals';
2020-06-05 08:50:06 +02:00
import RootValidate from './validate/root';
import res400 from './validate/res400';
import ChangelogModel from '../models/changelog';
import mongoose from 'mongoose';
import _ from 'lodash';
2020-01-14 13:25:13 +01:00
const router = express.Router();
router.get('/', (req, res) => {
2020-04-22 17:38:24 +02:00
res.json({status: 'API server up and running!'});
2020-01-14 13:25:13 +01:00
});
2020-04-23 13:59:45 +02:00
router.get('/authorized', (req, res) => {
if (!req.auth(res, globals.levels)) return;
2020-08-04 13:54:14 +02:00
res.json({
status: 'Authorization successful',
method: req.authDetails.method,
level: req.authDetails.level,
user_id: req.authDetails.id
});
2020-04-23 13:59:45 +02:00
});
2020-08-04 13:54:14 +02:00
// TODO: evaluate exact changelog functionality (restoring, deleting after time, etc.)
2020-06-05 08:50:06 +02:00
router.get('/changelog/:timestamp/:page?/:pagesize?', (req, res, next) => {
if (!req.auth(res, ['dev', 'admin'], 'basic')) return;
2020-06-05 08:50:06 +02:00
2020-08-04 13:54:14 +02:00
const {error, value: options} = RootValidate.changelogParams({
timestamp: req.params.timestamp,
page: req.params.page,
pagesize: req.params.pagesize
});
2020-06-05 08:50:06 +02:00
if (error) return res400(error, res);
2020-08-04 13:54:14 +02:00
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) => {
2020-06-05 08:50:06 +02:00
if (err) return next(err);
2020-08-04 13:54:14 +02:00
// validate all and filter null values from validation errors
res.json(_.compact(data.map(e => RootValidate.changelogOutput(e))));
2020-06-05 08:50:06 +02:00
});
});
2020-01-14 13:25:13 +01:00
module.exports = router;