54 lines
1.4 KiB
JavaScript
54 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 targetEntryDms = await Self.findById(id, null, myOptions);
|
|
const targetDms = await Self.app.models.Dms.removeFile(ctx, targetEntryDms.dmsFk, myOptions);
|
|
|
|
if (!targetDms || ! targetEntryDms)
|
|
throw new UserError('Try again');
|
|
|
|
const entryDmsDestroyed = await targetEntryDms.destroy(myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return entryDmsDestroyed;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|
|
|