fix: refs #6404 fix notification
gitea/salix/pipeline/pr-master There was a failure building this commit Details

This commit is contained in:
Pablo Natek 2024-06-14 17:40:08 +02:00
parent d41f1ff561
commit a30ff44837
10 changed files with 45 additions and 48 deletions

View File

@ -1,4 +1,3 @@
const {Email} = require('vn-print');
const UserError = require('vn-loopback/util/user-error'); const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
@ -21,19 +20,17 @@ module.exports = Self => {
}); });
Self.createShipment = async expeditionFk => { Self.createShipment = async expeditionFk => {
const mrwConfig = Self.app.models.MrwConfig; const models = Self.app.models;
const mrw = await Self.getConfig(); const mrw = await Self.getConfig();
const today = Date.vnNew(); const today = Date.vnNew();
const [hours, minutes] = mrw?.expeditionDeadLine ? mrw.expeditionDeadLine.split(':').map(Number) : [0, 0]; const [hours, minutes] = mrw?.expeditionDeadLine ? mrw.expeditionDeadLine.split(':').map(Number) : [0, 0];
const deadLine = Date.vnNew(); const deadLine = Date.vnNew();
deadLine.setHours(hours, minutes, 0); deadLine.setHours(hours, minutes, 0);
if (today > deadLine && (!mrw.notified || mrw.notified.setHours(0, 0, 0, 0) !== today.setHours(0, 0, 0, 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 models.NotificationQueue.create({notificationFk: 'mrw-deadline'});
await email.send();
await mrw.updateAttributes({notified: Date.vnNow()}); await mrw.updateAttributes({notified: Date.vnNow()});
} }
@ -88,10 +85,9 @@ module.exports = Self => {
); );
const shipmentId = Self.getTextByTag(shipmentResponse, 'NumeroEnvio'); const shipmentId = Self.getTextByTag(shipmentResponse, 'NumeroEnvio');
if (!shipmentId) if (!shipmentId) throw new UserError(Self.getTextByTag(shipmentResponse, 'Mensaje'));
throw new UserError(Self.getTextByTag(shipmentResponse, 'Mensaje'));
const file = await mrwConfig.getLabel(shipmentId); const file = await models.MrwConfig.getLabel(shipmentId);
return {shipmentId, file}; return {shipmentId, file};
}; };

View File

@ -2,7 +2,7 @@ const models = require('vn-loopback/server/server').models;
const axios = require('axios'); const axios = require('axios');
const fs = require('fs'); 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 mockBase64Binary = 'base64BinaryString';
const ticket1 = { const ticket1 = {
'id': '44', 'id': '44',
@ -53,15 +53,25 @@ describe('MRWConfig createShipment()', () => {
await models.Expedition.create(expedition1); await models.Expedition.create(expedition1);
}); });
beforeEach(() => { afterAll(async() => {
await cleanFixtures();
});
beforeEach(async() => {
const mockPostResponses = [ const mockPostResponses = [
{data: fs.readFileSync(__dirname + '/mockGetLabel.xml', 'utf-8')}, {data: fs.readFileSync(__dirname + '/mockGetLabel.xml', 'utf-8')},
{data: fs.readFileSync(__dirname + '/mockCreateShipment.xml', 'utf-8')} {data: fs.readFileSync(__dirname + '/mockCreateShipment.xml', 'utf-8')}
]; ];
spyOn(axios, 'post').and.callFake(() => Promise.resolve(mockPostResponses.pop())); 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() { async function createMrwConfig() {
await models.MrwConfig.create( 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() => { it('should create a shipment and return a base64Binary label', async() => {
const {file} = await models.MrwConfig.createShipment(expedition1.id); const {file} = await models.MrwConfig.createShipment(expedition1.id);
@ -87,7 +104,7 @@ describe('MRWConfig createShipment()', () => {
await models.MrwConfig.createShipment(expedition1.id).catch(e => { await models.MrwConfig.createShipment(expedition1.id).catch(e => {
error = e; error = e;
}).finally(async() => { }).finally(async() => {
expect(error.message).toEqual(`Some mrwConfig parameters are not set`); expect(error.message).toEqual(`MRW service is not configured`);
}); });
await createMrwConfig(); 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() => { 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.updateAll({id: 1}, {expeditionDeadLine: '10:00:00', notified: null});
await models.MrwConfig.createShipment(expedition1.id); await models.MrwConfig.createShipment(expedition1.id);
const notification = await getLastNotification();
const mail = await models.Mail.findOne({ expect(notification.notificationFk).toEqual(filter.notificationFk);
order: 'id DESC',
where: filter
});
expect(mail.subject).toEqual(filter.subject);
await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: null, notified: null});
}); });
it('should send mail if you are past the dead line and it is notified from another day', async() => { 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.updateAll({id: 1}, {expeditionDeadLine: '10:00:00', notified: new Date()});
await models.MrwConfig.createShipment(expedition1.id); await models.MrwConfig.createShipment(expedition1.id);
const notification = await getLastNotification();
const mail = await models.Mail.findOne({ expect(notification.notificationFk).toEqual(filter.notificationFk);
order: 'id DESC',
where: filter
});
expect(mail.subject).toEqual(filter.subject);
await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: null, notified: null});
}); });
it('should not send mail if you are past the dead line and it is notified', async() => { 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.updateAll({id: 1}, {expeditionDeadLine: '10:00:00', notified: Date.vnNew()});
await models.MrwConfig.createShipment(expedition1.id); await models.MrwConfig.createShipment(expedition1.id);
const notification = await getLastNotification();
const mail = await models.Mail.findOne({ expect(notification).toEqual(null);
order: 'id DESC',
where: filter
});
expect(mail).toEqual(null);
await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: null, notified: null});
}); });
}); });

View File

@ -11,7 +11,7 @@ module.exports = Self => {
Self.getConfig = async function() { Self.getConfig = async function() {
const mrw = await Self.app.models.MrwConfig.findOne(null); 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; return mrw;
}; };

View File

@ -2844,7 +2844,8 @@ INSERT INTO `util`.`notification` (`id`, `name`, `description`)
(5, 'modified-entry', 'An entry has been modified'), (5, 'modified-entry', 'An entry has been modified'),
(6, 'book-entry-deleted', 'accounting entries deleted'), (6, 'book-entry-deleted', 'accounting entries deleted'),
(7, 'zone-included','An email to notify zoneCollisions'), (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`; TRUNCATE `util`.`notificationAcl`;
INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`) INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`)
@ -2857,7 +2858,8 @@ INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`)
(5, 9), (5, 9),
(6, 9), (6, 9),
(7, 9), (7, 9),
(8, 66); (8, 66),
(9, 56);
TRUNCATE `util`.`notificationQueue`; TRUNCATE `util`.`notificationQueue`;
INSERT INTO `util`.`notificationQueue` (`id`, `notificationFk`, `params`, `authorFk`, `status`, `created`) INSERT INTO `util`.`notificationQueue` (`id`, `notificationFk`, `params`, `authorFk`, `status`, `created`)

View File

@ -1,3 +1,12 @@
-- Place your SQL code here -- Place your SQL code here
ALTER TABLE vn.mrwConfig ADD IF NOT EXISTS notified TIMESTAMP NULL ALTER TABLE vn.mrwConfig ADD IF NOT EXISTS notified TIMESTAMP NULL
COMMENT 'Date when it was notified that the web service deadline was exceeded'; 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'

View File

@ -2,7 +2,7 @@ const Component = require(`vn-print/core/component`);
const emailBody = new Component('email-body'); const emailBody = new Component('email-body');
module.exports = { module.exports = {
name: 'mrw-webService-deadline', name: 'mrw-deadline',
components: { components: {
'email-body': emailBody.build(), 'email-body': emailBody.build(),
} }