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
13 changes: 10 additions & 3 deletions src/controllers/fichas-tombos-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,16 @@ export default function fichaTomboController(request, response, next) {
const dataTombo = new Date(tombo.data_tombo);
const romanoDataTombo = (`${dataTombo.getDate()}/${romanos[dataTombo.getMonth()]}/${dataTombo.getFullYear()}`);

const identificador = tombo.identificadores?.[0]?.nome
&& tombo.identificadores?.[0]?.nome.toLowerCase() !== 'não-identificado'
? tombo.identificadores?.[0]?.nome
const identificador = tombo.identificadores && tombo.identificadores.length > 0
? tombo.identificadores
.filter(ident => ident.nome && ident.nome.toLowerCase() !== 'não-identificado')
.sort((a, b) => {
const ordemA = a.tombos_identificadores?.ordem || 0;
const ordemB = b.tombos_identificadores?.ordem || 0;
return ordemA - ordemB;
})
.map(ident => ident.nome)
.join('; ')
: '';

const romanoDataIdentificacao = formataDataIdentificacao(
Expand Down
81 changes: 45 additions & 36 deletions src/controllers/tombos-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,61 +681,72 @@ export const desativar = (request, response, next) => {
export const listagem = (request, response, next) => {
const { pagina, limite, offset } = request.paginacao;
const {
nome_cientifico: nomeCientifico, hcf, tipo, nome_popular: nomePopular, situacao,
nome_cientifico: nomeCientifico,
hcf,
tipo,
nome_popular: nomePopular,
situacao,
codigo_barra_foto,
} = request.query;

let where = {
rascunho: false,
};

if (nomeCientifico) {
where = {
...where,
nome_cientifico: { [Op.iLike]: `%${nomeCientifico}%` },
};
where.nome_cientifico = { [Op.iLike]: `%${nomeCientifico}%` };
}

if (hcf) {
where = {
...where,
hcf,
};
where.hcf = hcf;
}

if (tipo) {
where = {
...where,
tipo_id: tipo,
};
where.tipo_id = tipo;
}

if (nomePopular) {
where = {
...where,
nomes_populares: { [Op.iLike]: `%${nomePopular}%` },
};
where.nomes_populares = { [Op.iLike]: `%${nomePopular}%` };
}

if (situacao) {
where = {
...where,
situacao,
};
where.situacao = situacao;
}

let include = [
{
model: Coletor,
attributes: ['id', 'nome'],
required: false,
},
];

if (codigo_barra_foto) {
include.push({
model: TomboFoto,
where: {
codigo_barra: codigo_barra_foto,
},
attributes: ['id', 'codigo_barra', 'tombo_hcf'],
required: true,
});
}

let retorno = { // eslint-disable-line
let retorno = {
metadados: {
total: 0,
pagina,
limite,
},
tombos: [],
};

Promise.resolve()
.then(() => Tombo.count({ where }))
.then(() => Tombo.count({
where,
include: codigo_barra_foto ? include : [],
distinct: true,
}))
.then(total => {
retorno.metadados.total = total;
})
.then(() => Tombo.findAndCountAll({
.then(() => Tombo.findAll({
attributes: [
'hcf',
'nomes_populares',
Expand All @@ -744,21 +755,19 @@ export const listagem = (request, response, next) => {
'data_coleta_mes',
'data_coleta_ano',
'created_at',
'coletor_id',
'tipo_id',
],
include: {
model: Coletor,
attributes: ['id', 'nome'],
required: false,
},
include,
where,
subQuery: false,
order: [['hcf', 'DESC']],
limit: limite,
offset,
}))
.then(listaTombos => {
retorno.tombos = listaTombos.rows;
response.status(codigos.LISTAGEM)
.json(retorno);
retorno.tombos = listaTombos;
response.status(codigos.LISTAGEM).json(retorno);
})
.catch(next);
};
Expand Down
Loading