mirror of
https://github.com/dadosfera/maestro.git
synced 2026-09-13 21:24:47 +00:00
validating cron when creating or updating input
This commit is contained in:
@@ -3,6 +3,8 @@ import {
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Header,
|
||||
Headers,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
|
||||
@@ -1,16 +1,43 @@
|
||||
import { Body, HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { Timeout } from '@nestjs/schedule';
|
||||
import CronParser from 'cron-parser';
|
||||
import { InputsClientService } from 'src/clients/inputs/client.service';
|
||||
import { IIdRequest, Info } from 'src/clients/inputs/interfaces';
|
||||
|
||||
@Injectable()
|
||||
export class InputsService {
|
||||
constructor(private inputClient: InputsClientService) {}
|
||||
secondsInADay = 60 * 60 * 24;
|
||||
secondsInAnHour = 60 * 60;
|
||||
|
||||
adjustInputPayload(payload) {
|
||||
return payload?.input_s3 || payload?.input_jdbc;
|
||||
}
|
||||
getDifferenceInSeconds(date1, date2) {
|
||||
const diffInMs = Math.abs(date2 - date1);
|
||||
return diffInMs / 1000;
|
||||
}
|
||||
validateCron(data) {
|
||||
const { info, cron } = data;
|
||||
const { customer_tier } = info;
|
||||
const interval = CronParser.parseExpression(cron);
|
||||
const nextDate = interval.next().toDate();
|
||||
const afterNextDate = interval.next().toDate();
|
||||
const secondsApart = this.getDifferenceInSeconds(nextDate, afterNextDate);
|
||||
if (customer_tier === 'BASIC' && secondsApart < this.secondsInADay) {
|
||||
throw new HttpException(
|
||||
'Não é possível criar um input com intervalo de tempo menor do que um dia.',
|
||||
HttpStatus.FORBIDDEN,
|
||||
);
|
||||
} else if (secondsApart < this.secondsInAnHour) {
|
||||
throw new HttpException(
|
||||
'Não é possível criar um input com intervalo de tempo menor do que uma hora.',
|
||||
HttpStatus.FORBIDDEN,
|
||||
);
|
||||
}
|
||||
}
|
||||
async create(@Body() data) {
|
||||
this.validateCron(data);
|
||||
try {
|
||||
if (
|
||||
data.plugin == 'csv' ||
|
||||
@@ -65,6 +92,7 @@ export class InputsService {
|
||||
}
|
||||
|
||||
async update(id: string, data, info: Info) {
|
||||
this.validateCron(data);
|
||||
try {
|
||||
const updateInputResponse: any = await this.inputClient.update({
|
||||
id,
|
||||
|
||||
Reference in New Issue
Block a user