const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethod('createChild', { description: 'Creates a new child department', accessType: 'WRITE', accepts: [{ arg: 'parentId', type: 'Number' }, { arg: 'name', type: 'String', required: true, }], returns: { type: 'object', root: true }, http: { path: `/createChild`, verb: 'POST' } }); Self.createChild = async(parentId = null, name) => { const models = Self.app.models; const nameExists = await models.Department.count({name}); if (nameExists) throw new UserError(`The department name can't be repeated`); const newDep = await models.Department.create({ parentFk: parentId, name: name }); return newDep; }; };