refs #6184 saveCmr #1788

Merged
guillermo merged 58 commits from 6184-saveCmr into dev 2024-02-13 06:47:06 +00:00
4 changed files with 71 additions and 66 deletions
Showing only changes of commit 45fd96cee6 - Show all commits

View File

@ -49,7 +49,6 @@ module.exports = Self => {
Self.uploadFile = async(ctx, options) => {
const models = Self.app.models;
const TempContainer = models.TempContainer;
const DmsContainer = models.DmsContainer;
const fileOptions = {};
const args = ctx.args;
@ -79,19 +78,21 @@ module.exports = Self => {
const addedDms = [];
for (const uploadedFile of files) {
const newDms = await createDms(ctx, uploadedFile, myOptions);
const pathHash = DmsContainer.getHash(newDms.id);
const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
srcFile = path.join(file.client.root, file.container, file.name);
const dmsContainer = await DmsContainer.container(pathHash);
const dstFile = path.join(dmsContainer.client.root, pathHash, newDms.file);
await fs.move(srcFile, dstFile, {
overwrite: true
});
const data = {
workerFk: ctx.req.accessToken.userId,
dmsTypeFk: args.dmsTypeId,
companyFk: args.companyId,
warehouseFk: args.warehouseId,
reference: args.reference,
description: args.description,
contentType: file.type,
hasFile: args.hasFile
};
const extension = await models.DmsContainer.getFileExtension(uploadedFile.name);
const newDms = await Self.createFromFile(data, extension, srcFile, myOptions);
addedDms.push(newDms);
}
@ -107,27 +108,4 @@ module.exports = Self => {
throw e;
}
};
async function createDms(ctx, file, myOptions) {
const models = Self.app.models;
const myUserId = ctx.req.accessToken.userId;
const args = ctx.args;
const newDms = await Self.create({
workerFk: myUserId,
dmsTypeFk: args.dmsTypeId,
companyFk: args.companyId,
warehouseFk: args.warehouseId,
reference: args.reference,
description: args.description,
contentType: file.type,
hasFile: args.hasFile
}, myOptions);
let fileName = file.name;
const extension = models.DmsContainer.getFileExtension(fileName);
fileName = `${newDms.id}.${extension}`;
return newDms.updateAttribute('file', fileName, myOptions);
}
};

View File

@ -1,4 +1,6 @@
const UserError = require('vn-loopback/util/user-error');
const fs = require('fs-extra');
const path = require('path');
module.exports = Self => {
require('../methods/dms/downloadFile')(Self);
@ -35,4 +37,32 @@ module.exports = Self => {
return [stream, dms.contentType, `filename="${dms.file}"`];
};
Self.getPath = async function(dms) {
const models = Self.app.models;
const pathHash = await models.DmsContainer.getHash(dms.id);
const dmsContainer = await models.DmsContainer.container(pathHash);
const dstFile = path.join(dmsContainer.client.root, pathHash, dms.file);
return dstFile;
};
Self.createWithExtension = async function(data, extension, options) {
const newDms = await Self.create(data, options);
return newDms.updateAttribute('file', `${newDms.id}.${extension}`, options);
};
Self.createFromFile = async function(data, extension, srcFile, options) {
const dms = await Self.createWithExtension(data, extension, options);
const dstFile = await Self.getPath(dms);
await fs.move(srcFile, dstFile, {overwrite: true});
return dms;
};
Self.createFromStream = async function(data, extension, stream, options) {
const dms = await Self.createWithExtension(data, extension, options);
const dstFile = await Self.getPath(dms);
const writeStream = await fs.createWriteStream(dstFile);
await readStream.pipe(writeStream);
return dms;
};
};

View File

@ -45,14 +45,14 @@ module.exports = Self => {
const zipConfig = await models.ZipConfig.findOne(null, myOptions);
let totalSize = 0;
ids = ids.split(',');
try {
const baseUrl = (await Self.app.models.Url.getUrl()).replace('#!', 'api');
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});```
for (const id of ids) {
if (zipConfig && totalSize > zipConfig.maxSize) throw new UserError('Files are too large');
const baseUrl = (await Self.app.models.Url.getUrl()).replace('#!', 'api');
const response = await axios.get(
`${baseUrl}Routes/${id}/cmr`, {
for (const id of ids) {
if (zipConfig && totalSize > zipConfig.maxSize) throw new UserError('Files are too large');
const response = await axios.get(
guillermo marked this conversation as resolved Outdated
Outdated
Review

No he gastat mai .referer, pero crec que es millor gastar la funcio que crea pablo encomter de ctx.req.headers.origin
back/methods/url/getUrl.js

No he gastat mai .referer, pero crec que es millor gastar la funcio que crea pablo encomter de ctx.req.headers.origin back/methods/url/getUrl.js

El problema es que no quiero el "/#!/", ya que quiero apuntar a la API

El problema es que no quiero el "/#!/", ya que quiero apuntar a la API
`${baseUrl}Routes/${id}/cmr`, {
...myOptions,
headers: {
Authorization: ctx.req.accessToken.id
@ -60,17 +60,14 @@ module.exports = Self => {
responseType: 'arraybuffer',
});
if (response.headers['content-type'] !== 'application/pdf')
throw new UserError(`The response is not a PDF`);
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 zipStream = zip.generateNodeStream({ streamFiles: true });
return [zipStream, 'application/zip', `filename="cmrs.zip"`];
} catch (e) {
throw e;
zip.file(`${id}.pdf`, response.data, {binary: true});
}
const zipStream = zip.generateNodeStream({streamFiles: true});
return [zipStream, 'application/zip', `filename="cmrs.zip"`];
};
};

View File

@ -41,31 +41,31 @@ module.exports = Self => {
if (ticket.cmrFk) {
const hasDmsCmr = await models.TicketDms.findOne({
where: { ticketFk: ticketId },
where: {ticketFk: ticketId},
include: {
relation: 'dms',
fields: ['dmsFk'],
scope: {
where: { dmsTypeFk: dmsTypeCmr.id }
where: {dmsTypeFk: dmsTypeCmr.id}
}
}
}, myOptions);
if (!hasDmsCmr.dms()) {
const zip = await models.Route.downloadCmrsZip(ctx, ticket.cmrFk.toString(), myOptions);
ctx.req.file = Object.assign({}, zip);
const ctxUploadFile = {
...ctx,
args: {
warehouseId: ticket.warehouseFk,
companyId: ticket.companyFk,
dmsTypeId: dmsTypeCmr.id,
reference: ticket.id,
description: `${ticket.cmrFk} - ${ticket.id}`,
hasFile: false
}
const stream = Object.assign({}, zip);
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 data = {
workerFk: ctx.req.accessToken.userId,
dmsTypeFk: dmsTypeCmr.id,
companyFk: ticket.companyFk,
warehouseFk: ticket.warehouseFk,
reference: ticket.id,
description: `${ticket.cmrFk} - ${ticket.id}`,
guillermo marked this conversation as resolved Outdated

si es dms, en reference posa ticket.cmrFk

si es dms, en reference posa ticket.cmrFk
contentType: '?',
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
hasFile: false
};
const dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
const dms = await models.Dms.createFromStream(data, 'zip', stream, myOptions);
await models.TicketDms.create({
ticketFk: ticketId,
dmsFk: dms[0].id