refs #2051 feat: removeFile approach

This commit is contained in:
Javier Segarra 2023-12-12 12:09:06 +01:00
parent 977184f3e5
commit ae76776245
9 changed files with 54 additions and 29 deletions

View File

@ -34,20 +34,25 @@ module.exports = Self => {
} }
try { 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({ const trashDmsType = await models.DmsType.findOne({
where: {code: 'trash'} where: {code: 'trash'}
}, myOptions); }, myOptions);
const hasWriteRole = await models.DmsType.hasWriteRole(ctx, dms.dmsTypeFk, myOptions); const hasWriteRole = await models.DmsType.hasWriteRole(ctx, targetDms.dmsTypeFk, myOptions);
if (!hasWriteRole) if (!hasWriteRole)
throw new UserError(`You don't have enough privileges`); 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(); if (tx) await tx.commit();
if (modelDms) modelDms.destroy(myOptions);
return dms; return targetDms;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
throw e; throw e;

View File

@ -1,6 +1,6 @@
const {models} = require('vn-loopback/server/server'); const {models} = require('vn-loopback/server/server');
describe('dms removeFile()', () => { fdescribe('dms removeFile()', () => {
let dmsId = 1; let dmsId = 1;
it(`should return an error for a user without enough privileges`, async() => { it(`should return an error for a user without enough privileges`, async() => {

View File

@ -31,15 +31,15 @@ module.exports = Self => {
} }
try { try {
const models = Self.app.models; // const {models: ClaimDms, Dms} = Self.app.models;
const targetClaimDms = await models.ClaimDms.findById(id, null, myOptions); // const targetClaimDms = await ClaimDms.findById(id, null, myOptions);
const targetDms = await models.Dms.findById(targetClaimDms.dmsFk, null, myOptions); // await Dms.findById(targetClaimDms.dmsFk, null, myOptions);
const trashDmsType = await models.DmsType.findOne({where: {code: 'trash'}}, 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 targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions);
await targetClaimDms.destroy(myOptions);
await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions);
if (tx) await tx.commit(); if (tx) await tx.commit();

View File

@ -1,6 +1,7 @@
const app = require('vn-loopback/server/server'); const app = require('vn-loopback/server/server');
describe('TicketDms removeFile()', () => { // f
fdescribe('TicketDms removeFile()', () => {
const ticketDmsId = 1; const ticketDmsId = 1;
it(`should return an error for a user without enough privileges`, async() => { it(`should return an error for a user without enough privileges`, async() => {
let clientId = 1101; let clientId = 1101;

View File

@ -1,6 +1,7 @@
const models = require('vn-loopback/server/server').models; 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() => { it(`should return an error for a user without enough privileges`, async() => {
const tx = await models.Client.beginTransaction({}); const tx = await models.Client.beginTransaction({});

View File

@ -19,7 +19,6 @@ module.exports = Self => {
}); });
Self.removeFile = async(ctx, id, options) => { Self.removeFile = async(ctx, id, options) => {
const models = Self.app.models;
const myOptions = {}; const myOptions = {};
let tx; let tx;
@ -32,14 +31,15 @@ module.exports = Self => {
} }
try { try {
const targetTicketDms = await models.TicketDms.findById(id, null, myOptions); // const targetTicketDms = await models.TicketDms.findById(id, null, myOptions);
const targetDms = await models.Dms.findById(targetTicketDms.dmsFk, null, myOptions); myOptions.model = 'TicketDms';
const trashDmsType = await models.DmsType.findOne({where: {code: 'trash'}}, myOptions); // 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); const targetDms = await Self.app.models.Dms.removeFile(ctx, id, myOptions);
await targetTicketDms.destroy(myOptions); // await targetTicketDms.destroy(myOptions);
await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions); // await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions);
if (tx) await tx.commit(); if (tx) await tx.commit();

View File

@ -1,6 +1,6 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('TicketDms removeFile()', () => { fdescribe('TicketDms removeFile()', () => {
const ticketDmsId = 1; const ticketDmsId = 1;
it(`should return an error for a user without enough privileges`, async() => { it(`should return an error for a user without enough privileges`, async() => {
const tx = await models.TicketDms.beginTransaction({}); const tx = await models.TicketDms.beginTransaction({});

View File

@ -18,13 +18,30 @@ module.exports = Self => {
} }
}); });
Self.removeFile = async(ctx, id) => { Self.removeFile = async(ctx, id, options) => {
const models = Self.app.models; 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;
}
}; };
}; };

View File

@ -1,6 +1,7 @@
const app = require('vn-loopback/server/server'); const app = require('vn-loopback/server/server');
describe('WorkerDms removeFile()', () => { // f
fdescribe('WorkerDms removeFile()', () => {
const workerDmsFk = 1; const workerDmsFk = 1;
it(`should return an error for a user without enough privileges`, async() => { it(`should return an error for a user without enough privileges`, async() => {
let clientId = 1101; let clientId = 1101;