diff --git a/src/libs/routes/employees.js b/src/libs/routes/employees.js index e59cbde5e66ef790e605b3afcb1f5966b7bab040..fe143808b83d94fa76c4b26d5c640fd4aeeaddcf 100644 --- a/src/libs/routes/employees.js +++ b/src/libs/routes/employees.js @@ -254,6 +254,59 @@ rqf.addField({ } }); +function matchQueries(queryTotal, queryPartial) { + let match = []; + queryPartial.forEach((result) => { + let newObj = {}; + let keys = Object.keys(result); + keys.forEach((key) => { + newObj[key] = result[key]; + }); + // console.log('NEW OBJ'); + // console.log(newObj); + // remove total + let index = keys.indexOf('total'); + if(index > -1) keys.splice(index, 1); + // remove education_level_basic_id + index = keys.indexOf('education_level_basic_id'); + if(index > -1) keys.splice(index, 1); + // remove education_level_basic_name + index = keys.indexOf('education_level_basic_name'); + if(index > -1) keys.splice(index, 1); + let objMatch = null; + + for(let i = 0; i < queryTotal.length; ++i) { + let total = queryTotal[i]; + let foundMatch = true; + for(let j = 0; j < keys.length; ++j) { + let key = keys[j]; + if(total[key] !== result[key]) { + foundMatch = false; + break; + } + } + if(foundMatch) { + objMatch = total; + break; + } + } + + if(objMatch) { + // console.log('MATCH!!!!'); + // console.log(objMatch); + newObj.total = result.total - objMatch.total; + newObj.total_employees = result.total; + newObj.total_teachers = objMatch.total + match.push(newObj); + } + }); + // console.log('TAMANHOS'); + // console.log(queryTotal.length); + // console.log(queryPartial.length); + // console.log(match.length); + return match; +} + employeesApp.get('/', rqf.parse(), (req, res, next) => { req.allEmployees = {} req.allTeacher = {} @@ -296,12 +349,8 @@ employeesApp.get('/', rqf.parse(), (req, res, next) => { }, rqf.build(), query, id2str.transform(), (req, res, next) => { req.allTeacher = req.result; - - req.result = [] - req.result.push(req.allEmployees) - req.result.push(req.allTeacher) - console.log(req.allEmployees) - console.log(req.allTeacher) + let aux_employees = matchQueries(req.allTeacher, req.allEmployees); + req.result = aux_employees; next(); }, response('employees'));