From ae767762451137bb58fcaee927f8b401cfe18284 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 12 Dec 2023 12:09:06 +0100 Subject: [PATCH] refs #2051 feat: removeFile approach --- back/methods/dms/removeFile.js | 15 +++++++---- back/methods/dms/specs/removeFile.spec.js | 2 +- .../back/methods/claim-dms/removeFile.js | 16 ++++++------ .../claim-dms/specs/removeFile.spec.js | 3 ++- .../client-dms/specs/removeFile.spec.js | 3 ++- .../back/methods/ticket-dms/removeFile.js | 14 +++++------ .../ticket-dms/specs/removeFile.spec.js | 2 +- .../back/methods/worker-dms/removeFile.js | 25 ++++++++++++++++--- .../worker-dms/specs/removeFile.spec.js | 3 ++- 9 files changed, 54 insertions(+), 29 deletions(-) diff --git a/back/methods/dms/removeFile.js b/back/methods/dms/removeFile.js index a9ff36883..cb81ff322 100644 --- a/back/methods/dms/removeFile.js +++ b/back/methods/dms/removeFile.js @@ -34,20 +34,25 @@ module.exports = Self => { } try { - const dms = await models.Dms.findById(id, null, myOptions); + let modelDms = null; + if (myOptions.model) { + modelDms = await models[myOptions.model].findById(id, null, myOptions); + id = modelDms.dmsFk; + } + const targetDms = await models.Dms.findById(id, null, myOptions); const trashDmsType = await models.DmsType.findOne({ where: {code: 'trash'} }, myOptions); - const hasWriteRole = await models.DmsType.hasWriteRole(ctx, dms.dmsTypeFk, myOptions); + const hasWriteRole = await models.DmsType.hasWriteRole(ctx, targetDms.dmsTypeFk, myOptions); if (!hasWriteRole) throw new UserError(`You don't have enough privileges`); - await dms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions); + await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions); if (tx) await tx.commit(); - - return dms; + if (modelDms) modelDms.destroy(myOptions); + return targetDms; } catch (e) { if (tx) await tx.rollback(); throw e; diff --git a/back/methods/dms/specs/removeFile.spec.js b/back/methods/dms/specs/removeFile.spec.js index 59a2acecb..e0d1dc1e7 100644 --- a/back/methods/dms/specs/removeFile.spec.js +++ b/back/methods/dms/specs/removeFile.spec.js @@ -1,6 +1,6 @@ const {models} = require('vn-loopback/server/server'); -describe('dms removeFile()', () => { +fdescribe('dms removeFile()', () => { let dmsId = 1; it(`should return an error for a user without enough privileges`, async() => { diff --git a/modules/claim/back/methods/claim-dms/removeFile.js b/modules/claim/back/methods/claim-dms/removeFile.js index edc714235..257b09cf2 100644 --- a/modules/claim/back/methods/claim-dms/removeFile.js +++ b/modules/claim/back/methods/claim-dms/removeFile.js @@ -31,15 +31,15 @@ module.exports = Self => { } try { - const models = Self.app.models; - const targetClaimDms = await models.ClaimDms.findById(id, null, myOptions); - const targetDms = await models.Dms.findById(targetClaimDms.dmsFk, null, myOptions); - const trashDmsType = await models.DmsType.findOne({where: {code: 'trash'}}, myOptions); + // const {models: ClaimDms, Dms} = Self.app.models; + // const targetClaimDms = await ClaimDms.findById(id, null, myOptions); + // await Dms.findById(targetClaimDms.dmsFk, null, myOptions); + // const trashDmsType = await DmsType.findOne({where: {code: 'trash'}}, myOptions); + myOptions.model = 'ClaimDms'; + const targetDms = await Self.app.models.Dms.removeFile(ctx, id, myOptions); + // await targetClaimDms.destroy(myOptions); - await models.Dms.removeFile(ctx, targetClaimDms.dmsFk, myOptions); - await targetClaimDms.destroy(myOptions); - - await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions); + // await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions); if (tx) await tx.commit(); diff --git a/modules/claim/back/methods/claim-dms/specs/removeFile.spec.js b/modules/claim/back/methods/claim-dms/specs/removeFile.spec.js index 1cd3b16cb..210d805e7 100644 --- a/modules/claim/back/methods/claim-dms/specs/removeFile.spec.js +++ b/modules/claim/back/methods/claim-dms/specs/removeFile.spec.js @@ -1,6 +1,7 @@ const app = require('vn-loopback/server/server'); -describe('TicketDms removeFile()', () => { +// f +fdescribe('TicketDms removeFile()', () => { const ticketDmsId = 1; it(`should return an error for a user without enough privileges`, async() => { let clientId = 1101; diff --git a/modules/client/back/methods/client-dms/specs/removeFile.spec.js b/modules/client/back/methods/client-dms/specs/removeFile.spec.js index 6dac71293..61a00a610 100644 --- a/modules/client/back/methods/client-dms/specs/removeFile.spec.js +++ b/modules/client/back/methods/client-dms/specs/removeFile.spec.js @@ -1,6 +1,7 @@ const models = require('vn-loopback/server/server').models; -describe('ClientDms removeFile()', () => { +// f +fdescribe('ClientDms removeFile()', () => { it(`should return an error for a user without enough privileges`, async() => { const tx = await models.Client.beginTransaction({}); diff --git a/modules/ticket/back/methods/ticket-dms/removeFile.js b/modules/ticket/back/methods/ticket-dms/removeFile.js index f48dc7fef..2e3ccd149 100644 --- a/modules/ticket/back/methods/ticket-dms/removeFile.js +++ b/modules/ticket/back/methods/ticket-dms/removeFile.js @@ -19,7 +19,6 @@ module.exports = Self => { }); Self.removeFile = async(ctx, id, options) => { - const models = Self.app.models; const myOptions = {}; let tx; @@ -32,14 +31,15 @@ module.exports = Self => { } try { - const targetTicketDms = await models.TicketDms.findById(id, null, myOptions); - const targetDms = await models.Dms.findById(targetTicketDms.dmsFk, null, myOptions); - const trashDmsType = await models.DmsType.findOne({where: {code: 'trash'}}, myOptions); + // const targetTicketDms = await models.TicketDms.findById(id, null, myOptions); + myOptions.model = 'TicketDms'; + // await models.Dms.findById(targetTicketDms.dmsFk, null, myOptions); + // const trashDmsType = await models.DmsType.findOne({where: {code: 'trash'}}, myOptions); - await models.Dms.removeFile(ctx, targetTicketDms.dmsFk, myOptions); - await targetTicketDms.destroy(myOptions); + const targetDms = await Self.app.models.Dms.removeFile(ctx, id, myOptions); + // await targetTicketDms.destroy(myOptions); - await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions); + // await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions); if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/ticket-dms/specs/removeFile.spec.js b/modules/ticket/back/methods/ticket-dms/specs/removeFile.spec.js index 9296984c3..ca4cb5bc3 100644 --- a/modules/ticket/back/methods/ticket-dms/specs/removeFile.spec.js +++ b/modules/ticket/back/methods/ticket-dms/specs/removeFile.spec.js @@ -1,6 +1,6 @@ const models = require('vn-loopback/server/server').models; -describe('TicketDms removeFile()', () => { +fdescribe('TicketDms removeFile()', () => { const ticketDmsId = 1; it(`should return an error for a user without enough privileges`, async() => { const tx = await models.TicketDms.beginTransaction({}); diff --git a/modules/worker/back/methods/worker-dms/removeFile.js b/modules/worker/back/methods/worker-dms/removeFile.js index b441c56ce..397255bb2 100644 --- a/modules/worker/back/methods/worker-dms/removeFile.js +++ b/modules/worker/back/methods/worker-dms/removeFile.js @@ -18,13 +18,30 @@ module.exports = Self => { } }); - Self.removeFile = async(ctx, id) => { + Self.removeFile = async(ctx, id, options) => { const models = Self.app.models; - const workerDms = await Self.findById(id); + let tx; + try { + const myOptions = {}; - await models.Dms.removeFile(ctx, workerDms.dmsFk); + if (typeof options == 'object') + Object.assign(myOptions, options); - return workerDms.destroy(); + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + // const workerDms = await Self.findById(id); + myOptions.model = 'WorkerDms'; + await models.Dms.removeFile(ctx, id, myOptions); + // await workerDms.destroy(); + if (tx) await tx.commit(); + + return workerDms; + } catch (e) { + await tx.rollback(); + throw e; + } }; }; diff --git a/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js b/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js index 3efd09464..e66628656 100644 --- a/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js +++ b/modules/worker/back/methods/worker-dms/specs/removeFile.spec.js @@ -1,6 +1,7 @@ const app = require('vn-loopback/server/server'); -describe('WorkerDms removeFile()', () => { +// f +fdescribe('WorkerDms removeFile()', () => { const workerDmsFk = 1; it(`should return an error for a user without enough privileges`, async() => { let clientId = 1101;