const UserError = require('vn-loopback/util/user-error'); const fs = require('fs-extra'); 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' } }); Self.removeFile = async(ctx, id) => { const models = Self.app.models; const dms = await models.Dms.findById(id); const dmsType = await models.DmsType.findById(dms.dmsTypeFk); const trashDmsType = await models.DmsType.findOne({ where: { code: 'trash' } }); const hasWriteRole = await models.DmsType.hasWriteRole(ctx, dms.dmsTypeFk); if (!hasWriteRole) throw new UserError(`You don't have enough privileges`); const file = await models.Container.getFile(dmsType.path, dms.file); const originPath = `${file.client.root}/${dmsType.path}/${file.name}`; const destinationPath = `${file.client.root}/${trashDmsType.path}/${file.name}`; await fs.rename(originPath, destinationPath); return dms.updateAttribute('dmsTypeFk', trashDmsType.id); }; };