2019-11-22 09:42:05 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('removeFile', {
|
2019-11-22 12:46:38 +00:00
|
|
|
description: 'Removes a worker document',
|
2019-11-22 09:42:05 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: {
|
|
|
|
arg: 'id',
|
|
|
|
type: 'Number',
|
2020-03-02 06:50:07 +00:00
|
|
|
description: 'The worker document id',
|
2019-11-22 09:42:05 +00:00
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
type: 'Object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/removeFile`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-12 11:09:06 +00:00
|
|
|
Self.removeFile = async(ctx, id, options) => {
|
2023-12-13 13:24:05 +00:00
|
|
|
const myOptions = {};
|
2023-12-12 11:09:06 +00:00
|
|
|
let tx;
|
2023-12-13 13:24:05 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
2023-12-12 11:09:06 +00:00
|
|
|
try {
|
2023-12-13 14:38:46 +00:00
|
|
|
const WorkerDms = await Self.findOne({
|
|
|
|
where: {document: id}
|
|
|
|
}, myOptions);
|
2023-12-13 13:24:05 +00:00
|
|
|
|
2023-12-13 14:38:46 +00:00
|
|
|
const targetDms = await Self.app.models.Dms.removeFile(ctx, id, myOptions);
|
2023-12-13 13:24:05 +00:00
|
|
|
|
|
|
|
if (!targetDms || !WorkerDms)
|
|
|
|
throw new UserError('Try again');
|
|
|
|
|
|
|
|
const workerDestroyed = await WorkerDms.destroy(myOptions);
|
2023-12-12 11:09:06 +00:00
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
2023-12-13 13:24:05 +00:00
|
|
|
return workerDestroyed;
|
2023-12-12 11:09:06 +00:00
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-11-22 09:42:05 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|