import 'babel-polyfill';
import path from 'path';
import SourceMapSupport from 'source-map-support';
import express from 'express';
import bodyParser from 'body-parser';
import { MongoClient } from 'mongodb';
import Issue from './issue.js';

SourceMapSupport.install();

const app = express();
let db;

app.use(express.static('static'));
app.use(bodyParser.json());

app.get('/api/issues', (req, res) => {
  const filter = {};

  if (req.query.status) {
    filter.status = req.query.status;
  }

  db.collection('issues').find(filter).toArray().then((issues) => {
    const metadata = { total_count: issues.length };

    res.json({
      _metadata: metadata,
      records: issues,
    });
  })
    .catch((error) => {
      console.log(error);
      res.status(500).json({ message: `Internal Server Error: ${error}` });
    });
});

app.post('/api/issues', (req, res) => {
  const newIssue = req.body;
  newIssue.created = new Date();

  if (!newIssue.status) {
    newIssue.status = 'New';
  }

  const err = Issue.validateIssue(newIssue);

  if (err) {
    res.status(422).json({ message: `Invalid request: ${err}` });
    return;
  }

  db.collection('issues').insertOne(Issue.cleanUpIssue(newIssue))
    .then(result => db.collection('issues').find({ _id: result.insertedId }).limit(1).next())
    .then(insertedIssue => res.json(insertedIssue))
    .catch((error) => {
      console.log(error);
      res.status(500).json({ message: `Internal Server Error: ${error}` });
    });
});

app.get('*', (req, res) => res.sendFile(path.resolve('static/index.html')));

MongoClient.connect('mongodb://localhost/issue-tracker')
  .then((connection) => {
    db = connection;
    app.listen(3000, () => {
      console.log('App started on port 3000');
    });
  }).catch((error) => {
    console.log('ERROR', error);
  });

@@ -486,24 +486,299 @@
 tatus;%0A  %7D%0A%0A
+  if (req.query.effort_lte %7C%7C req.query.effort_gte) %7B%0A    filter.effort = %7B%7D;%0A  %7D%0A%0A  if (req.query.effort_lte) %7B%0A    filter.effort.$lte = parseInt(req.query.effort_lte, 10);%0A  %7D%0A%0A  if (req.query.effort_gte) %7B%0A    filter.effort.$gte = parseInt(req.query.effort_gte, 10);%0A  %7D%0A%0A
   db.collect

Add effort filter