Skip to content

Commit

Permalink
Merge pull request #14 from analytics-ufcg/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JoaquimCMH authored Jul 9, 2020
2 parents 8bd8bf4 + 6793589 commit c2b9cf7
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 9 deletions.
46 changes: 40 additions & 6 deletions src/controllers/contratos.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ const Op = models.Sequelize.Op;
const BAD_REQUEST = 400;
const SUCCESS = 200;

// Retorna todas os contratos de um município
// 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
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 @@ -31,8 +32,8 @@ exports.getContratoById = (req, res) => {
id_contrato: id
}
})
.then(contratos => res.json(contratos))
.catch(err => res.status(BAD_REQUEST).json({ err }));
.then(contratos => res.json(contratos))
.catch(err => res.status(BAD_REQUEST).json({ err }));
}

// Recupera todos os contratos de uma licitação
Expand All @@ -44,6 +45,39 @@ exports.getContratosByLicitacao = (req, res) => {
id_licitacao: id_licitacao
}
})
.then(contratos => res.json(contratos))
.catch(err => res.status(BAD_REQUEST).json({ err }));
.then(contratos => res.json(contratos))
.catch(err => res.status(BAD_REQUEST).json({ err }));
}

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

const termos = req.query.termo.split(' ').join(' & ')

let query = `SELECT \
id_contrato, \
de_obs \
FROM \
( \
SELECT \
*, \
to_tsvector( \
contrato.language::regconfig, \
contrato.de_obs \
) AS document \
FROM \
contrato \
) p_search \
WHERE \
p_search.document @@to_tsquery('portuguese', '${termos}') \
ORDER BY \
ts_rank( \
p_search.document, \
to_tsquery('portuguese', '${termos}') \
) DESC; `

models.sequelize.query(query, {
model: Contrato,
mapToModel: true
}).then(Contrato => res.status(SUCCESS).json(Contrato))
.catch(err => res.status(BAD_REQUEST).json({ err }));
}
8 changes: 8 additions & 0 deletions src/controllers/licitacoes.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const models = require("../models/index.model");
const Contrato = models.contrato;
const Licitacao = models.licitacao;
const Participante = models.participante;
const Op = models.Sequelize.Op;

const BAD_REQUEST = 400;
Expand Down Expand Up @@ -35,6 +36,13 @@ exports.getLicitacaoById = (req, res) => {
{
model: Contrato,
as: "contratosLicitacao"
},
{
model: Participante,
as: "participantesLicitacao",
where: {
nu_cpfcnpj: { [Op.ne]: '00000000000000'}
}
}
],
where: {
Expand Down
5 changes: 4 additions & 1 deletion src/models/contrato.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ module.exports = (sequelize, type) => {
dt_recebimento: type.DATE,
foto: type.STRING,
planilha: type.STRING,
ordem_servico : type.STRING
ordem_servico : type.STRING,
language : type.STRING,
de_ugestora : type.STRING,
no_fornecedor : type.STRING
},
{
freezeTableName: true,
Expand Down
4 changes: 3 additions & 1 deletion src/models/index.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ const { sequelize } = require("../config/db.config");
const LicitacaoModel = "./licitacao.model.js";
const MunicipioModel = "./municipio.model.js";
const ContratoModel = "./contrato.model.js";
const ParticipanteModel = "./participante.model.js"

global.models = {
Sequelize: Sequelize,
sequelize: sequelize,
// Adicione os módulos abaixo
licitacao: sequelize.import(LicitacaoModel),
municipio: sequelize.import(MunicipioModel),
contrato: sequelize.import(ContratoModel)
contrato: sequelize.import(ContratoModel),
participante: sequelize.import(ParticipanteModel)
};

Object.keys(global.models).forEach(modelName => {
Expand Down
9 changes: 8 additions & 1 deletion src/models/licitacao.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ module.exports = (sequelize, type) => {
de_obs: type.STRING,
dt_mes_ano: type.STRING,
registro_cge: type.STRING,
tp_regime_execucao: type.INTEGER
tp_regime_execucao: type.INTEGER,
de_ugestora: type.STRING,
de_tipo_licitacao : type.STRING
},
{
freezeTableName: true,
Expand All @@ -36,6 +38,11 @@ module.exports = (sequelize, type) => {
sourceKey: "id_licitacao",
as: "contratosLicitacao"
});
Licitacao.hasMany(models.participante, {
foreignKey: "id_licitacao",
sourceKey: "id_licitacao",
as: "participantesLicitacao"
});
}
return Licitacao;
};
30 changes: 30 additions & 0 deletions src/models/participante.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Arquivo: models/contrato.model.js
* Descrição: arquivo com o modelo do participante.
*/

module.exports = (sequelize, type) => {
Participante = sequelize.define(
"participante",
{
id_participante: {
type: type.STRING,
primaryKey: true
},
id_licitacao: type.STRING,
cd_u_gestora: type.INTEGER,
dt_ano: type.INTEGER,
nu_licitacao: type.STRING,
tp_licitacao: type.INTEGER,
nu_cpfcnpj: type.STRING,
dt_mes_ano: type.STRING,
no_fornecedor : type.STRING
},
{
freezeTableName: true,
timestamps: false
}

);
return Participante;
};
2 changes: 2 additions & 0 deletions src/routes/contratos.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ router.get('/contratos/:id', contratosController.getContratoById)
// Exemplo: http://localhost:3000/api/licitacoes/b06aa4ae558ddaaaeecac2ca4aa2e186/contratos
router.get('/licitacoes/:id_licitacao/contratos', contratosController.getContratosByLicitacao)

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

module.exports = router;

0 comments on commit c2b9cf7

Please sign in to comment.