Skip to content

Commit

Permalink
feature[Relatorio]: Adiciona periodicidade de relatório.
Browse files Browse the repository at this point in the history
Refs: #22
  • Loading branch information
DanielViniciusAlves committed Aug 5, 2024
1 parent abc9612 commit 29505c3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@prisma/client": "^5.17.0",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"cron": "^3.1.7",
"date-fns": "^3.6.0",
"dotenv": "^16.3.1",
"express": "^4.18.2",
Expand Down
7 changes: 1 addition & 6 deletions src/controllers/Report.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,8 @@ export default {
if (!result) {
return response.status(404).json({ error: "Relatório não encontrado" });
}
await updateReport(result);

const resultTest: Impressora | false = await findImpressoraWithReport(numberID);
if (!resultTest) {
return response.status(404).json({ error: "Relatório não encontrado" });
}
const filePath: string | false = await createPdf(generateMonthReport(resultTest));
const filePath: string | false = await createPdf(generateMonthReport(result));
if (!filePath) {
return response.status(500).json({ error: "Erro ao gerar o relatório" });
}
Expand Down
3 changes: 3 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import locationRoutes from './routes/location.route';
import padraoRoutes from './routes/padrao.route';
import impressoraRoutes from './routes/impressora.route';
import reportRoutes from './routes/report.route';
import { reportSchedule } from './usecases/report/schedule.report';

reportSchedule.start();

const corsOptions = {
origin: '*',
Expand Down
24 changes: 24 additions & 0 deletions src/usecases/report/schedule.report.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CronJob } from 'cron';
import { listImpressorasRelatorio } from '../../repository/Impressora.repository'
import { updateReport } from './update.report';
import { Impressora } from '../../types/Impressora.type';

export const reportSchedule = new CronJob(
// Roda todos os dias às 00:00
'0 0 0 * * *',
async function() {
console.log('Iniciando a atualização dos relatórios');
const impressoras: Impressora[] | false = await listImpressorasRelatorio();
if (!impressoras) {
console.error('Erro ao listar impressoras');
return
}

impressoras.forEach(async impressora => {
await updateReport(impressora)
});
},
null,
true,
'America/Sao_Paulo'
);
2 changes: 1 addition & 1 deletion src/usecases/report/update.report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const updateReport = async (impressora: Impressora): Promise<void> => {
const diffInDays = diffInMillis / (1000 * 60 * 60 * 24);

if (diffInDays < 30) {
console.log('Não passaram 30 dias desde a última atualização.');
console.debug('Não passaram 30 dias desde a última atualização.');
return;
}

Expand Down

0 comments on commit 29505c3

Please sign in to comment.