55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
import express from 'express';
|
|
import HelpModel from '../models/help';
|
|
import HelpValidate from './validate/help';
|
|
import res400 from './validate/res400';
|
|
import globals from '../globals';
|
|
|
|
const router = express.Router();
|
|
|
|
router.get('/help/:key', (req, res, next) => {
|
|
const {error: paramError, value: key} = HelpValidate.params(req.params);
|
|
if (paramError) return res400(paramError, res);
|
|
|
|
HelpModel.findOne(key).lean().exec((err, data) => {
|
|
if (err) return next(err);
|
|
|
|
if (!data) {
|
|
return res.status(404).json({status: 'Not found'});
|
|
}
|
|
if (data.level !== 'none') { // check level
|
|
if (!req.auth(res,
|
|
Object.values(globals.levels).slice(Object.values(globals.levels).findIndex(e => e === data.level))
|
|
, 'basic')) return;
|
|
}
|
|
res.json(HelpValidate.output(data));
|
|
})
|
|
});
|
|
|
|
router.post('/help/:key', (req, res, next) => {
|
|
if (!req.auth(res, ['dev', 'admin'], 'basic')) return;
|
|
const {error: paramError, value: key} = HelpValidate.params(req.params);
|
|
if (paramError) return res400(paramError, res);
|
|
const {error, value: help} = HelpValidate.input(req.body);
|
|
if (error) return res400(error, res);
|
|
|
|
HelpModel.findOneAndUpdate(key, help, {upsert: true}).log(req).lean().exec(err => {
|
|
if (err) return next(err);
|
|
res.json({status: 'OK'});
|
|
});
|
|
});
|
|
|
|
router.delete('/help/:key', (req, res, next) => {
|
|
if (!req.auth(res, ['dev', 'admin'], 'basic')) return;
|
|
const {error: paramError, value: key} = HelpValidate.params(req.params);
|
|
if (paramError) return res400(paramError, res);
|
|
|
|
HelpModel.findOneAndDelete(key).log(req).lean().exec((err, data) => {
|
|
if (err) return next(err);
|
|
if (!data) {
|
|
return res.status(404).json({status: 'Not found'});
|
|
}
|
|
res.json({status: 'OK'});
|
|
});
|
|
});
|
|
|
|
module.exports = router; |