Skip to content

Commit c37c0f3

Browse files
authored
novos campos de filtragem de tombos (#387)
1 parent 72f768d commit c37c0f3

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

src/controllers/coletor-controller.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,34 @@ export const desativaColetor = async (req, res, next) => {
117117
next(error);
118118
}
119119
};
120+
121+
export const listaNumerosColetaPorColetor = async (req, res, next) => {
122+
try {
123+
const { coletorId } = req.params;
124+
const { Tombo } = models;
125+
const coletor = await Coletor.findByPk(coletorId);
126+
if (!coletor) {
127+
return res.status(404).json({ mensagem: 'Coletor não encontrado.' });
128+
}
129+
const numerosColeta = await Tombo.findAll({
130+
attributes: ['numero_coleta'],
131+
where: {
132+
coletor_id: coletorId,
133+
rascunho: false,
134+
numero_coleta: { [Op.not]: null },
135+
},
136+
raw: true,
137+
order: [['numero_coleta', 'ASC']],
138+
});
139+
const numerosUnicos = [...new Set(numerosColeta.map(item => item.numero_coleta))].map((numero, index) => ({
140+
id: index,
141+
numero,
142+
}));
143+
144+
res.status(200).json({
145+
numerosColeta: numerosUnicos,
146+
});
147+
} catch (error) {
148+
next(error);
149+
}
150+
};

src/controllers/tombos-controller.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,8 @@ export const listagem = (request, response, next) => {
687687
nome_popular: nomePopular,
688688
situacao,
689689
codigo_barra_foto,
690+
coletor_id: coletorId,
691+
numero_coleta: numeroColeta,
690692
} = request.query;
691693

692694
let where = {
@@ -708,6 +710,12 @@ export const listagem = (request, response, next) => {
708710
if (situacao) {
709711
where.situacao = situacao;
710712
}
713+
if (coletorId) {
714+
where.coletor_id = coletorId;
715+
}
716+
if (numeroColeta) {
717+
where.numero_coleta = numeroColeta;
718+
}
711719

712720
let include = [
713721
{

src/routes/coletor.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,44 @@ export default app => {
302302
validacoesMiddleware(desativarColetorEsquema),
303303
coletoresController.desativaColetor,
304304
]);
305+
306+
/**
307+
* @swagger
308+
* /coletores/{coletorId}/numeros-coleta:
309+
* get:
310+
* summary: Lista números de coleta de um coletor
311+
* tags: [Coletores]
312+
* description: Retorna uma lista dos números de coleta cadastrados para um coletor específico.
313+
* parameters:
314+
* - in: path
315+
* name: coletorId
316+
* required: true
317+
* schema:
318+
* type: integer
319+
* description: ID do coletor
320+
* responses:
321+
* 200:
322+
* description: Lista de números de coleta do coletor
323+
* content:
324+
* application/json:
325+
* schema:
326+
* type: object
327+
* properties:
328+
* numerosColeta:
329+
* type: array
330+
* items:
331+
* type: object
332+
* properties:
333+
* id:
334+
* type: integer
335+
* numero:
336+
* type: integer
337+
* '404':
338+
* $ref: '#/components/responses/NotFound'
339+
* '500':
340+
* $ref: '#/components/responses/InternalServerError'
341+
*/
342+
app.route('/coletores/:coletorId/numeros-coleta').get([
343+
coletoresController.listaNumerosColetaPorColetor,
344+
]);
305345
};

src/validators/tombo-listagem.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export default {
4040
isInt: true,
4141
optional: true,
4242
},
43+
codigo_barra_foto: {
44+
in: 'query',
45+
isString: true,
46+
optional: true,
47+
},
4348
limite: {
4449
in: 'query',
4550
isInt: true,
@@ -50,4 +55,14 @@ export default {
5055
isInt: true,
5156
optional: true,
5257
},
58+
coletor_id: {
59+
in: 'query',
60+
isInt: true,
61+
optional: true,
62+
},
63+
numero_coleta: {
64+
in: 'query',
65+
isInt: true,
66+
optional: true,
67+
},
5368
};

0 commit comments

Comments
 (0)