add validation schema and use it to validate requests
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import * as express from 'express';
|
import * as express from 'express';
|
||||||
|
import { checkSchema, validationResult } from "express-validator";
|
||||||
import {ExportJob, ExportJobsList} from "../interfaces"
|
import {ExportJob, ExportJobsList} from "../interfaces"
|
||||||
import {PROCESSING_TIME_EXPORT} from "../config";
|
import {PROCESSING_TIME_EXPORT} from "../config";
|
||||||
|
import {exportJobSchema} from "./schema";
|
||||||
|
|
||||||
class ExportController {
|
class ExportController {
|
||||||
public path = '/export';
|
public path = '/export';
|
||||||
@@ -14,7 +16,7 @@ class ExportController {
|
|||||||
|
|
||||||
public initializeRoutes() {
|
public initializeRoutes() {
|
||||||
this.router.get(this.path, this.getAllExportJobs);
|
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) => {
|
getAllExportJobs = (request: express.Request, response: express.Response) => {
|
||||||
@@ -35,24 +37,28 @@ class ExportController {
|
|||||||
};
|
};
|
||||||
|
|
||||||
addExportJob = (request: express.Request, response: express.Response) => {
|
addExportJob = (request: express.Request, response: express.Response) => {
|
||||||
const receivedExportJob = request.body;
|
const validationResultArray = validationResult(request).array();
|
||||||
const fullExportJob:ExportJob = {
|
if (validationResultArray.length > 0){
|
||||||
bookId: receivedExportJob.bookId,
|
response.status(400).send(validationResultArray);
|
||||||
type: receivedExportJob.type,
|
}else {
|
||||||
state: "pending",
|
const receivedExportJob = request.body;
|
||||||
created_at: new Date(),
|
const fullExportJob: ExportJob = {
|
||||||
updated_at: new Date()
|
bookId: receivedExportJob.bookId,
|
||||||
};
|
type: receivedExportJob.type,
|
||||||
this.exportJobs.push(fullExportJob);
|
state: "pending",
|
||||||
|
created_at: new Date(),
|
||||||
|
updated_at: new Date()
|
||||||
|
};
|
||||||
|
this.exportJobs.push(fullExportJob);
|
||||||
|
|
||||||
const jobIndexToUpdate:number = this.exportJobs.length-1;
|
const jobIndexToUpdate: number = this.exportJobs.length - 1;
|
||||||
const timeout:number = PROCESSING_TIME_EXPORT[fullExportJob.type]*1000;
|
const timeout: number = PROCESSING_TIME_EXPORT[fullExportJob.type] * 1000;
|
||||||
setTimeout((jobIndex:number) => {
|
setTimeout((jobIndex: number) => {
|
||||||
this.exportJobs[jobIndex].state = "finished";
|
this.exportJobs[jobIndex].state = "finished";
|
||||||
this.exportJobs[jobIndex].updated_at = new Date();
|
this.exportJobs[jobIndex].updated_at = new Date();
|
||||||
}, timeout, jobIndexToUpdate);
|
}, timeout, jobIndexToUpdate);
|
||||||
|
response.send();
|
||||||
response.send();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 {ImportJobsList, ImportJob} from "../interfaces"
|
||||||
import {PROCESSING_TIME_IMPORT_ANY} from "../config";
|
import {PROCESSING_TIME_IMPORT_ANY} from "../config";
|
||||||
|
import {importJobSchema} from "./schema";
|
||||||
|
|
||||||
|
|
||||||
class ImportController {
|
class ImportController {
|
||||||
public path = '/import';
|
public path = '/import';
|
||||||
@@ -10,12 +13,12 @@ class ImportController {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.initializeRoutes();
|
this.initializeRoutes();
|
||||||
}
|
};
|
||||||
|
|
||||||
public initializeRoutes() {
|
public initializeRoutes() {
|
||||||
this.router.get(this.path, this.getAllImportJobs);
|
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) => {
|
getAllImportJobs = (request: express.Request, response: express.Response) => {
|
||||||
const result: ImportJobsList = {
|
const result: ImportJobsList = {
|
||||||
@@ -35,26 +38,32 @@ class ImportController {
|
|||||||
};
|
};
|
||||||
|
|
||||||
addImportJob = (request: express.Request, response: express.Response) => {
|
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 validationResultArray = validationResult(request).array();
|
||||||
const timeout:number = PROCESSING_TIME_IMPORT_ANY*1000;
|
if (validationResultArray.length > 0){
|
||||||
setTimeout((jobIndex:number) => {
|
response.status(400).send(validationResultArray);
|
||||||
this.importJobs[jobIndex].state = "finished";
|
}else{
|
||||||
this.importJobs[jobIndex].updated_at = new Date();
|
const receivedImportJob = request.body;
|
||||||
}, timeout, jobIndexToUpdate);
|
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;
|
export default ImportController;
|
||||||
|
|||||||
61
backend/src/controllers/schema/index.ts
Normal file
61
backend/src/controllers/schema/index.ts
Normal file
@@ -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]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user