converted settings, note revisions, password change and recent changes routes

This commit is contained in:
azivner
2018-03-30 13:56:46 -04:00
parent 8550ed72f2
commit 9edee9340b
8 changed files with 61 additions and 92 deletions

View File

@@ -1,31 +1,16 @@
"use strict";
const express = require('express');
const router = express.Router();
const sql = require('../../services/sql');
const auth = require('../../services/auth');
const protected_session = require('../../services/protected_session');
const sync_table = require('../../services/sync_table');
const wrap = require('express-promise-wrap').wrap;
router.get('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
async function getNoteRevisions(req) {
const noteId = req.params.noteId;
const revisions = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]);
protected_session.decryptNoteRevisions(req, revisions);
res.send(revisions);
}));
return revisions;
}
router.put('', auth.checkApiAuth, wrap(async (req, res, next) => {
const sourceId = req.headers.source_id;
await sql.doInTransaction(async () => {
await sql.replace("note_revisions", req.body);
await sync_table.addNoteRevisionSync(req.body.noteRevisionId, sourceId);
});
res.send();
}));
module.exports = router;
module.exports = {
getNoteRevisions
};

View File

@@ -1,16 +1,11 @@
"use strict";
const express = require('express');
const router = express.Router();
const sql = require('../../services/sql');
const changePassword = require('../../services/change_password');
const auth = require('../../services/auth');
const wrap = require('express-promise-wrap').wrap;
const changePasswordService = require('../../services/change_password');
router.post('/change', auth.checkApiAuth, wrap(async (req, res, next) => {
const result = await changePassword.changePassword(req.body['current_password'], req.body['new_password'], req);
async function changePassword(req) {
return await changePasswordService.changePassword(req.body['current_password'], req.body['new_password'], req);
}
res.send(result);
}));
module.exports = router;
module.exports = {
changePassword
};

View File

@@ -1,12 +1,8 @@
"use strict";
const express = require('express');
const router = express.Router();
const sql = require('../../services/sql');
const auth = require('../../services/auth');
const wrap = require('express-promise-wrap').wrap;
router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => {
async function getRecentChanges() {
const recentChanges = await sql.getRows(
`SELECT
notes.isDeleted AS current_isDeleted,
@@ -19,7 +15,9 @@ router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => {
dateModifiedTo DESC
LIMIT 1000`);
res.send(recentChanges);
}));
return recentChanges;
}
module.exports = router;
module.exports = {
getRecentChanges
};

View File

@@ -1,44 +1,35 @@
"use strict";
const express = require('express');
const router = express.Router();
const sql = require('../../services/sql');
const options = require('../../services/options');
const auth = require('../../services/auth');
const wrap = require('express-promise-wrap').wrap;
// options allowed to be updated directly in settings dialog
const ALLOWED_OPTIONS = ['protected_session_timeout', 'note_revision_snapshot_time_interval'];
router.get('/all', auth.checkApiAuth, wrap(async (req, res, next) => {
const settings = await sql.getMap("SELECT name, value FROM options");
async function getAllSettings() {
return await sql.getMap("SELECT name, value FROM options");
}
res.send(settings);
}));
router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => {
async function getAllowedSettings() {
const settings = await sql.getMap("SELECT name, value FROM options WHERE name IN ("
+ ALLOWED_OPTIONS.map(x => '?').join(",") + ")", ALLOWED_OPTIONS);
res.send(settings);
}));
return settings;
}
router.post('/', auth.checkApiAuth, wrap(async (req, res, next) => {
async function updateSetting(req) {
const body = req.body;
const sourceId = req.headers.source_id;
if (ALLOWED_OPTIONS.includes(body['name'])) {
const optionName = await options.getOption(body['name']);
await sql.doInTransaction(async () => {
await options.setOption(body['name'], body['value'], sourceId);
});
res.send({});
if (!ALLOWED_OPTIONS.includes(body['name'])) {
return [400, "not allowed option to set"];
}
else {
res.send("not allowed option to set");
}
}));
module.exports = router;
await options.setOption(body['name'], body['value'], sourceId);
}
module.exports = {
getAllowedSettings,
getAllSettings,
updateSetting
};