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 {
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;

View File

@ -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() => {

View File

@ -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();

View File

@ -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;

View File

@ -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({});

View File

@ -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();

View File

@ -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({});

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 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');
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;