salix/modules/account/back/methods/user-account/sync.js

52 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-10-31 20:16:58 +00:00
const SyncEngine = require('../../util/sync-engine');
2020-09-21 11:24:43 +00:00
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`,
2020-09-21 11:24:43 +00:00
verb: 'PATCH'
}
});
Self.sync = async function(userName, password) {
let $ = Self.app.models;
let user = await $.Account.findOne({
2020-09-21 14:09:34 +00:00
fields: ['id'],
2020-09-21 11:24:43 +00:00
where: {name: userName}
});
2020-09-21 14:09:34 +00:00
let isSync = !await $.UserSync.exists(userName);
2020-09-21 11:24:43 +00:00
2020-09-21 14:09:34 +00:00
if (user && isSync) return;
2020-09-21 11:24:43 +00:00
2020-10-31 20:16:58 +00:00
let err;
2020-11-02 18:58:07 +00:00
let engine = new SyncEngine();
await engine.init($);
2020-09-21 11:24:43 +00:00
2020-10-31 20:16:58 +00:00
try {
2020-11-02 18:58:07 +00:00
await engine.sync(userName, password, true);
2020-10-31 20:16:58 +00:00
await $.UserSync.destroyById(userName);
} catch (e) {
err = e;
2020-09-21 11:24:43 +00:00
}
2020-11-02 18:58:07 +00:00
await engine.deinit();
2020-10-31 20:16:58 +00:00
if (err) throw err;
2020-09-21 11:24:43 +00:00
};
};
2020-10-31 20:16:58 +00:00