import { JsonController , UseBefore , Authorized , Param , BodyParam , Get , Put , UnauthorizedError } from 'routing-controllers' ; import { Config } from '../models/Config' ; import passportJwtMiddleware from '../security/passportJwtMiddleware' ; function isPublicConfig ( name ) { return /^legalnotice$|^infoBox$|^privacy$|^downloadMaxFileSize$/ . test ( name ) ; } @ JsonController ( '/config' ) export class ConfigController { private async findConfig ( name ) { const config = await Config . findOne ( { name } ) ; return config ? config . toObject ( ) : { name , value : '' } ; } api apiName apiGroup apiParam apiSuccess apiSuccessExample apiError @ Get ( '/public/:id' ) getPublicConfig ( @ Param ( 'id' ) name ) { if ( ! isPublicConfig ( name ) ) { throw new UnauthorizedError ( ) ; } return this . findConfig ( name ) ; } api apiName apiGroup apiPermission apiParam apiParam apiSuccess apiSuccessExample @ UseBefore ( passportJwtMiddleware ) @ Authorized ( [ 'admin' ] ) @ Put ( '/:id' ) async putConfig ( @ Param ( 'id' ) name , @ BodyParam ( 'data' ) value ) { await Config . findOneAndUpdate ( { name } , { name , value } , { 'upsert' : true , 'new' : true } ) ; return { } ; } api apiName apiGroup apiPermission apiParam apiSuccess apiSuccessExample @ UseBefore ( passportJwtMiddleware ) @ Authorized ( [ 'admin' ] ) @ Get ( '/:id' ) getConfig ( @ Param ( 'id' ) name ) { return this . findConfig ( name ) ; } }