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

53 lines
1.4 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('removeFile', {
description: 'Removes a worker document',
accessType: 'WRITE',
accepts: {
arg: 'id',
type: 'Number',
description: 'The worker document id',
http: {source: 'path'}
},
returns: {
type: 'Object',
root: true
},
http: {
path: `/:id/removeFile`,
verb: 'POST'
}
});
Self.removeFile = async(ctx, dmsFk, 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 WorkerDms = await Self.findOne({
where: {document: dmsFk}
}, myOptions);
const targetDms = await Self.app.models.Dms.removeFile(ctx, dmsFk, myOptions);
if (!targetDms || !WorkerDms)
throw new UserError('Try again');
const workerDmsDestroyed = await WorkerDms.destroy(myOptions);
if (tx) await tx.commit();
return workerDmsDestroyed;
} catch (e) {
await tx.rollback();
throw e;
}
};
};