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) => { Self.removeFile = async(ctx, id, options) => {
const models = Self.app.models; const models = Self.app.models;
let tx;
const myOptions = {}; const myOptions = {};
let tx;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);

View File

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

View File

@ -19,9 +19,8 @@ module.exports = Self => {
}); });
Self.removeFile = async(ctx, id, options) => { Self.removeFile = async(ctx, id, options) => {
const models = Self.app.models;
let tx;
const myOptions = {}; const myOptions = {};
let tx;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
@ -34,13 +33,16 @@ module.exports = Self => {
try { try {
const clientDms = await Self.findById(id, null, myOptions); 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(); if (tx) await tx.commit();
return destroyedClient; return clientDestroyed;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
throw e; throw e;

View File

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

View File

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