2019-06-06 11:59:11 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('removeFile', {
|
|
|
|
description: 'Makes a logical delete moving a file to a trash folder',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'Number',
|
|
|
|
description: 'The document id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'Object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/removeFile`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-29 13:18:49 +00:00
|
|
|
Self.removeFile = async(ctx, id, options) => {
|
2021-07-08 13:53:30 +00:00
|
|
|
const models = Self.app.models;
|
2021-03-29 13:18:49 +00:00
|
|
|
let tx;
|
2021-07-08 13:53:30 +00:00
|
|
|
const myOptions = {};
|
2021-03-29 13:18:49 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-12-13 12:48:22 +00:00
|
|
|
const dms = await models.Dms.findById(id, null, myOptions);
|
2021-03-29 13:18:49 +00:00
|
|
|
const trashDmsType = await models.DmsType.findOne({
|
|
|
|
where: {code: 'trash'}
|
|
|
|
}, myOptions);
|
|
|
|
|
2023-12-13 12:48:22 +00:00
|
|
|
const hasWriteRole = await models.DmsType.hasWriteRole(ctx, dms.dmsTypeFk, myOptions);
|
2021-03-29 13:18:49 +00:00
|
|
|
if (!hasWriteRole)
|
|
|
|
throw new UserError(`You don't have enough privileges`);
|
|
|
|
|
2023-12-13 12:48:22 +00:00
|
|
|
await dms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions);
|
2021-03-29 13:18:49 +00:00
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
2023-12-13 12:48:22 +00:00
|
|
|
|
|
|
|
return dms;
|
2021-03-29 13:18:49 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-06-06 11:59:11 +00:00
|
|
|
};
|
|
|
|
};
|