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

53 lines
1.3 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, 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 WorkerDms = await Self.findOne({
where: {document: id}
}, myOptions);
const targetDms = await Self.app.models.Dms.removeFile(ctx, id, myOptions);
if (!targetDms || !WorkerDms)
throw new UserError('Try again');
const workerDestroyed = await WorkerDms.destroy(myOptions);
if (tx) await tx.commit();
return workerDestroyed;
} catch (e) {
await tx.rollback();
throw e;
}
};
};