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

53 lines
1.3 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
Self.remoteMethodCtx('removeFile', {
description: 'Removes a ticket 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 ticketDms = await Self.findById(id, null, myOptions);
const targetDms = await Self.app.models.Dms.removeFile(ctx, ticketDms.dmsFk, myOptions);
if (!targetDms || !ticketDms)
throw new UserError('Try again');
const ticketDestroyed = await ticketDms.destroy(myOptions);
if (tx) await tx.commit();
return ticketDestroyed;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};