fix: refs #6404 fix notification
gitea/salix/pipeline/pr-master There was a failure building this commit
Details
gitea/salix/pipeline/pr-master There was a failure building this commit
Details
This commit is contained in:
parent
d41f1ff561
commit
a30ff44837
|
@ -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};
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -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`)
|
||||
|
|
|
@ -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'
|
|
@ -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(),
|
||||
}
|
Loading…
Reference in New Issue