Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/controllers/tombos-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1784,7 +1784,7 @@ export const verificarCoordenada = async (request, response, next) => {
return response.status(400).json({ error: 'Parâmetros Inválidos' });
}

const query = `
const selectedCityQuery = `
SELECT ST_Contains(
poligono,
ST_SetSRID(ST_POINT($1, $2), ${SRID.SIRGAS_2000})
Expand All @@ -1793,16 +1793,37 @@ export const verificarCoordenada = async (request, response, next) => {
WHERE id = $3;
`;

const rows = await sequelize.query(query, {
const selectedCityRows = await sequelize.query(selectedCityQuery, {
bind: [longitude, latitude, cidadeId],
type: models.Sequelize.QueryTypes.SELECT,
});

if (!rows || rows.length === 0) {
if (!selectedCityRows || selectedCityRows.length === 0) {
return response.status(404).json({ error: 'Cidade não encontrada' });
}

return response.json({ dentro: rows[0].dentro });
const cidadeEncontradaQuery = `
SELECT c.id,
c.nome,
e.nome AS estado_nome,
e.sigla AS estado_sigla
FROM cidades c
JOIN estados e ON c.estado_id = e.id
WHERE c.poligono IS NOT NULL
AND c.poligono && ST_SetSRID(ST_POINT($1, $2), 4674)
AND ST_Contains(c.poligono, ST_SetSRID(ST_POINT($1, $2), 4674))
LIMIT 1;
`;

const cidadeEncontradaRows = await sequelize.query(cidadeEncontradaQuery, {
bind: [longitude, latitude],
type: models.Sequelize.QueryTypes.SELECT,
});

return response.json({
dentro: selectedCityRows[0].dentro === true,
cidadeEncontrada: cidadeEncontradaRows[0] || null,
});
} catch (err) {
return next(err);
}
Expand Down
12 changes: 12 additions & 0 deletions src/routes/tombos.js
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,18 @@ export default app => {
* properties:
* dentro:
* type: boolean
* cidadeEncontrada:
* type: object
* nullable: true
* properties:
* id:
* type: integer
* nome:
* type: string
* estado_nome:
* type: string
* estado_sigla:
* type: string
* '400':
* $ref: '#/components/responses/BadRequest'
* '401':
Expand Down
Loading