patientRoutes.ts 3.05 KB
Newer Older
1
2
3
import { NextFunction, Request, Response, Router } from "express";
import { writeFile } from "fs";
import multer = require("multer");
4
5
import { extname } from "path";

6
7
8
//import * as bodyParser from "body-parser"; // not necessary, since body-parser is part of NodeJS

export class patientRoutes {
Omar Elkadi's avatar
Omar Elkadi committed
9
  router: Router;
10

Omar Elkadi's avatar
Omar Elkadi committed
11
12
13
14
  constructor() {
    this.router = Router();
    this.routes();
  }
15

Omar Elkadi's avatar
Omar Elkadi committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  routes() {
    // configure a multer Storage path (folder + filename)
    var storage = multer.diskStorage({
      destination: function (req, file, cb) {
        //uploads is the folder name
        cb(null, "uploads");
      },
      filename: function (req, file, cb) {
        var patientStudent = JSON.parse(req.body.patientStudent);
        cb(null, patientStudent.MatrikelNumber + extname(file.originalname));
      }
    });
    //create upload instance (will recive the file and the body from request)
    var upload = multer({ storage: storage });
30

Omar Elkadi's avatar
Omar Elkadi committed
31
32
    // recives the file from request -> the name of the file in the single() must be the same on the client side in FormData
    this.router.post("/ppatient", upload.single("sickNoteEvidence"), this.post);
33

Omar Elkadi's avatar
Omar Elkadi committed
34
35
    this.router.get("/patient", this.get);
  }
36

Omar Elkadi's avatar
Omar Elkadi committed
37
38
39
  get(req: Request, res: Response) {
    let vn = req.query.vorname;
    console.log(vn);
40

Omar Elkadi's avatar
Omar Elkadi committed
41
42
    res.status(200).send("Vorname: " + vn);
  }
43

Omar Elkadi's avatar
Omar Elkadi committed
44
45
46
  post(req: Request, res: Response) {
    //recived the body as a stringified JOSN
    var body = req.body; // response body
47

Omar Elkadi's avatar
Omar Elkadi committed
48
49
    // parse JSON to Objects
    var patientStudent = JSON.parse(body.patientStudent);
50
    var sickNote = JSON.parse(body.SickNote);
Omar Elkadi's avatar
nothing    
Omar Elkadi committed
51
    
Omar Elkadi's avatar
Omar Elkadi committed
52
    const path = "./uploads/reports/" + patientStudent.MatrikelNumber + ".json";
Omar Elkadi's avatar
Omar Elkadi committed
53
    var sickReport = {
54
55
56
      patientStudent,
      sickNote,
      sickNoteFilePath : path,
Omar Elkadi's avatar
Omar Elkadi committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    }
    const file = req.file;
    if (!file || sickReport == null) {
      console.log("the data is invalid");
      return res.status(400).send("Bad Request");
    }
    
 // define mongoDb instance 
 var MongoClient = require('mongodb').MongoClient
 // connect to the server
 MongoClient.connect('mongodb://localhost:27017',async function (err, client) {
   if (err) throw err
 
      // cereat the DB instance
      var db = client.db('admin')
      // create the collection if not exist
      try{
      await db.createCollection( "Students",{} )
Omar Elkadi's avatar
nothing    
Omar Elkadi committed
75
        console.log("new collection Student is created")
Omar Elkadi's avatar
Omar Elkadi committed
76
77
      }
      catch(error){
Omar Elkadi's avatar
nothing    
Omar Elkadi committed
78
        console.log("Collection is already exists, so it would not be created")
Omar Elkadi's avatar
Omar Elkadi committed
79
80
81
82
83
84
85
86
87
88
89
90
      }
      // define a collection and insert a new  record
      try {
        await db.collection('Students').insertOne(sickReport)
        console.log("a record added succsully to the collection Students")
      } catch (error) {
        console.log(error)
      }

    })
    // save the recived Object as Json file with the mat number as a name
    writeFile(path, JSON.stringify(sickReport), function (err) {
Omar Elkadi's avatar
Omar Elkadi committed
91
92
93
      if (err) return console.log(err);
      else return console.log("file generated in path: " + path);
    });
94
    res.status(200).send("the body and the file are recived" + JSON.stringify(body));
Omar Elkadi's avatar
Omar Elkadi committed
95
96
  }
}