<commit_before>var express = require('express');
var monk = require('monk');
var bodyParser = require('body-parser');

var dbAPI = require('api/db.js');
var common = require('api/routing_common.js');

var User = require('api/models/User.js');
var Activity = require('api/models/Activity.js');

var router = express.Router();
router.use(bodyParser.json());

/*
 * Route: GET /activities -- Retrieve list of all activities.
 * Requires no parameters.
 */
router.get('/activities', common.asyncMiddleware(
    async (req, res) => {
        let activitiesList = await dbAPI.activities.find({}, {});
        let summaries = await Promise.all(activitiesList.map((doc) => {
            act = new Activity(doc._id);
            return act.summary();
        }));

        res.status(200).json(summaries);
    }
));

/*
 * Route: POST /activities -- create a new Activity.
 * Requires:
 * - 'title' [string]
 * - 'description' [string]
 * - 'startTime' [Date]
 * - 'endTime' [Date]
 * - 'maxHours' [double]
 */
router.post('/activities', common.asyncMiddleware(
    async (req, res) => {
        if(!await req.user.activityCreator()) return Promise.reject("User is not authorized to create Activities.");

        var exists = (await dbAPI.activities.count( {title: req.body.title}) > 0);
        if(exists) return Promise.reject("Activity already exists.");

        await common.checkRequestParameters(req, 'title', 'description', 'startTime', 'endTime', 'maxHours');

        var newActivity = new Activity();
        newActivity.title(req.body.title);
        newActivity.description(req.body.description);

        await Promise.all([
            newActivity.startTime(new Date(req.body.startTime)),
            newActivity.endTime(new Date(req.body.endTime)),
            newActivity.maxHours(req.body.maxHours),
            newActivity.userHours([]),
        ]);

        await newActivity.save();
        res.status(201).json(await newActivity.summary());
    }
));

/* Middleware for getting the Activity object corresponding to :aid. */
router.use('/activities/:aid', common.asyncMiddleware(
    async (req, res, next) => {
        var activity = new Activity(monk.id(req.params.aid));
        if(!(await activity.exists())) return Promise.reject("Activity does not exist.");

        req.activity = activity;
        next();<commit_msg>Fix sequencing bug when creating new Activities<commit_after>var express = require('express');
var monk = require('monk');
var bodyParser = require('body-parser');

var dbAPI = require('api/db.js');
var common = require('api/routing_common.js');

var User = require('api/models/User.js');
var Activity = require('api/models/Activity.js');

var router = express.Router();
router.use(bodyParser.json());

/*
 * Route: GET /activities -- Retrieve list of all activities.
 * Requires no parameters.
 */
router.get('/activities', common.asyncMiddleware(
    async (req, res) => {
        let activitiesList = await dbAPI.activities.find({}, {});
        let summaries = await Promise.all(activitiesList.map((doc) => {
            act = new Activity(doc._id);
            return act.summary();
        }));

        res.status(200).json(summaries);
    }
));

/*
 * Route: POST /activities -- create a new Activity.
 * Requires:
 * - 'title' [string]
 * - 'description' [string]
 * - 'startTime' [Date]
 * - 'endTime' [Date]
 * - 'maxHours' [double]
 */
router.post('/activities', common.asyncMiddleware(
    async (req, res) => {
        if(!await req.user.activityCreator()) return Promise.reject("User is not authorized to create Activities.");

        var exists = (await dbAPI.activities.count( {title: req.body.title}) > 0);
        if(exists) return Promise.reject("Activity already exists.");

        await common.checkRequestParameters(req, 'title', 'description', 'startTime', 'endTime', 'maxHours');

        var newActivity = new Activity();
        newActivity.title(req.body.title);
        newActivity.description(req.body.description);

        await newActivity.userHours([]); // must be set before maxHours to prevent a bug
        await Promise.all([
            newActivity.startTime(new Date(req.body.startTime)),
            newActivity.endTime(new Date(req.body.endTime)),
            newActivity.maxHours(req.body.maxHours),
        ]);

        await newActivity.save();
        res.status(201).json(await newActivity.summary());
    }
));

/* Middleware for getting the Activity object corresponding to :aid. */
router.use('/activities/:aid', common.asyncMiddleware(
    async (req, res, next) => {
        var activity = new Activity(monk.id(req.params.aid));
        if(!(await activity.exists())) return Promise.reject("Activity does not exist.");

        req.activity = activity;
        next();
{'fraction_non_alphanumeric': 0.15672268907563025, 'fraction_numerical': 0.0029411764705882353, 'mean_word_length': 3.147212543554007, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1315142', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1378, 'n_words': 323}