mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-28 05:40:05 +00:00
Better cron validation
This commit is contained in:
Generated
+1333
-1475
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -44,7 +44,7 @@
|
|||||||
"swagger-ui-express": "^4.3.0"
|
"swagger-ui-express": "^4.3.0"
|
||||||
},
|
},
|
||||||
"overrides": {
|
"overrides": {
|
||||||
"busboy@<1.6.0": "^1.6.0"
|
"multer": "1.4.5-lts.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nestjs/cli": "^8.2.4",
|
"@nestjs/cli": "^8.2.4",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Body, HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { Body, HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { Timeout } from '@nestjs/schedule';
|
import { Timeout } from '@nestjs/schedule';
|
||||||
import CronParser from 'cron-parser';
|
import CronParser, { CronExpression } from 'cron-parser';
|
||||||
import { InputsClientService } from 'src/clients/inputs/client.service';
|
import { InputsClientService } from 'src/clients/inputs/client.service';
|
||||||
import { IIdRequest, Info } from 'src/clients/inputs/interfaces';
|
import { IIdRequest, Info } from 'src/clients/inputs/interfaces';
|
||||||
|
|
||||||
@@ -13,25 +13,34 @@ export class InputsService {
|
|||||||
adjustInputPayload(payload) {
|
adjustInputPayload(payload) {
|
||||||
return payload?.input_s3 || payload?.input_jdbc;
|
return payload?.input_s3 || payload?.input_jdbc;
|
||||||
}
|
}
|
||||||
getDifferenceInSeconds(date1, date2) {
|
getDifferenceInSeconds(date1: Date, date2: Date) {
|
||||||
const diffInMs = Math.abs(date2 - date1);
|
const diffInMs = Math.abs(date2.getTime() - date1.getTime());
|
||||||
return diffInMs / 1000;
|
return diffInMs / 1000;
|
||||||
}
|
}
|
||||||
validateCron(data) {
|
validateCron(data) {
|
||||||
const { info, cron } = data;
|
const { info, cron } = data;
|
||||||
const { customer_tier } = info;
|
const { customer_tier } = info;
|
||||||
const interval = CronParser.parseExpression(cron);
|
if (!cron) return;
|
||||||
|
let interval: CronExpression;
|
||||||
|
try {
|
||||||
|
interval = CronParser.parseExpression(cron);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Intervalo de tempo inválido',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
const nextDate = interval.next().toDate();
|
const nextDate = interval.next().toDate();
|
||||||
const afterNextDate = interval.next().toDate();
|
const afterNextDate = interval.next().toDate();
|
||||||
const secondsApart = this.getDifferenceInSeconds(nextDate, afterNextDate);
|
const secondsApart = this.getDifferenceInSeconds(nextDate, afterNextDate);
|
||||||
if (customer_tier === 'BASIC' && secondsApart < this.secondsInADay) {
|
if (customer_tier === 'BASIC' && secondsApart < this.secondsInADay) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Não é possível criar um input com intervalo de tempo menor do que um dia.',
|
'Intervalo de tempo não pode ser inferior a um dia.',
|
||||||
HttpStatus.FORBIDDEN,
|
HttpStatus.FORBIDDEN,
|
||||||
);
|
);
|
||||||
} else if (secondsApart < this.secondsInAnHour) {
|
} else if (secondsApart < this.secondsInAnHour) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Não é possível criar um input com intervalo de tempo menor do que uma hora.',
|
'Intervalo de tempo não pode ser inferior a uma hora.',
|
||||||
HttpStatus.FORBIDDEN,
|
HttpStatus.FORBIDDEN,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -92,7 +101,7 @@ export class InputsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, data, info: Info) {
|
async update(id: string, data, info: Info) {
|
||||||
this.validateCron(data);
|
this.validateCron({ ...data, info });
|
||||||
try {
|
try {
|
||||||
const updateInputResponse: any = await this.inputClient.update({
|
const updateInputResponse: any = await this.inputClient.update({
|
||||||
id,
|
id,
|
||||||
|
|||||||
Reference in New Issue
Block a user