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
35 changes: 26 additions & 9 deletions src/controllers/rfids-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ const getColetorPrincipal = tombo => (
tombo?.coletor?.nome || tombo?.Coletor?.nome || 'N/A'
);

const normalizarTid = tid => (typeof tid === 'string' ? tid.trim() : '');

const tidValido = tid => {
const tidNormalizado = normalizarTid(tid);
return tidNormalizado.length > 0 && tidNormalizado.toUpperCase() !== 'N/A';
};

const codificarParaEpcHex = (tomboHcf, codigoBarra) => {
const tomboId = parseInt(tomboHcf, 10);
const match = codigoBarra.match(/([a-zA-Z]+)(\d+)/);
Expand Down Expand Up @@ -145,16 +152,23 @@ export const finalizarGravacao = async (request, response, next) => {
break;

case 'CONCLUIDO':
if (tid) {
const tagExistente = await Rfid.findOne({ where: { tid } });
if (!tidValido(tid)) {
return response.status(400).json({
erro: 'TID obrigatório para concluir a gravação RFID.',
});
}

{
const tidNormalizado = normalizarTid(tid);
const tagExistente = await Rfid.findOne({ where: { tid: tidNormalizado } });

if (tagExistente && tagExistente.id !== Number(id)) {
return response.status(409).json({
erro: 'TID já se encontra vinculado a outro tombo no sistema.',
});
}

rfid.tid = tid;
rfid.tid = tidNormalizado;
}
break;

Expand Down Expand Up @@ -204,7 +218,8 @@ export const listagem = async (request, response, next) => {
whereFoto.codigo_barra = codigo_barra;
}

const rfids = await Rfid.findAndCountAll({
const rfids = await Rfid.unscoped().findAndCountAll({
attributes: ['id', 'tombo_foto_id', 'epc', 'tid', 'status', 'created_at', 'updated_at'],
limit: limite,
offset,
where: whereRfid,
Expand Down Expand Up @@ -311,12 +326,13 @@ export const listarPendentesRfid = async (request, response, next) => {
}
};

export const validarEpc = async (request, response, next) => {
const { epc } = request.params;
export const validarTid = async (request, response, next) => {
const { tid } = request.params;
const tidNormalizado = normalizarTid(tid);

try {
const rfid = await Rfid.findOne({
where: { epc: epc },
where: { tid: tidNormalizado },
include: [
{
model: TomboFoto,
Expand Down Expand Up @@ -344,7 +360,7 @@ export const validarEpc = async (request, response, next) => {
if (!rfid) {
return response.status(404).json({
valido: false,
mensagem: 'EPC não encontrado.',
mensagem: 'TID não encontrado.',
});
}

Expand All @@ -354,9 +370,10 @@ export const validarEpc = async (request, response, next) => {

return response.status(200).json({
valido: true,
mensagem: 'EPC validado com sucesso.',
mensagem: 'TID validado com sucesso.',
dados: {
id_rfid: rfidJson.id,
tid: rfidJson.tid,
epc_formatado: rfidJson.epc,
epc: decodificarEpcHex(rfidJson.epc),
status_rfid: rfidJson.status,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/rfids.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ export default app => {

app.route('/rfids/tombos-pendentes').get(RfidsController.listarPendentesRfid);

app.route('/rfids/validar/:epc').get(RfidsController.validarEpc);
app.route('/rfids/validar-tid/:tid').get(RfidsController.validarTid);

};
Loading