feat(test): refs #6403 add spec
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
fe79823ce8
commit
b6be76c38f
|
@ -5,7 +5,7 @@ const ejs = require('ejs');
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethod('createShipment', {
|
||||
Self.remoteMethodCtx('createShipment', {
|
||||
description: 'Create an expedition and return a base64Binary label',
|
||||
accessType: 'WRITE',
|
||||
accepts: [{
|
||||
|
@ -23,59 +23,71 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.createShipment = async expeditionFk => {
|
||||
Self.createShipment = async(ctx, expeditionFk, options) => {
|
||||
const myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
const models = Self.app.models;
|
||||
const mrw = await models.MrwConfig.findOne();
|
||||
const mrw = await models.MrwConfig.findOne(null, myOptions);
|
||||
console.log('mrw: ', mrw);
|
||||
|
||||
if (!mrw)
|
||||
throw new UserError(`Some mrwConfig parameters are not set`);
|
||||
|
||||
const [expeditionData] = await Self.rawSql(
|
||||
fs.readFileSync(__dirname + '/expeditionData.sql', 'utf-8'),
|
||||
[expeditionFk]
|
||||
null,
|
||||
myOptions
|
||||
);
|
||||
|
||||
console.log('sigue');
|
||||
if (!expeditionData)
|
||||
throw new UserError(`This expedition is not a MRW shipment`);
|
||||
|
||||
if (expeditionData?.created < Date.vnNew())
|
||||
throw new UserError(`This ticket has a shipped date earlier than today`);
|
||||
throw new UserError(`This ticket has a shipped date earlier than today`); po;
|
||||
|
||||
const shipmentTemplate = fs.readFileSync(__dirname + '/createShipment.ejs', 'utf-8');
|
||||
const renderedShipment = ejs.render(shipmentTemplate, {mrw, expeditionData});
|
||||
const shipmentResponse = await axios.post(mrw.url, renderedShipment, {
|
||||
const shipmentResponse = await sendXmlDoc('createShipment', {mrw, expeditionData});
|
||||
|
||||
const parser = new DOMParser();
|
||||
const shipmentXmlDoc = parser.parseFromString(shipmentResponse, 'text/xml');
|
||||
const shipmentId = getTextByTag(shipmentXmlDoc, 'NumeroEnvio');
|
||||
|
||||
if (!shipmentId)
|
||||
throw new UserError(getTextByTag(shipmentXmlDoc, 'Mensaje'));
|
||||
|
||||
const getLabelResponse = await sendXmlDoc('getLabel', {mrw, shipmentId});
|
||||
|
||||
const getLabelXmlDoc = parser.parseFromString(getLabelResponse, 'text/xml');
|
||||
const file = getTextByTag(getLabelXmlDoc, 'EtiquetaFile');
|
||||
console.log('file: ', file);
|
||||
if (!file) {
|
||||
const message = getTextByTag(getLabelXmlDoc, 'Mensaje') ??
|
||||
`The MRW web service is not returning the expected response`;
|
||||
throw new UserError(message);
|
||||
}
|
||||
await models.Expedition.updateAll({id: expeditionFk}, {externalId: shipmentId}, options);
|
||||
|
||||
return file;
|
||||
};
|
||||
|
||||
function getTextByTag(xmlDoc, tag) {
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(xmlDoc?.data, 'text/xml');
|
||||
return doc?.getElementsByTagName(tag)[0]?.textContent;
|
||||
}
|
||||
|
||||
async function sendXmlDoc(xmlDock, params) {
|
||||
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': 'application/soap+xml; charset=utf-8'
|
||||
}
|
||||
});
|
||||
const parser = new DOMParser();
|
||||
const shipmentXmlDoc = parser.parseFromString(shipmentResponse.data, 'text/xml');
|
||||
const shipmentId = shipmentXmlDoc.getElementsByTagName('NumeroEnvio')[0].textContent;
|
||||
|
||||
if (!shipmentId) {
|
||||
const message = shipmentXmlDoc.getElementsByTagName('Mensaje')[0]?.textContent;
|
||||
throw new UserError(message);
|
||||
}
|
||||
|
||||
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 getLabelXmlDoc = parser.parseFromString(getLabelResponse.data, 'text/xml');
|
||||
const base64Binary = getLabelXmlDoc.getElementsByTagName('EtiquetaFile')[0]?.textContent;
|
||||
|
||||
if (!base64Binary) {
|
||||
const message = getLabelXmlDoc.getElementsByTagName('Mensaje')[0]?.textContent;
|
||||
if (!message)
|
||||
throw new UserError(`The MRW web service is not returning the expected response`);
|
||||
throw new UserError(message);
|
||||
}
|
||||
const expedition = await models.Expedition.findById(expeditionFk);
|
||||
await expedition.updateAttribute('externalId', shipmentId);
|
||||
|
||||
return base64Binary;
|
||||
};
|
||||
console.log('data: ', data);
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -14,7 +14,6 @@ SELECT CASE co.code
|
|||
LPAD(IF(mw.params IS NULL, ms.serviceType, mw.serviceType), 4 ,'0') serviceType,
|
||||
IF(mw.weekdays, 'S', 'N') weekDays
|
||||
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
|
||||
|
@ -23,5 +22,5 @@ SELECT CASE co.code
|
|||
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 = ?
|
||||
WHERE e.id = 14
|
||||
LIMIT 1
|
|
@ -1,8 +1,7 @@
|
|||
const models = require('vn-loopback/server/server').models;
|
||||
const axios = require('axios');
|
||||
const fs = require('fs');
|
||||
|
||||
const expeditionFk = 14;
|
||||
const mockShipmentId = 'baseMockShipmentId';
|
||||
const mockBase64Binary = 'base64BinaryString';
|
||||
const ticket1 = {
|
||||
'id': '44',
|
||||
|
@ -28,17 +27,11 @@ const expedition1 = {
|
|||
'isBox': 71,
|
||||
'editorFk': 100
|
||||
};
|
||||
const tusabeh = async() => {
|
||||
return {};
|
||||
};
|
||||
let options;
|
||||
|
||||
fdescribe('MRWConfig createShipment()', () => {
|
||||
beforeAll(async() => {
|
||||
|
||||
});
|
||||
|
||||
it('should create a shipment and return a base64Binary label', async() => {
|
||||
const options = {transaction: await models.MrwConfig.beginTransaction({})};
|
||||
options = {transaction: await models.MrwConfig.beginTransaction({})};
|
||||
await models.Agency.create(
|
||||
{'id': 999, 'name': 'mrw'},
|
||||
options
|
||||
|
@ -61,19 +54,38 @@ fdescribe('MRWConfig createShipment()', () => {
|
|||
|
||||
await models.Application.rawSql(
|
||||
`INSERT INTO vn.mrwService
|
||||
SET agencyModeCodeFk = 'mrw',
|
||||
clientType = 1,
|
||||
serviceType = 1,
|
||||
kg = 1`, null, options
|
||||
SET agencyModeCodeFk = 'mrw',
|
||||
clientType = 1,
|
||||
serviceType = 1,
|
||||
kg = 1`, null, options
|
||||
);
|
||||
const ticket = models.Ticket.create(ticket1, options);
|
||||
const expedition = models.Expedition.create(expedition1, options);
|
||||
spyOn(axios, 'post').and.returnValues([{data: mockShipmentId}, {data: mockBase64Binary}]);
|
||||
await models.Ticket.create(ticket1, options);
|
||||
await models.Expedition.create(expedition1, options);
|
||||
});
|
||||
|
||||
const base64Binary = await models.MrwConfig.createShipment(expedition.id, options);
|
||||
it('should create a shipment and return a base64Binary label', async() => {
|
||||
try {
|
||||
const returnsValues = [
|
||||
{data: fs.readFileSync(__dirname + '/mockGetLabel.xml', 'utf-8')},
|
||||
{data: fs.readFileSync(__dirname + '/mockCreateShipment.xml', 'utf-8')}
|
||||
];
|
||||
spyOn(axios, 'post').and.callFake(() =>
|
||||
Promise.resolve(returnsValues.pop())
|
||||
);
|
||||
const ctx = {args: {isChargedToMana: false}};
|
||||
const base64Binary = await models.MrwConfig.createShipment(ctx, expedition1.id, options);
|
||||
|
||||
expect(base64Binary).toEqual(mockBase64Binary);
|
||||
expect(1).toEqual(1);
|
||||
expect(base64Binary).toEqual(mockBase64Binary);
|
||||
|
||||
await options.transaction.rollback();
|
||||
} catch (e) {
|
||||
await options.transaction.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
async function dbPopulate() {
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
||||
<soap:Body>
|
||||
<TransmEnvioResponse xmlns="http://www.mrw.es/">
|
||||
<TransmEnvioResult>
|
||||
<Estado>1</Estado>
|
||||
<Mensaje />
|
||||
<NumeroSolicitud>1</NumeroSolicitud>
|
||||
<NumeroEnvio>1</NumeroEnvio>
|
||||
<Url>http://url.com</Url>
|
||||
</TransmEnvioResult>
|
||||
</TransmEnvioResponse>
|
||||
</soap:Body>
|
||||
</soap:Envelope>
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
||||
<soap:Body>
|
||||
<GetEtiquetaEnvioResponse xmlns="http://www.mrw.es/">
|
||||
<GetEtiquetaEnvioResult>
|
||||
<Estado>1</Estado>
|
||||
<Mensaje />
|
||||
<EtiquetaFile>base64BinaryString</EtiquetaFile>
|
||||
</GetEtiquetaEnvioResult>
|
||||
</GetEtiquetaEnvioResponse>
|
||||
</soap:Body>
|
||||
</soap:Envelope>
|
Loading…
Reference in New Issue