feat(cancelShipment): refs #6403 mrwintegration

This commit is contained in:
Pablo Natek 2024-01-16 15:04:06 +01:00
parent 73c0ca5382
commit 7e3660aece
11 changed files with 195 additions and 114 deletions

View File

@ -3,7 +3,7 @@
// Carácter predeterminado de final de línea.
"files.eol": "\n",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"search.useIgnoreFiles": false,
"editor.defaultFormatter": "dbaeumer.vscode-eslint",

View File

@ -0,0 +1,20 @@
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:mrw="http://www.mrw.es/">
<soap:Header>
<mrw:AuthInfo>
<mrw:CodigoFranquicia><%= mrw.franchiseCode %></mrw:CodigoFranquicia>
<mrw:CodigoAbonado><%= mrw.subscriberCode %></mrw:CodigoAbonado>
<mrw:CodigoDepartamento/>
<mrw:UserName><%= mrw.user %></mrw:UserName>
<mrw:Password><%= mrw.password %></mrw:Password>
</mrw:AuthInfo>
</soap:Header>
<soap:Body>
<mrw:CancelarEnvio>
<mrw:request>
<mrw:CancelaEnvio>
<mrw:NumeroEnvioOriginal><%= externalId %></mrw:NumeroEnvioOriginal>
</mrw:CancelaEnvio>
</mrw:request>
</mrw:CancelarEnvio>
</soap:Body>
</soap:Envelope>

View File

@ -0,0 +1,46 @@
const axios = require('axios');
const fs = require('fs');
const {DOMParser} = require('xmldom');
module.exports = Self => {
Self.remoteMethod('cancelShipment', {
description: 'Cancel a shipment by providing the expedition ID, interacting with MRW WS',
accessType: 'WRITE',
accepts: [{
arg: 'expeditionFk',
type: 'number',
required: true
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/cancelShipment`,
verb: 'POST'
}
});
Self.deleteExpedition = async expeditionFk => {
const models = Self.app.models;
const mrw = await models.MrwConfig.findOne();
const {externalId} = await models.Expedition.findById(expeditionFk);
const template = fs.readFileSync(__dirname + '/cancelShipment.ejs', 'utf-8');
const renderedXml = ejs.render(template, {mrw, externalId});
const response = await axios.post(mrw.url, renderedXml, {
headers: {
'Content-Type': 'application/soap+xml; charset=utf-8'
}
});
const xmlString = response.data;
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
const resultElement = xmlDoc.getElementsByTagName('Mensaje')[0];
const result = resultElement.textContent;
return result;
};
};

View File

@ -0,0 +1,25 @@
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mrw="http://www.mrw.es/">
<soapenv:Header>
<mrw:AuthInfo>
<mrw:CodigoFranquicia><%= mrw.franchiseCode %></mrw:CodigoFranquicia>
<mrw:CodigoAbonado><%= mrw.subscriberCode %></mrw:CodigoAbonado>
<mrw:CodigoDepartamento/>
<mrw:UserName><%= mrw.user %></mrw:UserName>
<mrw:Password><%= mrw.password %></mrw:Password>
</mrw:AuthInfo>
</soapenv:Header>
<soapenv:Body>
<mrw:GetEtiquetaEnvio>
<mrw:request>
<mrw:NumeroEnvio><%= shipmentId %></mrw:NumeroEnvio>
<mrw:NumerosEtiqueta>1</mrw:NumerosEtiqueta>
<mrw:SeparadorNumerosEnvio></mrw:SeparadorNumerosEnvio>
<mrw:FechaInicioEnvio></mrw:FechaInicioEnvio>
<mrw:FechaFinEnvio></mrw:FechaFinEnvio>
<mrw:TipoEtiquetaEnvio>0</mrw:TipoEtiquetaEnvio>
<mrw:ReportTopMargin>0</mrw:ReportTopMargin>
<mrw:ReportLeftMargin>0</mrw:ReportLeftMargin>
</mrw:request>
</mrw:GetEtiquetaEnvio>
</soapenv:Body>
</soapenv:Envelope>

View File

@ -1,59 +0,0 @@
const fs = require('fs');
const ejs = require('ejs');
module.exports = Self => {
Self.remoteMethod('renderer', {
description: 'Renders the data from an XML',
accessType: 'READ',
accepts: [{
arg: 'expeditionFk',
type: 'number',
required: true
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/renderer`,
verb: 'GET'
}
});
Self.renderer = async expeditionFk => {
const mrw = await Self.app.models.MrwConfig.findOne();
const [expedition] = await Self.rawSql(
`SELECT CASE co.code
WHEN 'ES' THEN a.postalCode
WHEN 'PT' THEN LEFT(a.postalCode, 4)
WHEN 'AD' THEN REPLACE(a.postalCode, 'AD', '00')
END postalCode,
a.city,
a.street,
co.code countryCode,
c.fi,
c.name clientName,
c.phone,
e.created,
e.id expeditionId,
LPAD(ms.serviceType, 4 ,'0') serviceType,
pa.height,
pa.depth,
pa.width
FROM expedition e
JOIN packaging pa ON pa.id = e.packagingFk
JOIN ticket t ON e.ticketFk = t.id
JOIN agencyMode am ON am.id = t.agencyModeFk
JOIN mrwService ms ON ms.agencyModeCodeFk = am.code
JOIN client c ON t.clientFk = c.id
JOIN address a ON t.addressFk = a.id
JOIN province p ON a.provinceFk = p.id
JOIN country co ON co.id = p.countryFk
WHERE e.id = ?`, [expeditionFk]
);
const template = fs.readFileSync(__dirname + '/template.ejs', 'utf-8');
return ejs.render(template, {mrw, expedition});
};
};

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:mrw="http://www.mrw.es/">
<soap:Header>
<mrw:AuthInfo>
<mrw:CodigoFranquicia><%= mrw.franchiseCode %></mrw:CodigoFranquicia>
<mrw:CodigoAbonado><%= mrw.subscriberCode %></mrw:CodigoAbonado>
<mrw:CodigoDepartamento/>
<mrw:UserName><%= mrw.user %></mrw:UserName>
<mrw:Password><%= mrw.password %></mrw:Password>
</mrw:AuthInfo>
</soap:Header>
<soap:Body>
<mrw:TransmEnvio>
<mrw:request>
<mrw:DatosEntrega>
<mrw:Direccion>
<mrw:CodigoTipoVia/>
<mrw:Via><%= expeditionData.street %></mrw:Via>
<mrw:Numero/>
<mrw:Resto/>
<mrw:CodigoPostal><%= expeditionData.postalCode %></mrw:CodigoPostal>
<mrw:Poblacion><%= expeditionData.city %></mrw:Poblacion>
<mrw:Provincia/>
<mrw:CodigoPais/>
</mrw:Direccion>
<mrw:Nif><%= expeditionData.fi %></mrw:Nif>
<mrw:Nombre><%= expeditionData.clientName %></mrw:Nombre>
<mrw:Telefono><%= expeditionData.phone %></mrw:Telefono>
</mrw:DatosEntrega>
<mrw:DatosServicio>
<mrw:Fecha><%= expeditionData.created %></mrw:Fecha>
<mrw:Referencia><%= expeditionData.expeditionDataId %></mrw:Referencia>
<mrw:CodigoServicio><%= expeditionData.serviceType %></mrw:CodigoServicio>
<mrw:NumeroBultos>1</mrw:NumeroBultos>
<mrw:Peso><%= expeditionData.kg %></mrw:Peso>
<mrw:Reembolso/>
<mrw:ImporteReembolso/>
</mrw:DatosServicio>
</mrw:request>
</mrw:TransmEnvio>
</soap:Body>
</soap:Envelope>

View File

@ -1,5 +1,7 @@
const axios = require('axios');
const {DOMParser} = require('xmldom');
const fs = require('fs');
const ejs = require('ejs');
module.exports = Self => {
Self.remoteMethod('shipmentCreation', {
@ -22,25 +24,62 @@ module.exports = Self => {
Self.shipmentCreation = async expeditionFk => {
const models = Self.app.models;
const mrw = await models.MrwConfig.findOne();
const MrwConfig = await models.MrwConfig.findOne({
fields: ['url']
});
const [expeditionData] = await Self.rawSql(
`SELECT CASE co.code
WHEN 'ES' THEN a.postalCode
WHEN 'PT' THEN LEFT(a.postalCode, 4)
WHEN 'AD' THEN REPLACE(a.postalCode, 'AD', '00')
END postalCode,
a.city,
a.street,
co.code countryCode,
c.fi,
c.name clientName,
c.phone,
DATE_FORMAT(e.created, '%d/%m/%Y') created,
e.id expeditionId,
LPAD(ms.serviceType, 4 ,'0') serviceType,
pa.height,
pa.depth,
pa.width
FROM expedition e
JOIN packaging pa ON pa.id = e.packagingFk
JOIN ticket t ON e.ticketFk = t.id
JOIN agencyMode am ON am.id = t.agencyModeFk
JOIN mrwService ms ON ms.agencyModeCodeFk = am.code
JOIN client c ON t.clientFk = c.id
JOIN address a ON t.addressFk = a.id
JOIN province p ON a.provinceFk = p.id
JOIN country co ON co.id = p.countryFk
WHERE e.id = ?`, [expeditionFk]
);
const renderedXml = await models.MrwConfig.renderer(expeditionFk);
const response = await axios.post(MrwConfig.url, renderedXml, {
const shipmentTemplate = fs.readFileSync(__dirname + '/shipmentCreation.ejs', 'utf-8');
const renderedShipment = ejs.render(shipmentTemplate, {mrw, expeditionData});
const shipmentResponse = await axios.post(mrw.url, renderedShipment, {
headers: {
'Content-Type': 'application/soap+xml; charset=utf-8'
}
});
const xmlString = response.data;
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
const transmEnvioResponse = xmlDoc.getElementsByTagName('TransmEnvioResponse')[0];
console.log(xmlDoc.getElementsByTagName('TransmEnvioResponse'));
console.log('transmEnvioResponse: ', transmEnvioResponse);
const shipmentXmlDoc = parser.parseFromString(shipmentResponse.data, 'text/xml');
const shipmentId = shipmentXmlDoc.getElementsByTagName('NumeroEnvio')[0].textContent;
return transmEnvioResponse.textContent;
const getLabelTemplate = fs.readFileSync(__dirname + '/getLabel.ejs', 'utf-8');
const renderedGetLabel = ejs.render(getLabelTemplate, {mrw, shipmentId});
const getLabelResponse = await axios.post(mrw.url, renderedGetLabel, {
headers: {
'Content-Type': 'text/xml; charset=utf-8'
}
});
const getLableXmlDoc = parser.parseFromString(getLabelResponse.data, 'text/xml');
const base64Binary = getLableXmlDoc.getElementsByTagName('EtiquetaFile')[0].textContent;
const expedition = await models.Expedition.findById(expeditionFk);
await expedition.updateAttribute('externalId', shipmentId);
return base64Binary;
};
};

View File

@ -1,40 +0,0 @@
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:mrw="http://www.mrw.es/">
<soap:Header>
<mrw:AuthInfo>
<mrw:CodigoFranquicia><%= mrw.franchiseCode %></mrw:CodigoFranquicia>
<mrw:CodigoAbonado><%= mrw.subscriberCode %></mrw:CodigoAbonado>
<mrw:UserName><%= mrw.user %></mrw:UserName>
<mrw:Password><%= mrw.password %></mrw:Password>
</mrw:AuthInfo>
</soap:Header>
<soap:Body>
<mrw:TransmEnvio>
<mrw:request>
<mrw:DatosEntrega>
<mrw:Direccion>
<mrw:Via><%= expedition.street %></mrw:Via>
<mrw:CodigoPostal><%= expedition.postalCode %></mrw:CodigoPostal>
<mrw:Poblacion><%= expedition.city %></mrw:Poblacion>
<mrw:CodigoPais><%= expedition.countryCode %></mrw:CodigoPais>
</mrw:Direccion>
<mrw:Nif><%= expedition.fi %></mrw:Nif>
<mrw:Nombre><%= expedition.clientName %></mrw:Nombre>
<mrw:Telefono><%= expedition.phone %></mrw:Telefono>
</mrw:DatosEntrega>
<mrw:DatosServicio>
<mrw:Fecha><%= expedition.created %></mrw:Fecha>
<mrw:Referencia><%= expedition.expeditionId %></mrw:Referencia>
<mrw:CodigoServicio><%= expedition.serviceType %></mrw:CodigoServicio>
<mrw:Bultos>
<mrw:BultoRequest>
<mrw:Alto><%= expedition.height %></mrw:Alto>
<mrw:Largo><%= expedition.depth %></mrw:Largo>
<mrw:Ancho><%= expedition.width %></mrw:Ancho>
</mrw:BultoRequest>
</mrw:Bultos>
<mrw:Peso><%= expedition.kg %></mrw:Peso>
</mrw:DatosServicio>
</mrw:request>
</mrw:TransmEnvio>
</soap:Body>
</soap:Envelope>

View File

@ -1,4 +1,4 @@
module.exports = Self => {
require('../methods/mrw-config/shipmentCreation')(Self);
require('../methods/mrw-config/renderer')(Self);
require('../methods/mrw-config/cancelShipment')(Self);
};

View File

@ -18,6 +18,9 @@
"user": {
"type": "string"
},
"password": {
"type": "string"
},
"franchiseCode": {
"type": "string"
},

View File

@ -13,7 +13,10 @@ COLLATE=utf8mb3_unicode_ci;
INSERT INTO vn.mrwConfig (url, `user`,password,franchiseCode,subscriberCode)
VALUES ('https://sagec-test.mrw.es/MRWEnvio.asmx', '04301SGVERDNATURA', 'Verdnatura@4301V', '009731', '04301');
VALUES ('https://sagec-test.mrw.es/MRWEnvio.asmx', '04301SGVERDNATURA', 'Verdnatura@4301V', '04301', '009731');
INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId)
VALUES('MrwConfig', 'cancelShipment', 'WRITE', 'ALLOW', 'ROLE', 'employee');
INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId)
VALUES ('MrwConfig','renderer','READ','ALLOW','ROLE','employee');
@ -33,3 +36,5 @@ INSERT INTO vn.ticket (id, clientFk, warehouseFk, shipped, nickname, refFk, addr
INSERT INTO vn.expedition (agencyModeFk,ticketFk,freightItemFk,created,counter,workerFk,packagingFk,hostFk,stateTypeFk,hasNewRoute,isBox,editorFk)
VALUES (25,33,71,'2001-01-01 01:00:00.000',1,18,'94','',3,0,71,100);
INSERT INTO vn.mrwService (agencyModeCodeFk, clientType, serviceType, kg) VALUES('MRW', 9731, 205, 10);