-
Notifications
You must be signed in to change notification settings - Fork 55
/
web.js
147 lines (124 loc) · 5.16 KB
/
web.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
var twilio = require('twilio');
var express = require('express');
var logfmt = require('logfmt');
var moment = require('moment');
var db = require('./db');
var app = express();
// Express Middleware
app.use(logfmt.requestLogger());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.cookieParser(process.env.COOKIE_SECRET));
app.use(express.cookieSession());
// Allows CORS
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
// Enable CORS support for IE8.
app.get('/proxy.html', function(req, res) {
res.send('<!DOCTYPE HTML>\n' + '<script src="http://jpillora.com/xdomain/dist/0.6/xdomain.min.js" master="http://court.atlantaga.gov"></script>');
});
app.get('/', function(req, res) {
res.send('Hello, I am Courtbot! I have a heart of justice and a knowledge of court cases.');
});
// Fuzzy search that returns cases with a partial name match or
// an exact citation match
app.get('/cases', function(req, res) {
if (!req.query || !req.query.q) return res.send(400);
db.fuzzySearch(req.query.q, function(err, data) {
console.log("Fuzzy Search Error = " + err);
// Add readable dates, to avoid browser side date issues
data.forEach(function(d) {
console.log("Fuzzy Search Record = " + d);
d.readableDate = moment(d.date).format('dddd, MMM Do');
d.payable = canPayOnline(d);
});
res.send(data);
});
});
// Respond to text messages that come in from Twilio
app.post('/sms', function(req, res) {
var twiml = new twilio.TwimlResponse();
var text = req.body.Body.toUpperCase();
if (req.session.askedReminder) {
if (text === 'YES' || text === 'YEA' || text === 'YUP' || text === 'Y') {
var match = req.session.match;
db.addReminder({
caseId: match.id,
phone: req.body.From,
originalCase: JSON.stringify(match)
}, function(err, data) {});
twiml.sms('Sounds good. We\'ll text you a day before your case. Call us at (404) 954-7914 with any other questions.');
req.session.askedReminder = false;
res.send(twiml.toString());
} else if (text === 'NO' || text ==='N') {
twiml.sms('Alright, no problem. See you on your court date. Call us at (404) 954-7914 with any other questions.');
req.session.askedReminder = false;
res.send(twiml.toString());
}
}
if (req.session.askedQueued) {
if (text === 'YES' || text === 'YEA' || text === 'YUP' || text === 'Y') {
db.addQueued({
citationId: req.session.citationId,
phone: req.body.From,
}, function(err, data) {});
twiml.sms('Sounds good. We\'ll text you in the next 14 days. Call us at (404) 954-7914 with any other questions.');
req.session.askedQueued = false;
res.send(twiml.toString());
} else if (text === 'NO' || text ==='N') {
twiml.sms('No problem. Call us at (404) 954-7914 with any other questions.');
req.session.askedQueued = false;
res.send(twiml.toString());
}
}
db.findCitation(text, function(err, results) {
// If we can't find the case, or find more than one case with the citation
// number, give an error and recommend they call in.
if (!results || results.length === 0 || results.length > 1) {
var correctLengthCitation = 6 <= text.length && text.length <= 9;
if (correctLengthCitation) {
twiml.sms('Couldn\'t find your case. It takes 14 days for new citations to appear in the sytem. Would you like a text when we find your information? (Reply YES or NO)');
req.session.askedQueued = true;
req.session.citationId = text;
} else {
twiml.sms('Sorry, we couldn\'t find that court case. Please call us at (404) 954-7914.');
}
} else {
var match = results[0];
var name = cleanupName(match.defendant);
var date = moment(match.date).format('dddd, MMM Do');
if (canPayOnline(match)){
twiml.sms('You can pay now and skip court. Just call (404) 658-6940 or visit court.atlantaga.gov. \n\nOtherwise, your court date is ' + date + ' at ' + match.time +', in courtroom ' + match.room + '.');
} else {
twiml.sms('Found a court case for ' + name + ' on ' + date + ' at ' + match.time +', in courtroom ' + match.room +'. Would you like a reminder the day before? (reply YES or NO)');
req.session.match = match;
req.session.askedReminder = true;
}
}
res.send(twiml.toString());
});
});
// You can pay online if ALL your individual citations can be paid online
var canPayOnline = function(courtCase) {
var eligible = true;
courtCase.citations.forEach(function(citation) {
if (citation.payable !== '1') eligible = false;
});
return eligible;
};
var cleanupName = function(name) {
// Switch LAST, FIRST to FIRST LAST
var bits = name.split(',');
name = bits[1] + ' ' + bits[0];
name = name.trim();
// Change FIRST LAST to First Last
name = name.replace(/\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
return name;
};
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
console.log("Listening on " + port);
});