const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethod('removes', { description: 'Delete an client dms', accessType: 'WRITE', accepts: { arg: 'dmsId', type: 'number', required: true, description: 'dms identifier', }, returns: { type: 'string', root: true }, http: { path: `/removes`, verb: 'POST' } }); Self.removes = async dmsId => { if (!dmsId) throw new UserError('There is nothing to delete'); let targetClientDms = await Self.app.models.ClientDms.findOne({where: {dmsFk: dmsId}}); let targetDms = await Self.app.models.Dms.findById(dmsId); let trashDmsType = await Self.app.models.DmsType.findOne({where: {code: 'trash'}}); await targetClientDms.destroy(); await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id); }; };