diff --git a/back/models/dms.js b/back/models/dms.js index d6eab4fe4..b21a91a8a 100644 --- a/back/models/dms.js +++ b/back/models/dms.js @@ -16,13 +16,12 @@ module.exports = Self => { return await models.DmsType.hasReadRole(ctx, dms.dmsTypeFk); }; - Self.getFile = async function(id) { - const models = Self.app.models; - const DmsContainer = models.DmsContainer; + Self.getFile = async function(id, container = 'DmsContainer') { + const Container = Self.app.models.models[container]; const dms = await Self.findById(id); - const pathHash = DmsContainer.getHash(dms.id); + const pathHash = Container.getHash(dms.id); try { - await DmsContainer.getFile(pathHash, dms.file); + await Container.getFile(pathHash, dms.file); } catch (e) { if (e.code != 'ENOENT') throw e; @@ -33,7 +32,7 @@ module.exports = Self => { throw error; } - const stream = DmsContainer.downloadStream(pathHash, dms.file); + const stream = Container.downloadStream(pathHash, dms.file); return [stream, dms.contentType, `filename="${dms.file}"`]; }; @@ -65,4 +64,67 @@ module.exports = Self => { await stream.pipe(writeStream); return dms; }; + + Self.uploadFileTemplate = async(ctx, id, model, foreignKey, options) => { + const {Dms, [model]: modelDms} = Self.app.models; + const myOptions = {}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const uploadedFiles = await Dms.uploadFile(ctx, myOptions); + const promises = uploadedFiles.map(dms => + modelDms.create({ + [foreignKey]: id, + dmsFk: dms.id + }, myOptions) + + ); + await Promise.all(promises); + + if (tx) await tx.commit(); + + return uploadedFiles; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; + + Self.removeFileTemplate = async(ctx, id, currentSelf, options) => { + const myOptions = {}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await currentSelf.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const targetModelDms = await currentSelf.findById(id, null, myOptions); + const targetDms = await Self.removeFile(ctx, targetModelDms.dmsFk, myOptions); + + if (!targetDms || !targetModelDms) + throw new UserError('Try again'); + + const modelDmsDestroyed = await targetModelDms.destroy(myOptions); + + if (tx) await tx.commit(); + + return modelDmsDestroyed; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; }; diff --git a/modules/claim/back/methods/claim-dms/removeFile.js b/modules/claim/back/methods/claim-dms/removeFile.js index 28e78c9d7..eb3adea31 100644 --- a/modules/claim/back/methods/claim-dms/removeFile.js +++ b/modules/claim/back/methods/claim-dms/removeFile.js @@ -1,5 +1,3 @@ -const UserError = require('vn-loopback/util/user-error'); - module.exports = Self => { Self.remoteMethodCtx('removeFile', { description: 'Removes a claim document', @@ -20,35 +18,7 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, id, options) => { - const myOptions = {}; - let tx; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const claimDms = await Self.findById(id, null, myOptions); - - const targetDms = await Self.app.models.Dms.removeFile(ctx, claimDms.dmsFk, myOptions); - - if (!targetDms || ! claimDms) - throw new UserError('Try again'); - - const claimDmsDestroyed = await claimDms.destroy(myOptions); - - if (tx) await tx.commit(); - - return claimDmsDestroyed; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.removeFile = async(ctx, id, options) => + Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options); }; diff --git a/modules/client/back/methods/client-dms/removeFile.js b/modules/client/back/methods/client-dms/removeFile.js index bc9a0f719..2cab64d28 100644 --- a/modules/client/back/methods/client-dms/removeFile.js +++ b/modules/client/back/methods/client-dms/removeFile.js @@ -18,35 +18,7 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, id, options) => { - const myOptions = {}; - let tx; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const clientDms = await Self.findById(id, null, myOptions); - - const targetDms = await Self.app.models.Dms.removeFile(ctx, clientDms.dmsFk, myOptions); - - if (!targetDms || !clientDms) - throw new UserError('Try again'); - - const clientDmsDestroyed = await clientDms.destroy(myOptions); - - if (tx) await tx.commit(); - - return clientDmsDestroyed; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.removeFile = async(ctx, id, options) => + Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options); }; diff --git a/modules/entry/back/methods/entry-dms/removeFile.js b/modules/entry/back/methods/entry-dms/removeFile.js index 89a87755c..f28c3fa2b 100644 --- a/modules/entry/back/methods/entry-dms/removeFile.js +++ b/modules/entry/back/methods/entry-dms/removeFile.js @@ -1,5 +1,3 @@ -const UserError = require('vn-loopback/util/user-error'); - module.exports = Self => { Self.remoteMethodCtx('removeFile', { description: 'Removes a entry document', @@ -20,34 +18,7 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, id, options) => { - const myOptions = {}; - let tx; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const targetEntryDms = await Self.findById(id, null, myOptions); - const targetDms = await Self.app.models.Dms.removeFile(ctx, targetEntryDms.dmsFk, myOptions); - - if (!targetDms) - throw new UserError('Try again'); - - const entryDmsDestroyed = await targetEntryDms.destroy(myOptions); - - if (tx) await tx.commit(); - - return entryDmsDestroyed; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.removeFile = async(ctx, id, options) => + Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options); }; diff --git a/modules/supplier/back/methods/supplier/supplier-dms/removeFile.js b/modules/supplier/back/methods/supplier/supplier-dms/removeFile.js index 04a9011f1..3671a747c 100644 --- a/modules/supplier/back/methods/supplier/supplier-dms/removeFile.js +++ b/modules/supplier/back/methods/supplier/supplier-dms/removeFile.js @@ -1,5 +1,3 @@ -const UserError = require('vn-loopback/util/user-error'); - module.exports = Self => { Self.remoteMethodCtx('removeFile', { description: 'Removes a entry document', @@ -20,34 +18,7 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, id, options) => { - const myOptions = {}; - let tx; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const targetSupplierDms = await Self.findById(id, null, myOptions); - const targetDms = await Self.app.models.Dms.removeFile(ctx, targetSupplierDms.dmsFk, myOptions); - - if (!targetDms) - throw new UserError('Try again'); - - const supplierDmsDestroyed = await targetSupplierDms.destroy(myOptions); - - if (tx) await tx.commit(); - - return supplierDmsDestroyed; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.removeFile = async(ctx, id, options) => + Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options); }; diff --git a/modules/ticket/back/methods/ticket-dms/removeFile.js b/modules/ticket/back/methods/ticket-dms/removeFile.js index 6fba4c552..54fc5ff56 100644 --- a/modules/ticket/back/methods/ticket-dms/removeFile.js +++ b/modules/ticket/back/methods/ticket-dms/removeFile.js @@ -18,35 +18,7 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, id, options) => { - const myOptions = {}; - let tx; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const ticketDms = await Self.findById(id, null, myOptions); - - const targetDms = await Self.app.models.Dms.removeFile(ctx, ticketDms.dmsFk, myOptions); - - if (!targetDms || !ticketDms) - throw new UserError('Try again'); - - const ticketDmsDestroyed = await ticketDms.destroy(myOptions); - - if (tx) await tx.commit(); - - return ticketDmsDestroyed; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.removeFile = async(ctx, id, options) => + Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options); }; diff --git a/modules/worker/back/methods/worker-dms/removeFile.js b/modules/worker/back/methods/worker-dms/removeFile.js index 8eb6c05fd..540b8ff84 100644 --- a/modules/worker/back/methods/worker-dms/removeFile.js +++ b/modules/worker/back/methods/worker-dms/removeFile.js @@ -18,35 +18,7 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, dmsFk, options) => { - const myOptions = {}; - let tx; - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const WorkerDms = await Self.findOne({ - where: {document: dmsFk} - }, myOptions); - - const targetDms = await Self.app.models.Dms.removeFile(ctx, dmsFk, myOptions); - - if (!targetDms || !WorkerDms) - throw new UserError('Try again'); - - const workerDmsDestroyed = await WorkerDms.destroy(myOptions); - if (tx) await tx.commit(); - - return workerDmsDestroyed; - } catch (e) { - await tx.rollback(); - throw e; - } - }; + Self.removeFile = async(ctx, id, options) => + Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options); };