4797-lilium-worker-notifications #1229
|
@ -22,13 +22,10 @@ module.exports = Self => {
|
|||
http: {source: 'context'}
|
||||
},
|
||||
{
|
||||
arg: 'userFk',
|
||||
type: 'string'
|
||||
arg: 'notificationId',
|
||||
type: 'number',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
arg: 'notificationFk',
|
||||
type: 'number'
|
||||
}
|
||||
],
|
||||
returns: {
|
||||
type: 'object',
|
||||
|
@ -40,28 +37,35 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.deleteNotification = async function(ctx, userFk, notificationFk) {
|
||||
Self.deleteNotification = async function(ctx, notificationId) {
|
||||
const models = Self.app.models;
|
||||
const user = ctx.req.accessToken.userId;
|
||||
const modifiedUser = await getUserToModify(userFk, models);
|
||||
const modifiedUser = await getUserToModify(notificationId, models);
|
||||
|
||||
if (user == modifiedUser.id || modifiedUser.bossFk == user) {
|
||||
const query = `DELETE FROM util.notificationSubscription
|
||||
WHERE notificationFk = ? AND userFk = ?`;
|
||||
|
||||
await Self.rawSql(query, [notificationFk, userFk]);
|
||||
|
||||
await models.NotificationSubscription.destroyById(notificationId);
|
||||
return;
|
||||
pau marked this conversation as resolved
|
||||
} else
|
||||
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
|
||||
}
|
||||
});
|
||||
async function getUserToModify(notificationId = null, userFk = null, 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?
|
||||
if (notificationId != null) {
|
||||
const subscription = await models.NotificationSubscription.findById(notificationId);
|
||||
const user = await subscription.userFk;
|
||||
return await models.Worker.findOne({
|
||||
fields: ['id', 'bossFk'],
|
||||
where: {
|
||||
id: user
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return await models.Worker.findOne({
|
||||
fields: ['id', 'bossFk'],
|
||||
where: {
|
||||
id: userFk
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -7,15 +7,18 @@
|
|||
}
|
||||
},
|
||||
"properties": {
|
||||
"notificationFk": {
|
||||
"id": {
|
||||
"type": "number",
|
||||
"id": true,
|
||||
"description": "Identifier"
|
||||
"description": "Primary key"
|
||||
},
|
||||
"notificationFk": {
|
||||
"type": "number",
|
||||
"description": "Foreign key to Notification"
|
||||
},
|
||||
"userFk": {
|
||||
"type": "number",
|
||||
"id": true,
|
||||
"description": "Identifier"
|
||||
"description": "Foreign key to Account"
|
||||
}
|
||||
},
|
||||
"relations": {
|
||||
|
|
|
@ -19726,9 +19726,10 @@ DROP TABLE IF EXISTS `notificationSubscription`;
|
|||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8 */;
|
||||
CREATE TABLE `notificationSubscription` (
|
||||
`Id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`notificationFk` int(11) NOT NULL,
|
||||
`userFk` int(10) unsigned NOT NULL,
|
||||
PRIMARY KEY (`notificationFk`,`userFk`),
|
||||
PRIMARY KEY (`Id`),
|
||||
KEY `notificationSubscription_ibfk_2` (`userFk`),
|
||||
CONSTRAINT `notificationSubscription_ibfk_1` FOREIGN KEY (`notificationFk`) REFERENCES `notification` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
CONSTRAINT `notificationSubscription_ibfk_2` FOREIGN KEY (`userFk`) REFERENCES `account`.`user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
|
|
Loading…
Reference in New Issue
Self.deleteNotification = async function(ctx, userId, notificationId)
Y asi te ahorras poner