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

View File

@ -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": {

View File

@ -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