2
0

crons.controller.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { NextFunction, Request, Response, Router } from "express";
  2. import { dtoValidationMiddleware } from "../middlewares/validation";
  3. import { CronsService, SchedulerRegistry } from "./crons.service";
  4. import { collectionsService } from "../collections";
  5. import { ToggleCronJobByNameDto } from "./dto";
  6. export class CronsController {
  7. private router: Router;
  8. private schedulerRegistry: SchedulerRegistry;
  9. private cronsService: CronsService;
  10. constructor() {
  11. this.schedulerRegistry = new SchedulerRegistry([]);
  12. this.cronsService = new CronsService(
  13. collectionsService,
  14. this.schedulerRegistry
  15. );
  16. this.router = Router();
  17. }
  18. generateRoutes() {
  19. this.router.put(
  20. "/",
  21. dtoValidationMiddleware(ToggleCronJobByNameDto),
  22. this.toggleCronJobByName.bind(this)
  23. );
  24. return this.router;
  25. }
  26. async toggleCronJobByName(req: Request, res: Response, next: NextFunction) {
  27. const cronData = req.body;
  28. try {
  29. const result = await this.cronsService.toggleCronJobByName(cronData);
  30. res.send(result);
  31. } catch (error) {
  32. next(error);
  33. }
  34. }
  35. }