2019-06-06 11:59:11 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2019-06-20 05:46:30 +00:00
|
|
|
const fs = require('fs-extra');
|
2020-12-18 16:23:04 +00:00
|
|
|
const path = require('path');
|
2019-06-06 11:59:11 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('uploadFile', {
|
|
|
|
description: 'Uploads a file and inserts into dms model',
|
|
|
|
accessType: 'WRITE',
|
2019-06-13 07:21:36 +00:00
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'warehouseId',
|
2022-03-04 11:01:32 +00:00
|
|
|
type: 'number',
|
2019-10-21 06:31:14 +00:00
|
|
|
description: 'The warehouse id',
|
|
|
|
required: true
|
2019-06-13 07:21:36 +00:00
|
|
|
}, {
|
|
|
|
arg: 'companyId',
|
2022-03-04 11:01:32 +00:00
|
|
|
type: 'number',
|
2019-10-21 06:31:14 +00:00
|
|
|
description: 'The company id',
|
|
|
|
required: true
|
2019-06-13 07:21:36 +00:00
|
|
|
}, {
|
|
|
|
arg: 'dmsTypeId',
|
2022-03-04 11:01:32 +00:00
|
|
|
type: 'number',
|
2019-10-21 06:31:14 +00:00
|
|
|
description: 'The dms type id',
|
|
|
|
required: true
|
2019-06-13 07:21:36 +00:00
|
|
|
}, {
|
|
|
|
arg: 'reference',
|
2022-03-04 11:01:32 +00:00
|
|
|
type: 'string',
|
2019-10-21 06:31:14 +00:00
|
|
|
required: true
|
2019-06-13 07:21:36 +00:00
|
|
|
}, {
|
|
|
|
arg: 'description',
|
2022-03-04 11:01:32 +00:00
|
|
|
type: 'string',
|
2019-10-21 06:31:14 +00:00
|
|
|
required: true
|
2019-06-13 07:21:36 +00:00
|
|
|
}, {
|
|
|
|
arg: 'hasFile',
|
2022-03-04 11:01:32 +00:00
|
|
|
type: 'boolean',
|
2019-10-21 06:31:14 +00:00
|
|
|
description: 'True if has an attached file',
|
|
|
|
required: true
|
2019-06-20 05:46:30 +00:00
|
|
|
}],
|
2019-06-06 11:59:11 +00:00
|
|
|
returns: {
|
2022-03-04 11:01:32 +00:00
|
|
|
type: 'object',
|
2019-06-06 11:59:11 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/uploadFile`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-20 07:23:55 +00:00
|
|
|
Self.uploadFile = async(ctx, options) => {
|
2019-06-20 05:46:30 +00:00
|
|
|
const models = Self.app.models;
|
2020-12-18 16:23:04 +00:00
|
|
|
const TempContainer = models.TempContainer;
|
2019-06-06 11:59:11 +00:00
|
|
|
const fileOptions = {};
|
2019-06-20 05:46:30 +00:00
|
|
|
const args = ctx.args;
|
2019-06-06 11:59:11 +00:00
|
|
|
|
2019-06-13 07:21:36 +00:00
|
|
|
let tx;
|
2021-11-18 10:17:30 +00:00
|
|
|
const myOptions = {};
|
2019-06-13 07:21:36 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
2019-06-20 07:23:55 +00:00
|
|
|
Object.assign(myOptions, options);
|
2019-06-13 07:21:36 +00:00
|
|
|
|
2019-06-20 07:23:55 +00:00
|
|
|
if (!myOptions.transaction) {
|
2019-06-13 07:21:36 +00:00
|
|
|
tx = await Self.beginTransaction({});
|
2019-06-20 07:23:55 +00:00
|
|
|
myOptions.transaction = tx;
|
2019-06-13 07:21:36 +00:00
|
|
|
}
|
2019-06-06 11:59:11 +00:00
|
|
|
|
2020-12-18 16:23:04 +00:00
|
|
|
let srcFile;
|
2019-06-06 11:59:11 +00:00
|
|
|
try {
|
2019-11-22 12:46:38 +00:00
|
|
|
const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId, myOptions);
|
2019-06-06 11:59:11 +00:00
|
|
|
if (!hasWriteRole)
|
|
|
|
throw new UserError(`You don't have enough privileges`);
|
|
|
|
|
2019-06-19 05:36:36 +00:00
|
|
|
// Upload file to temporary path
|
2020-12-18 16:23:04 +00:00
|
|
|
const tempContainer = await TempContainer.container('dms');
|
|
|
|
const uploaded = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
|
2019-06-06 11:59:11 +00:00
|
|
|
const files = Object.values(uploaded.files).map(file => {
|
|
|
|
return file[0];
|
|
|
|
});
|
|
|
|
|
2019-06-20 05:46:30 +00:00
|
|
|
const addedDms = [];
|
2020-12-18 16:23:04 +00:00
|
|
|
for (const uploadedFile of files) {
|
|
|
|
const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
|
|
|
|
srcFile = path.join(file.client.root, file.container, file.name);
|
2019-06-06 11:59:11 +00:00
|
|
|
|
2024-01-17 12:07:26 +00:00
|
|
|
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);
|
2019-06-20 05:46:30 +00:00
|
|
|
addedDms.push(newDms);
|
|
|
|
}
|
2019-06-06 11:59:11 +00:00
|
|
|
|
2019-06-13 07:21:36 +00:00
|
|
|
if (tx) await tx.commit();
|
2021-10-15 14:00:50 +00:00
|
|
|
|
2019-06-20 05:46:30 +00:00
|
|
|
return addedDms;
|
2019-06-06 11:59:11 +00:00
|
|
|
} catch (e) {
|
2019-06-13 07:21:36 +00:00
|
|
|
if (tx) await tx.rollback();
|
2020-12-18 16:23:04 +00:00
|
|
|
|
|
|
|
if (fs.existsSync(srcFile))
|
|
|
|
await fs.unlink(srcFile);
|
|
|
|
|
2019-06-06 11:59:11 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|