From b8d5c9f0f6721546477fe961889e9f331e999fa9 Mon Sep 17 00:00:00 2001 From: Maciej Ziarkowski Date: Thu, 14 Nov 2019 12:56:03 +0000 Subject: [PATCH] Add basic global edit history API endpoint --- app/src/api/api.ts | 3 +++ .../api/controllers/editHistoryController.ts | 18 ++++++++++++++++ app/src/api/services/editHistory.ts | 21 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 app/src/api/controllers/editHistoryController.ts create mode 100644 app/src/api/services/editHistory.ts diff --git a/app/src/api/api.ts b/app/src/api/api.ts index fd1c1426..59471b6b 100644 --- a/app/src/api/api.ts +++ b/app/src/api/api.ts @@ -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 diff --git a/app/src/api/controllers/editHistoryController.ts b/app/src/api/controllers/editHistoryController.ts new file mode 100644 index 00000000..27d6a2a9 --- /dev/null +++ b/app/src/api/controllers/editHistoryController.ts @@ -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 +}; diff --git a/app/src/api/services/editHistory.ts b/app/src/api/services/editHistory.ts new file mode 100644 index 00000000..8448449a --- /dev/null +++ b/app/src/api/services/editHistory.ts @@ -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 +};