From 7895decf4eea09243c552d6cd94ad3d9d7ffae78 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 19 Jul 2022 15:17:22 +0200 Subject: [PATCH] feat(notification): create models --- back/methods/notification/clean.js | 0 back/methods/notification/send.js | 18 ++++++ back/models/notifcation.json | 22 +++++++ back/models/notifcationAcl.json | 21 +++++++ back/models/notifcationConfig.json | 19 ++++++ back/models/notifcationQueue.json | 37 ++++++++++++ back/models/notification.js | 4 ++ back/models/notificationSubscription.json | 21 +++++++ db/dump/fixtures.sql | 5 +- db/dump/structure.sql | 71 ++++++++++++++++++++++- 10 files changed, 214 insertions(+), 4 deletions(-) create mode 100644 back/methods/notification/clean.js create mode 100644 back/methods/notification/send.js create mode 100644 back/models/notifcation.json create mode 100644 back/models/notifcationAcl.json create mode 100644 back/models/notifcationConfig.json create mode 100644 back/models/notifcationQueue.json create mode 100644 back/models/notification.js create mode 100644 back/models/notificationSubscription.json diff --git a/back/methods/notification/clean.js b/back/methods/notification/clean.js new file mode 100644 index 000000000..e69de29bb diff --git a/back/methods/notification/send.js b/back/methods/notification/send.js new file mode 100644 index 000000000..bb2d79f2a --- /dev/null +++ b/back/methods/notification/send.js @@ -0,0 +1,18 @@ +module.exports = Self => { + Self.remoteMethod('send', { + description: 'Send notifications from queue', + accessType: 'WRITE', + returns: { + type: 'object', + root: true + }, + http: { + path: `/send`, + verb: 'POST' + } + }); + + Self.send = async() => { + + }; +}; diff --git a/back/models/notifcation.json b/back/models/notifcation.json new file mode 100644 index 000000000..e4f0be17e --- /dev/null +++ b/back/models/notifcation.json @@ -0,0 +1,22 @@ +{ + "name": "Notificacion", + "base": "VnModel", + "options": { + "mysql": { + "table": "notificacion" + } + }, + "properties": { + "id": { + "type": "number", + "id": true, + "description": "Identifier" + }, + "name": { + "type": "string" + }, + "description": { + "type": "string" + } + } +} \ No newline at end of file diff --git a/back/models/notifcationAcl.json b/back/models/notifcationAcl.json new file mode 100644 index 000000000..f5861943f --- /dev/null +++ b/back/models/notifcationAcl.json @@ -0,0 +1,21 @@ +{ + "name": "NotificacionAcl", + "base": "VnModel", + "options": { + "mysql": { + "table": "notificacionAcl" + } + }, + "relations": { + "notificacion": { + "type": "belongsTo", + "model": "Notificacion", + "foreignKey": "notificacionFk" + }, + "role": { + "type": "belongsTo", + "model": "Role", + "foreignKey": "roleFk" + } + } +} \ No newline at end of file diff --git a/back/models/notifcationConfig.json b/back/models/notifcationConfig.json new file mode 100644 index 000000000..73882b6b7 --- /dev/null +++ b/back/models/notifcationConfig.json @@ -0,0 +1,19 @@ +{ + "name": "NotificationConfig", + "base": "VnModel", + "options": { + "mysql": { + "table": "notificationConfig" + } + }, + "properties": { + "id": { + "type": "number", + "id": true, + "description": "Identifier" + }, + "cleanDays": { + "type": "number" + } + } +} \ No newline at end of file diff --git a/back/models/notifcationQueue.json b/back/models/notifcationQueue.json new file mode 100644 index 000000000..7a9199bc0 --- /dev/null +++ b/back/models/notifcationQueue.json @@ -0,0 +1,37 @@ +{ + "name": "NotificacionQueue", + "base": "VnModel", + "options": { + "mysql": { + "table": "notificacionQueue" + } + }, + "properties": { + "id": { + "type": "number", + "id": true, + "description": "Identifier" + }, + "params": { + "type": "string" + }, + "status": { + "type": "string" + }, + "created": { + "type": "date" + } + }, + "relations": { + "notificacion": { + "type": "belongsTo", + "model": "Notificacion", + "foreignKey": "notificacionFk" + }, + "author": { + "type": "belongsTo", + "model": "User", + "foreignKey": "authorFk" + } + } +} \ No newline at end of file diff --git a/back/models/notification.js b/back/models/notification.js new file mode 100644 index 000000000..da766d442 --- /dev/null +++ b/back/models/notification.js @@ -0,0 +1,4 @@ +module.exports = Self => { + require('../methods/notification/send')(Self); + require('../methods/notification/clear')(Self); +}; diff --git a/back/models/notificationSubscription.json b/back/models/notificationSubscription.json new file mode 100644 index 000000000..ed8bb1907 --- /dev/null +++ b/back/models/notificationSubscription.json @@ -0,0 +1,21 @@ +{ + "name": "NotificationSubscription", + "base": "VnModel", + "options": { + "mysql": { + "table": "notificationSubscription" + } + }, + "relations": { + "notificacion": { + "type": "belongsTo", + "model": "Notificacion", + "foreignKey": "notificacionFk" + }, + "user": { + "type": "belongsTo", + "model": "User", + "foreignKey": "userFk" + } + } +} \ No newline at end of file diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 0609a6a6a..dabc73139 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2610,4 +2610,7 @@ INSERT INTO `vn`.`sectorCollectionSaleGroup` (`sectorCollectionFk`, `saleGroupFk INSERT INTO `vn`.`workerTimeControlConfig` (`id`, `dayBreak`, `dayBreakDriver`, `shortWeekBreak`, `longWeekBreak`, `weekScope`, `mailPass`, `mailHost`, `mailSuccessFolder`, `mailErrorFolder`, `mailUser`, `minHoursToBreak`, `breakHours`, `hoursCompleteWeek`, `startNightlyHours`, `endNightlyHours`, `maxTimePerDay`, `breakTime`, `timeToBreakTime`, `dayMaxTime`, `shortWeekDays`, `longWeekDays`) VALUES - (1, 43200, 32400, 129600, 259200, 604800, '', '', 'Leidos.exito', 'Leidos.error', 'timeControl', 5.33, 0.33, 40, '22:00:00', '06:00:00', 57600, 1200, 18000, 57600, 6, 13); \ No newline at end of file + (1, 43200, 32400, 129600, 259200, 604800, '', '', 'Leidos.exito', 'Leidos.error', 'timeControl', 5.33, 0.33, 40, '22:00:00', '06:00:00', 57600, 1200, 18000, 57600, 6, 13); + +INSERT INTO `util`.`notificationConfig` + SET `cleanDays` = 90; \ No newline at end of file diff --git a/db/dump/structure.sql b/db/dump/structure.sql index 2c0e8231a..923855762 100644 --- a/db/dump/structure.sql +++ b/db/dump/structure.sql @@ -20849,9 +20849,74 @@ CREATE DATABASE /*!32312 IF NOT EXISTS*/ `util` /*!40100 DEFAULT CHARACTER SET u USE `util`; --- --- Table structure for table `config` --- +CREATE TABLE notification( + id INT PRIMARY KEY, + `name` VARCHAR(255) UNIQUE, + `description` VARCHAR(255) +); + +CREATE TABLE notificationAcl ( + notificationFk INT(11), + roleFk INT(10) unsigned, + PRIMARY KEY(notificationFk, roleFk), + CONSTRAINT `notificationAcl_ibfk_1` FOREIGN KEY (`notificationFk`) REFERENCES `notification` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `notificationAcl_ibfk_2` FOREIGN KEY (`roleFk`) REFERENCES `account`.`role` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +); + +CREATE TABLE notificationSubscription( + notificationFk INT, + userFk INT(10) unsigned, + PRIMARY KEY(notificationFk, 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 +); + +CREATE TABLE notificationQueue( + id INT PRIMARY KEY AUTO_INCREMENT, + notificationFk VARCHAR(255), + params TEXT, + authorFk INT(10) unsigned NULL, + `status` ENUM('pending', 'sent', 'error') NOT NULL DEFAULT 'pending', + created DATETIME DEFAULT CURRENT_TIMESTAMP, + INDEX(notificationFk), + INDEX(authorFk), + INDEX(status), + CONSTRAINT `notificationQueue_ibfk_1` FOREIGN KEY (`notificationFk`) REFERENCES `notification` (`name`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `notificationQueue_ibfk_2` FOREIGN KEY (`authorFk`) REFERENCES `account`.`user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +); + +CREATE TABLE notificationConfig( + id INT PRIMARY KEY AUTO_INCREMENT, + cleanDays MEDIUMINT +); + +DROP FUNCTION IF EXISTS util.notification_send; +DELIMITER $$ +CREATE FUNCTION util.notification_send(vNotificationName VARCHAR(255), vParams TEXT, vAuthorFk INT) + RETURNS INT +BEGIN +/** + * Sends a notification. + * + * @param vNotificationName The notification name + * @param vParams The notification parameters formatted as JSON + * @param vAuthorFk The notification author or %NULL if there is no author + * @return The notification id + */ + DECLARE vNotificationFk INT; + + SELECT id INTO vNotificationFk + FROM `notification` + WHERE `name` = vNotificationName; + + INSERT INTO notificationQueue + SET notificationFk = vNotificationFk, + params = vParams, + authorFk = vAuthorFk; + + RETURN LAST_INSERT_ID(); +END$$ +DELIMITER ; DROP TABLE IF EXISTS `config`; /*!40101 SET @saved_cs_client = @@character_set_client */;