{"size": 2518, "ext": "js", "max_stars_repo_path": "backend/src/api/cep/cepService.js", "max_stars_repo_name": "AquarelaDesign/cep", "max_stars_repo_stars_event_min_datetime": "2020-07-07T21:24:37.000Z", "max_stars_repo_stars_event_max_datetime": "2020-07-07T21:24:37.000Z", "max_issues_repo_path": "backend/src/api/cep/cepService.js", "max_issues_repo_name": "AquarelaDesign/cep", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "backend/src/api/cep/cepService.js", "max_forks_repo_name": "AquarelaDesign/cep", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.9306930693, "max_line_length": 68, "alphanum_fraction": 0.3216838761}
const CEP = require('./cep')
const Global = require('../common/global')

exports.servico = (req, res) => {
        
    console.log(req)
    
    var Cep = 0
    
    if (req.params.id == "" || req.params.id === undefined) {
        Cep = 0
    } else {
        Cep = parseInt(req.params.id)
    }
    
    if (Cep < 0 || Cep === 0 || Cep === undefined) {
        response = {
            "error": true, 
            "message": "CEP inválido", 
            "data": [],
            "total": 0,
        };
        return res.json(response)
    }

    CEP.aggregate([
        {
            $match: { CEP: Cep }
        },
        {
            '$lookup': {
                'from': 'geo', 
                'localField': 'CEP', 
                'foreignField': 'cep', 
                'as': 'geo'
            }
        },
        {
            '$lookup': {
                'from': 'cidade', 
                'localField': 'cod_cidade', 
                'foreignField': 'cod_ibge', 
                'as': 'cidade'
            }
        },
        {
            "$unwind": {
                "path": "$geo",
                "preserveNullAndEmptyArrays": false
            }
        }, 
        { 
            $facet: {
                metadata: [ 
                    { $count: "total" }, 
                ],
                data: [
                    { $skip: 0 }, 
                ]
            }
        }       
    ]).exec((err, data) => {
        if (err) {
            response = {
                "error": true, 
                "message": "Falha na busca dos dados", 
                "data": [],
                "total": 0,
            };
        } else {
            var total = data.length;

            Data = Global.ajustaRes(data)
            
            if (Object.keys(Data[0].metadata).length < 1) {
                total = 0

                response = {
                    "error": true, 
                    "message": "A consulta não retornou resultado", 
                    "data": [],
                    "total": 0,
                };
                res.setHeader('content-range', 0 + '/' + 0)
            } else {
                total = Data[0].metadata[0].total

                response = {
                    "error": false, 
                    "message":"", 
                    "data": Data,
                    "total": total,
                };
                res.setHeader('content-range', total + '/' + total)
            }
            
        }
        res.send(response)
    })
}
