add controllers for import and export jobs

This commit is contained in:
Bilal Catic
2020-02-14 21:06:16 +01:00
parent 7fdda1b32e
commit 5c7e45b60b
3 changed files with 123 additions and 20 deletions

View File

@@ -0,0 +1,60 @@
import * as express from 'express';
import {ImportJobsList, ImportJob} from "../interfaces"
import {PROCESSING_TIME_IMPORT_ANY} from "../config";
class ImportController {
public path = '/import';
public router = express.Router();
private importJobs: ImportJob[] = [];
constructor() {
this.initializeRoutes();
}
public initializeRoutes() {
this.router.get(this.path, this.getAllImportJobs);
this.router.post(this.path, this.addImportJob);
}
getAllImportJobs = (request: express.Request, response: express.Response) => {
const result: ImportJobsList = {
pending: [],
finished: []
};
this.importJobs.forEach((importJob:ImportJob) => {
if (importJob.state === "pending"){
result.pending.push(importJob);
}else if (importJob.state === "finished"){
result.finished.push(importJob);
}
});
response.send(result);
};
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);
response.send();
}
}
export default ImportController;