changed let to const and removed duplicate code
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Pau 2022-12-27 14:59:23 +01:00
parent ee0cd2174f
commit 0a602892e6
1 changed files with 18 additions and 18 deletions

View File

@ -2,14 +2,10 @@ const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.observe('before save', async function(ctx) { Self.observe('before save', async function(ctx) {
let models = Self.app.models; const models = Self.app.models;
let userId = ctx.options.accessToken.userId; const userId = ctx.options.accessToken.userId;
let modifiedUser = await models.Worker.findOne({ const user = await ctx.instance.userFk;
fields: ['id', 'bossFk'], const modifiedUser = await getUserToModify(user, models);
where: {
id: ctx.instance.userFk
}
});
if (userId == modifiedUser.id || userId == modifiedUser.bossFk) if (userId == modifiedUser.id || userId == modifiedUser.bossFk)
return; return;
@ -49,16 +45,11 @@ module.exports = Self => {
}); });
Self.deleteNotification = async function(ctx) { Self.deleteNotification = async function(ctx) {
let models = Self.app.models; const models = Self.app.models;
let user = await ctx.args.authorId; const user = await ctx.args.authorId;
let notificationId = await ctx.args.notificationId; const notificationId = await ctx.args.notificationId;
let userId = await ctx.args.userId; const userId = await ctx.args.userId;
let modifiedUser = await models.Worker.findOne({ const modifiedUser = await getUserToModify(userId, models);
fields: ['id', 'bossFk'],
where: {
id: ctx.args.userId
}
});
if (user == modifiedUser.id || modifiedUser.bossFk == user) { if (user == modifiedUser.id || modifiedUser.bossFk == user) {
const query = `DELETE FROM util.notificationSubscription const query = `DELETE FROM util.notificationSubscription
@ -70,4 +61,13 @@ module.exports = Self => {
} else } else
throw new UserError('You dont have permission to modify this user'); throw new UserError('You dont have permission to modify this user');
}; };
async function getUserToModify(user, models) {
return await models.Worker.findOne({
fields: ['id', 'bossFk'],
where: {
id: user
}
});
}
}; };