validating cron when creating or updating input

This commit is contained in:
Gabriel Rosa
2022-05-31 11:34:28 -03:00
parent 4e2e6787a4
commit d3bcbdc43c
6 changed files with 78 additions and 12 deletions
+2
View File
@@ -3,6 +3,8 @@ import {
Controller,
Delete,
Get,
Header,
Headers,
Param,
Patch,
Post,
+28
View File
@@ -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,