2017-12-04 11:29:23 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-03 09:25:20 +08:00
|
|
|
const sqlInit = require('../../services/sql_init');
|
2018-07-24 14:12:36 +08:00
|
|
|
const setupService = require('../../services/setup');
|
2018-07-25 15:46:57 +08:00
|
|
|
const optionService = require('../../services/options');
|
|
|
|
const syncService = require('../../services/sync');
|
|
|
|
const log = require('../../services/log');
|
|
|
|
const rp = require('request-promise');
|
2017-12-04 11:29:23 +08:00
|
|
|
|
2018-07-23 01:56:20 +08:00
|
|
|
async function setupNewDocument(req) {
|
2017-12-04 11:29:23 +08:00
|
|
|
const { username, password } = req.body;
|
|
|
|
|
2018-07-21 14:55:24 +08:00
|
|
|
await sqlInit.createInitialDatabase(username, password);
|
2018-03-31 05:07:41 +08:00
|
|
|
}
|
2017-12-04 11:29:23 +08:00
|
|
|
|
2018-07-23 01:56:20 +08:00
|
|
|
async function setupSyncFromServer(req) {
|
2018-07-25 14:30:41 +08:00
|
|
|
const { syncServerHost, syncProxy, username, password } = req.body;
|
2018-07-23 01:56:20 +08:00
|
|
|
|
2018-07-25 14:30:41 +08:00
|
|
|
return await setupService.setupSyncFromSyncServer(syncServerHost, syncProxy, username, password);
|
2018-07-23 01:56:20 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:46:57 +08:00
|
|
|
async function setupSyncToSyncServer() {
|
|
|
|
log.info("Initiating sync to server");
|
|
|
|
|
|
|
|
const syncServerHost = await optionService.getOption('syncServerHost');
|
|
|
|
const syncProxy = await optionService.getOption('syncProxy');
|
|
|
|
|
|
|
|
const rpOpts = {
|
|
|
|
uri: syncServerHost + '/api/setup/sync-seed',
|
|
|
|
method: 'POST',
|
|
|
|
json: true,
|
|
|
|
body: {
|
|
|
|
options: await setupService.getSyncSeedOptions()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (syncProxy) {
|
|
|
|
rpOpts.proxy = syncProxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
await rp(rpOpts);
|
|
|
|
|
|
|
|
// this is completely new sync, need to reset counters. If this would not be new sync,
|
|
|
|
// the previous request would have failed.
|
|
|
|
await optionService.setOption('lastSyncedPush', 0);
|
|
|
|
await optionService.setOption('lastSyncedPull', 0);
|
|
|
|
|
|
|
|
syncService.sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function saveSyncSeed(req) {
|
2018-07-25 02:35:03 +08:00
|
|
|
const options = req.body.options;
|
|
|
|
|
|
|
|
await sqlInit.createDatabaseForSync(options);
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:46:57 +08:00
|
|
|
async function getSyncSeed() {
|
|
|
|
log.info("Serving sync seed.");
|
|
|
|
|
|
|
|
return await setupService.getSyncSeedOptions();
|
|
|
|
}
|
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
module.exports = {
|
2018-07-23 01:56:20 +08:00
|
|
|
setupNewDocument,
|
2018-07-25 02:35:03 +08:00
|
|
|
setupSyncFromServer,
|
2018-07-25 15:46:57 +08:00
|
|
|
setupSyncToSyncServer,
|
|
|
|
getSyncSeed,
|
|
|
|
saveSyncSeed
|
2018-03-31 05:07:41 +08:00
|
|
|
};
|