refs #6184 saveCmr #1788
|
@ -49,7 +49,6 @@ module.exports = Self => {
|
||||||
Self.uploadFile = async(ctx, options) => {
|
Self.uploadFile = async(ctx, options) => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const TempContainer = models.TempContainer;
|
const TempContainer = models.TempContainer;
|
||||||
const DmsContainer = models.DmsContainer;
|
|
||||||
const fileOptions = {};
|
const fileOptions = {};
|
||||||
const args = ctx.args;
|
const args = ctx.args;
|
||||||
|
|
||||||
|
@ -79,19 +78,21 @@ module.exports = Self => {
|
||||||
|
|
||||||
const addedDms = [];
|
const addedDms = [];
|
||||||
for (const uploadedFile of files) {
|
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);
|
const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
|
||||||
srcFile = path.join(file.client.root, file.container, file.name);
|
srcFile = path.join(file.client.root, file.container, file.name);
|
||||||
|
|
||||||
const dmsContainer = await DmsContainer.container(pathHash);
|
const data = {
|
||||||
const dstFile = path.join(dmsContainer.client.root, pathHash, newDms.file);
|
workerFk: ctx.req.accessToken.userId,
|
||||||
|
dmsTypeFk: args.dmsTypeId,
|
||||||
await fs.move(srcFile, dstFile, {
|
companyFk: args.companyId,
|
||||||
overwrite: true
|
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);
|
addedDms.push(newDms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,27 +108,4 @@ module.exports = Self => {
|
||||||
throw e;
|
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);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
const fs = require('fs-extra');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
require('../methods/dms/downloadFile')(Self);
|
require('../methods/dms/downloadFile')(Self);
|
||||||
|
@ -35,4 +37,32 @@ module.exports = Self => {
|
||||||
|
|
||||||
return [stream, dms.contentType, `filename="${dms.file}"`];
|
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;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -45,14 +45,14 @@ module.exports = Self => {
|
||||||
const zipConfig = await models.ZipConfig.findOne(null, myOptions);
|
const zipConfig = await models.ZipConfig.findOne(null, myOptions);
|
||||||
let totalSize = 0;
|
let totalSize = 0;
|
||||||
ids = ids.split(',');
|
ids = ids.split(',');
|
||||||
try {
|
|
||||||
const baseUrl = (await Self.app.models.Url.getUrl()).replace('#!', 'api');
|
|
||||||
|
|
||||||
|
|||||||
for (const id of ids) {
|
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(
|
for (const id of ids) {
|
||||||
`${baseUrl}Routes/${id}/cmr`, {
|
if (zipConfig && totalSize > zipConfig.maxSize) throw new UserError('Files are too large');
|
||||||
|
|
||||||
|
const response = await axios.get(
|
||||||
guillermo marked this conversation as resolved
Outdated
alexm
commented
No he gastat mai .referer, pero crec que es millor gastar la funcio que crea pablo encomter de ctx.req.headers.origin 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
guillermo
commented
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,
|
...myOptions,
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: ctx.req.accessToken.id
|
Authorization: ctx.req.accessToken.id
|
||||||
|
@ -60,17 +60,14 @@ module.exports = Self => {
|
||||||
responseType: 'arraybuffer',
|
responseType: 'arraybuffer',
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.headers['content-type'] !== 'application/pdf')
|
if (response.headers['content-type'] !== 'application/pdf')
|
||||||
throw new UserError(`The response is not a PDF`);
|
throw new UserError(`The response is not a PDF`);
|
||||||
|
|
||||||
zip.file(`${id}.pdf`, response.data, { binary: true });
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const zipStream = zip.generateNodeStream({streamFiles: true});
|
||||||
|
|
||||||
|
return [zipStream, 'application/zip', `filename="cmrs.zip"`];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -41,31 +41,31 @@ module.exports = Self => {
|
||||||
|
|
||||||
if (ticket.cmrFk) {
|
if (ticket.cmrFk) {
|
||||||
const hasDmsCmr = await models.TicketDms.findOne({
|
const hasDmsCmr = await models.TicketDms.findOne({
|
||||||
where: { ticketFk: ticketId },
|
where: {ticketFk: ticketId},
|
||||||
include: {
|
include: {
|
||||||
relation: 'dms',
|
relation: 'dms',
|
||||||
fields: ['dmsFk'],
|
fields: ['dmsFk'],
|
||||||
scope: {
|
scope: {
|
||||||
where: { dmsTypeFk: dmsTypeCmr.id }
|
where: {dmsTypeFk: dmsTypeCmr.id}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, myOptions);
|
}, myOptions);
|
||||||
|
|
||||||
if (!hasDmsCmr.dms()) {
|
if (!hasDmsCmr.dms()) {
|
||||||
const zip = await models.Route.downloadCmrsZip(ctx, ticket.cmrFk.toString(), myOptions);
|
const zip = await models.Route.downloadCmrsZip(ctx, ticket.cmrFk.toString(), myOptions);
|
||||||
ctx.req.file = Object.assign({}, zip);
|
const stream = Object.assign({}, zip);
|
||||||
guillermo marked this conversation as resolved
Outdated
jgallego
commented
const const
guillermo
commented
En eixe cas te que ser let, fijat baix. En eixe cas te que ser let, fijat baix.
Per a que siga const he tingut que juntar-ho.
|
|||||||
const ctxUploadFile = {
|
const data = {
|
||||||
...ctx,
|
workerFk: ctx.req.accessToken.userId,
|
||||||
args: {
|
dmsTypeFk: dmsTypeCmr.id,
|
||||||
warehouseId: ticket.warehouseFk,
|
companyFk: ticket.companyFk,
|
||||||
companyId: ticket.companyFk,
|
warehouseFk: ticket.warehouseFk,
|
||||||
dmsTypeId: dmsTypeCmr.id,
|
reference: ticket.id,
|
||||||
reference: ticket.id,
|
description: `${ticket.cmrFk} - ${ticket.id}`,
|
||||||
guillermo marked this conversation as resolved
Outdated
jgallego
commented
si es dms, en reference posa ticket.cmrFk si es dms, en reference posa ticket.cmrFk
|
|||||||
description: `${ticket.cmrFk} - ${ticket.id}`,
|
contentType: '?',
|
||||||
guillermo marked this conversation as resolved
Outdated
jgallego
commented
al ser el tipo cmr, no es neceario poner cmr, pueden buscar por tipo 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
|
hasFile: false
|
||||||
}
|
|
||||||
};
|
};
|
||||||
const dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
|
|
||||||
|
const dms = await models.Dms.createFromStream(data, 'zip', stream, myOptions);
|
||||||
await models.TicketDms.create({
|
await models.TicketDms.create({
|
||||||
ticketFk: ticketId,
|
ticketFk: ticketId,
|
||||||
dmsFk: dms[0].id
|
dmsFk: dms[0].id
|
||||||
|
|
Loading…
Reference in New Issue
En javascript se puede hacer
const [data] = ...
y cojera el primer valor de la arrayAsi luego puedes hacer
zip.file(`${id}.pdf`, data, {binary: true});