salix/back/methods/viaexpress-config/renderer.js

165 lines
7.4 KiB
JavaScript
Raw Normal View History

2023-06-19 12:27:24 +00:00
const fs = require('fs');
const handlebars = require('handlebars');
2023-06-20 09:25:47 +00:00
// const Vue = require('vue');
// const renderer = require('vue-server-renderer').createRenderer();
2023-06-19 12:27:24 +00:00
module.exports = Self => {
Self.remoteMethod('renderer', {
description: 'Renders the data from an XML',
2023-06-19 12:27:24 +00:00
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;
};
};