UNPKG

@dxdeveloperexperience/hygie

Version:

Hygie is an easy-to-use Open-Source REST API allowing you to interact with GIT events. This NestJS API expose a set of customizable rules to automate your project's life cycle.

50 lines (45 loc) 1.59 kB
import { Controller, Body, Post, Res } from '@nestjs/common'; import { ScheduleService } from '../scheduler/scheduler.service'; import { CronType } from '../scheduler/cron.interface'; import { DataAccessService } from '../data_access/dataAccess.service'; import { logger } from '../logger/logger.service'; import { HttpResponse } from '../utils/httpResponse'; @Controller('cron') export class CronController { constructor( private readonly scheduleService: ScheduleService, private readonly dataAccessService: DataAccessService, ) { this.loadCronJobs().catch(err => logger.error(err)); } /** * Reload all Cron Jobs when application restart */ private async loadCronJobs(): Promise<void> { const CronStandardClassArray: CronType = await this.dataAccessService.getAllCrons(); if (this.dataAccessService.removeAllCrons()) { this.scheduleService.createCronJobs(CronStandardClassArray).catch(err => logger.error(err, { location: 'CronController', }), ); } else { logger.error('Can not remove old cron jobs', { location: 'CronController', }); } } /** * Should be remove in the near futur * Registration of cron job is now handle by the /webhook endpoint */ @Post('/') async cronJobs(@Body() cronType: CronType, @Res() response): Promise<void> { const httpResponse: HttpResponse = await this.scheduleService .createCronJobs(cronType) .catch(err => { return err; }); response.status(httpResponse.status).send(httpResponse.message); } }