const SyncEngine = require('../../util/sync-engine'); module.exports = Self => { Self.remoteMethod('sync', { description: 'Synchronizes the user with the other user databases', accepts: [ { arg: 'userName', type: 'string', description: 'The user name', required: true }, { arg: 'password', type: 'string', description: 'The password' } ], http: { path: `/:userName/sync`, verb: 'PATCH' } }); Self.sync = async function(userName, password) { let $ = Self.app.models; let user = await $.Account.findOne({ fields: ['id'], where: {name: userName} }); let isSync = !await $.UserSync.exists(userName); if (user && isSync) return; let err; let se = new SyncEngine(); await se.init($); try { await se.sync(userName, password, true); await $.UserSync.destroyById(userName); } catch (e) { err = e; } await se.deinit(); if (err) throw err; }; };