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
31 changes: 31 additions & 0 deletions src/controllers/coletor-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,34 @@ export const desativaColetor = async (req, res, next) => {
next(error);
}
};

export const listaNumerosColetaPorColetor = async (req, res, next) => {
try {
const { coletorId } = req.params;
const { Tombo } = models;
const coletor = await Coletor.findByPk(coletorId);
if (!coletor) {
return res.status(404).json({ mensagem: 'Coletor não encontrado.' });
}
const numerosColeta = await Tombo.findAll({
attributes: ['numero_coleta'],
where: {
coletor_id: coletorId,
rascunho: false,
numero_coleta: { [Op.not]: null },
},
raw: true,
order: [['numero_coleta', 'ASC']],
});
const numerosUnicos = [...new Set(numerosColeta.map(item => item.numero_coleta))].map((numero, index) => ({
id: index,
numero,
}));

res.status(200).json({
numerosColeta: numerosUnicos,
});
} catch (error) {
next(error);
}
};
8 changes: 8 additions & 0 deletions src/controllers/tombos-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,8 @@ export const listagem = (request, response, next) => {
nome_popular: nomePopular,
situacao,
codigo_barra_foto,
coletor_id: coletorId,
numero_coleta: numeroColeta,
} = request.query;

let where = {
Expand All @@ -708,6 +710,12 @@ export const listagem = (request, response, next) => {
if (situacao) {
where.situacao = situacao;
}
if (coletorId) {
where.coletor_id = coletorId;
}
if (numeroColeta) {
where.numero_coleta = numeroColeta;
}

let include = [
{
Expand Down
40 changes: 40 additions & 0 deletions src/routes/coletor.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,44 @@ export default app => {
validacoesMiddleware(desativarColetorEsquema),
coletoresController.desativaColetor,
]);

/**
* @swagger
* /coletores/{coletorId}/numeros-coleta:
* get:
* summary: Lista números de coleta de um coletor
* tags: [Coletores]
* description: Retorna uma lista dos números de coleta cadastrados para um coletor específico.
* parameters:
* - in: path
* name: coletorId
* required: true
* schema:
* type: integer
* description: ID do coletor
* responses:
* 200:
* description: Lista de números de coleta do coletor
* content:
* application/json:
* schema:
* type: object
* properties:
* numerosColeta:
* type: array
* items:
* type: object
* properties:
* id:
* type: integer
* numero:
* type: integer
* '404':
* $ref: '#/components/responses/NotFound'
* '500':
* $ref: '#/components/responses/InternalServerError'
*/
app.route('/coletores/:coletorId/numeros-coleta').get([
coletoresController.listaNumerosColetaPorColetor,
]);
};
15 changes: 15 additions & 0 deletions src/validators/tombo-listagem.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export default {
isInt: true,
optional: true,
},
codigo_barra_foto: {
in: 'query',
isString: true,
optional: true,
},
limite: {
in: 'query',
isInt: true,
Expand All @@ -50,4 +55,14 @@ export default {
isInt: true,
optional: true,
},
coletor_id: {
in: 'query',
isInt: true,
optional: true,
},
numero_coleta: {
in: 'query',
isInt: true,
optional: true,
},
};
Loading