module.exports = Self => { Self.remoteMethod('isValidClient', { description: 'Checks if the user havent got the role employee, is active and has verified data', accessType: 'READ', accepts: [ { arg: 'id', type: 'string', required: true, description: 'The user id', http: {source: 'path'} }, { arg: 'context', type: 'object', required: true, description: 'Filter defining where', http: function(context) { return context.req.query; } } ], returns: { type: 'boolean', root: true }, http: { path: `/:id/isValidClient`, verb: 'GET' } }); Self.isValidClient = async function(id) { let query = `SELECT r.name FROM salix.Account A JOIN vn.client C ON A.id = C.id JOIN salix.RoleMapping rm ON rm.principalId = A.id JOIN salix.Role r ON r.id = rm.roleId WHERE A.id = ? AND C.isActive AND C.isTaxDataChecked`; let roleNames = await Self.rawSql(query, [id]); if (!roleNames.length) return false; roleNames.forEach(role => { if (role.name === 'employee') return false; }); return true; }; };