2019-06-06 11:59:11 +00:00
|
|
|
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',
|
2019-06-06 11:59:11 +00:00
|
|
|
description: 'The document id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
returns: {
|
2021-07-08 13:53:30 +00:00
|
|
|
type: 'object',
|
2019-06-06 11:59:11 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/removeFile`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-08 13:53:30 +00:00
|
|
|
Self.removeFile = async(ctx, id, options) => {
|
2019-06-06 11:59:11 +00:00
|
|
|
const models = Self.app.models;
|
2021-07-08 13:53:30 +00:00
|
|
|
let tx;
|
|
|
|
const myOptions = {};
|
2019-06-06 11:59:11 +00:00
|
|
|
|
2021-07-08 13:53:30 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
2019-06-06 11:59:11 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2019-06-06 11:59:11 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|