2020-10-31 20:16:58 +00:00
|
|
|
|
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'
|
2020-11-12 22:20:25 +00:00
|
|
|
}, {
|
|
|
|
arg: 'force',
|
|
|
|
type: 'boolean',
|
|
|
|
description: 'Whether to force synchronization'
|
2020-09-21 11:24:43 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
http: {
|
2020-10-30 23:02:27 +00:00
|
|
|
path: `/:userName/sync`,
|
2020-09-21 11:24:43 +00:00
|
|
|
verb: 'PATCH'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-12 22:20:25 +00:00
|
|
|
Self.sync = async function(userName, password, force) {
|
2023-01-24 08:04:43 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const user = await models.VnUser.findOne({
|
2020-09-21 14:09:34 +00:00
|
|
|
fields: ['id'],
|
2020-09-21 11:24:43 +00:00
|
|
|
where: {name: userName}
|
|
|
|
});
|
2023-01-24 08:04:43 +00:00
|
|
|
const isSync = !await models.UserSync.exists(userName);
|
2020-09-21 11:24:43 +00:00
|
|
|
|
2020-11-12 22:20:25 +00:00
|
|
|
if (!force && isSync && user) return;
|
2023-01-24 08:04:43 +00:00
|
|
|
await models.AccountConfig.syncUser(userName, password);
|
|
|
|
await models.UserSync.destroyById(userName);
|
2020-09-21 11:24:43 +00:00
|
|
|
};
|
|
|
|
};
|
2020-10-31 20:16:58 +00:00
|
|
|
|