Add basic global edit history API endpoint

This commit is contained in:
Maciej Ziarkowski 2019-11-14 12:56:03 +00:00
parent 59b04d2d93
commit b8d5c9f0f6
3 changed files with 42 additions and 0 deletions

View File

@ -1,6 +1,7 @@
import bodyParser from 'body-parser';
import express from 'express';
import * as editHistoryController from './controllers/editHistoryController';
import buildingsRouter from './routes/buildingsRouter';
import extractsRouter from './routes/extractsRouter';
import usersRouter from './routes/usersRouter';
@ -17,6 +18,8 @@ server.use('/buildings', buildingsRouter);
server.use('/users', usersRouter);
server.use('/extracts', extractsRouter);
server.get('/history', editHistoryController.getGlobalEditHistory);
// POST user auth
server.post('/login', function (req, res) {
authUser(req.body.username, req.body.password).then(function (user: any) { // TODO: remove any

View File

@ -0,0 +1,18 @@
import express from 'express';
import asyncController from "../routes/asyncController";
import * as editHistoryService from '../services/editHistory';
const getGlobalEditHistory = asyncController(async (req: express.Request, res: express.Response) => {
try {
const result = await editHistoryService.getGlobalEditHistory();
res.send(result);
} catch(error) {
console.error(error);
res.send({ error: 'Database error' });
}
});
export {
getGlobalEditHistory
};

View File

@ -0,0 +1,21 @@
import db from '../../db';
async function getGlobalEditHistory() {
try {
return await db.manyOrNone(
`SELECT log_id as revision_id, forward_patch, reverse_patch, date_trunc('minute', log_timestamp), username, building_id
FROM logs, users
WHERE logs.user_id = users.user_id
AND log_timestamp >= now() - interval '21 days'
ORDER BY log_timestamp DESC`
);
} catch (error) {
console.error(error);
return [];
}
}
export {
getGlobalEditHistory
};