refs #4734 feat: xml en otro archivo
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2023-06-19 14:27:24 +02:00
parent 303be74382
commit cfe340eeff
5 changed files with 285 additions and 163 deletions

View File

@ -1,4 +1,5 @@
const axios = require('axios'); const axios = require('axios');
const fs = require('fs');
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('internationalExpedition', { Self.remoteMethod('internationalExpedition', {
@ -19,163 +20,72 @@ module.exports = Self => {
} }
}); });
Self.internationalExpedition = async(expeditionFk, options) => { Self.internationalExpedition = async expeditionFk => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const expedition = await models.Expedition.findOne({
fields: ['id', 'ticketFk'],
where: {id: expeditionFk},
include: [
{
relation: 'ticket',
scope: {
fields: ['shipped', 'addressFk', 'clientFk', 'companyFk'],
include: [
{
relation: 'client',
scope: {
fields: ['mobile', 'phone', 'email']
}
},
{
relation: 'address',
scope: {
fields: [
'nickname',
'street',
'postalCode',
'city',
'mobile',
'phone',
'provinceFk'
],
include: {
relation: 'province',
scope: {
fields: ['name', 'countryFk'],
include: {
relation: 'country',
scope: {
fields: ['code'],
}
}
}
}
}
},
{
relation: 'company',
scope: {
fields: ['clientFk'],
include: {
relation: 'client',
scope: {
fields: ['socialName', 'mobile', 'phone', 'email', 'defaultAddressFk'],
include: {
relation: 'defaultAddress',
scope: {
fields: [
'street',
'postalCode',
'city',
'mobile',
'phone',
'provinceFk'
],
include: {
relation: 'province',
scope: {
fields: ['name']
}
}
}
}
}
}
}
}
]
}
}
]
}, myOptions);
const viaexpressConfig = await models.ViaexpressConfig.findOne({ const viaexpressConfig = await models.ViaexpressConfig.findOne({
fields: ['url', 'clientViaexpress', 'userViaexpress', 'passwordViaexpress', 'defaultWeight', 'deliveryType'] fields: ['url']
}, myOptions); });
const shipped = expedition.ticket().shipped; const renderedXml = await models.ViaexpressConfig.renderer(expeditionFk);
const year = shipped.getFullYear(); // const renderedXml = `<?xml version="1.0" encoding="utf-8"?>
const month = shipped.getMonth() + 1; // <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
const day = shipped.getDate(); // <soap12:Body>
const date = `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`; // <PutExpedicionInternacional xmlns="http://82.223.6.71:82">
// <ObjetoEnvio>
const xmlData = `<?xml version="1.0" encoding="utf-8"?> // <Peso>10</Peso>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> // <Bultos>1</Bultos>
<soap12:Body> // <Reembolso>0</Reembolso>
<PutExpedicionInternacional xmlns="http://82.223.6.71:82"> // <Fecha>2023-06-12</Fecha>
<ObjetoEnvio> // <ConRetorno>0</ConRetorno>
<Peso>${viaexpressConfig.defaultWeight}</Peso> // <Tipo>E</Tipo>
<Bultos>1</Bultos> // <Debidos>0</Debidos>
<Reembolso>0</Reembolso> // <Asegurado>0</Asegurado>
<Fecha>${date}</Fecha> // <Imprimir>0</Imprimir>
<ConRetorno>0</ConRetorno> // <ConDevolucionAlbaran>0</ConDevolucionAlbaran>
<Tipo>${viaexpressConfig.deliveryType}</Tipo> // <Intradia>0</Intradia>
<Debidos>0</Debidos> // <Observaciones></Observaciones>
<Asegurado>0</Asegurado> // <AlbaranRemitente></AlbaranRemitente>
<Imprimir>0</Imprimir> // <Modo>0</Modo>
<ConDevolucionAlbaran>0</ConDevolucionAlbaran> // <TextoAgencia></TextoAgencia>
<Intradia>0</Intradia> // <Terminal></Terminal>
<Observaciones></Observaciones> // <ObjetoRemitente>
<AlbaranRemitente></AlbaranRemitente> // <RazonSocial>VERDNATURA LEVANTE SL</RazonSocial>
<Modo>0</Modo> // <Domicilio>FENOLLARS, 2</Domicilio>
<TextoAgencia></TextoAgencia> // <Cpostal>46680</Cpostal>
<Terminal></Terminal> // <Poblacion>Algemesi</Poblacion>
<ObjetoRemitente> // <Provincia>Valencia</Provincia>
<RazonSocial>${expedition.ticket().company().client().socialName}</RazonSocial> // <Contacto></Contacto>
<Domicilio>${expedition.ticket().company().client().defaultAddress().street}</Domicilio> // <Telefono>963677177</Telefono>
<Cpostal>${expedition.ticket().company().client().defaultAddress().postalCode}</Cpostal> // <Email>pako.natek@gmail.com</Email>
<Poblacion>${expedition.ticket().company().client().defaultAddress().city}</Poblacion> // </ObjetoRemitente>
<Provincia>${expedition.ticket().company().client().defaultAddress().province().name}</Provincia> // <ObjetoDestinatario>
<Contacto></Contacto> // <RazonSocial>ROSAMARY FLORISTERIA</RazonSocial>
<Telefono>${expedition.ticket().company().client().defaultAddress().mobile || expedition.ticket().company().client().defaultAddress().phone || expedition.ticket().company().client().mobile || expedition.ticket().company().client().phone}</Telefono> // <Domicilio>C SANTA ROSA,25</Domicilio>
<Email>${expedition.ticket().company().client().email}</Email> // <Cpostal>03802</Cpostal>
</ObjetoRemitente> // <Poblacion>ALCOY</Poblacion>
<ObjetoDestinatario> // <Municipio>ALCOY</Municipio>
<RazonSocial>${expedition.ticket().address().nickname}</RazonSocial> // <Provincia>Alicante</Provincia>
<Domicilio>${expedition.ticket().address().street}</Domicilio> // <Contacto></Contacto>
<Cpostal>${expedition.ticket().address().postalCode}</Cpostal> // <Telefono>653967489</Telefono>
<Poblacion>${expedition.ticket().address().city}</Poblacion> // <Email>correo@floristeriarosamary.com</Email>
<Municipio></Municipio> // <Pais>ES</Pais>
<Provincia>${expedition.ticket().address().province().name}</Provincia> // </ObjetoDestinatario>
<Contacto></Contacto> // <ObjetoLogin>
<Telefono>${expedition.ticket().address().mobile || expedition.ticket().address().phone || expedition.ticket().client().mobile || expedition.ticket().client().phone}</Telefono> // <IdCliente>16092</IdCliente>
<Email>${expedition.ticket().client().email}</Email> // <Usuario>B97367486</Usuario>
<Pais>${expedition.ticket().address().province().country().code}</Pais> // <Password>VERDNA22</Password>
</ObjetoDestinatario> // </ObjetoLogin>
<ObjetoLogin> // </ObjetoEnvio>
<IdCliente>${viaexpressConfig.clientViaexpress}</IdCliente> // </PutExpedicionInternacional>
<Usuario>${viaexpressConfig.userViaexpress}</Usuario> // </soap12:Body>
<Password>${viaexpressConfig.passwordViaexpress}</Password> // </soap12:Envelope>`;
</ObjetoLogin> const response = await axios.post(`${viaexpressConfig.url}ServicioVxClientes.asmx`, renderedXml, {
</ObjetoEnvio>
</PutExpedicionInternacional>
</soap12:Body>
</soap12:Envelope>`;
try {
const response = await axios.post(`${viaexpressConfig.url}ServicioVxClientes.asmx`, xmlData, {
headers: { headers: {
'Content-Type': 'application/soap+xml; charset=utf-8' 'Content-Type': 'application/soap+xml; charset=utf-8'
} }
}); });
console.log(response);
const startTag = '<ReferenciaVx>'; const startTag = '<ReferenciaVx>';
const endTag = '</ReferenciaVx>'; const endTag = '</ReferenciaVx>';
const startIndex = response.data.indexOf(startTag) + startTag.length; const startIndex = response.data.indexOf(startTag) + startTag.length;
@ -183,9 +93,5 @@ module.exports = Self => {
const referenciaVx = response.data.substring(startIndex, endIndex); const referenciaVx = response.data.substring(startIndex, endIndex);
return referenciaVx; return referenciaVx;
} catch (error) {
if (error?.response?.data) throw Error(error.response.data);
throw Error(error);
}
}; };
}; };

View File

@ -0,0 +1,162 @@
const fs = require('fs');
const handlebars = require('handlebars');
module.exports = Self => {
Self.remoteMethod('renderer', {
description: 'Returns the lastest campaigns',
accessType: 'READ',
accepts: [{
arg: 'expeditionFk',
type: 'number',
required: true
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/renderer`,
verb: 'GET'
}
});
Self.renderer = async expeditionFk => {
const models = Self.app.models;
const viaexpressConfig = await models.ViaexpressConfig.findOne({
fields: ['clientViaexpress', 'userViaexpress', 'passwordViaexpress', 'defaultWeight', 'deliveryType']
});
const expedition = await models.Expedition.findOne({
fields: ['id', 'ticketFk'],
where: {id: expeditionFk},
include: [
{
relation: 'ticket',
scope: {
fields: ['shipped', 'addressFk', 'clientFk', 'companyFk'],
include: [
{
relation: 'client',
scope: {
fields: ['mobile', 'phone', 'email']
}
},
{
relation: 'address',
scope: {
fields: [
'nickname',
'street',
'postalCode',
'city',
'mobile',
'phone',
'provinceFk'
],
include: {
relation: 'province',
scope: {
fields: ['name', 'countryFk'],
include: {
relation: 'country',
scope: {
fields: ['code'],
}
}
}
}
}
},
{
relation: 'company',
scope: {
fields: ['clientFk'],
include: {
relation: 'client',
scope: {
fields: ['socialName', 'mobile', 'phone', 'email', 'defaultAddressFk'],
include: {
relation: 'defaultAddress',
scope: {
fields: [
'street',
'postalCode',
'city',
'mobile',
'phone',
'provinceFk'
],
include: {
relation: 'province',
scope: {
fields: ['name']
}
}
}
}
}
}
}
}
]
}
}
]
});
const shipped = expedition.ticket().shipped;
const year = shipped.getFullYear();
const month = shipped.getMonth() + 1;
const day = shipped.getDate();
const date = `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
const data = {
defaultWeight: viaexpressConfig.defaultWeight,
date: date, // const date = new Date().toISOString();
deliveryType: viaexpressConfig.deliveryType,
senderName: expedition.ticket().company().client().socialName,
senderStreet: expedition.ticket().company().client().defaultAddress().street,
senderPostalCode: expedition.ticket().company().client().defaultAddress().postalCode,
senderCity: expedition.ticket().company().client().defaultAddress().city,
senderProvince: expedition.ticket().company().client().defaultAddress().province().name,
senderPhone: expedition.ticket().company().client().defaultAddress().mobile
|| expedition.ticket().company().client().defaultAddress().phone
|| expedition.ticket().company().client().mobile
|| expedition.ticket().company().client().phone,
senderEmail: expedition.ticket().company().client().email,
receiverName: expedition.ticket().address().nickname,
receiverStreet: expedition.ticket().address().street,
receiverPostalCode: expedition.ticket().address().postalCode,
receiverCity: expedition.ticket().address().city,
receiverProvince: expedition.ticket().address().province().name,
receiverPhone: expedition.ticket().address().mobile
|| expedition.ticket().address().phone
|| expedition.ticket().client().mobile
|| expedition.ticket().client().phone,
receiverEmail: expedition.ticket().client().email,
receiverCountry: expedition.ticket().address().province().country().code,
clientViaexpress: viaexpressConfig.clientViaexpress,
userViaexpress: viaexpressConfig.userViaexpress,
passwordViaexpress: viaexpressConfig.passwordViaexpress
};
const templateXml = fs.readFileSync(__dirname + '/template.xml', 'utf-8');
// // Crea una instancia de Vue con los datos
// const vueInstance = new Vue({
// data: data,
// template: templateXml,
// });
// // Renderiza la plantilla con los datos utilizando vue-server-renderer
// const renderer = createRenderer();
// return renderer.renderToString(vueInstance);
const compiledTemplate = handlebars.compile(templateXml);
const renderedHtml = compiledTemplate(data);
return renderedHtml;
};
};

View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<PutExpedicionInternacional xmlns="http://82.223.6.71:82">
<ObjetoEnvio>
<Peso>{{ defaultWeight }}</Peso>
<Bultos>1</Bultos>
<Reembolso>0</Reembolso>
<Fecha>{{ date }}</Fecha>
<ConRetorno>0</ConRetorno>
<Tipo>{{ deliveryType }}</Tipo>
<Debidos>0</Debidos>
<Asegurado>0</Asegurado>
<Imprimir>0</Imprimir>
<ConDevolucionAlbaran>0</ConDevolucionAlbaran>
<Intradia>0</Intradia>
<Observaciones></Observaciones>
<AlbaranRemitente></AlbaranRemitente>
<Modo>0</Modo>
<TextoAgencia></TextoAgencia>
<Terminal></Terminal>
<ObjetoRemitente>
<RazonSocial>{{ senderName }}</RazonSocial>
<Domicilio>{{ senderStreet }}</Domicilio>
<Cpostal>{{ senderPostalCode }}</Cpostal>
<Poblacion>{{ senderCity }}</Poblacion>
<Provincia>{{ senderProvince }}</Provincia>
<Contacto></Contacto>
<Telefono>{{ senderPhone }}</Telefono>
<Email>{{ senderEmail }}</Email>
</ObjetoRemitente>
<ObjetoDestinatario>
<RazonSocial>{{ receiverName }}</RazonSocial>
<Domicilio>{{ receiverStreet }}</Domicilio>
<Cpostal>{{ receiverPostalCode }}</Cpostal>
<Poblacion>{{ receiverCity }}</Poblacion>
<Municipio></Municipio>
<Provincia>{{ receiverProvince }}</Provincia>
<Contacto></Contacto>
<Telefono>{{ receiverPhone }}</Telefono>
<Email>{{ receiverEmail }}</Email>
<Pais>{{ receiverCountry }}</Pais>
</ObjetoDestinatario>
<ObjetoLogin>
<IdCliente>{{ clientViaexpress }}</IdCliente>
<Usuario>{{ userViaexpress }}</Usuario>
<Password>{{ passwordViaexpress }}</Password>
</ObjetoLogin>
</ObjetoEnvio>
</PutExpedicionInternacional>
</soap12:Body>
</soap12:Envelope>

View File

@ -1,3 +1,4 @@
module.exports = Self => { module.exports = Self => {
require('../methods/viaexpress-config/internationalExpedition')(Self); require('../methods/viaexpress-config/internationalExpedition')(Self);
require('../methods/viaexpress-config/renderer')(Self);
}; };

View File

@ -22,6 +22,7 @@
"ftps": "^1.2.0", "ftps": "^1.2.0",
"gm": "^1.25.0", "gm": "^1.25.0",
"got": "^10.7.0", "got": "^10.7.0",
"handlebars": "^4.7.7",
"helmet": "^3.21.2", "helmet": "^3.21.2",
"i18n": "^0.8.4", "i18n": "^0.8.4",
"image-type": "^4.1.0", "image-type": "^4.1.0",