4797-lilium-worker-notifications #1229
|
@ -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)
|
||||||
pau marked this conversation as resolved
Outdated
|
|||||||
return;
|
return;
|
||||||
pau marked this conversation as resolved
alexm
commented
Te ahorres fer if else, fent:
I ns si faria falta ficar return al final o no Te ahorres fer if else, fent:
```
if (userId != modifiedUser.id && userId != modifiedUser.bossFk)
throw new UserError('You dont have permission to modify this user');
```
*I ns si faria falta ficar return al final o no*
|
|||||||
|
@ -49,16 +45,11 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
pau marked this conversation as resolved
Outdated
alexm
commented
Te ahorres fer if else, fent:
Te ahorres fer if else, fent:
```
if (user != modifiedUser.id && user != modifiedUser.bossFk)
throw new UserError('You dont have permission to modify this user');
await models.NotificationSubscription.destroyById(notificationId);
```
|
|||||||
|
|
||||||
Self.deleteNotification = async function(ctx) {
|
Self.deleteNotification = async function(ctx) {
|
||||||
pau marked this conversation as resolved
alexm
commented
Y asi te ahorras poner
`Self.deleteNotification = async function(ctx, userId, notificationId)`
Y asi te ahorras poner
```
const notificationId = await ctx.args.notificationId;
const userId = await ctx.args.userId;
```
|
|||||||
let models = Self.app.models;
|
const models = Self.app.models;
|
||||||
let user = await ctx.args.authorId;
|
const user = await ctx.args.authorId;
|
||||||
pau marked this conversation as resolved
Outdated
alexm
commented
No seria mejor utilizar ctx.req.accessToken.userId y no pasarlo por parametro? No seria mejor utilizar ctx.req.accessToken.userId y no pasarlo por parametro?
|
|||||||
let notificationId = await ctx.args.notificationId;
|
const notificationId = await ctx.args.notificationId;
|
||||||
pau marked this conversation as resolved
Outdated
alexm
commented
Provar:
Provar:
let userToCheck = userFk;
if (notificationId != null)
userToCheck = await models.NotificationSubscription.findById(notificationId).userFk;
return await models.Worker.findOne({
fields: ['id', 'bossFk'],
where: {
id: userToCheck
}
});
|
|||||||
let userId = await ctx.args.userId;
|
const userId = await ctx.args.userId;
|
||||||
pau marked this conversation as resolved
Outdated
joan
commented
Además de lo que comenta alex, quitar el await. Además de lo que comenta alex, quitar el await.
|
|||||||
let modifiedUser = await models.Worker.findOne({
|
const modifiedUser = await getUserToModify(userId, models);
|
||||||
pau marked this conversation as resolved
alexm
commented
Pq per defecte els dos parametres son null? Pq per defecte els dos parametres son null?
|
|||||||
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
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Pasar a funcion la parte de comprobar el usuario, ya que se repite en el before y en la ruta de delete