'use strict';

const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');


const restService = express();
restService.use(bodyParser.json());


const departments = JSON.parse(fs.readFileSync('data/departments.json', 'utf8'));
const doctors = JSON.parse(fs.readFileSync('data/doctors.json', 'utf8'));


restService.post('/hook', function (req, res) {

    console.log('hook request');

    try {
        var speech = 'empty speech';

        if (req.body) {
            var requestBody = req.body;

            if (requestBody.result) {
                speech = '';
                
                
            /*    switch(requestBody.result.parameters.department){                        
                        
                    case 'Cardiology':
                            speech = 'Cardiology Doctors: Dipanik, Moumita';
                            break;
                    case 'Paediatrics':
                            speech = 'Paediatrics Doctors: Avinash, Anindita';
                            break;
                    case 'Neurology':
                            speech = 'Neurology Doctors: Taiseef, Sutrishna';
                            break;
                            
                    default:
                            speech = 'General Doctors: Biswajit';
                }*/
                
                
             
                
                 var doctorForDept = doctors.filter(function(doc){

                    return (doc.department === requestBody.result.parameters.department)

                });   
                            
                                  
                
                
                
                doctorForDept.forEach(function(doc){
                    speech += doc.title + '<br />';
                    
                });

                                
                
            }
        }

        console.log('result: ', speech);

        return res.json({
            speech: speech,
            displayText: speech,
            source: 'bookjent'
        });
    } catch (err) {
        console.error("Can't process request", err);

        return res.status(400).json({
            status: {
                code: 400,
                errorType: err.message
            }
        });
    }
});

restService.listen((process.env.PORT || 5000), function () {
    console.log("Server listening");
});
@@ -572,16 +572,56 @@
 y.result
+.action === 'search.doctorsByDepartment'
 ) %7B%0A    
@@ -644,17 +644,16 @@
 ch = '';
-%0A
         
@@ -661,32 +661,16 @@
        %0A
-                
 %0A       
@@ -678,51 +678,38 @@
     
-/*
     
-switch(requestBody.result.parameters.
+var requestedDepartment = 
 depa
@@ -718,479 +718,117 @@
 ment
-)%7B                        %0A                        %0A                    case 'Cardiology':%0A                            speech = 'Cardiology Doctors: Dipanik, Moumita';%0A                            break;%0A                    case 'Paediatrics':%0A                            speech = 'Paediatrics Doctors: Avinash, Anindita';%0A                            break;%0A                    case 'Neurology':%0A                            speech = 'Neurology Doctors: Taiseef, Sutrishna'
+s.filter(function(dept)%7B%0A                    return (dept.value === requestBody.result.parameters.department)
 ;%0A  
@@ -845,25 +845,10 @@
     
-            break
+%7D)
 ;%0A  
@@ -853,32 +853,37 @@
                 
+%0A    
             %0A   
@@ -899,173 +899,76 @@
     
-    default:%0A                            speech = 'General Doctors: Biswajit';%0A                %7D*/%0A                %0A                %0A             %0A                %0A 
+requestedDepartment = requestedDepartment %7C%7C 'GEN'%0A                %0A
     
@@ -1029,17 +1029,16 @@
 n(doc)%7B%0A
-%0A
         
@@ -1117,17 +1117,17 @@
 artment)
-%0A
+;
 %0A       
@@ -1150,32 +1150,33 @@
                 
+%0A
             %0A   
@@ -1175,43 +1175,135 @@
     
-%0A                                  
+    var doctorNames = %5B%5D;%0A                doctorForDept.forEach(function(doc)%7B%0A                    doctorNames.push(doc.title);
 %0A   
@@ -1307,32 +1307,36 @@
                 
+    
 %0A               
@@ -1328,36 +1328,35 @@
                 
-%0A   
+%7D);
              %0A  
@@ -1373,44 +1373,8 @@
     
-doctorForDept.forEach(function(doc)%7B
 %0A   
@@ -1378,32 +1378,45 @@
                 
+%0A            
     speech += do
@@ -1414,71 +1414,137 @@
 ech 
-+
 = 
-doc.title + '%3Cbr /%3E';%0A                    %0A                %7D
+'Available doctors from ' + requestedDepartment%5B0%5D.title + ' department are: '; %0A                speech += doctorNames.join(','
 );%0A%0A

return doctor list by departments