salix/modules/claim/back/methods/claim-dms/removeFile.js

55 lines
1.4 KiB
JavaScript

const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('removeFile', {
description: 'Removes a claim document',
accessType: 'WRITE',
accepts: {
arg: 'id',
type: 'number',
description: 'The document id',
http: {source: 'path'}
},
returns: {
type: 'object',
root: true
},
http: {
path: `/:id/removeFile`,
verb: 'POST'
}
});
Self.removeFile = async(ctx, id, options) => {
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const claimDms = await Self.findById(id, null, myOptions);
const targetDms = await Self.app.models.Dms.removeFile(ctx, claimDms.dmsFk, myOptions);
if (!targetDms || ! claimDms)
throw new UserError('Try again');
const claimDmsDestroyed = await claimDms.destroy(myOptions);
if (tx) await tx.commit();
return claimDmsDestroyed;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};