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

51 lines
1.2 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
Self.remoteMethodCtx('removeFile', {
description: 'Removes a client document',
accessType: 'WRITE',
accepts: {
arg: 'id',
2021-07-08 13:53:30 +00:00
type: 'number',
description: 'The document id',
http: {source: 'path'}
},
returns: {
2021-07-08 13:53:30 +00:00
type: 'object',
root: true
},
http: {
path: `/:id/removeFile`,
verb: 'POST'
}
});
2021-07-08 13:53:30 +00:00
Self.removeFile = async(ctx, id, options) => {
const models = Self.app.models;
2021-07-08 13:53:30 +00:00
let tx;
const myOptions = {};
2021-07-08 13:53:30 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
2021-07-08 13:53:30 +00:00
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const clientDms = await Self.findById(id, null, myOptions);
await models.Dms.removeFile(ctx, clientDms.dmsFk, myOptions);
const destroyedClient = await clientDms.destroy(myOptions);
if (tx) await tx.commit();
return destroyedClient;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};