diff --git a/src/routes/routes.js b/src/routes/routes.js index 0f109aa3d..716ba432e 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -101,7 +101,12 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio if (resultHandler) { if (result && result.then) { - result.then(actualResult => resultHandler(req, res, actualResult)) + result + .then(actualResult => { + resultHandler(req, res, actualResult); + + log.request(req, res, Date.now() - start); + }) .catch(e => { log.error(`${method} ${path} threw exception: ` + e.stack); @@ -110,6 +115,8 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio } else { resultHandler(req, res, result); + + log.request(req, res, Date.now() - start); } } } @@ -118,8 +125,6 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio res.status(500).send(e.message); } - - log.request(req, res, Date.now() - start); }); } @@ -239,7 +244,7 @@ function register(app) { // group of services below are meant to be executed from outside route(GET, '/api/setup/status', [], setupApiRoute.getStatus, apiResultHandler); - route(POST, '/api/setup/new-document', [auth.checkAppNotInitialized], setupApiRoute.setupNewDocument, apiResultHandler); + route(POST, '/api/setup/new-document', [auth.checkAppNotInitialized], setupApiRoute.setupNewDocument, apiResultHandler, false); route(POST, '/api/setup/sync-from-server', [auth.checkAppNotInitialized], setupApiRoute.setupSyncFromServer, apiResultHandler, false); route(GET, '/api/setup/sync-seed', [auth.checkCredentials], setupApiRoute.getSyncSeed, apiResultHandler); route(POST, '/api/setup/sync-seed', [auth.checkAppNotInitialized], setupApiRoute.saveSyncSeed, apiResultHandler, false); diff --git a/src/services/options_init.js b/src/services/options_init.js index 81ac7b53c..e7a55cff2 100644 --- a/src/services/options_init.js +++ b/src/services/options_init.js @@ -27,10 +27,10 @@ function initSyncedOptions(username, password) { passwordEncryptionService.setDataKey(password, utils.randomSecureToken(16), true); } -function initNotSyncedOptions(initialized, startNotePath = 'root', opts = {}) { +function initNotSyncedOptions(initialized, opts = {}) { optionService.createOption('openTabs', JSON.stringify([ { - notePath: startNotePath, + notePath: 'root', active: true } ]), false); @@ -98,7 +98,7 @@ function initStartupOptions() { if (!(name in optionsMap)) { optionService.createOption(name, value, isSynced); - log.info(`Created missing option "${name}" with default value "${value}"`); + log.info(`Created option "${name}" with default value "${value}"`); } } diff --git a/src/services/sql_init.js b/src/services/sql_init.js index bff5001d8..dcb74ff02 100644 --- a/src/services/sql_init.js +++ b/src/services/sql_init.js @@ -46,7 +46,7 @@ async function initDbConnection() { } async function createInitialDatabase(username, password, theme) { - log.info("Creating initial database ..."); + log.info("Creating database schema ..."); if (isDbInitialized()) { throw new Error("DB is already initialized"); @@ -57,6 +57,8 @@ async function createInitialDatabase(username, password, theme) { let rootNote; + log.info("Creating root note ..."); + sql.transactional(() => { sql.executeScript(schema); @@ -81,21 +83,36 @@ async function createInitialDatabase(username, password, theme) { isExpanded: true, notePosition: 10 }).save(); + + const optionsInitService = require('./options_init'); + + optionsInitService.initDocumentOptions(); + optionsInitService.initSyncedOptions(username, password); + optionsInitService.initNotSyncedOptions(true, { theme }); + optionsInitService.initStartupOptions(); }); + log.info("Importing demo content ..."); + const dummyTaskContext = new TaskContext("initial-demo-import", 'import', false); const zipImportService = require("./import/zip"); await zipImportService.importZip(dummyTaskContext, demoFile, rootNote); sql.transactional(() => { + // this needs to happen after ZIP import + // previous solution was to move option initialization here but then the important parts of initialization + // are not all in one transaction (because ZIP import is async and thus not transactional) + const startNoteId = sql.getValue("SELECT noteId FROM branches WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition"); - const optionsInitService = require('./options_init'); - - optionsInitService.initDocumentOptions(); - optionsInitService.initSyncedOptions(username, password); - optionsInitService.initNotSyncedOptions(true, startNoteId, { theme }); + const optionService = require("./options"); + optionService.setOption('openTabs', JSON.stringify([ + { + notePath: startNoteId, + active: true + } + ])); }); log.info("Schema and initial content generated."); @@ -115,7 +132,7 @@ function createDatabaseForSync(options, syncServerHost = '', syncProxy = '') { sql.transactional(() => { sql.executeScript(schema); - require('./options_init').initNotSyncedOptions(false, 'root', { syncServerHost, syncProxy }); + require('./options_init').initNotSyncedOptions(false, { syncServerHost, syncProxy }); // document options required for sync to kick off for (const opt of options) {