refs #2051 perf: removeFile after review

This commit is contained in:
Javier Segarra 2023-12-13 14:24:05 +01:00
parent 7bdb1b2078
commit 7b9e3b0985
5 changed files with 42 additions and 37 deletions

View File

@ -22,8 +22,8 @@ module.exports = Self => {
Self.removeFile = async(ctx, id, options) => {
const models = Self.app.models;
let tx;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);

View File

@ -1,3 +1,5 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('removeFile', {
description: 'Removes a claim document',
@ -19,8 +21,8 @@ module.exports = Self => {
});
Self.removeFile = async(ctx, id, options) => {
let tx;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
@ -31,19 +33,18 @@ module.exports = Self => {
}
try {
// 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);
const claimDms = await Self.findById(id, null, myOptions);
// await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions);
const targetDms = await Self.app.models.Dms.removeFile(ctx, claimDms.dmsFk, myOptions);
if (!targetDms || ! claimDms)
throw new UserError('Try again');
const claimDestroyed = await claimDms.destroy(myOptions);
if (tx) await tx.commit();
return targetDms;
return claimDestroyed;
} catch (e) {
if (tx) await tx.rollback();
throw e;

View File

@ -19,9 +19,8 @@ module.exports = Self => {
});
Self.removeFile = async(ctx, id, options) => {
const models = Self.app.models;
let tx;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
@ -34,13 +33,16 @@ module.exports = Self => {
try {
const clientDms = await Self.findById(id, null, myOptions);
await models.Dms.removeFile(ctx, clientDms.dmsFk, myOptions);
const targetDms = await Self.app.models.Dms.removeFile(ctx, clientDms.dmsFk, myOptions);
const destroyedClient = await clientDms.destroy(myOptions);
if (!targetDms || !clientDms)
throw new UserError('Try again');
const clientDestroyed = await clientDms.destroy(myOptions);
if (tx) await tx.commit();
return destroyedClient;
return clientDestroyed;
} catch (e) {
if (tx) await tx.rollback();
throw e;

View File

@ -31,19 +31,18 @@ module.exports = Self => {
}
try {
// 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);
const ticketDms = await Self.findById(id, null, myOptions);
const targetDms = await Self.app.models.Dms.removeFile(ctx, id, myOptions);
// await targetTicketDms.destroy(myOptions);
const targetDms = await Self.app.models.Dms.removeFile(ctx, ticketDms.dmsFk, myOptions);
// await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions);
if (!targetDms || !ticketDms)
throw new UserError('Try again');
const ticketDestroyed = await ticketDms.destroy(myOptions);
if (tx) await tx.commit();
return targetDms;
return ticketDestroyed;
} catch (e) {
if (tx) await tx.rollback();
throw e;

View File

@ -19,11 +19,8 @@ module.exports = Self => {
});
Self.removeFile = async(ctx, id, options) => {
const models = Self.app.models;
let tx;
try {
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
@ -31,13 +28,19 @@ module.exports = Self => {
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();
try {
const WorkerDms = await Self.findById(id, null, myOptions);
const targetDms = await Self.app.models.Dms.removeFile(ctx, WorkerDms.dmsFk, myOptions);
if (!targetDms || !WorkerDms)
throw new UserError('Try again');
const workerDestroyed = await WorkerDms.destroy(myOptions);
if (tx) await tx.commit();
return workerDms;
return workerDestroyed;
} catch (e) {
await tx.rollback();
throw e;