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

53 lines
1.3 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 myOptions = {};
let tx;
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);
const targetDms = await Self.app.models.Dms.removeFile(ctx, clientDms.dmsFk, myOptions);
if (!targetDms || !clientDms)
throw new UserError('Try again');
2021-07-08 13:53:30 +00:00
const clientDmsDestroyed = await clientDms.destroy(myOptions);
2021-07-08 13:53:30 +00:00
if (tx) await tx.commit();
return clientDmsDestroyed;
2021-07-08 13:53:30 +00:00
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};