refs #6023 Transactioned sync
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Guillermo Bonet 2023-08-31 09:20:33 +02:00
parent 9f6d034f9c
commit 0dbb77fc64
1 changed files with 20 additions and 11 deletions

View File

@ -25,8 +25,10 @@ module.exports = Self => {
});
Self.sync = async function(userName, password, force, options) {
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
@ -35,16 +37,23 @@ module.exports = Self => {
myOptions.transaction = tx;
};
const models = Self.app.models;
const user = await models.VnUser.findOne({
fields: ['id'],
where: {name: userName}
}, myOptions);
const isSync = !await models.UserSync.exists(userName, myOptions);
try {
const user = await models.VnUser.findOne({
fields: ['id'],
where: {name: userName}
}, myOptions);
const isSync = !await models.UserSync.exists(userName, myOptions);
if (!force && isSync && user) return;
await models.AccountConfig.syncUser(userName, password);
await models.UserSync.destroyById(userName, myOptions);
if (!force && isSync && user) {
if (tx) await tx.rollback();
return;
}
await models.AccountConfig.syncUser(userName, password);
await models.UserSync.destroyById(userName, myOptions);
if (tx) await tx.commit();
} catch (err) {
if (tx) await tx.rollback();
throw err;
}
};
};