var otisMail = require('../server_modules/otis-mailer.js');
var express = require('express');
var router = express.Router();
var db = require('../server_modules/db.js');

router.get('/', function(req, res) {
    console.log(req.query);
    handleWebhook(req.query, res);
});

router.post('/', function(req, res) {
    console.log(req.body);
    handleWebhook(req.body, res);
});

function handleWebhook(params, res) {
    if (!params['messageId']) {
        console.log('This is not a delivery receipt');
    } else {
        //This is a DLR, check that your message has been delivered correctly
        var querySet = {
            from: params.msisdn,
            to: params.to,
            message_id: params.messageId,
            text: params.text,
            trashcan_id: getTrashcanFromText(params.text),
            timestamp: params["message-timestamp"]
        };

        function getTrashcanFromText(text){
            var idRegex = /([^\d]|^)(\d{4})([^\d]|$)/g;
            var match = idRegex.exec(text);
            console.log("regex match " + match[2]);
            return match[2];
        }

        db.query('INSERT INTO trash_project.Report SET ?', [querySet], function(err, result){
            err ? console.log(err) : console.log(result);
        })

        otisMail.hostname('email-smtp.us-east-1.amazonaws.com', true)
            .credentials("AKIAJFE5ZQ36LTPKYUWA", "AjGSUyq7VvMYh6vQZHfzFzp5QTWubMHZgoFUK4tJGsSN")
            .connect()
            .email()
            .text('There is a report of a full trashcan\n Trashcan ID: ' + getTrashcanFromText(params.text))
            .subject('subject line')
            .to('marta.trash.report@aeonsoftworks.com')
            .from('test@otisapp.com')
            .send(function(err, result){
                console.log(err || result);
            });

      /*
        * The following parameters in the delivery receipt should match the ones
        * in your request:
        * Request - from, dlr - to\n
        * Response - message-id, dlr - messageId
        * Request - to, Responese - to, dlr - msisdn
        * Request - client-ref, dlr - client-ref
       */
    }
    res.sendStatus(200);
}

module.exports = router;
@@ -1295,24 +1295,26 @@
 sult);%0D%0A
+%0D%0A
         
 %7D)%0D%0A%0D%0A  
@@ -1309,14 +1309,515 @@
     
-%7D)%0D%0A%0D%0A
+    db.query('SELECT * FROM trash_project.Report r ' + %0D%0A                'LEFT OUTER JOIN trash_project.Traschan t ON ' +%0D%0A                't.id = r.trashcan_id LEFT OUTER JOIN trash_project.Location l ' +%0D%0A                'ON l.id = t.location_id WHERE r.id = ?', %5Bresult.insertId%5D, function(err, result)%7B%0D%0A                    var trashcanId = result.trashcan_id;%0D%0A                    var location = result.shortname;%0D%0A                    var locationDescript = result.location_description;%0D%0A%0D%0A            
     
@@ -1883,16 +1883,28 @@
  true)%0D%0A
+            
         
@@ -2005,16 +2005,28 @@
         
+           
+ 
 .connect
@@ -2025,24 +2025,36 @@
 .connect()%0D%0A
+            
             
@@ -2059,24 +2059,36 @@
   .email()%0D%0A
+            
             
@@ -2148,16 +2148,46 @@
 n ID: ' 
+%0D%0A                            
 + getTra
@@ -2212,16 +2212,98 @@
 ms.text)
+ + '%5Cn' +%0D%0A                            'at ' + location + ' - ' + locationDescript
 )%0D%0A     
@@ -2313,34 +2313,76 @@
     
+ 
-.subject('subject line')%0D%0A
+           .subject('Trashcan full id:' + trashcanId)%0D%0A            
     
@@ -2446,16 +2446,28 @@
         
+       
+     
 .from('t
@@ -2497,16 +2497,28 @@
         
+        
+    
 .send(fu
@@ -2547,32 +2547,44 @@
                 
+        
+    
 console.log(err 
@@ -2608,19 +2608,65 @@
         
-%7D);
+            %7D);%0D%0A                %7D)%0D%0A        %7D)%0D%0A
 %0D%0A%0D%0A    

send email once the report is generated with more details in email