refs #6184 saveCmr #1788

Merged
guillermo merged 58 commits from 6184-saveCmr into dev 2024-02-13 06:47:06 +00:00
5 changed files with 77 additions and 29 deletions
Showing only changes of commit 1a52e58eaa - Show all commits

View File

@ -62,7 +62,7 @@ module.exports = Self => {
const dms = await Self.createWithExtension(data, extension, options);
const dstFile = await Self.getPath(dms);
const writeStream = await fs.createWriteStream(dstFile);
await readStream.pipe(writeStream);
await stream.pipe(writeStream);
return dms;
};
};

View File

@ -1,6 +1,4 @@
const JSZip = require('jszip');
const axios = require('axios');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('downloadCmrsZip', {
@ -42,28 +40,11 @@ module.exports = Self => {
if (typeof options == 'object')
Object.assign(myOptions, options);
const zipConfig = await models.ZipConfig.findOne(null, myOptions);
let totalSize = 0;
ids = ids.split(',');
const baseUrl = (await Self.app.models.Url.getUrl()).replace('#!', 'api');
for (const id of ids) {
if (zipConfig && totalSize > zipConfig.maxSize) throw new UserError('Files are too large');
const response = await axios.get(
`${baseUrl}Routes/${id}/cmr`, {
...myOptions,
headers: {
Authorization: ctx.req.accessToken.id
},
responseType: 'arraybuffer',
});
if (response.headers['content-type'] !== 'application/pdf')
throw new UserError(`The response is not a PDF`);
zip.file(`${id}.pdf`, response.data, {binary: true});
const data = models.Route.generateCmrPdf(id);
zip.file(`${id}.pdf`, data, {binary: true});
}
const zipStream = zip.generateNodeStream({streamFiles: true});

View File

@ -0,0 +1,64 @@
const axios = require('axios');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('generateCmrPdf', {
description: 'Generate a pdf of a cmr',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'string',
description: 'The cmr',
}
],
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: '/generateCmrPdf',
verb: 'GET'
}
});
Self.generateCmrPdf = async function(ctx, id, options) {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const zipConfig = await models.ZipConfig.findOne(null, myOptions);
let totalSize = 0;
const baseUrl = (await Self.app.models.Url.getUrl()).replace('#!', 'api');
if (zipConfig && totalSize > zipConfig.maxSize) throw new UserError('Files are too large');
const response = await axios.get(
`${baseUrl}Routes/${id}/cmr`, {
...myOptions,
headers: {
Authorization: ctx.req.accessToken.id
},
responseType: 'arraybuffer',
});
if (response.headers['content-type'] !== 'application/pdf')
throw new UserError(`The response is not a PDF`);
return response.data;
};
};

View File

@ -17,6 +17,7 @@ module.exports = Self => {
require('../methods/route/cmr')(Self);
require('../methods/route/getExternalCmrs')(Self);
require('../methods/route/downloadCmrsZip')(Self);
require('../methods/route/generateCmrPdf')(Self);
require('../methods/route/getExpeditionSummary')(Self);
require('../methods/route/getByWorker')(Self);

View File

@ -1,3 +1,5 @@
const {Readable} = require('stream');
module.exports = Self => {
Self.remoteMethodCtx('saveCmr', {
description: 'Save cmr',
@ -51,9 +53,9 @@ module.exports = Self => {
}
}, myOptions);
if (!hasDmsCmr.dms()) {
const zip = await models.Route.downloadCmrsZip(ctx, ticket.cmrFk.toString(), myOptions);
const stream = Object.assign({}, zip);
if (!hasDmsCmr?.dms()) {
const response = await models.Route.generateCmrPdf(ctx, ticket.cmrFk.toString(), myOptions);
const pdfStream = Readable.from(Buffer.from(response));
const data = {
workerFk: ctx.req.accessToken.userId,
dmsTypeFk: dmsTypeCmr.id,
@ -61,14 +63,14 @@ module.exports = Self => {
warehouseFk: ticket.warehouseFk,
reference: ticket.id,
description: `${ticket.cmrFk} - ${ticket.id}`,
contentType: '?',
hasFile: false
contentType: 'application/pdf',
hasFile: true
};
const dms = await models.Dms.createFromStream(data, 'zip', stream, myOptions);
const dms = await models.Dms.createFromStream(data, 'pdf', pdfStream, myOptions);
await models.TicketDms.create({
ticketFk: ticketId,
dmsFk: dms[0].id
dmsFk: dms.id
}, myOptions);
}
}