2024-01-18 14:09:52 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('removeFile', {
|
2024-02-26 06:22:51 +00:00
|
|
|
description: 'Removes a entry document',
|
2024-01-18 14:09:52 +00:00
|
|
|
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);
|
|
|
|
|
2024-02-26 06:22:51 +00:00
|
|
|
if (!targetDms)
|
2024-01-18 14:09:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|