-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterceptions.js
147 lines (135 loc) · 4.98 KB
/
interceptions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const express = require('express');
const mongoose = require('mongoose');
const envUtils = require('../helpers/envutils');
const router = express.Router();
//import models
require('../models/HttpInterception');
require('../models/HttpInterceptionDump');
const HttpInterception = mongoose.model('httpinterception');
const HttpInterceptionDump = mongoose.model('httpinterceptiondump');
//receptions index page
router.get('/', (req, res) => {
HttpInterceptionDump.find({})
.sort({ date: 'desc' })
.then(httpInterceptionDumps => {
HttpInterception.find({})
.sort({ date: 'desc' })
.then(httpInterceptions => {
res.render('interceptions/index', {
httpInterceptionDumps: httpInterceptionDumps,
httpInterceptions: httpInterceptions,
indexView: true
});
})
.catch(err => {
res.send("Error occured while retrieving http interceptions");
});
})
.catch(err => {
res.send("Error occured while retrieving http interception dumps");
});
});
//display only selected http reception
router.get('/:id', (req, res) => {
HttpInterceptionDump.find({ httpInterceptionId: req.params.id })
.sort({ date: 'desc' })
.then(httpInterceptionDumps => {
HttpInterception.find({})
.sort({ date: 'desc' })
.then(httpInterceptions => {
HttpInterception.findById({
_id: req.params.id
})
.then(selectedInterception => {
res.render('interceptions/index', {
interceptionUrl: envUtils.getHostName() + "/interception/" + req.params.id,
selectedInterception : selectedInterception,
httpInterceptionDumps: httpInterceptionDumps,
httpInterceptions: httpInterceptions
});
})
.catch(err => {
});
})
.catch(err => {
res.send("Error occured while retrieving http interceptions");
});
})
.catch(err => {
res.send("Error occured while retrieving http interception dumps");
});
});
//receptions add page
router.get('/add/new', (req, res) => {
//res.render('interceptions/add');
const newHttpInterception = new HttpInterception({});
newHttpInterception.save()
.then(createdHttpInterception => {
res.render('interceptions/add', {
url: envUtils.getHostName() + "/interception/" + newHttpInterception._id,
interceptionId: createdHttpInterception._id
});
})
.catch(err => {
console.log(err);
res.send("Unable to save http interception " + err);
})
});
//interceptions add router
router.post('/:id', (req, res) => {
HttpInterception.findById({
_id: req.params.id
})
.then(interception => {
//set necessary parameters for interception
interception.forwardUrl = req.body.forwardUrl,
interception.name = req.body.name;
interception.save()
.then(updatedInterception => {
res.redirect('/interceptions');
})
.catch(err => {
console.log(err);
res.send("Unable to update http interception : " + err);
});
})
.catch(err => {
console.log(err);
res.send("Error!" + err);
})
})
//delete route for a selected http interception dump
router.get('/dump/:id/delete', (req, res) => {
HttpInterceptionDump.deleteOne({ _id: req.params.id })
.then(() => {
req.flash('warning_msg', 'Http Interception Dump Removed Successfully!');
res.redirect('/interceptions')
})
.catch(err => {
console.log(err);
res.send("Unable to delete")
})
});
//delete route for receptions
router.get('/:id/delete', (req, res) => {
//first find the reception to delete
HttpInterceptionDump.deleteMany({
httpInterceptionId: req.params.id
})
.then(() => {
HttpInterception.deleteOne({
_id: req.params.id
})
.then(() => {
req.flash('warning_msg', 'HttpInterception and Associated Interceptiondumps Removed Successfully!');
res.redirect('/interceptions');
})
.catch(err => {
res.send("Error when deleting HttpReception")
});
})
.catch(err => {
res.send("Error when deleting HttpDump")
});
});
module.exports = router;