4797-lilium-worker-notifications #1229

Merged
pau merged 37 commits from 4797-lilium-worker-notifications into dev 2023-02-02 08:53:29 +00:00
3 changed files with 33 additions and 25 deletions
Showing only changes of commit 24b8b172b9 - Show all commits

View File

@ -22,13 +22,10 @@ module.exports = Self => {
http: {source: 'context'} http: {source: 'context'}
}, },
{ {
arg: 'userFk', arg: 'notificationId',
type: 'string' type: 'number',
required: true
}, },
{
arg: 'notificationFk',
type: 'number'
}
], ],
returns: { returns: {
type: 'object', 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 models = Self.app.models;
const user = ctx.req.accessToken.userId; 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) { if (user == modifiedUser.id || modifiedUser.bossFk == user) {
const query = `DELETE FROM util.notificationSubscription await models.NotificationSubscription.destroyById(notificationId);
WHERE notificationFk = ? AND userFk = ?`;
await Self.rawSql(query, [notificationFk, userFk]);
return; return;
pau marked this conversation as resolved
Review

Self.deleteNotification = async function(ctx, userId, notificationId)

Y asi te ahorras poner

const notificationId = await ctx.args.notificationId;
const userId = await ctx.args.userId;
`Self.deleteNotification = async function(ctx, userId, notificationId)` Y asi te ahorras poner ``` const notificationId = await ctx.args.notificationId; const userId = await ctx.args.userId; ```
} 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) { async function getUserToModify(notificationId = null, userFk = null, models) {
pau marked this conversation as resolved
Review

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({ return await models.Worker.findOne({
fields: ['id', 'bossFk'], fields: ['id', 'bossFk'],
where: { where: {
id: user id: user
} }
}); });
} else {
return await models.Worker.findOne({
fields: ['id', 'bossFk'],
where: {
id: userFk
}
});
}
} }
}; };

View File

@ -7,15 +7,18 @@
} }
}, },
"properties": { "properties": {
"notificationFk": { "id": {
"type": "number", "type": "number",
"id": true, "id": true,
"description": "Identifier" "description": "Primary key"
},
"notificationFk": {
"type": "number",
"description": "Foreign key to Notification"
}, },
"userFk": { "userFk": {
"type": "number", "type": "number",
"id": true, "description": "Foreign key to Account"
"description": "Identifier"
} }
}, },
"relations": { "relations": {

View File

@ -19726,9 +19726,10 @@ DROP TABLE IF EXISTS `notificationSubscription`;
/*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */; /*!40101 SET character_set_client = utf8 */;
CREATE TABLE `notificationSubscription` ( CREATE TABLE `notificationSubscription` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`notificationFk` int(11) NOT NULL, `notificationFk` int(11) NOT NULL,
`userFk` int(10) unsigned NOT NULL, `userFk` int(10) unsigned NOT NULL,
PRIMARY KEY (`notificationFk`,`userFk`), PRIMARY KEY (`Id`),
KEY `notificationSubscription_ibfk_2` (`userFk`), KEY `notificationSubscription_ibfk_2` (`userFk`),
CONSTRAINT `notificationSubscription_ibfk_1` FOREIGN KEY (`notificationFk`) REFERENCES `notification` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, 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 CONSTRAINT `notificationSubscription_ibfk_2` FOREIGN KEY (`userFk`) REFERENCES `account`.`user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE