module.exports = Self => { Self.remoteMethod('activeBuyer', { description: 'Returns actives workers with Buyer role', accessType: 'READ', accepts: [{ arg: 'filter', type: 'Object', required: false, description: 'Filter defining where and paginated data', http: {source: 'query'} }], returns: { type: 'Worker', root: true }, http: { path: `/activeBuyer`, verb: 'get' } }); Self.activeBuyer = async filter => { let sqlWhere = ''; let sqlLimit = ''; let sqlOffset = ''; let params = []; if (filter.where) { if (filter.where.firstName) { sqlWhere = `AND (worker.firstName LIKE ? OR worker.name LIKE ?)`; let search = where.firstName.like; params.push(search); params.push(search); } if (filter.where.id) { sqlWhere = `AND worker.id = ?`; params.push(filter.where.id); } } if (filter.limit) { sqlLimit = `LIMIT ?`; params.push(filter.limit); } if (filter.skip) { sqlOffset = `OFFSET ?`; params.push(filter.skip); } let query = `SELECT worker.id, worker.firstName, worker.name FROM vn.worker JOIN account.user user ON user.id = worker.userFk JOIN account.role role ON role.name = 'buyer' JOIN account.roleRole inheritance ON inheritance.role = user.role AND inheritance.inheritsFrom = role.id WHERE user.active IS TRUE ${sqlWhere} ORDER BY worker.firstName ASC ${sqlLimit} ${sqlOffset}`; return await Self.rawSql(query, params); }; };