From c637f2c8798989b1684ec16f79d33d7e654ea791 Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 14 Oct 2022 13:22:53 +0200 Subject: [PATCH] feat(send): use vn-print libary and adapt test --- back/methods/notification/send.js | 59 ++++++++++--------- back/methods/notification/specs/send.spec.js | 2 +- .../10491-august/00-notificationTables.sql | 14 ++--- db/dump/fixtures.sql | 8 ++- 4 files changed, 44 insertions(+), 39 deletions(-) diff --git a/back/methods/notification/send.js b/back/methods/notification/send.js index b56d9d6c0..5ae7e748b 100644 --- a/back/methods/notification/send.js +++ b/back/methods/notification/send.js @@ -1,4 +1,5 @@ -const axios = require('axios'); +const {Email} = require('vn-print'); +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethod('send', { @@ -15,27 +16,18 @@ module.exports = Self => { }); Self.send = async options => { - // const headers = ctx.req.headers; - // const origin = headers.origin || 'http://' + headers.host; - // const auth = ctx.req.accessToken; - // console.log(origin); + if (process.env.NODE_ENV == 'test') + throw new UserError(`Action not allowed on the test environment`); const models = Self.app.models; - const status = 'pending'; + const findStatus = 'pending'; const myOptions = {}; - let tx; - if (typeof options == 'object') Object.assign(myOptions, options); - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - const notificationQueue = await models.NotificationQueue.find({ - where: {status: status}, + where: {status: findStatus}, include: [ { relation: 'notification', @@ -46,7 +38,7 @@ module.exports = Self => { include: { relation: 'user', scope: { - fields: ['name'] + fields: ['name', 'email', 'lang'] } } } @@ -59,24 +51,33 @@ module.exports = Self => { const statusSent = 'sent'; const statusError = 'error'; + for (const queue of notificationQueue) { - // console.log(queue); - // console.log(origin); - // console.log(auth); - // console.log(queue.notification().name); - // console.log(queue.params); + const queueName = queue.notification().name; + const queueParams = JSON.parse(queue.params); - // const queueParams = Object.assign({}, JSON.parse(queue.params)); - // queueParams.authorization = auth.id; + for (const notificationUser of queue.notification().subscription()) { + try { + const sendParams = { + recipient: notificationUser.user().email, + lang: notificationUser.user().lang + }; - try { - // await print axios.get(`print`, queueParams) - await queue.updateAttribute('status', statusSent); - } catch (error) { - await queue.updateAttribute('status', statusError); + if (notificationUser.userFk == queue.authorFk) { + await queue.updateAttribute('status', statusSent); + continue; + } + + const newParams = Object.assign({}, queueParams, sendParams); + const email = new Email(queueName, newParams); + + await email.send(); + + await queue.updateAttribute('status', statusSent); + } catch (error) { + await queue.updateAttribute('status', statusError); + } } } - - return notificationQueue; }; }; diff --git a/back/methods/notification/specs/send.spec.js b/back/methods/notification/specs/send.spec.js index 015d1cb4d..f0b186e06 100644 --- a/back/methods/notification/specs/send.spec.js +++ b/back/methods/notification/specs/send.spec.js @@ -27,7 +27,7 @@ describe('Notification Send()', () => { throw e; } - expect(before.length).toEqual(1); + expect(before.length).toEqual(3); expect(after.length).toEqual(0); }); }); diff --git a/db/changes/10491-august/00-notificationTables.sql b/db/changes/10491-august/00-notificationTables.sql index d5fcf00b2..2db7d9874 100644 --- a/db/changes/10491-august/00-notificationTables.sql +++ b/db/changes/10491-august/00-notificationTables.sql @@ -7,8 +7,8 @@ CREATE TABLE notification( ); CREATE TABLE notificationAcl( - notificationFk INT, -- FK notification.id - roleFk INT(10) unsigned, -- FK account.role.id + notificationFk INT, + roleFk INT(10) unsigned, PRIMARY KEY(notificationFk, roleFk) ); @@ -21,8 +21,8 @@ ALTER TABLE `util`.`notificationAcl` ADD CONSTRAINT `notificationAcl_ibfk_2` FOR ON UPDATE CASCADE; CREATE TABLE notificationSubscription( - notificationFk INT, -- FK notification.id - userFk INT(10) unsigned, -- FK account.user.id + notificationFk INT, + userFk INT(10) unsigned, PRIMARY KEY(notificationFk, userFk) ); @@ -36,9 +36,9 @@ ALTER TABLE `util`.`notificationSubscription` ADD CONSTRAINT `notificationSubscr CREATE TABLE notificationQueue( id INT PRIMARY KEY AUTO_INCREMENT, - notificationFk VARCHAR(255), -- FK notification.name - params TEXT, -- JSON - authorFk INT(10) unsigned NULL, -- FK account.user.id + notificationFk VARCHAR(255), + params JSON, + authorFk INT(10) unsigned NULL, `status` ENUM('pending', 'sent', 'error') NOT NULL DEFAULT 'pending', created DATETIME DEFAULT CURRENT_TIMESTAMP, INDEX(notificationFk), diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index cd2a1c6dd..9d873f2c6 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2653,7 +2653,7 @@ INSERT INTO `util`.`notificationConfig` INSERT INTO `util`.`notification` (`id`, `name`, `description`) VALUES - (1, 'invoice', 'notification fixture one'); + (1, 'print-email', 'notification fixture one'); INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`) VALUES @@ -2661,11 +2661,15 @@ INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`) INSERT INTO `util`.`notificationQueue` (`id`, `notificationFk`, `params`, `authorFk`, `status`, `created`) VALUES - (1, 'invoice', '{"invoiceId": 1}', 9, 'pending', util.VN_CURDATE()); + (1, 'print-email', '{"id": "1"}', 9, 'pending', util.VN_CURDATE()), + (2, 'print-email', '{"id": "2"}', null, 'pending', util.VN_CURDATE()), + (3, 'print-email', null, null, 'pending', util.VN_CURDATE()); INSERT INTO `util`.`notificationSubscription` (`notificationFk`, `userFk`) VALUES + (1, 1109), (1, 1110); + INSERT INTO `vn`.`routeConfig` (`id`, `defaultWorkCenterFk`) VALUES (1, 9);