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 dms = await Self.createWithExtension(data, extension, options);
const dstFile = await Self.getPath(dms); const dstFile = await Self.getPath(dms);
const writeStream = await fs.createWriteStream(dstFile); const writeStream = await fs.createWriteStream(dstFile);
await readStream.pipe(writeStream); await stream.pipe(writeStream);
return dms; return dms;
}; };
}; };

View File

@ -1,6 +1,4 @@
const JSZip = require('jszip'); const JSZip = require('jszip');
const axios = require('axios');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('downloadCmrsZip', { Self.remoteMethodCtx('downloadCmrsZip', {
@ -42,28 +40,11 @@ module.exports = Self => {
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
const zipConfig = await models.ZipConfig.findOne(null, myOptions);
let totalSize = 0;
ids = ids.split(','); ids = ids.split(',');
const baseUrl = (await Self.app.models.Url.getUrl()).replace('#!', 'api');
for (const id of ids) { for (const id of ids) {
if (zipConfig && totalSize > zipConfig.maxSize) throw new UserError('Files are too large'); const data = models.Route.generateCmrPdf(id);
zip.file(`${id}.pdf`, data, {binary: true});
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});
} }
Outdated
Review

En javascript se puede hacer const [data] = ... y cojera el primer valor de la array
Asi luego puedes hacer zip.file(`${id}.pdf`, data, {binary: true});

En javascript se puede hacer `const [data] = ...` y cojera el primer valor de la array Asi luego puedes hacer ```zip.file(`${id}.pdf`, data, {binary: true});```
const zipStream = zip.generateNodeStream({streamFiles: 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;
guillermo marked this conversation as resolved Outdated

totalSize per a que es gasta?

totalSize per a que es gasta?

Para nada, lo voy a quitar, ya no tiene sentido que esté ahí, bien visto

Para nada, lo voy a quitar, ya no tiene sentido que esté ahí, bien visto
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/cmr')(Self);
require('../methods/route/getExternalCmrs')(Self); require('../methods/route/getExternalCmrs')(Self);
require('../methods/route/downloadCmrsZip')(Self); require('../methods/route/downloadCmrsZip')(Self);
require('../methods/route/generateCmrPdf')(Self);
require('../methods/route/getExpeditionSummary')(Self); require('../methods/route/getExpeditionSummary')(Self);
require('../methods/route/getByWorker')(Self); require('../methods/route/getByWorker')(Self);

View File

@ -1,3 +1,5 @@
const {Readable} = require('stream');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('saveCmr', { Self.remoteMethodCtx('saveCmr', {
description: 'Save cmr', description: 'Save cmr',
@ -51,9 +53,9 @@ module.exports = Self => {
} }
}, myOptions); }, myOptions);
if (!hasDmsCmr.dms()) { if (!hasDmsCmr?.dms()) {
guillermo marked this conversation as resolved Outdated

const

const

En eixe cas te que ser let, fijat baix.
Per a que siga const he tingut que juntar-ho.

En eixe cas te que ser let, fijat baix. Per a que siga const he tingut que juntar-ho.
const zip = await models.Route.downloadCmrsZip(ctx, ticket.cmrFk.toString(), myOptions); const response = await models.Route.generateCmrPdf(ctx, ticket.cmrFk.toString(), myOptions);
const stream = Object.assign({}, zip); const pdfStream = Readable.from(Buffer.from(response));
const data = { const data = {
workerFk: ctx.req.accessToken.userId, workerFk: ctx.req.accessToken.userId,
dmsTypeFk: dmsTypeCmr.id, dmsTypeFk: dmsTypeCmr.id,
@ -61,14 +63,14 @@ module.exports = Self => {
warehouseFk: ticket.warehouseFk, warehouseFk: ticket.warehouseFk,
guillermo marked this conversation as resolved Outdated

si es dms, en reference posa ticket.cmrFk

si es dms, en reference posa ticket.cmrFk
reference: ticket.id, reference: ticket.id,
guillermo marked this conversation as resolved Outdated

al ser el tipo cmr, no es neceario poner cmr, pueden buscar por tipo
quitar texto en español.
Propuesta: poner cmr: cmrFk, ticket: ticketFk

al ser el tipo cmr, no es neceario poner cmr, pueden buscar por tipo quitar texto en español. Propuesta: poner cmr: cmrFk, ticket: ticketFk
description: `${ticket.cmrFk} - ${ticket.id}`, description: `${ticket.cmrFk} - ${ticket.id}`,
contentType: '?', contentType: 'application/pdf',
hasFile: false 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({ await models.TicketDms.create({
ticketFk: ticketId, ticketFk: ticketId,
dmsFk: dms[0].id dmsFk: dms.id
}, myOptions); }, myOptions);
} }
} }