<commit_before>var twilio = require('twilio');
var Knex = require('knex');
var express = require('express');
var logfmt = require('logfmt');
var moment = require('moment');
var app = express();

var knex = Knex.initialize({
  client: 'pg',
  connection: process.env.DATABASE_URL
});

// 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://www.atlantamunicipalcourt.org"></script>');
});

app.get('/', function(req, res) {
  res.send('Hello World!');
});

// Get a list of court cases that match either
// a citation number of a given name. To make it
// automatically query against both.
app.get('/cases', function(req, res) {
  if (!req.query || !req.query.searchParameter) return res.send(400);
  
  // Split the name so we can search more strategically
  var param = req.query.searchParameter.toUpperCase();
  var params = param.split(" ");

  // Search for Names
  var query = knex('cases').where('defendant', 'like', '%' + params[0] + '%');
  if (params.length > 1) query = query.andWhere('defendant', 'like', '%' + params[1] + '%')

  // Search for Citations
  var query = query.orWhere('citation', 'like', '%' + params[0] + '%');

  // Limit to ten results
  var query = query.limit(10);

  query.exec(function(err, data) {
    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') {
      var match = req.session.match;
      knex('reminders').insert({
        citation: match.citation,
        sent: false,
        phone: req.body.From,
        created_at: new Date(),
      }).exec(function(err, results) {
        if (err) console.log(err);
      });

      twiml.sms('Sounds good. We\'ll text you a day before your case. Call us at (404) 658-6940 with any other questions.');
      req.session.askedReminder = false;
      res.send(twiml.toString());
    } else if (text === 'NO') {
      twiml.sms('Alright, no problem. See you on your court date. Call us at (404) 658-6940 with any other questions.');
      req.session.askedReminder = false;
      res.send(twiml.toString());
    }
  }<commit_msg>Add readable dates for the JSON api.<commit_after>var twilio = require('twilio');
var Knex = require('knex');
var express = require('express');
var logfmt = require('logfmt');
var moment = require('moment');
var app = express();

var knex = Knex.initialize({
  client: 'pg',
  connection: process.env.DATABASE_URL
});

// 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://www.atlantamunicipalcourt.org"></script>');
});

app.get('/', function(req, res) {
  res.send('Hello World!');
});

// Get a list of court cases that match either
// a citation number of a given name. To make it
// automatically query against both.
app.get('/cases', function(req, res) {
  if (!req.query || !req.query.searchParameter) return res.send(400);
  
  // Split the name so we can search more strategically
  var param = req.query.searchParameter.toUpperCase();
  var params = param.split(" ");

  // Search for Names
  var query = knex('cases').where('defendant', 'like', '%' + params[0] + '%');
  if (params.length > 1) query = query.andWhere('defendant', 'like', '%' + params[1] + '%')

  // Search for Citations
  var query = query.orWhere('citation', 'like', '%' + params[0] + '%');

  // Limit to ten results
  var query = query.limit(10);

  query.exec(function(err, data) {
    // Add readable dates, to avoid browser side date issues
    data.forEach(function(d) {
      d.readableDate = moment(d.date).format('dddd, MMM Do');
    });
    
    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') {
      var match = req.session.match;
      knex('reminders').insert({
        citation: match.citation,
        sent: false,
        phone: req.body.From,
        created_at: new Date(),
      }).exec(function(err, results) {
        if (err) console.log(err);
      });

      twiml.sms('Sounds good. We\'ll text you a day before your case. Call us at (404) 658-6940 with any other questions.');
      req.session.askedReminder = false;
      res.send(twiml.toString());
    } else if (text === 'NO') {
      twiml.sms('Alright, no problem. See you on your court date. Call us at (404) 658-6940 with any other questions.');
      req.session.askedReminder = false;
      res.send(twiml.toString());
    }
  }
{'fraction_non_alphanumeric': 0.17810630059838084, 'fraction_numerical': 0.011263639563533967, 'mean_word_length': 3.5138999205718826, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1123673', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1864, 'n_words': 476}