Skip to content
Snippets Groups Projects
id2str.js 4.66 KiB
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 admDependencyPriv = require(`${libs}/convert/admDependencyPriv`);
const location = require(`${libs}/convert/location`);
const ruralLocation = require(`${libs}/convert/ruralLocation`);
const ethnicGroup = require(`${libs}/convert/ethnicGroup`);
Fernando Erd's avatar
Fernando Erd committed
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 educationLevelShort = require(`${libs}/convert/educationLevelShort`);
Vytor Calixto's avatar
Vytor Calixto committed
const educationType = require(`${libs}/convert/educationType`);
const citySize = require(`${libs}/convert/citySize`);
const incomeLevel = require(`${libs}/convert/incomeLevel`);
Fernando Erd's avatar
Fernando Erd committed
const idhmLevel = require(`${libs}/convert/idhmLevel`);
Fernando Erd's avatar
Fernando Erd committed
const stateName = require(`${libs}/convert/stateName`);
Fernando Erd's avatar
Fernando Erd committed
const contractType = require(`${libs}/convert/contractType`);
const ethnicGroupPnad = require(`${libs}/convert/ethnicGroupPnad`);
const ageRange = require(`${libs}/convert/ageRange`);
const fullAgeRange = require(`${libs}/convert/fullAgeRange`);
const genderPnad = require(`${libs}/convert/genderPnad`);
const fifthHouseholdIncome = require(`${libs}/convert/fifthHouseholdIncome`);
const extremesHouseholdIncome = require(`${libs}/convert/extremesHouseholdIncome`);
Fernando Erd's avatar
Fernando Erd committed
const educationLevelBasic = require(`${libs}/convert/educationLevelBasic`);
Vytor Calixto's avatar
Vytor Calixto committed

const ids = {
    gender_id: gender,
    period_id: period,
    education_level_id: educationLevel,
Fernando Erd's avatar
Fernando Erd committed
    education_level_basic_id: educationLevelBasic,
    education_level_mod_id: educationLevelMod,
    education_level_short_id: educationLevelShort,
    adm_dependency_id: admDependency,
    adm_dependency_detailed_id: admDependencyPriv,
    rural_location_id: ruralLocation,
    location_detailed_id: ruralLocation,
    ethnic_group_id: ethnicGroup,
Fernando Erd's avatar
Fernando Erd committed
    agreement_id: agreement,
Vytor Calixto's avatar
Vytor Calixto committed
    integral_time_id: booleanVariable,
    government_agreement_id: booleanVariable,
    education_day_care_child_id: booleanVariable,
    education_preschool_child_id: booleanVariable,
    education_begin_elementary_school_id: booleanVariable,
    education_end_elementary_school_id: booleanVariable,
    education_middle_school_id: booleanVariable,
    education_professional_id: booleanVariable,
    education_eja_id: booleanVariable,
    education_type_id: educationType,
    income_level_id: incomeLevel,
Fernando Erd's avatar
Fernando Erd committed
    city_size_id: citySize,
Fernando Erd's avatar
Fernando Erd committed
    idhm_level_id: idhmLevel,
Fernando Erd's avatar
Fernando Erd committed
    state_id: stateName,
    contract_type_id: contractType,
    ethnic_group_pnad_id: ethnicGroupPnad,
    age_range_id: ageRange,
    full_age_range_id: fullAgeRange,
Fernando Erd's avatar
Fernando Erd committed
    gender_pnad_id: genderPnad,
    fifth_household_income_id: fifthHouseholdIncome,
    extremes_household_income_id: extremesHouseholdIncome
Vytor Calixto's avatar
Vytor Calixto committed
};

function transform(removeId=false) {
    return (req, res, next) => {
        if(req.result.length <= 0) {
            return next();
        }
Vytor Calixto's avatar
Vytor Calixto committed
        // 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();
    };
}

function multitransform(removeId=false) {
    return (req, res, next) => {
        Object.keys(req.result[0]).forEach((query) => {
            req.result[0][query].forEach((obj) => {
                Object.keys(obj).forEach((key) => {
                    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,
    multitransform,
Vytor Calixto's avatar
Vytor Calixto committed
    educationLevel,
Fernando Erd's avatar
Fernando Erd committed
    educationLevelBasic,
Vytor Calixto's avatar
Vytor Calixto committed
    educationLevelMod,
    educationLevelShort,
Vytor Calixto's avatar
Vytor Calixto committed
    admDependencyPriv,
Vytor Calixto's avatar
Vytor Calixto committed
    ruralLocation,
Fernando Erd's avatar
Fernando Erd committed
    agreement,
    booleanVariable,
Vytor Calixto's avatar
Vytor Calixto committed
    educationType,
    incomeLevel,
    citySize,
    idhmLevel,
Fernando Erd's avatar
Fernando Erd committed
    stateName,
    contractType,
    ethnicGroupPnad,
    genderPnad,
    fifthHouseholdIncome,
    extremesHouseholdIncome