2023-06-19 12:27:24 +00:00
|
|
|
const fs = require('fs');
|
2023-06-20 12:07:36 +00:00
|
|
|
const ejs = require('ejs');
|
2023-06-19 12:27:24 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('renderer', {
|
2023-06-20 05:19:32 +00:00
|
|
|
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({
|
2023-07-18 05:56:44 +00:00
|
|
|
fields: ['client', 'user', 'password', 'defaultWeight', 'deliveryType']
|
2023-06-19 12:27:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2023-07-18 05:56:44 +00:00
|
|
|
const ticket = expedition.ticket();
|
|
|
|
const sender = ticket.company().client();
|
|
|
|
const shipped = ticket.shipped.toISOString();
|
2023-06-19 12:27:24 +00:00
|
|
|
const data = {
|
2023-07-18 05:56:44 +00:00
|
|
|
viaexpressConfig,
|
|
|
|
sender,
|
|
|
|
senderAddress: sender.defaultAddress(),
|
|
|
|
client: ticket.client(),
|
|
|
|
address: ticket.address(),
|
|
|
|
shipped
|
2023-06-19 12:27:24 +00:00
|
|
|
};
|
|
|
|
|
2023-06-20 12:07:36 +00:00
|
|
|
const template = fs.readFileSync(__dirname + '/template.ejs', 'utf-8');
|
|
|
|
const renderedXml = ejs.render(template, data);
|
|
|
|
return renderedXml;
|
2023-06-19 12:27:24 +00:00
|
|
|
};
|
|
|
|
};
|