import {HttpService, Injectable, Logger} from '@nestjs/common'; import {Cron, CronExpression} from '@nestjs/schedule'; import {AuthService} from '../auth/auth.service'; import {Store} from "../store"; @Injectable() export class AuthCron { private logger: Logger = new Logger('AuthCron'); constructor( private readonly http: HttpService, private readonly authService: AuthService, ) { } @Cron(CronExpression.EVERY_MINUTE) async handleCron() { const tokenLife = await this.authService.tokenLife(); this.logger.debug(`Check token life ${String(tokenLife)}%`); if (tokenLife < 50) { const isActive = await this.authService.isActive(); this.logger.debug(`Is Active Token ${isActive}`); if (isActive) { await this.authService.refresh(); } else { await this.authService.login(); } } } }