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

35 lines
1.3 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;
res.json({status: 'Authorization successful', method: req.authDetails.method});
});
2020-06-05 08:50:06 +02:00
router.get('/changelog/:timestamp/:page?/:pagesize?', (req, res, next) => {
if (!req.auth(res, ['maintain', '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);
res.json(_.compact(data.map(e => RootValidate.changelogOutput(e)))); // validate all and filter null values from validation errors
});
});
2020-01-14 13:25:13 +01:00
module.exports = router;