refs #2051 perf: removeFile after review
This commit is contained in:
parent
7bdb1b2078
commit
7b9e3b0985
|
@ -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);
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -19,11 +19,8 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.removeFile = async(ctx, id, options) => {
|
Self.removeFile = async(ctx, id, options) => {
|
||||||
const models = Self.app.models;
|
|
||||||
let tx;
|
|
||||||
try {
|
|
||||||
const myOptions = {};
|
const myOptions = {};
|
||||||
|
let tx;
|
||||||
if (typeof options == 'object')
|
if (typeof options == 'object')
|
||||||
Object.assign(myOptions, options);
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
|
@ -31,13 +28,19 @@ module.exports = Self => {
|
||||||
tx = await Self.beginTransaction({});
|
tx = await Self.beginTransaction({});
|
||||||
myOptions.transaction = tx;
|
myOptions.transaction = tx;
|
||||||
}
|
}
|
||||||
// const workerDms = await Self.findById(id);
|
|
||||||
myOptions.model = 'WorkerDms';
|
try {
|
||||||
await models.Dms.removeFile(ctx, id, myOptions);
|
const WorkerDms = await Self.findById(id, null, myOptions);
|
||||||
// await workerDms.destroy();
|
|
||||||
|
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();
|
if (tx) await tx.commit();
|
||||||
|
|
||||||
return workerDms;
|
return workerDestroyed;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await tx.rollback();
|
await tx.rollback();
|
||||||
throw e;
|
throw e;
|
||||||
|
|
Loading…
Reference in New Issue