2023-12-13 13:24:05 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
2019-07-30 06:51:38 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('removeFile', {
|
2019-11-22 12:46:38 +00:00
|
|
|
description: 'Removes a claim document',
|
2019-07-30 06:51:38 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: {
|
|
|
|
arg: 'id',
|
2021-03-29 13:42:14 +00:00
|
|
|
type: 'number',
|
2019-07-30 06:51:38 +00:00
|
|
|
description: 'The document id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
returns: {
|
2021-03-29 13:42:14 +00:00
|
|
|
type: 'object',
|
2019-07-30 06:51:38 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/removeFile`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-29 13:42:14 +00:00
|
|
|
Self.removeFile = async(ctx, id, options) => {
|
2021-11-18 10:17:30 +00:00
|
|
|
const myOptions = {};
|
2023-12-13 13:24:05 +00:00
|
|
|
let tx;
|
2019-07-30 06:51:38 +00:00
|
|
|
|
2021-03-29 13:42:14 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
2019-07-30 06:51:38 +00:00
|
|
|
|
2021-03-29 13:42:14 +00:00
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-12-13 13:24:05 +00:00
|
|
|
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');
|
2023-12-12 11:09:06 +00:00
|
|
|
|
2023-12-14 11:36:36 +00:00
|
|
|
const claimDmsDestroyed = await claimDms.destroy(myOptions);
|
2021-03-29 13:42:14 +00:00
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
2023-12-14 11:36:36 +00:00
|
|
|
return claimDmsDestroyed;
|
2021-03-29 13:42:14 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-07-30 06:51:38 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|