Skip to content

Commit

Permalink
Merge pull request #22 from analytics-ufcg/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JoaquimCMH authored Aug 5, 2020
2 parents 4747bef + 1b88724 commit 58b9c8c
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 12 deletions.
73 changes: 65 additions & 8 deletions src/controllers/contratos.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@

const models = require("../models/index.model");
const Contrato = models.contrato;
const Previsao = models.previsao;

const Op = models.Sequelize.Op;
const sequelize = models.sequelize;

const BAD_REQUEST = 400;
const SUCCESS = 200;

// Retorna todas os contratos de um município que possuem ID da licitação
// Retorna todas os contratos de um município que possuem ID da licitação
exports.getContratosPorMunicipio = (req, res) => {
const cd_municipio = req.query.cd_municipio

Contrato.findAll({ where: {
cd_municipio: cd_municipio,
id_licitacao: { [Op.ne]: null}

Contrato.findAll({
include: [
{
model: Previsao,
as: "previsaoContrato"
}
],
where: {
cd_municipio: cd_municipio,
id_licitacao: { [Op.ne]: null
}
}})
.then(contratos => res.status(SUCCESS).json(contratos))
.catch(err => res.status(BAD_REQUEST).json({ err }));
Expand All @@ -28,6 +39,12 @@ exports.getContratoById = (req, res) => {
const id = req.params.id

Contrato.findOne({
include: [
{
model: Previsao,
as: "previsaoContrato"
}
],
where: {
id_contrato: id
}
Expand All @@ -49,13 +66,53 @@ exports.getContratosByLicitacao = (req, res) => {
.catch(err => res.status(BAD_REQUEST).json({ err }));
}

// Retorna todas os contratos, ordenados pelo Risco
exports.getContratosPorRisco = (req, res) => {

Contrato.findAll({
include: [
{
model: Previsao,
as: "previsaoContrato",
where: {
id_contrato: { [Op.ne]: null }
}
}
],
where: {
id_licitacao: { [Op.ne]: null },
where: sequelize.where( sequelize.col('pr_vigencia'), '>=', sequelize.literal('CURRENT_TIMESTAMP'))
},
order: [
[
{model: Previsao, as: 'previsaoContrato'},
'vig_prob_1',
'DESC'
]
]
})
.then(contratos => res.status(SUCCESS).json(contratos))
.catch(err => res.status(BAD_REQUEST).json({ err }));
};

exports.getContratosByQuery = (req, res) => {

const termos = req.query.termo.split(' ').join(' & ')
const termos = req.query.termo.replace(/[&|!<()\\:',]/gi, '').replace( /\s+/g, ' ').trim().split(' ').join(' & ');

let query = `SELECT \
id_contrato, \
de_obs \
de_obs, \
de_ugestora, \
vl_total_contrato, \
dt_ano, \
(\
SELECT \
vig_prob_1 \
FROM \
previsao \
WHERE \
p_search.id_contrato = previsao.id_contrato\
) \
FROM \
( \
SELECT \
Expand All @@ -80,4 +137,4 @@ exports.getContratosByQuery = (req, res) => {
mapToModel: true
}).then(Contrato => res.status(SUCCESS).json(Contrato))
.catch(err => res.status(BAD_REQUEST).json({ err }));
}
}
10 changes: 9 additions & 1 deletion src/controllers/licitacoes.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const models = require("../models/index.model");
const Contrato = models.contrato;
const Licitacao = models.licitacao;
const Participante = models.participante;
const Previsao = models.previsao;

const Op = models.Sequelize.Op;

const BAD_REQUEST = 400;
Expand Down Expand Up @@ -35,7 +37,13 @@ exports.getLicitacaoById = (req, res) => {
include: [
{
model: Contrato,
as: "contratosLicitacao"
as: "contratosLicitacao",
include: [
{
model: Previsao,
as: "previsaoContrato"
}
]
},
{
model: Participante,
Expand Down
13 changes: 13 additions & 0 deletions src/controllers/regioes.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,16 @@ exports.getMunicipios = async (req, res) => {
.then(municipios => res.status(SUCCESS).json(municipios))
.catch(err => res.status(BAD_REQUEST).json({ err }));
};

// Recupera o município pelo ID
exports.getMunicipioById = (req, res) => {
const id = req.params.id

Municipio.findOne({
where: {
cd_municipio: id
}
})
.then(municipios => res.json(municipios))
.catch(err => res.status(BAD_REQUEST).json({ err }));
}
8 changes: 8 additions & 0 deletions src/models/contrato.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,13 @@ module.exports = (sequelize, type) => {
}

);

Contrato.associate = function (models) {
Contrato.hasOne(models.previsao, {
foreignKey: "id_contrato",
sourceKey: "id_contrato",
as: "previsaoContrato"
});
}
return Contrato;
};
4 changes: 3 additions & 1 deletion src/models/index.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const LicitacaoModel = "./licitacao.model.js";
const MunicipioModel = "./municipio.model.js";
const ContratoModel = "./contrato.model.js";
const ParticipanteModel = "./participante.model.js"
const PrevisaoModel = "./previsao.model.js"

global.models = {
Sequelize: Sequelize,
Expand All @@ -18,7 +19,8 @@ global.models = {
licitacao: sequelize.import(LicitacaoModel),
municipio: sequelize.import(MunicipioModel),
contrato: sequelize.import(ContratoModel),
participante: sequelize.import(ParticipanteModel)
participante: sequelize.import(ParticipanteModel),
previsao: sequelize.import(PrevisaoModel)
};

Object.keys(global.models).forEach(modelName => {
Expand Down
47 changes: 47 additions & 0 deletions src/models/previsao.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* TEMPORÁRIO
* Arquivo: models/previsao.model.js
* Descrição: arquivo com o modelo da previsao.
*/

module.exports = (sequelize, type) => {
Previsao = sequelize.define(
"previsao",
{
id_contrato: {
type: type.STRING,
primaryKey: true
},
cd_u_gestora: type.INTEGER,
nu_licitacao: type.STRING,
nu_contrato: type.STRING,
dt_ano: type.INTEGER,
data_inicio: type.DATE,
nu_cpfcnpj: type.STRING,
tp_licitacao: type.INTEGER,
vl_total_contrato: type.INTEGER,
n_licitacoes_part: type.INTEGER,
n_licitacoes_venceu: type.INTEGER,
montante_lic_venceu: type.INTEGER,
perc_vitoria: type.INTEGER,
media_n_licitacoes_part: type.INTEGER,
media_n_licitacoes_venceu: type.INTEGER,
n_propostas: type.INTEGER,
media_n_propostas: type.INTEGER,
total_ganho: type.INTEGER,
status_tramita: type.INTEGER,
razao_contrato_por_vl_recebido: type.INTEGER,
media_num_contratos: type.INTEGER,
num_contratos: type.INTEGER,
vig_pred: type.STRING,
vig_prob_0: type.INTEGER,
vig_prob_1: type.INTEGER
},
{
freezeTableName: true,
timestamps: false
}

);
return Previsao;
};
4 changes: 4 additions & 0 deletions src/routes/contratos.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ router.get('/licitacoes/:id_licitacao/contratos', contratosController.getContrat

router.get('/search', contratosController.getContratosByQuery)

// Busca todos os contratos
// Exemplo: http://localhost:3000/api/contratos/riscos/paraiba
router.get('/contratos/riscos/paraiba', contratosController.getContratosPorRisco)

module.exports = router;
8 changes: 6 additions & 2 deletions src/routes/regioes.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
*/

const router = require('express-promise-router')();
const licitacoesController = require('../controllers/regioes.controller');
const regioesController = require('../controllers/regioes.controller');

// Retorna todos os municipios
// Exemplo: http://localhost:3000/api/municipios/
router.get('/municipios', licitacoesController.getMunicipios)
router.get('/municipios', regioesController.getMunicipios)

// Retorna um municipio pelo id
// Exemplo: http://localhost:3000/api/municipios/001
router.get('/municipios/:id', regioesController.getMunicipioById)

module.exports = router;

0 comments on commit 58b9c8c

Please sign in to comment.