From c5227d6cf652515eb53c708d43862aadfd3fe9e3 Mon Sep 17 00:00:00 2001 From: pablone Date: Mon, 10 Jun 2024 12:24:23 +0200 Subject: [PATCH] feat: refs #6404 add mail notification --- back/methods/mrw-config/createShipment.js | 73 ++++------ back/methods/mrw-config/getLabel.js | 27 ++++ .../mrw-config/specs/createShipment.spec.js | 133 ++++++++++++------ back/models/mrw-config.js | 31 ++++ back/models/mrw-config.json | 6 + .../11086-grayCataractarum/00-firstScript.sql | 3 + loopback/locale/es.json | 4 +- .../components/email-footer/email-footer.html | 8 +- .../assets/css/import.js | 11 ++ .../mrw-webService-deadline/locale/en.yml | 5 + .../mrw-webService-deadline/locale/es.yml | 5 + .../mrw-webService-deadline.html | 10 ++ .../mrw-webService-deadline.js | 9 ++ 13 files changed, 228 insertions(+), 97 deletions(-) create mode 100644 back/methods/mrw-config/getLabel.js create mode 100644 db/versions/11086-grayCataractarum/00-firstScript.sql create mode 100644 print/templates/email/mrw-webService-deadline/assets/css/import.js create mode 100644 print/templates/email/mrw-webService-deadline/locale/en.yml create mode 100644 print/templates/email/mrw-webService-deadline/locale/es.yml create mode 100644 print/templates/email/mrw-webService-deadline/mrw-webService-deadline.html create mode 100755 print/templates/email/mrw-webService-deadline/mrw-webService-deadline.js diff --git a/back/methods/mrw-config/createShipment.js b/back/methods/mrw-config/createShipment.js index b5bea648d..ca155919f 100644 --- a/back/methods/mrw-config/createShipment.js +++ b/back/methods/mrw-config/createShipment.js @@ -1,7 +1,4 @@ -const axios = require('axios'); -const {DOMParser} = require('xmldom'); -const fs = require('fs'); -const ejs = require('ejs'); +const {Email} = require('vn-print'); const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { @@ -23,24 +20,23 @@ module.exports = Self => { } }); - Self.createShipment = async(expeditionFk, options) => { - const myOptions = {}; - let tx; + Self.createShipment = async expeditionFk => { + const mrwConfig = Self.app.models.MrwConfig; + const mrw = await Self.getConfig(); - if (typeof options == 'object') - Object.assign(myOptions, options); + const today = Date.vnNew(); - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; + 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 mrw.updateAttributes({notified: Date.vnNow()}); } - const models = Self.app.models; - const mrw = await models.MrwConfig.findOne(null, myOptions); - - if (!mrw) - throw new UserError(`Some mrwConfig parameters are not set`); - const query = `SELECT CASE co.code @@ -64,7 +60,8 @@ module.exports = Self => { JOIN ticket t ON e.ticketFk = t.id JOIN agencyMode am ON am.id = t.agencyModeFk JOIN mrwService ms ON ms.agencyModeCodeFk = am.code - LEFT JOIN mrwServiceWeekday mw ON mw.weekdays | 1 << WEEKDAY(t.landed) + LEFT JOIN mrwServiceWeekday mw ON mw.agencyModeCodeFk = am.code + AND mw.weekDays & (1 << WEEKDAY(t.landed)) JOIN client c ON t.clientFk = c.id JOIN address a ON t.addressFk = a.id LEFT JOIN addressObservation oa ON oa.addressFk = a.id @@ -76,44 +73,26 @@ module.exports = Self => { WHERE e.id = ? LIMIT 1`; - const [expeditionData] = await Self.rawSql(query, [expeditionFk], myOptions); + const [expeditionData] = await Self.rawSql(query, [expeditionFk]); if (!expeditionData) throw new UserError(`This expedition is not a MRW shipment`); - const today = Date.vnNew(); - today.setHours(0, 0, 0, 0); - if (expeditionData?.shipped.setHours(0, 0, 0, 0) < today) + if (expeditionData?.shipped.setHours(0, 0, 0, 0) < today.setHours(0, 0, 0, 0)) throw new UserError(`This ticket has a shipped date earlier than today`); - const shipmentResponse = await sendXmlDoc('createShipment', {mrw, expeditionData}, 'application/soap+xml'); - const shipmentId = getTextByTag(shipmentResponse, 'NumeroEnvio'); + const shipmentResponse = await Self.sendXmlDoc( + __dirname + `/createShipment.ejs`, + {mrw, expeditionData}, + 'application/soap+xml' + ); + const shipmentId = Self.getTextByTag(shipmentResponse, 'NumeroEnvio'); if (!shipmentId) - throw new UserError(getTextByTag(shipmentResponse, 'Mensaje')); + throw new UserError(Self.getTextByTag(shipmentResponse, 'Mensaje')); - const getLabelResponse = await sendXmlDoc('getLabel', {mrw, shipmentId}, 'text/xml'); - const file = getTextByTag(getLabelResponse, 'EtiquetaFile'); - - if (tx) await tx.commit(); + const file = await mrwConfig.getLabel(shipmentId); return {shipmentId, file}; }; - - function getTextByTag(xmlDoc, tag) { - return xmlDoc?.getElementsByTagName(tag)[0]?.textContent; - } - - async function sendXmlDoc(xmlDock, params, contentType) { - const parser = new DOMParser(); - - const xmlTemplate = fs.readFileSync(__dirname + `/${xmlDock}.ejs`, 'utf-8'); - const renderedTemplate = ejs.render(xmlTemplate, params); - const data = await axios.post(params.mrw.url, renderedTemplate, { - headers: { - 'Content-Type': `${contentType}; charset=utf-8` - } - }); - return parser.parseFromString(data.data, 'text/xml'); - } }; diff --git a/back/methods/mrw-config/getLabel.js b/back/methods/mrw-config/getLabel.js new file mode 100644 index 000000000..ae6bc8f21 --- /dev/null +++ b/back/methods/mrw-config/getLabel.js @@ -0,0 +1,27 @@ +module.exports = Self => { + Self.remoteMethod('getLabel', { + description: 'Return a base64Binary label from de MRW WebService', + accessType: 'READ', + accepts: [{ + arg: 'shipmentId', + type: 'number', + required: true + }], + returns: { + type: 'string', + root: true + }, + http: { + path: `/getLabel`, + verb: 'GET' + } + }); + + Self.getLabel = async shipmentId => { + const mrw = await Self.getConfig(); + + const getLabelResponse = await Self.sendXmlDoc(__dirname + `/getLabel.ejs`, {mrw, shipmentId}, 'text/xml'); + + return Self.getTextByTag(getLabelResponse, 'EtiquetaFile'); + }; +}; diff --git a/back/methods/mrw-config/specs/createShipment.spec.js b/back/methods/mrw-config/specs/createShipment.spec.js index f05f9a81d..a73331971 100644 --- a/back/methods/mrw-config/specs/createShipment.spec.js +++ b/back/methods/mrw-config/specs/createShipment.spec.js @@ -2,6 +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 mockBase64Binary = 'base64BinaryString'; const ticket1 = { 'id': '44', @@ -28,25 +29,40 @@ const expedition1 = { 'editorFk': 100 }; -let tx; -let options; - -describe('MRWConfig createShipment()', () => { - beforeEach(async() => { - options = tx = undefined; - tx = await models.MrwConfig.beginTransaction({}); - options = {transaction: tx}; - +fdescribe('MRWConfig createShipment()', () => { + beforeAll(async() => { await models.Agency.create( - {'id': 999, 'name': 'mrw'}, - options + {'id': 999, 'name': 'mrw'} ); await models.AgencyMode.create( - {'id': 999, 'name': 'mrw', 'agencyFk': 999, 'code': 'mrw'}, - options + {'id': 999, 'name': 'mrw', 'agencyFk': 999, 'code': 'mrw'} + ); + await createMrwConfig(); + + await models.Application.rawSql( + `INSERT INTO vn.mrwService + SET agencyModeCodeFk = 'mrw', + clientType = 1, + serviceType = 1, + kg = 1`, null + ); + await models.Ticket.create(ticket1); + await models.Expedition.create(expedition1); + }); + + beforeEach(() => { + 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())); + }); + + async function createMrwConfig() { await models.MrwConfig.create( { 'id': 1, @@ -55,67 +71,100 @@ describe('MRWConfig createShipment()', () => { 'password': 'password', 'franchiseCode': 'franchiseCode', 'subscriberCode': 'subscriberCode' - }, options + } ); - - await models.Application.rawSql( - `INSERT INTO vn.mrwService - SET agencyModeCodeFk = 'mrw', - clientType = 1, - serviceType = 1, - kg = 1`, null, options - ); - await models.Ticket.create(ticket1, options); - await models.Expedition.create(expedition1, options); - }); - - afterEach(async() => { - await tx.rollback(); - }); + } it('should create a shipment and return a base64Binary label', 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())); - - const {file} = await models.MrwConfig.createShipment(expedition1.id, options); + const {file} = await models.MrwConfig.createShipment(expedition1.id); expect(file).toEqual(mockBase64Binary); }); it('should fail if mrwConfig has no data', async() => { let error; + await models.MrwConfig.destroyAll(); await models.MrwConfig.createShipment(expedition1.id).catch(e => { error = e; }).finally(async() => { expect(error.message).toEqual(`Some mrwConfig parameters are not set`); }); + await createMrwConfig(); expect(error).toBeDefined(); }); it('should fail if expeditionFk is not a MrwExpedition', async() => { let error; - await models.MrwConfig.createShipment(undefined, options).catch(e => { + await models.MrwConfig.createShipment(undefined).catch(e => { error = e; }).finally(async() => { expect(error.message).toEqual(`This expedition is not a MRW shipment`); }); }); - it(' should fail if the creation date of this ticket is before the current date it', async() => { + it('should fail if the creation date of this ticket is before the current date', async() => { let error; const yesterday = Date.vnNew(); - yesterday.setDate(yesterday.getDate() - 1); - await models.Ticket.updateAll({id: ticket1.id}, {shipped: yesterday}, options); - await models.MrwConfig.createShipment(expedition1.id, options).catch(e => { + + await models.Ticket.updateAll({id: ticket1.id}, {shipped: yesterday}); + await models.MrwConfig.createShipment(expedition1.id).catch(e => { error = e; }).finally(async() => { expect(error.message).toEqual(`This ticket has a shipped date earlier than today`); }); + await models.Ticket.updateAll({id: ticket1.id}, {shipped: Date.vnNew()}); + }); + + 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 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}); + }); + + 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 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}); + }); + + 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 mail = await models.Mail.findOne({ + order: 'id DESC', + where: filter + }); + + expect(mail).toEqual(null); + + await models.MrwConfig.updateAll({id: 1}, {expeditionDeadLine: null, notified: null}); }); }); diff --git a/back/models/mrw-config.js b/back/models/mrw-config.js index f764b91cc..4496a9a56 100644 --- a/back/models/mrw-config.js +++ b/back/models/mrw-config.js @@ -1,4 +1,35 @@ module.exports = Self => { require('../methods/mrw-config/createShipment')(Self); + require('../methods/mrw-config/getLabel')(Self); require('../methods/mrw-config/cancelShipment')(Self); + + const fs = require('fs'); + const ejs = require('ejs'); + const UserError = require('vn-loopback/util/user-error'); + const {DOMParser} = require('xmldom'); + const axios = require('axios'); + + Self.getConfig = async function() { + const mrw = await Self.app.models.MrwConfig.findOne(null); + if (!mrw) throw new UserError(`Some mrwConfig parameters are not set`); + return mrw; + }; + + Self.getTextByTag = function(xmlDoc, tag) { + return xmlDoc?.getElementsByTagName(tag)[0]?.textContent; + }; + + Self.sendXmlDoc = async function(path, params, contentType) { + const parser = new DOMParser(); + + const xmlTemplate = fs.readFileSync(path, 'utf-8'); + const renderedTemplate = ejs.render(xmlTemplate, params); + const data = await axios.post(params.mrw.url, renderedTemplate, { + headers: { + 'Content-Type': `${contentType}; charset=utf-8` + } + }); + return parser.parseFromString(data.data, 'text/xml'); + }; }; + diff --git a/back/models/mrw-config.json b/back/models/mrw-config.json index b0e9754bd..c96b68cf3 100644 --- a/back/models/mrw-config.json +++ b/back/models/mrw-config.json @@ -39,6 +39,12 @@ }, "defaultWeight": { "type": "number" + }, + "expeditionDeadLine": { + "type": "string" + }, + "notified":{ + "type": "date" } } } diff --git a/db/versions/11086-grayCataractarum/00-firstScript.sql b/db/versions/11086-grayCataractarum/00-firstScript.sql new file mode 100644 index 000000000..20ed9d8f2 --- /dev/null +++ b/db/versions/11086-grayCataractarum/00-firstScript.sql @@ -0,0 +1,3 @@ +-- 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'; diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 4a7e1505c..1324e21d3 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -359,5 +359,7 @@ "It was not able to create the invoice": "No se pudo crear la factura", "ticketCommercial": "El ticket {{ ticket }} para el vendedor {{ salesMan }} está en preparación. (mensaje generado automáticamente)", "This PDA is already assigned to another user": "Este PDA ya está asignado a otro usuario", - "You can only have one PDA": "Solo puedes tener un PDA" + "You can only have one PDA": "Solo puedes tener un PDA", + "1) Fecha de recogida incorrecta (fecha cerrada).": "1) Fecha de recogida incorrecta (fecha cerrada).", + "This expedition is not a MRW shipment": "This expedition is not a MRW shipment" } \ No newline at end of file diff --git a/print/core/components/email-footer/email-footer.html b/print/core/components/email-footer/email-footer.html index 9c5df59a9..5cacdd129 100644 --- a/print/core/components/email-footer/email-footer.html +++ b/print/core/components/email-footer/email-footer.html @@ -1,19 +1,16 @@ \ No newline at end of file diff --git a/print/templates/email/mrw-webService-deadline/assets/css/import.js b/print/templates/email/mrw-webService-deadline/assets/css/import.js new file mode 100644 index 000000000..4b4bb7086 --- /dev/null +++ b/print/templates/email/mrw-webService-deadline/assets/css/import.js @@ -0,0 +1,11 @@ +const Stylesheet = require(`vn-print/core/stylesheet`); + +const path = require('path'); +const vnPrintPath = path.resolve('print'); + +module.exports = new Stylesheet([ + `${vnPrintPath}/common/css/spacing.css`, + `${vnPrintPath}/common/css/misc.css`, + `${vnPrintPath}/common/css/layout.css`, + `${vnPrintPath}/common/css/email.css`]) + .mergeStyles(); diff --git a/print/templates/email/mrw-webService-deadline/locale/en.yml b/print/templates/email/mrw-webService-deadline/locale/en.yml new file mode 100644 index 000000000..e10ec9e58 --- /dev/null +++ b/print/templates/email/mrw-webService-deadline/locale/en.yml @@ -0,0 +1,5 @@ +subject: Exceeding MRW Cut-off Time +title: Exceeding MRW Cut-off Time +greeting: Dear Team. +body: Please be informed that we have exceeded the cut-off time indicated by MRW. From this moment, all generated labels will have a delivery date for tomorrow.It is necessary to contact the MRW representatives to manage any urgencies or clarifications that may arise. +footer: Best regards. diff --git a/print/templates/email/mrw-webService-deadline/locale/es.yml b/print/templates/email/mrw-webService-deadline/locale/es.yml new file mode 100644 index 000000000..656639fc3 --- /dev/null +++ b/print/templates/email/mrw-webService-deadline/locale/es.yml @@ -0,0 +1,5 @@ +subject: Superación de la Hora de Corte de MRW +title: Superación de la Hora de Corte de MRW +greeting: Estimado equipo. +body: Les informo que hemos superado la hora de corte indicada por MRW. A partir de este momento, todas las etiquetas generadas tendrán fecha de entrega para mañana.Es necesario que se pongan en contacto con los responsables de MRW para gestionar cualquier urgencia o aclaración que puedan necesitar. +footer: Saludos cordiales. diff --git a/print/templates/email/mrw-webService-deadline/mrw-webService-deadline.html b/print/templates/email/mrw-webService-deadline/mrw-webService-deadline.html new file mode 100644 index 000000000..adcf0228c --- /dev/null +++ b/print/templates/email/mrw-webService-deadline/mrw-webService-deadline.html @@ -0,0 +1,10 @@ + +
+
+

{{ $t('title') }}

+

{{$t('greeting')}}

+

{{$t('body')}}

+

{{$t('footer')}}

+
+
+
diff --git a/print/templates/email/mrw-webService-deadline/mrw-webService-deadline.js b/print/templates/email/mrw-webService-deadline/mrw-webService-deadline.js new file mode 100755 index 000000000..f010a861f --- /dev/null +++ b/print/templates/email/mrw-webService-deadline/mrw-webService-deadline.js @@ -0,0 +1,9 @@ +const Component = require(`vn-print/core/component`); +const emailBody = new Component('email-body'); + +module.exports = { + name: 'mrw-webService-deadline', + components: { + 'email-body': emailBody.build(), + } +};