diff --git a/backend/src/controllers/exportController.ts b/backend/src/controllers/exportController.ts index 5c38567..1edd14d 100644 --- a/backend/src/controllers/exportController.ts +++ b/backend/src/controllers/exportController.ts @@ -1,6 +1,8 @@ import * as express from 'express'; +import { checkSchema, validationResult } from "express-validator"; import {ExportJob, ExportJobsList} from "../interfaces" import {PROCESSING_TIME_EXPORT} from "../config"; +import {exportJobSchema} from "./schema"; class ExportController { public path = '/export'; @@ -14,7 +16,7 @@ class ExportController { public initializeRoutes() { this.router.get(this.path, this.getAllExportJobs); - this.router.post(this.path, this.addExportJob); + this.router.post(this.path, checkSchema(exportJobSchema), this.addExportJob); } getAllExportJobs = (request: express.Request, response: express.Response) => { @@ -35,24 +37,28 @@ class ExportController { }; addExportJob = (request: express.Request, response: express.Response) => { - const receivedExportJob = request.body; - const fullExportJob:ExportJob = { - bookId: receivedExportJob.bookId, - type: receivedExportJob.type, - state: "pending", - created_at: new Date(), - updated_at: new Date() - }; - this.exportJobs.push(fullExportJob); + const validationResultArray = validationResult(request).array(); + if (validationResultArray.length > 0){ + response.status(400).send(validationResultArray); + }else { + const receivedExportJob = request.body; + const fullExportJob: ExportJob = { + bookId: receivedExportJob.bookId, + type: receivedExportJob.type, + state: "pending", + created_at: new Date(), + updated_at: new Date() + }; + this.exportJobs.push(fullExportJob); - const jobIndexToUpdate:number = this.exportJobs.length-1; - const timeout:number = PROCESSING_TIME_EXPORT[fullExportJob.type]*1000; - setTimeout((jobIndex:number) => { - this.exportJobs[jobIndex].state = "finished"; - this.exportJobs[jobIndex].updated_at = new Date(); - }, timeout, jobIndexToUpdate); - - response.send(); + const jobIndexToUpdate: number = this.exportJobs.length - 1; + const timeout: number = PROCESSING_TIME_EXPORT[fullExportJob.type] * 1000; + setTimeout((jobIndex: number) => { + this.exportJobs[jobIndex].state = "finished"; + this.exportJobs[jobIndex].updated_at = new Date(); + }, timeout, jobIndexToUpdate); + response.send(); + } } } diff --git a/backend/src/controllers/importController.ts b/backend/src/controllers/importController.ts index dcbbfbf..efa0027 100644 --- a/backend/src/controllers/importController.ts +++ b/backend/src/controllers/importController.ts @@ -1,6 +1,9 @@ -import * as express from 'express'; +import * as express from "express"; +import { checkSchema, validationResult } from "express-validator"; import {ImportJobsList, ImportJob} from "../interfaces" import {PROCESSING_TIME_IMPORT_ANY} from "../config"; +import {importJobSchema} from "./schema"; + class ImportController { public path = '/import'; @@ -10,12 +13,12 @@ class ImportController { constructor() { this.initializeRoutes(); - } + }; public initializeRoutes() { this.router.get(this.path, this.getAllImportJobs); - this.router.post(this.path, this.addImportJob); - } + this.router.post(this.path, checkSchema(importJobSchema), this.addImportJob); + }; getAllImportJobs = (request: express.Request, response: express.Response) => { const result: ImportJobsList = { @@ -35,26 +38,32 @@ class ImportController { }; addImportJob = (request: express.Request, response: express.Response) => { - const receivedImportJob = request.body; - const fullImportJob:ImportJob = { - bookId: receivedImportJob.bookId, - type: receivedImportJob.type, - url: receivedImportJob.url, - state: "pending", - created_at: new Date(), - updated_at: new Date() - }; - this.importJobs.push(fullImportJob); - const jobIndexToUpdate:number = this.importJobs.length-1; - const timeout:number = PROCESSING_TIME_IMPORT_ANY*1000; - setTimeout((jobIndex:number) => { - this.importJobs[jobIndex].state = "finished"; - this.importJobs[jobIndex].updated_at = new Date(); - }, timeout, jobIndexToUpdate); + const validationResultArray = validationResult(request).array(); + if (validationResultArray.length > 0){ + response.status(400).send(validationResultArray); + }else{ + const receivedImportJob = request.body; + const fullImportJob:ImportJob = { + bookId: receivedImportJob.bookId, + type: receivedImportJob.type, + url: receivedImportJob.url, + state: "pending", + created_at: new Date(), + updated_at: new Date() + }; + this.importJobs.push(fullImportJob); - response.send(); - } + const jobIndexToUpdate:number = this.importJobs.length-1; + const timeout:number = PROCESSING_TIME_IMPORT_ANY*1000; + setTimeout((jobIndex:number) => { + this.importJobs[jobIndex].state = "finished"; + this.importJobs[jobIndex].updated_at = new Date(); + }, timeout, jobIndexToUpdate); + + response.send(); + } + }; } export default ImportController; diff --git a/backend/src/controllers/schema/index.ts b/backend/src/controllers/schema/index.ts new file mode 100644 index 0000000..c6b52a6 --- /dev/null +++ b/backend/src/controllers/schema/index.ts @@ -0,0 +1,61 @@ +import {ValidationSchema} from "express-validator"; +import {VALID_EXPORT_TYPES_ARRAY, VALID_IMPORT_TYPES_ARRAY} from "../../config"; + +export const importJobSchema:ValidationSchema = { + bookId: { + in: ['body'], + isString: { + errorMessage: "bookId field should be a string" + }, + exists: { + errorMessage: "bookId field is required" + } + }, + type: { + in: ['body'], + isString: { + errorMessage: "type field should be a string" + }, + exists: { + errorMessage: "type field is required" + }, + isIn: { + errorMessage: "type is not valid", + options: [VALID_IMPORT_TYPES_ARRAY] + } + }, + url: { + in: ['body'], + isString: { + errorMessage: "url field should be a string" + }, + exists: { + errorMessage: "url field is required" + } + } +}; + +export const exportJobSchema:ValidationSchema = { + bookId: { + in: ['body'], + isString: { + errorMessage: "bookId field should be a string" + }, + exists: { + errorMessage: "bookId field is required" + } + }, + type: { + in: ['body'], + isString: { + errorMessage: "type field should be a string" + }, + exists: { + errorMessage: "type field is required" + }, + isIn: { + errorMessage: "type is not valid", + options: [VALID_EXPORT_TYPES_ARRAY] + } + } +};