From a30ff44837588b8b6cf85012aaff07cef7c91943 Mon Sep 17 00:00:00 2001 From: pablone Date: Fri, 14 Jun 2024 17:40:08 +0200 Subject: [PATCH] fix: refs #6404 fix notification --- back/methods/mrw-config/createShipment.js | 12 ++-- .../mrw-config/specs/createShipment.spec.js | 62 ++++++++----------- back/models/mrw-config.js | 2 +- db/dump/fixtures.before.sql | 6 +- .../11086-grayCataractarum/00-firstScript.sql | 9 +++ .../assets/css/import.js | 0 .../locale/en.yml | 0 .../locale/es.yml | 0 .../mrw-deadline.html} | 0 .../mrw-deadline.js} | 2 +- 10 files changed, 45 insertions(+), 48 deletions(-) rename print/templates/email/{mrw-webService-deadline => mrw-deadline}/assets/css/import.js (100%) rename print/templates/email/{mrw-webService-deadline => mrw-deadline}/locale/en.yml (100%) rename print/templates/email/{mrw-webService-deadline => mrw-deadline}/locale/es.yml (100%) rename print/templates/email/{mrw-webService-deadline/mrw-webService-deadline.html => mrw-deadline/mrw-deadline.html} (100%) rename print/templates/email/{mrw-webService-deadline/mrw-webService-deadline.js => mrw-deadline/mrw-deadline.js} (83%) diff --git a/back/methods/mrw-config/createShipment.js b/back/methods/mrw-config/createShipment.js index ca155919f..a2fccb95b 100644 --- a/back/methods/mrw-config/createShipment.js +++ b/back/methods/mrw-config/createShipment.js @@ -1,4 +1,3 @@ -const {Email} = require('vn-print'); const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { @@ -21,19 +20,17 @@ module.exports = Self => { }); Self.createShipment = async expeditionFk => { - const mrwConfig = Self.app.models.MrwConfig; + const models = Self.app.models; const mrw = await Self.getConfig(); const today = Date.vnNew(); - const [hours, minutes] = mrw?.expeditionDeadLine ? mrw.expeditionDeadLine.split(':').map(Number) : [0, 0]; const deadLine = Date.vnNew(); deadLine.setHours(hours, minutes, 0); if (today > deadLine && (!mrw.notified || mrw.notified.setHours(0, 0, 0, 0) !== today.setHours(0, 0, 0, 0))) { - const email = new Email('mrw-webService-deadline', {recipient: 'agencias@verdnatura.es', lang: 'es'}); - await email.send(); + await models.NotificationQueue.create({notificationFk: 'mrw-deadline'}); await mrw.updateAttributes({notified: Date.vnNow()}); } @@ -88,10 +85,9 @@ module.exports = Self => { ); const shipmentId = Self.getTextByTag(shipmentResponse, 'NumeroEnvio'); - if (!shipmentId) - throw new UserError(Self.getTextByTag(shipmentResponse, 'Mensaje')); + if (!shipmentId) throw new UserError(Self.getTextByTag(shipmentResponse, 'Mensaje')); - const file = await mrwConfig.getLabel(shipmentId); + const file = await models.MrwConfig.getLabel(shipmentId); return {shipmentId, file}; }; diff --git a/back/methods/mrw-config/specs/createShipment.spec.js b/back/methods/mrw-config/specs/createShipment.spec.js index b96efeac4..ad168b139 100644 --- a/back/methods/mrw-config/specs/createShipment.spec.js +++ b/back/methods/mrw-config/specs/createShipment.spec.js @@ -2,7 +2,7 @@ const models = require('vn-loopback/server/server').models; const axios = require('axios'); const fs = require('fs'); -const filter = {subject: 'SuperaciĆ³n de la Hora de Corte de MRW'}; +const filter = {notificationFk: 'mrw-deadline'}; const mockBase64Binary = 'base64BinaryString'; const ticket1 = { 'id': '44', @@ -53,15 +53,25 @@ describe('MRWConfig createShipment()', () => { await models.Expedition.create(expedition1); }); - beforeEach(() => { + afterAll(async() => { + await cleanFixtures(); + }); + + beforeEach(async() => { const mockPostResponses = [ {data: fs.readFileSync(__dirname + '/mockGetLabel.xml', 'utf-8')}, {data: fs.readFileSync(__dirname + '/mockCreateShipment.xml', 'utf-8')} ]; spyOn(axios, 'post').and.callFake(() => Promise.resolve(mockPostResponses.pop())); + await cleanFixtures(); }); + async function cleanFixtures() { + await models.NotificationQueue.destroyAll(filter); + await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: null, notified: null}); + } + async function createMrwConfig() { await models.MrwConfig.create( { @@ -75,6 +85,13 @@ describe('MRWConfig createShipment()', () => { ); } + async function getLastNotification() { + return models.NotificationQueue.findOne({ + order: 'id DESC', + where: filter + }); + } + it('should create a shipment and return a base64Binary label', async() => { const {file} = await models.MrwConfig.createShipment(expedition1.id); @@ -87,7 +104,7 @@ describe('MRWConfig createShipment()', () => { await models.MrwConfig.createShipment(expedition1.id).catch(e => { error = e; }).finally(async() => { - expect(error.message).toEqual(`Some mrwConfig parameters are not set`); + expect(error.message).toEqual(`MRW service is not configured`); }); await createMrwConfig(); @@ -118,53 +135,26 @@ describe('MRWConfig createShipment()', () => { }); it('should send mail if you are past the dead line and is not notified today', async() => { - await models.Mail.destroyAll(filter); - await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: '10:00:00', notified: null}); - await models.MrwConfig.createShipment(expedition1.id); + const notification = await getLastNotification(); - const mail = await models.Mail.findOne({ - order: 'id DESC', - where: filter - }); - - expect(mail.subject).toEqual(filter.subject); - - await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: null, notified: null}); + expect(notification.notificationFk).toEqual(filter.notificationFk); }); it('should send mail if you are past the dead line and it is notified from another day', async() => { - await models.Mail.destroyAll(filter); - await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: '10:00:00', notified: new Date()}); - await models.MrwConfig.createShipment(expedition1.id); + const notification = await getLastNotification(); - const mail = await models.Mail.findOne({ - order: 'id DESC', - where: filter - }); - - expect(mail.subject).toEqual(filter.subject); - - await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: null, notified: null}); + expect(notification.notificationFk).toEqual(filter.notificationFk); }); it('should not send mail if you are past the dead line and it is notified', async() => { - await models.Mail.destroyAll(filter); - await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: '10:00:00', notified: Date.vnNew()}); - await models.MrwConfig.createShipment(expedition1.id); + const notification = await getLastNotification(); - const mail = await models.Mail.findOne({ - order: 'id DESC', - where: filter - }); - - expect(mail).toEqual(null); - - await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: null, notified: null}); + expect(notification).toEqual(null); }); }); diff --git a/back/models/mrw-config.js b/back/models/mrw-config.js index 4496a9a56..c738c9c0e 100644 --- a/back/models/mrw-config.js +++ b/back/models/mrw-config.js @@ -11,7 +11,7 @@ module.exports = Self => { Self.getConfig = async function() { const mrw = await Self.app.models.MrwConfig.findOne(null); - if (!mrw) throw new UserError(`Some mrwConfig parameters are not set`); + if (!mrw) throw new UserError(`MRW service is not configured`); return mrw; }; diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index 49cb17f0f..d973e94f8 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -2844,7 +2844,8 @@ INSERT INTO `util`.`notification` (`id`, `name`, `description`) (5, 'modified-entry', 'An entry has been modified'), (6, 'book-entry-deleted', 'accounting entries deleted'), (7, 'zone-included','An email to notify zoneCollisions'), - (8, 'backup-printer-selected','A backup printer has been selected'); + (8, 'backup-printer-selected','A backup printer has been selected'), + (9, 'mrw-deadline','The MRW deadline has passed'); TRUNCATE `util`.`notificationAcl`; INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`) @@ -2857,7 +2858,8 @@ INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`) (5, 9), (6, 9), (7, 9), - (8, 66); + (8, 66), + (9, 56); TRUNCATE `util`.`notificationQueue`; INSERT INTO `util`.`notificationQueue` (`id`, `notificationFk`, `params`, `authorFk`, `status`, `created`) diff --git a/db/versions/11086-grayCataractarum/00-firstScript.sql b/db/versions/11086-grayCataractarum/00-firstScript.sql index 20ed9d8f2..86107b159 100644 --- a/db/versions/11086-grayCataractarum/00-firstScript.sql +++ b/db/versions/11086-grayCataractarum/00-firstScript.sql @@ -1,3 +1,12 @@ -- Place your SQL code here ALTER TABLE vn.mrwConfig ADD IF NOT EXISTS notified TIMESTAMP NULL COMMENT 'Date when it was notified that the web service deadline was exceeded'; + +INSERT IGNORE INTO util.notification + SET name = 'mrw-deadline', + description = 'The MRW deadline has passed'; + +INSERT IGNORE INTO util.notificationAcl (notificationFk, roleFK) + SELECT LAST_INSERT_ID(), r.id + FROM account.role r + WHERE r.name = 'delivery' \ No newline at end of file diff --git a/print/templates/email/mrw-webService-deadline/assets/css/import.js b/print/templates/email/mrw-deadline/assets/css/import.js similarity index 100% rename from print/templates/email/mrw-webService-deadline/assets/css/import.js rename to print/templates/email/mrw-deadline/assets/css/import.js diff --git a/print/templates/email/mrw-webService-deadline/locale/en.yml b/print/templates/email/mrw-deadline/locale/en.yml similarity index 100% rename from print/templates/email/mrw-webService-deadline/locale/en.yml rename to print/templates/email/mrw-deadline/locale/en.yml diff --git a/print/templates/email/mrw-webService-deadline/locale/es.yml b/print/templates/email/mrw-deadline/locale/es.yml similarity index 100% rename from print/templates/email/mrw-webService-deadline/locale/es.yml rename to print/templates/email/mrw-deadline/locale/es.yml diff --git a/print/templates/email/mrw-webService-deadline/mrw-webService-deadline.html b/print/templates/email/mrw-deadline/mrw-deadline.html similarity index 100% rename from print/templates/email/mrw-webService-deadline/mrw-webService-deadline.html rename to print/templates/email/mrw-deadline/mrw-deadline.html diff --git a/print/templates/email/mrw-webService-deadline/mrw-webService-deadline.js b/print/templates/email/mrw-deadline/mrw-deadline.js similarity index 83% rename from print/templates/email/mrw-webService-deadline/mrw-webService-deadline.js rename to print/templates/email/mrw-deadline/mrw-deadline.js index f010a861f..574d88a0d 100755 --- a/print/templates/email/mrw-webService-deadline/mrw-webService-deadline.js +++ b/print/templates/email/mrw-deadline/mrw-deadline.js @@ -2,7 +2,7 @@ const Component = require(`vn-print/core/component`); const emailBody = new Component('email-body'); module.exports = { - name: 'mrw-webService-deadline', + name: 'mrw-deadline', components: { 'email-body': emailBody.build(), }