From 06bae4482495653d63c30762bc7dcc1a3b84b6cc Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 13 Dec 2023 15:38:46 +0100 Subject: [PATCH] refs #2051 perf: uploadFile after review --- .../claim/back/methods/claim/uploadFile.js | 56 +++---------------- .../client/back/methods/client/uploadFile.js | 23 +++----- .../ticket/back/methods/ticket/uploadFile.js | 22 +++----- .../back/methods/worker-dms/removeFile.js | 10 +++- .../worker/back/methods/worker/uploadFile.js | 19 ++----- modules/worker/front/dms/edit/index.js | 42 ++++++++------ 6 files changed, 64 insertions(+), 108 deletions(-) diff --git a/modules/claim/back/methods/claim/uploadFile.js b/modules/claim/back/methods/claim/uploadFile.js index 7e4798fef..4fd041456 100644 --- a/modules/claim/back/methods/claim/uploadFile.js +++ b/modules/claim/back/methods/claim/uploadFile.js @@ -54,7 +54,7 @@ module.exports = Self => { }); Self.uploadFile = async(ctx, id, options) => { - const models = Self.app.models; + const {Dms, ClaimDms} = Self.app.models; const myOptions = {}; let tx; @@ -66,56 +66,14 @@ module.exports = Self => { myOptions.transaction = tx; } - // const promises = []; - // const TempContainer = models.TempContainer; - // const ClaimContainer = models.ClaimContainer; - // const fileOptions = {}; - // const args = ctx.args; - - // let srcFile; try { - myOptions.model = 'ClaimDms'; - myOptions.create = {claimFk: id}; - const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions); + const uploadedFiles = await Dms.uploadFile(ctx, myOptions); - // const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId, myOptions); - // if (!hasWriteRole) - // throw new UserError(`You don't have enough privileges`); - - // // Upload file to temporary path - // const tempContainer = await TempContainer.container('dms'); - // const uploaded = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions); - // const files = Object.values(uploaded.files).map(file => { - // return file[0]; - // }); - - // const addedDms = []; - // for (const uploadedFile of files) { - // const newDms = await createDms(ctx, uploadedFile, myOptions); - // const pathHash = ClaimContainer.getHash(newDms.id); - - // const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name); - // srcFile = path.join(file.client.root, file.container, file.name); - - // const claimContainer = await ClaimContainer.container(pathHash); - // const dstFile = path.join(claimContainer.client.root, pathHash, newDms.file); - - // await fs.move(srcFile, dstFile, { - // overwrite: true - // }); - - // addedDms.push(newDms); - // } - - // addedDms.forEach(dms => { - // const newClaimDms = models.ClaimDms.create({ - // claimFk: id, - // dmsFk: dms.id - // }, myOptions); - - // promises.push(newClaimDms); - // }); - // const resolvedPromises = await Promise.all(promises); + const promises = uploadedFiles.map(dms => ClaimDms.create({ + claimFk: id, + dmsFk: dms.id + }, myOptions)); + await Promise.all(promises); if (tx) await tx.commit(); diff --git a/modules/client/back/methods/client/uploadFile.js b/modules/client/back/methods/client/uploadFile.js index 8f471f1b6..1a5965955 100644 --- a/modules/client/back/methods/client/uploadFile.js +++ b/modules/client/back/methods/client/uploadFile.js @@ -55,7 +55,7 @@ module.exports = Self => { }); Self.uploadFile = async(ctx, id, options) => { - const models = Self.app.models; + const {Dms, ClientDms} = Self.app.models; const myOptions = {}; let tx; @@ -67,21 +67,16 @@ module.exports = Self => { myOptions.transaction = tx; } - // const promises = []; - try { - myOptions.model = 'ClientDms'; - myOptions.create = {clientFk: id}; - const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions); - // uploadedFiles.forEach(dms => { - // const newClientDms = models.ClientDms.create({ - // clientFk: id, - // dmsFk: dms.id - // }, myOptions); + const uploadedFiles = await Dms.uploadFile(ctx, myOptions); + const promises = uploadedFiles.map(dms => + ClientDms.create({ + clientFk: id, + dmsFk: dms.id + }, myOptions) - // promises.push(newClientDms); - // }); - // const resolvedPromises = await Promise.all(promises); + ); + await Promise.all(promises); if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/ticket/uploadFile.js b/modules/ticket/back/methods/ticket/uploadFile.js index 63bbe845a..eb563783c 100644 --- a/modules/ticket/back/methods/ticket/uploadFile.js +++ b/modules/ticket/back/methods/ticket/uploadFile.js @@ -47,7 +47,7 @@ module.exports = Self => { }); Self.uploadFile = async(ctx, id, options) => { - const models = Self.app.models; + const {Dms, TicketDms} = Self.app.models; const myOptions = {}; let tx; @@ -59,21 +59,15 @@ module.exports = Self => { myOptions.transaction = tx; } - // const promises = []; try { - myOptions.model = 'TicketDms'; - myOptions.create = {ticketFk: id}; - const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions); - /* uploadedFiles.forEach(dms => { - const newTicketDms = models.TicketDms.create({ - ticketFk: id, - dmsFk: dms.id - }, myOptions); + const uploadedFiles = await Dms.uploadFile(ctx, myOptions); + const promises = uploadedFiles.map(dms => TicketDms.create({ + ticketFk: id, + dmsFk: dms.id + }, myOptions)); + + await Promise.all(promises); - promises.push(newTicketDms); - }); - const resolvedPromises = await Promise.all(promises); -*/ if (tx) await tx.commit(); return uploadedFiles; diff --git a/modules/worker/back/methods/worker-dms/removeFile.js b/modules/worker/back/methods/worker-dms/removeFile.js index 6c7b6b850..9fddc8b4b 100644 --- a/modules/worker/back/methods/worker-dms/removeFile.js +++ b/modules/worker/back/methods/worker-dms/removeFile.js @@ -30,9 +30,15 @@ module.exports = Self => { } try { - const WorkerDms = await Self.findById(id, null, myOptions); + // const dms = await Self.app.models.Dms.findById(id, null, myOptions); + const WorkerDms = await Self.findOne({ + where: {document: id} + }, myOptions); - const targetDms = await Self.app.models.Dms.removeFile(ctx, WorkerDms.dmsFk, myOptions); + // const targetDms = await Self.app.models.Dms.removeFile(ctx, id, myOptions); + // const WorkerDms = await Self.findById(+targetDms.reference, null, myOptions); + + const targetDms = await Self.app.models.Dms.removeFile(ctx, id, myOptions); if (!targetDms || !WorkerDms) throw new UserError('Try again'); diff --git a/modules/worker/back/methods/worker/uploadFile.js b/modules/worker/back/methods/worker/uploadFile.js index e624dc904..c3526cbb1 100644 --- a/modules/worker/back/methods/worker/uploadFile.js +++ b/modules/worker/back/methods/worker/uploadFile.js @@ -47,10 +47,9 @@ module.exports = Self => { }); Self.uploadFile = async(ctx, id) => { - const models = Self.app.models; + const {Dms, WorkerDms} = Self.app.models; const myOptions = {}; let tx; - // const promises = []; if (typeof options == 'object') Object.assign(myOptions, options); @@ -61,20 +60,14 @@ module.exports = Self => { } try { - myOptions.model = 'WorkerDms'; - myOptions.create = {workerFk: id}; - - const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions); - /* uploadedFiles.forEach(dms => { - const newWorkerDms = models.WorkerDms.create({ + const uploadedFiles = await Dms.uploadFile(ctx, myOptions); + const promises = uploadedFiles.map(dms => + WorkerDms.create({ workerFk: id, dmsFk: dms.id - }, options); + }, myOptions)); + await Promise.all(promises); - promises.push(newWorkerDms); - }); - const resolvedPromises = await Promise.all(promises); -*/ if (tx) await tx.commit(); return uploadedFiles; diff --git a/modules/worker/front/dms/edit/index.js b/modules/worker/front/dms/edit/index.js index 8a1790563..31d4c2853 100644 --- a/modules/worker/front/dms/edit/index.js +++ b/modules/worker/front/dms/edit/index.js @@ -1,8 +1,8 @@ import ngModule from '../../module'; -import SectionDms from 'salix/components/section-dms'; +import Section from 'salix/components/section'; import './style.scss'; -class Controller extends SectionDms { +class Controller extends Section { get worker() { return this._worker; } @@ -11,29 +11,39 @@ class Controller extends SectionDms { this._worker = value; if (value) { - this.getDms(this.$params.dmsId).then(handleDefaultParams); - - this.getAllowedContentTypes().then(data => this.allowedContentTypes = data); + this.setDefaultParams(); + this.getAllowedContentTypes(); } } + getAllowedContentTypes() { + this.$http.get('DmsContainers/allowedContentTypes').then(res => { + const contentTypes = res.data.join(', '); + this.allowedContentTypes = contentTypes; + }); + } + get contentTypesInfo() { return this.$t('ContentTypesInfo', { allowedContentTypes: this.allowedContentTypes }); } - handleDefaultParams({data: dms}) { - this.dms = { - reference: dms.reference, - warehouseId: dms.warehouseFk, - companyId: dms.companyFk, - dmsTypeId: dms.dmsTypeFk, - description: dms.description, - hasFile: dms.hasFile, - hasFileAttached: false, - files: [] - }; + setDefaultParams() { + const path = `Dms/${this.$params.dmsId}`; + this.$http.get(path).then(res => { + const dms = res.data && res.data; + this.dms = { + reference: dms.reference, + warehouseId: dms.warehouseFk, + companyId: dms.companyFk, + dmsTypeId: dms.dmsTypeFk, + description: dms.description, + hasFile: dms.hasFile, + hasFileAttached: false, + files: [] + }; + }); } onSubmit() {