feat: el pdf de las rutas se descarga en un zip
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Vicent Llopis 2023-01-17 14:06:18 +01:00
parent 5473b2ca2e
commit ea36c0deb5
3 changed files with 72 additions and 5 deletions

View File

@ -0,0 +1,62 @@
const JSZip = require('jszip');
module.exports = Self => {
Self.remoteMethodCtx('downloadZip', {
description: 'Download a zip file with multiple routes pdfs',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'string',
description: 'The routes ids',
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'string',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'string',
http: {target: 'header'}
}
],
http: {
path: '/downloadZip',
verb: 'GET'
}
});
Self.downloadZip = async function(ctx, id, options) {
const models = Self.app.models;
const myOptions = {};
const zip = new JSZip();
if (typeof options == 'object')
Object.assign(myOptions, options);
const ids = id.split(',');
for (let id of ids) {
ctx.args.id = id;
const routePdf = await models.Route.driverRoutePdf(ctx, id);
const fileName = extractFileName(routePdf[2]);
const body = routePdf[0];
zip.file(fileName, body);
}
const stream = zip.generateNodeStream({streamFiles: true});
return [stream, 'application/zip', `filename="download.zip"`];
};
function extractFileName(str) {
const matches = str.match(/"(.*?)"/);
return matches ? matches[1] : str;
}
};

View File

@ -13,6 +13,7 @@ module.exports = Self => {
require('../methods/route/driverRoutePdf')(Self); require('../methods/route/driverRoutePdf')(Self);
require('../methods/route/driverRouteEmail')(Self); require('../methods/route/driverRouteEmail')(Self);
require('../methods/route/sendSms')(Self); require('../methods/route/sendSms')(Self);
require('../methods/route/downloadZip')(Self);
Self.validate('kmStart', validateDistance, { Self.validate('kmStart', validateDistance, {
message: 'Distance must be lesser than 1000' message: 'Distance must be lesser than 1000'

View File

@ -34,12 +34,16 @@ export default class Controller extends Section {
} }
showRouteReport() { showRouteReport() {
const routes = []; const routesIds = [];
for (let route of this.checked) for (let route of this.checked)
routes.push(route.id); routesIds.push(route.id);
const routesId = routes.join(','); const stringRoutesIds = routesIds.join(',');
const serializedParams = this.$httpParamSerializer({
this.vnReport.show(`Routes/${routesId}/driver-route-pdf`); access_token: this.vnToken.token,
id: stringRoutesIds
});
const url = `api/Routes/downloadZip?${serializedParams}`;
window.open(url, '_blank');
} }
openClonationDialog() { openClonationDialog() {