Newer
Older
const libs = `${process.cwd()}/libs`;
const gender = require(`${libs}/convert/gender`);
const period = require(`${libs}/convert/period`);
const schoolYear = require(`${libs}/convert/schoolYear`);
const admDependency = require(`${libs}/convert/admDependency`);
const location = require(`${libs}/convert/location`);
const ethnicGroup = require(`${libs}/convert/ethnicGroup`);
const agreement = require(`${libs}/convert/agreement`);
const booleanVariable = require(`${libs}/convert/booleanVariable`);
const educationLevel = require(`${libs}/convert/educationLevel`);
const educationLevelMod = require(`${libs}/convert/educationLevelMod`);
const ids = {
gender_id: gender,
period_id: period,
school_year_id: schoolYear,
education_level_id: educationLevel,
education_level_mod_id: educationLevelMod,
adm_dependency_id: admDependency,
adm_dependency_detailed_id: admDependency,
location_id: location,
integral_time_id: booleanVariable,
government_agreement_id: booleanVariable
};
function transform(removeId=false) {
return (req, res, next) => {
// Para cada objeto do resultado
req.result.forEach((obj) => {
Object.keys(obj).forEach((key) => {
// Se não há uma função especificada, retorna
if(typeof ids[key] === 'undefined') return;
let id = obj[key];
obj[key.replace('_id', '_name')] = ids[key](id);
if(removeId) delete obj[key];
});
});
next();
};
}
module.exports = {
transform,
gender,
period,
schoolYear,
admDependency,
location,
educationLevel,
educationLevelMod