module.exports = Self => { Self.remoteMethod('activeSalesPerson', { description: 'Returns actives workers with salesperson 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: `/activeSalesPerson`, verb: 'get' } }); Self.activeSalesPerson = async filter => { let where = filter.where; let sqlWhere = ''; let sqlLimit = ''; let sqlOffset = ''; let params = []; if (where) { if (where.firstName) { sqlWhere = `AND (em.firstName LIKE ? OR em.name LIKE ?)`; let search = where.firstName.like; params.push(search); params.push(search); } if (where.id) { sqlWhere = `AND em.id = ?`; params.push(where.id); } } if (filter.limit) { sqlLimit = `LIMIT ?`; params.push(filter.limit); } if (filter.skip) { sqlOffset = `OFFSET ?`; params.push(filter.skip); } let query = `SELECT em.id, em.firstName, em.name FROM worker em JOIN account.user ac ON em.userFk = ac.id JOIN account.role r ON r.id = ac.role WHERE ac.active ${sqlWhere} ORDER BY em.name ASC ${sqlLimit} ${sqlOffset}`; return await Self.rawSql(query, params); }; };