From 19ec0592e4f2ae0cc26d42f73d34877822b5abb3 Mon Sep 17 00:00:00 2001 From: feliperm17 Date: Tue, 9 Jun 2026 20:48:36 -0300 Subject: [PATCH] =?UTF-8?q?Adi=C3=A7=C3=A3o=20da=20busca=20pela=20cidade?= =?UTF-8?q?=20correta=20para=20a=20coordenada=20informada?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/tombos-controller.js | 29 ++++++++++++++++++++++++---- src/routes/tombos.js | 12 ++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/controllers/tombos-controller.js b/src/controllers/tombos-controller.js index 05af31f..ff4c39b 100644 --- a/src/controllers/tombos-controller.js +++ b/src/controllers/tombos-controller.js @@ -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}) @@ -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); } diff --git a/src/routes/tombos.js b/src/routes/tombos.js index b326f5a..11179bf 100644 --- a/src/routes/tombos.js +++ b/src/routes/tombos.js @@ -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':