Created
August 1, 2018 10:12
-
-
Save tlaitinen/b54e5c16462a58f4d90be312331b4c70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {IBaseProtocol} from 'pg-promise'; | |
| import cfg from '../app-config'; | |
| import * as pgp from 'pg-promise'; | |
| import * as types from './admin-api-types'; | |
| import {Auth} from './auth'; | |
| import {InvalidRequest, AccessDenied} from './errors'; | |
| import * as path from 'path'; | |
| const requestTypes:string[] = types.RequestT.types.map(rt => rt.props.type.value); | |
| const sql:{[requestType:string]:pgp.QueryFile} = {}; | |
| requestTypes.forEach(rt => { | |
| sql[rt] = new pgp.QueryFile(path.join(__dirname, 'sql', 'admin', rt + '.sql'), { | |
| minify: true, | |
| debug: cfg.debug | |
| }); | |
| }); | |
| export function processRequest(db:IBaseProtocol<any>, auth:Auth, request:types.Request):Promise<types.Response> { | |
| return db.tx(async tx => { | |
| if (!auth.admin) { | |
| throw new AccessDenied('only for admin'); | |
| } | |
| const payload = Object.assign({}, request.props, auth); | |
| return tx.oneOrNone(sql[request.type], payload); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment