Skip to content
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"ejs": "^2.6.1",
"express": "5.1.0",
"express-validator": "7.2.1",
"fast-csv": "^5.0.5",
"handlebars": "^4.7.8",
"jsonwebtoken": "9.0.2",
"knex": "2.5.1",
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/fichas-tombos-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export default function fichaTomboController(request, response, next) {
familia: tombo.familia,
imprimir: request.params.imprimir_cod,

identificador: tombo.identificadores[0].nome,
identificador: tombo.identificadores?.[0]?.nome,
identificacao: {
...identificacao,
data_identificacao: formataColunasSeparadas(
Expand Down
4 changes: 4 additions & 0 deletions src/controllers/pendencias-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,10 @@
updateTombo.nomes_populares = alteracao.nomes_populares;
}

if (alteracao.unicata !== undefined) {
updateTombo.unicata = alteracao.unicata;
}

if (alteracao.numero_coleta !== undefined) {
updateTombo.numero_coleta = alteracao.numero_coleta;
}
Expand Down Expand Up @@ -1370,7 +1374,7 @@

if (nomesCientificosPartes.length > 0) {
updateTombo.nome_cientifico = nomesCientificosPartes.join(' ');
} else if (Object.keys(updateTombo).some(key => key.includes('familia_id') || key.includes('genero_id') || key.includes('especie_id') || key.includes('sub_especie_id') || key.includes('variedade_id'))) {

Check warning on line 1377 in src/controllers/pendencias-controller.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 207. Maximum allowed is 150
updateTombo.nome_cientifico = null;
}

Expand Down Expand Up @@ -1755,21 +1759,21 @@
if (objetoAlterado.genero_nome) parametros.genero = objetoAlterado.genero_nome;
if (objetoAlterado.especie_nome) parametros.especie = objetoAlterado.especie_nome;

if (objetoAlterado.familia_id) parametros.familia = await Familia.findOne({ where: { id: objetoAlterado.familia_id }, raw: true, nest: true });

Check warning on line 1762 in src/controllers/pendencias-controller.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 151. Maximum allowed is 150
if (objetoAlterado.subfamilia_id) parametros.subfamilia = await Subfamilia.findOne({ where: { id: objetoAlterado.subfamilia_id }, raw: true, nest: true });

Check warning on line 1763 in src/controllers/pendencias-controller.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 163. Maximum allowed is 150
if (objetoAlterado.genero_id) parametros.genero = await Genero.findOne({ where: { id: objetoAlterado.genero_id }, raw: true, nest: true });
if (objetoAlterado.especie_id) parametros.especie = await Especie.findOne({ where: { id: objetoAlterado.especie_id }, raw: true, nest: true });

Check warning on line 1765 in src/controllers/pendencias-controller.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 151. Maximum allowed is 150

if (objetoAlterado.identificadores) parametros.identificador = await Usuario.findOne({ where: { id: objetoAlterado.identificadores }, raw: true, nest: true });

Check warning on line 1767 in src/controllers/pendencias-controller.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 167. Maximum allowed is 150
if (objetoAlterado.fase_sucessional_id) parametros.faseSucessional = await FaseSucessional.findOne({ where: { numero: objetoAlterado.fase_sucessional_id }, raw: true, nest: true });

Check warning on line 1768 in src/controllers/pendencias-controller.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 189. Maximum allowed is 150
if (objetoAlterado.vegetacao_id) parametros.vegetacao = await Vegetacao.findOne({ where: { id: objetoAlterado.vegetacao_id }, raw: true, nest: true });

Check warning on line 1769 in src/controllers/pendencias-controller.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 159. Maximum allowed is 150
if (objetoAlterado.relevo_id) parametros.relevo = await Relevo.findOne({ where: { id: objetoAlterado.relevo_id }, raw: true, nest: true });
if (objetoAlterado.solo_id) parametros.solo = await Solo.findOne({ where: { id: objetoAlterado.solo_id }, raw: true, nest: true });
if (objetoAlterado.cidade_id) parametros.cidade = await Cidade.findOne({ where: { id: objetoAlterado.cidade_id }, raw: true, nest: true });
if (objetoAlterado.tipo_id) parametros.tipo = await Tipo.findOne({ where: { id: objetoAlterado.tipo_id }, raw: true, nest: true });
if (objetoAlterado.entidade_id) parametros.entidade = await Herbario.findOne({ where: { id: objetoAlterado.entidade_id }, raw: true, nest: true });

Check warning on line 1774 in src/controllers/pendencias-controller.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 155. Maximum allowed is 150
if (objetoAlterado.variedade_id) parametros.variedade = await Variedade.findOne({ where: { id: objetoAlterado.variedade_id }, raw: true, nest: true });

Check warning on line 1775 in src/controllers/pendencias-controller.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 159. Maximum allowed is 150
if (objetoAlterado.sub_especie_id) parametros.subespecie = await Subespecie.findOne({ where: { id: objetoAlterado.sub_especie_id }, raw: true, nest: true });

Check warning on line 1776 in src/controllers/pendencias-controller.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 165. Maximum allowed is 150

const tombo = await Tombo.findOne({
where: { hcf: alteracao.dataValues.tombo_hcf, ativo: true },
Expand Down
100 changes: 80 additions & 20 deletions src/controllers/tombos-controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable quotes */
// @ts-nocheck
import { ForeignKeyConstraintError } from 'sequelize';

import { padronizarNomeDarwincore } from '~/helpers/padroniza-nome-darwincore';

import BadRequestExeption from '../errors/bad-request-exception';
Expand Down Expand Up @@ -32,6 +34,7 @@ export const cadastro = (request, response, next) => {
coletor_complementar,
colecoes_anexas: colecoesAnexas,
observacoes,
unicata,
} = request.body.json;
let tomboCriado = null;

Expand Down Expand Up @@ -276,13 +279,16 @@ export const cadastro = (request, response, next) => {
coletor_id: coletor,
};

if (paisagem.descricao) {
if (paisagem?.descricao) {
jsonTombo.descricao = paisagem.descricao;
}

if (observacoes) {
jsonTombo.observacao = observacoes;
}
if (unicata !== undefined) {
jsonTombo.unicata = unicata;
}
if (principal.nome_popular) {
jsonTombo.nomes_populares = principal.nome_popular;
}
Expand Down Expand Up @@ -518,6 +524,9 @@ function alteracaoCuradorouOperador(request, response, transaction) {
const descricao = body?.paisagem?.descricao;
if (descricao !== undefined) update.descricao = descricao;

const unicata = body?.unicata;
if (unicata !== undefined) update.unicata = unicata;

const faseSucessionalId = body?.paisagem?.fase_sucessional_id;
if (faseSucessionalId !== undefined) update.fase_sucessional_id = faseSucessionalId;

Expand Down Expand Up @@ -988,6 +997,7 @@ export const obterTombo = async (request, response, next) => {
'data_identificacao_mes',
'data_identificacao_ano',
'descricao',
'unicata',
],
include: [
{
Expand Down Expand Up @@ -1193,6 +1203,7 @@ export const obterTombo = async (request, response, next) => {
tipo: tombo.colecoes_anexa !== null ? tombo.colecoes_anexa?.tipo : '',
observacao: tombo.colecoes_anexa !== null ? tombo.colecoes_anexa?.observacoes : '',
},
unicata: tombo.unicata ?? null,
};
let dataCol = '';
let dataIdent = '';
Expand Down Expand Up @@ -1508,6 +1519,74 @@ export const getUltimoNumeroTombo = (request, response, next) => {
.catch(next);
};

export async function deletarCodigoBarras(request, response, next) {
try {
const count = await TomboFoto.destroy({
where: { num_barra: request.params.codigo },
});

if (count === 0) {
return response.status(404).json({ error: 'Código de barras não encontrado' });
}
return response.status(204).end();
} catch (err) {
return next(err);
}
}

export const getUltimoCodigoBarra = async (req, res, next) => {
try {
const row = await TomboFoto.findOne({
attributes: ['id', 'codigo_barra', 'num_barra', 'caminho_foto'],
order: [
['tombo_hcf', 'DESC'],
['num_barra', 'DESC'],
['id', 'DESC'],
],
});
return res.status(codigos.BUSCAR_UM_ITEM).json(row || null);
} catch (err) {
return next(err);
}
};

const toHCFFormat = codigo => {
const codigoStr = String(codigo || '').padStart(9, '0');
return `HCF${codigoStr}`;
};

export const postCodigoBarraTombo = (request, response, next) => {
const criarTransacaoCodigoBarra = transaction =>
Promise.resolve().then(() => {
const { hcf, codigo_barra: codigoBarra } = request.body || {};

const formattedCodigoBarra = toHCFFormat(codigoBarra);
if (!formattedCodigoBarra) {
throw new BadRequestExeption(417);
}

const payload = {
tombo_hcf: hcf,
em_vivo: true,
codigo_barra: formattedCodigoBarra,
num_barra: codigoBarra,
caminho_foto: null,
};

return TomboFoto.create(payload, { transaction });
});

return sequelize
.transaction(criarTransacaoCodigoBarra)
.then(foto => response.status(201).json(foto))
.catch(err => {
if (err instanceof ForeignKeyConstraintError) {
return response.status(400).json({ error: 'Violação de chave estrangeira.' });
}
return next(err);
});
};

export const getUltimoNumeroCodigoBarras = (request, response, next) => {
const { emVivo } = request.params;
Promise.resolve()
Expand Down Expand Up @@ -1573,23 +1652,4 @@ export const editarCodigoBarra = (request, response, next) => {
.catch(next);
};

export const deletaCodigoBarra = codBarra => TomboFoto.destroy({
where: {
codigo_barra: codBarra,
},
});

export const deletarCodigoBarra = (request, response, next) => {
const { idTombo } = request.params;
Promise.resolve()
.then(() => deletaCodigoBarra(idTombo))
.then(retorno => {
if (!retorno) {
throw new BadRequestExeption(111);
}
response.status(codigos.EDITAR_SEM_RETORNO).send();
})
.catch(next);
};

export default {};
33 changes: 33 additions & 0 deletions src/database/migration/20250827115833_update_locais_coleta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable */

import { parseFile } from 'fast-csv'
import { Knex } from 'knex'

export async function run(knex: Knex): Promise<void> {
const updates: Array<{
HCF: string;
LOCAL_COLETA: string;
DESCRICAO_COLETA: string;
}> = []

await new Promise<void>((resolve, reject) => {
parseFile(`${__dirname}/locais_coleta_corrigidos.csv`, { headers: true })
.on('error', error => reject(error))
.on('data', row => {
updates.push(row)
})
.on('end', () => resolve())
})

console.log(updates.length)

await knex.transaction(async trx => {
for (const update of updates) {
const { LOCAL_COLETA, DESCRICAO_COLETA } = update
await trx('locais_coleta').where({ id: LOCAL_COLETA })
.update({
descricao: DESCRICAO_COLETA
})
}
})
}
173 changes: 173 additions & 0 deletions src/database/migration/20250827122308_create_locais_coleta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/* eslint-disable */

import { Knex } from 'knex'

export async function run(knex: Knex): Promise<void> {
await knex.transaction(async trx => {
let res

res = await trx('locais_coleta').insert({
descricao:
'Parque Estadual Serra da Baitaca-Caminho do Itupava. Trilha entre a entrada p/ o antes da entrada para o Morro Pão de Loth',
cidade_id: 4120804
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 42136
})

res = await trx('locais_coleta').insert({
descricao:
'Parque Estadual Serra da Baitaca-Caminho do Itupava. Trilha entre a entrada p/ o Morro Pão de Loth até as Ruínas do Ipiranga',
cidade_id: 4120804
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 41996
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama-Cabeça de Veado.ARIE do Córrego do Cedro. R.A. do Núcleo Bandeirante; SMPW. Q.26, conj. 3. Próximo à A.E.E. (EMBRAPA)',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30781
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama-Cabeça de Veado.ARIE do Córrego do Cedro. R.A. do Núcleo Bandeirante; Lagoa do Córrego do Cedro. Em frente ao SMPW Q.16 conj. 1',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30869
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama-Cabeça de Veado.ARIE do Córrego do Cedro. R.A. do Núcleo Bandeirante; Park Way. Clube da EMBRAPA. Q. 26. Conjunto 3 (área pública)',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30619
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama-Cabeça de Veado. ARIE Córrego do Cedro-R.A. Núcleo Bandeirante. SMPW Q 26 entre o conjunto 10 e a EMBRAPA (A.E.E)',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 20082
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30868
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama-Cabeça de Veado. ARIE Córrego do Cedro-R.A. Núcleo Bandeirante. SMPW Lagoa do Córrego do Cedro. Q 16. Conj. 01. Lote 4',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30780
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30801
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama-Cabeça de Veado. ARIE Córrego do Cedro-R.A. Núcleo Bandeirante. SMPW Park Way. Clube da EMBRAPA. Q 26. Conjunto 3 (área pública)',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30620
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 30621
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama e Cabeça de Veado, R.A. do Lago Sul. Fazenda Água Limpa (FAL-UnB). Área da "MOA"',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 20083
})

res = await trx('locais_coleta').insert({
descricao:
'APA Gama e Cabeça de Veado, R.A. do Lago Sul. Fazenda Água Limpa (FAL-UnB). Área Borda de mata de galeria do Córrego do Macaco, afluente Gama',
cidade_id: 5300108
})

await trx('tombos')
.update({
local_coleta_id: res[0]
})
.where({
hcf: 38121
})
})
}
Loading