Skip to content

Commit

Permalink
feat: cronjobs
Browse files Browse the repository at this point in the history
  • Loading branch information
yxuo committed Mar 5, 2024
1 parent 880e64e commit 906c604
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 38 deletions.
19 changes: 0 additions & 19 deletions src/cnab/cnab-caixa.service.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/cnab/cnab.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Module } from '@nestjs/common';
import { CnabService } from './cnab.service';
import { CnabService } from './service/cnab.service';

@Module({
providers: [CnabService],
})
export class CnabModule {}
export class CnabModule { }
4 changes: 0 additions & 4 deletions src/cnab/cnab.service.ts

This file was deleted.

File renamed without changes.
19 changes: 19 additions & 0 deletions src/cnab/service/cnab.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable } from '@nestjs/common';
import { HeaderArquivoService } from './header-arquivo.service';
import { TransacaoService } from './transacao.service';

@Injectable()
export class CnabService {
constructor(
private readonly headerArquivoService: HeaderArquivoService,
private readonly transacaoService: TransacaoService,
) { }

public async updateTransacaoFromJae() {
await this.transacaoService.updateTransacaoFromJae()
}

public async sendNewCNABs() {
await this.headerArquivoService.saveRemessa()
}
}
2 changes: 1 addition & 1 deletion src/cnab/service/transacao.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class TransacaoService {
private pagadorService: PagadorService,
private bigqueryOrdemPagamentoService: BigqueryOrdemPagamentoService,
) { }
public async insereTransacoes() {
public async updateTransacaoFromJae() {
//Atualiza todos os favorecidos
await this.clienteFavorecidoService.updateAllFromUsers();

Expand Down
54 changes: 42 additions & 12 deletions src/cron-jobs/cron-jobs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HttpStatus, Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { SchedulerRegistry } from '@nestjs/schedule';
import { CronJob, CronJobParameters } from 'cron';
import { CnabService } from 'src/cnab/service/cnab.service';
import { InviteStatus } from 'src/mail-history-statuses/entities/mail-history-status.entity';
import { InviteStatusEnum } from 'src/mail-history-statuses/mail-history-status.enum';
import { MailHistory } from 'src/mail-history/entities/mail-history.entity';
Expand All @@ -27,6 +28,8 @@ export enum CrobJobsEnum {
sendStatusReport = 'sendStatusReport',
pollDb = 'pollDb',
bulkResendInvites = 'bulkResendInvites',
updateTransacaoFromJae = 'updateTransacaoFromJae',
sendNewCNABs = 'sendNewCNABs',
}

interface ICronJob {
Expand All @@ -53,7 +56,8 @@ export class CronJobsService implements OnModuleInit {
private mailService: MailService,
private mailHistoryService: MailHistoryService,
private usersService: UsersService,
) {}
private cnabService: CnabService,
) { }

onModuleInit() {
const THIS_CLASS_WITH_METHOD = `${CronJobsService.name}.${this.onModuleInit.name}`;
Expand Down Expand Up @@ -107,6 +111,24 @@ export class CronJobsService implements OnModuleInit {
},
},
},
{
name: CrobJobsEnum.updateTransacaoFromJae,
cronJobParameters: {
cronTime: '45 14 * * *', // 14:45 GMT = 11:45BRT (GMT-3)
onTick: async () => {
await this.bulkResendInvites();
},
},
},
{
name: CrobJobsEnum.sendNewCNABs,
cronJobParameters: {
cronTime: '45 14 * * *', // 14:45 GMT = 11:45BRT (GMT-3)
onTick: async () => {
await this.sendNewCNABs();
},
},
},
);

for (const jobConfig of this.jobsConfig) {
Expand Down Expand Up @@ -149,7 +171,7 @@ export class CronJobsService implements OnModuleInit {
this.logger.log(
formatLog(
`Tarefa cancelada pois 'setting.${appSettings.any__activate_auto_send_invite.name}' = 'false'.` +
` Para ativar, altere na tabela 'setting'`,
` Para ativar, altere na tabela 'setting'`,
THIS_METHOD,
),
);
Expand All @@ -165,8 +187,8 @@ export class CronJobsService implements OnModuleInit {
this.logger.log(
formatLog(
`Iniciando tarefa - a enviar: ${unsent.length},` +
` enviado: ${sentToday.length}/${dailyQuota()},` +
` falta enviar: ${remainingQuota}`,
` enviado: ${sentToday.length}/${dailyQuota()},` +
` falta enviar: ${remainingQuota}`,
THIS_METHOD,
),
);
Expand Down Expand Up @@ -326,7 +348,7 @@ export class CronJobsService implements OnModuleInit {
this.logger.log(
formatLog(
`Tarefa cancelada pois 'setting.${appSettings.any__mail_report_enabled.name}' = 'false'.` +
` Para ativar, altere na tabela 'setting'`,
` Para ativar, altere na tabela 'setting'`,
THIS_METHOD,
),
);
Expand All @@ -345,7 +367,7 @@ export class CronJobsService implements OnModuleInit {
this.logger.log(
formatLog(
`Tarefa cancelada pois 'setting.${appSettings.any__mail_report_enabled.name}' = 'false'.` +
` Para ativar, altere na tabela 'setting'`,
` Para ativar, altere na tabela 'setting'`,
THIS_METHOD,
),
);
Expand All @@ -361,7 +383,7 @@ export class CronJobsService implements OnModuleInit {
this.logger.error(
formatLog(
`Tarefa cancelada pois a configuração 'mail.statusReportRecipients'` +
` não foi encontrada (retornou: ${mailRecipients}).`,
` não foi encontrada (retornou: ${mailRecipients}).`,
'sendStatusReport()',
),
);
Expand All @@ -372,7 +394,7 @@ export class CronJobsService implements OnModuleInit {
this.logger.error(
formatLog(
`Tarefa cancelada pois a configuração 'mail.statusReportRecipients'` +
` não contém uma lista de emails válidos. Retornou: ${mailRecipients}.`,
` não contém uma lista de emails válidos. Retornou: ${mailRecipients}.`,
THIS_METHOD,
),
);
Expand Down Expand Up @@ -446,7 +468,7 @@ export class CronJobsService implements OnModuleInit {
this.logger.log(
formatLog(
`Tarefa cancelada pois setting.${appSettings.any__poll_db_enabled.name}' = 'false'` +
` Para ativar, altere na tabela 'setting'`,
` Para ativar, altere na tabela 'setting'`,
THIS_METHOD,
),
);
Expand Down Expand Up @@ -503,8 +525,8 @@ export class CronJobsService implements OnModuleInit {
this.logger.log(
formatLog(
`Alteração encontrada em` +
` setting.'${args.setting.name}': ` +
`${job?.cronJobParameters.cronTime} --> ${setting}.`,
` setting.'${args.setting.name}': ` +
`${job?.cronJobParameters.cronTime} --> ${setting}.`,
thisMethod,
),
);
Expand Down Expand Up @@ -583,7 +605,7 @@ export class CronJobsService implements OnModuleInit {
formatLog(
String(
'Enviando emails específicos para ' +
`${notRegisteredUsers.length} usuários não totalmente registrados`,
`${notRegisteredUsers.length} usuários não totalmente registrados`,
),
THIS_METHOD,
),
Expand Down Expand Up @@ -638,4 +660,12 @@ export class CronJobsService implements OnModuleInit {
);
}
}

async updateTransacaoFromJae() {
await this.cnabService.updateTransacaoFromJae();
}

async sendNewCNABs() {
await this.cnabService.sendNewCNABs();
}
}

0 comments on commit 906c604

Please sign in to comment.