js
const loadInfractions = () => {
if (fs.existsSync(__dirname + '/infracoes.json')) {
return JSON.parse(fs.readFileSync(__dirname + '/infracoes.json', 'utf8'));
}
return [];
};
const saveInfractions = (infractions) => {
fs.writeFileSync(__dirname + '/infracoes.json', JSON.stringify(infractions, null, 2), 'utf8');
};
const getInfracoesMed = async () => {
const token = await fetchToken();
const headers = {
Authorization:
Bearer ${token},
};
const URL =
https://pix.api.efipay.com.br/v2/gn/infracoes?inicio=2025-03-01T00:00:00Z&fim=${getCurrentDateRFC3339()}&paginacao.itensPorPagina=100;
try {
const response = await axios.get(URL, { headers, httpsAgent });
return response.data;
} catch (error) {
console.log('Error fetching PIX:', error);
throw error;
}
};
let processedInfractions = loadInfractions();
const checkNewInfractions = async () => {
try {
const response = await getcontestacoesmedEFI();
const newInfractions = response.infracoes.filter(
(infraction) => !processedInfractions.some((old) => old.idInfracao === infraction.idInfracao)
);
console.log("Quantidade de MEDs encontrado: "+newInfractions.length)
if (newInfractions.length > 0) {
newInfractions.forEach(async(infraction) => {
console.log("Nova Infração Detectada:");
console.log(JSON.stringify(infraction, null, 2))
//req webhook aqui
});
processedInfractions.push(...newInfractions);
saveInfractions(processedInfractions);
}
} catch (error) {
console.error('Erro ao buscar infrações:', error);
}
};