<commit_before>const CONSTANTS = require("../config/constants");
const SECRETS = require("../secret");
const bodyParser = require('body-parser');
const manager = require("../helpers/manager");
const mongo = require('mongodb');

module.exports = function(app, db) {

    /**
     * Check last updated time
     * if earlier than latest server update
     * user will pull again
     * 
     * Method: GET
     * 
     * query {
     *      lastUpdated
     * }
     * 
     * res {
     *      error
     *      [list of locations]
     * }
     */
    app.get(CONSTANTS.ROUTES.SYNC_LOCATIONS, function(req, res, next) {
        db.collection(CONSTANTS.COLLECTION.STATUS).findOne({
            name: "lastUpdated"
        }, function(err, doc) {
            if (err) manager.handleError(err, res);
            else {
                if (req.query.lastUpdated > doc['updatedTime'])
                    res.status(200).json({ hasUpdates: false });
                else {
                    let stops = [];
                    let cursor = db.collection(CONSTANTS.COLLECTION.STOP).find();
                    cursor.each(function(err, item) {
                        if (item != null) {
                            stops.push({
                                name: item['name'],
                                stopId: item['_id']
                            });
                        } else {
                            res.status(200).json({
                                hasUpdates: true,
                                stops: stops
                            })
                        }
                    });
                }
            }
        });
    });

    app.use(bodyParser.urlencoded({ extended: true }));

    /**
     * For pubsafe to update locations
     * 
     * Method: POST
     * 
     * body {
     *      password
     *      stop1
     *      stop2
     *      ...
     *      stopX
     * }
     * 
     * res {
     *      error
     *      inserted
     * }
     */
    app.post(CONSTANTS.ROUTES.UPDATE_LOCATIONS, function(req, res, next) {
        if (req.body.password !== SECRETS.password) {
            res.status(401).json({ error: "Password incorrect" });
            return;
        }

        var stops = [];
        delete req.body['password'];
        for (let key in req.body) {
            stops.push({
                name: req.body[key],
                waiting: 0
            });
        }
        db.collection(CONSTANTS.COLLECTION.STOP).deleteMany({}, function(err, result) {
            if (err) manager.handleError(err, res);
            else {
                db.collection(CONSTANTS.COLLECTION.STOP).insertMany(stops, function(err, result) {
                    if (err) manager.handleError(err, res);
                    else {
                        // update lastUpdated time in database
                        db.collection(CONSTANTS.COLLECTION.STATUS).updateOne({
                            name: "lastUpdated"
                        }, {
                            $set: { "updatedTime": new Date().getTime() }
                        }, {
                            upsert: true
                        }, function(err, result) {
                            if (err) manager.handleError(err, res);
                            else {
                                res.status(200).json({
                                    error: "",
                                    inserted: result.insertedCount
                                });
                            }
                        });
                    }
                });
            }
        });
    });

    /**
     * Make request
     * 
     * Method: POST
     * 
     * body {
     *      email
     *      password
     *      stopId
     * }
     * 
     * res {
     *      error
     * }
     */
    app.post(CONSTANTS.ROUTES.MAKE_REQUEST, function(req, res, next) {
        manager.findUser(db, req.body.email, function(err, user) {
            if (err) manager.handleError(err, res);
            else {
                if (user.password !== req.body.password) {
                    res.status(401).json({ error: "Authentication error" });
                    return;
                }
                db.collection(CONSTANTS.COLLECTION.STOP).updateOne({
                    _id: new mongo.ObjectID(req.body.stopId)
                }, {
                    $inc: { waiting: 1 }
                }, function(err, result) {
                    if (err) { manager.handleError(err, res); return; }
                    res.status(200).json({ error: "" });
                });
            }
        });
    });

    /**
     * Cancel request
     * 
     * Method: DELETE
     * 
     * body {
     *      email
     *      password
     *      stopId
     * }
     * 
     * res {
     *      error
     * }
     */
    app.delete(CONSTANTS.ROUTES.CANCEL_REQUEST, function(req, res, next) {
        manager.findUser(db, req.query.email, function(err, user) {
            if (err) manager.handleError(err, res);
            else {
                if (user.password !== req.query.password) {
                    res.status(401).json({ error: "Authentication error" });
                    return;
                }
                db.collection(CONSTANTS.COLLECTION.STOP).updateOne({
                    _id: new mongo.ObjectID(req.query.stopId)
                }, {
                    $inc: { waiting: -1 }
                }, function(err, result) {
                    if (err) { manager.handleError(err, res); return; }
                    res.status(200).json({ error: "" });
                });
            }
        });
    });
<commit_msg>Changed how requests are saved in database<commit_after>const CONSTANTS = require("../config/constants");
const SECRETS = require("../secret");
const bodyParser = require('body-parser');
const manager = require("../helpers/manager");
const mongo = require('mongodb');

module.exports = function(app, db) {

    /**
     * Check last updated time
     * if earlier than latest server update
     * user will pull again
     * 
     * Method: GET
     * 
     * query {
     *      lastUpdated
     * }
     * 
     * res {
     *      error
     *      [list of locations]
     * }
     */
    app.get(CONSTANTS.ROUTES.SYNC_LOCATIONS, function(req, res, next) {
        db.collection(CONSTANTS.COLLECTION.STATUS).findOne({
            name: "lastUpdated"
        }, function(err, doc) {
            if (err) manager.handleError(err, res);
            else {
                if (req.query.lastUpdated > doc['updatedTime'])
                    res.status(200).json({ hasUpdates: false });
                else {
                    let stops = [];
                    let cursor = db.collection(CONSTANTS.COLLECTION.STOP).find();
                    cursor.each(function(err, item) {
                        if (item != null) {
                            stops.push({
                                name: item['name'],
                                stopId: item['_id']
                            });
                        } else {
                            res.status(200).json({
                                hasUpdates: true,
                                stops: stops
                            })
                        }
                    });
                }
            }
        });
    });

    app.use(bodyParser.urlencoded({ extended: true }));

    /**
     * For pubsafe to update locations
     * 
     * Method: POST
     * 
     * body {
     *      password
     *      stop1
     *      stop2
     *      ...
     *      stopX
     * }
     * 
     * res {
     *      error
     *      inserted
     * }
     */
    app.post(CONSTANTS.ROUTES.UPDATE_LOCATIONS, function(req, res, next) {
        if (req.body.password !== <PASSWORD>) {
            res.status(401).json({ error: "Password incorrect" });
            return;
        }

        var stops = [];
        delete req.body['password'];
        for (let key in req.body) {
            stops.push({
                name: req.body[key],
                waiting: []
            });
        }
        db.collection(CONSTANTS.COLLECTION.STOP).deleteMany({}, function(err, result) {
            if (err) manager.handleError(err, res);
            else {
                db.collection(CONSTANTS.COLLECTION.STOP).insertMany(stops, function(err, result) {
                    if (err) manager.handleError(err, res);
                    else {
                        // update lastUpdated time in database
                        db.collection(CONSTANTS.COLLECTION.STATUS).updateOne({
                            name: "lastUpdated"
                        }, {
                            $set: { "updatedTime": new Date().getTime() }
                        }, {
                            upsert: true
                        }, function(err, result) {
                            if (err) manager.handleError(err, res);
                            else {
                                res.status(200).json({
                                    error: "",
                                    inserted: result.insertedCount
                                });
                            }
                        });
                    }
                });
            }
        });
    });

    /**
     * Make request
     * 
     * Method: POST
     * 
     * body {
     *      email
     *      password
     *      stopId
     * }
     * 
     * res {
     *      error
     * }
     */
    app.post(CONSTANTS.ROUTES.MAKE_REQUEST, function(req, res, next) {
        manager.findUser(db, req.body.email, function(err, user) {
            if (err) manager.handleError(err, res);
            else {
                if (user.password !== <PASSWORD>.body.password) {
                    res.status(401).json({ error: "Authentication error" });
                    return;
                }
                db.collection(CONSTANTS.COLLECTION.STOP).updateOne({
                    _id: new mongo.ObjectID(req.body.stopId)
                }, {
                    $push: { waiting:
                        {
                            email: req.body.email,
                            time: new Date().getTime()
                        }
                    }
                }, function(err, result) {
                    if (err) { manager.handleError(err, res); return; }
                    res.status(200).json({ error: "" });
                });
            }
        });
    });

    /**
     * Cancel request
     * 
     * Method: DELETE
     * 
     * body {
     *      email
     *      password
     *      stopId
     * }
     * 
     * res {
     *      error
     * }
     */
    app.delete(CONSTANTS.ROUTES.CANCEL_REQUEST, function(req, res, next) {
        manager.findUser(db, req.query.email, function(err, user) {
            if (err) manager.handleError(err, res);
            else {
                if (user.password !== <PASSWORD>.query.password) {
                    res.status(401).json({ error: "Authentication error" });
                    return;
                }
                db.collection(CONSTANTS.COLLECTION.STOP).updateOne({
                    _id: new mongo.ObjectID(req.query.stopId)
                }, {
                    $pull: { waiting: 
                        {
                            email: req.query.email 
                        }
                    }
                }, function(err, result) {
                    if (err) { manager.handleError(err, res); return; }
                    res.status(200).json({ error: "" });
                });
            }
        });
    });

{'fraction_non_alphanumeric': 0.11885665529010239, 'fraction_numerical': 0.00469283276450512, 'mean_word_length': 0.9916737468139337, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2441665', 'n_tokens_mistral': 3154, 'n_tokens_neox': 2971, 'n_words': 534}