<commit_before>'use strict';

var _ = require('lodash');
var Promise = require('bluebird');
var uuid = require('../utils/uuid');
var controller = require('../controller.js');
var using = Promise.using;

var Cycles = require('../collections/Cycles');
var cycles = new Cycles();

var Project = require('../models/Project');
var Projects = require('../collections/Projects');
var projects = new Projects();

function prepare (record) {
    return record;
}

module.exports = function (config, resources) {

    // This contains globally accessible components
    // TODO: this feels really dirty, and breaks some of the separation of responsibility
    // that the models provide. I need to think through a cleaner way to configure this.
    Project.actions = resources.actions;

    return {
        query: function (req, res, next) {
            // parse the query
            var query = controller.parseQuery(req.query);

            // restrict to user
            if (typeof req.query.user === 'string') {
                query.userId = req.query.user;
            }

            // restrict to cycle
            if (typeof req.query.cycle === 'string') {
                query.cycleId = req.query.cycle;
            }

            return using(resources.db.disposer(), function (conn) {
                return projects.query(conn, query, req.admin || req.user).then(function (projects) {
                    var includes = controller.parseIncludes(req.query.includes, Project);

                    // nothing to include
                    if (!includes) {
                        return projects;
                    }

                    // include embedded models
                    return Promise.map(projects, function (project) {
                        var result = _.assign({}, project);
                        return Promise.props(includes, function (field) {
                            return project[field].query(conn, {}).then(function (c) {
                                result[field] = _.keyBy(c, 'id');
                            });
                        }).then(function () {
                            return result;
                        });
                    }).then(function (results) {
                        results.total = projects.total;
                        return results;
                    });
                });
            }).then(function (projects) {

                // set pagination headers
                return res.set(controller.makePageHeaders(
                    query.skip,
                    query.limit,
                    projects.total,
                    req.path,
                    req.query,
                )).send(prepare(projects));
            }).catch(next);
        },

        get: function (req, res, next) {
            return using(resources.db.disposer(), function (conn) {
                return projects.get(conn, req.params.project, req.admin || req.user);
            }).then(function (project) {
                res.send(prepare(project));
            }).catch(next);
        },<commit_msg>fix includes<commit_after>'use strict';

var _ = require('lodash');
var Promise = require('bluebird');
var uuid = require('../utils/uuid');
var controller = require('../controller.js');
var using = Promise.using;

var Cycles = require('../collections/Cycles');
var cycles = new Cycles();

var Project = require('../models/Project');
var Projects = require('../collections/Projects');
var projects = new Projects();

function prepare (record) {
    return record;
}

module.exports = function (config, resources) {

    // This contains globally accessible components
    // TODO: this feels really dirty, and breaks some of the separation of responsibility
    // that the models provide. I need to think through a cleaner way to configure this.
    Project.actions = resources.actions;

    return {
        query: function (req, res, next) {
            // parse the query
            var query = controller.parseQuery(req.query);

            // restrict to user
            if (typeof req.query.user === 'string') {
                query.userId = req.query.user;
            }

            // restrict to cycle
            if (typeof req.query.cycle === 'string') {
                query.cycleId = req.query.cycle;
            }

            return using(resources.db.disposer(), function (conn) {
                return projects.query(conn, query, req.admin || req.user).then(function (projects) {
                    var includes = controller.parseIncludes(req.query.includes, Project);

                    // nothing to include
                    if (!includes) {
                        return projects;
                    }

                    // include embedded models
                    return Promise.map(projects, function (project) {
                        var result = _.assign({}, project);
                        return Promise.map(includes, function (field) {
                            return project[field].query(conn, {}).then(function (c) {
                                result[field] = _.keyBy(c, 'id');
                            });
                        }).then(function () {
                            return result;
                        });
                    }).then(function (results) {
                        results.total = projects.total;
                        return results;
                    });
                });
            }).then(function (projects) {

                // set pagination headers
                return res.set(controller.makePageHeaders(
                    query.skip,
                    query.limit,
                    projects.total,
                    req.path,
                    req.query,
                )).send(prepare(projects));
            }).catch(next);
        },

        get: function (req, res, next) {
            return using(resources.db.disposer(), function (conn) {
                return projects.get(conn, req.params.project, req.admin || req.user);
            }).then(function (project) {
                res.send(prepare(project));
            }).catch(next);
        },
{'fraction_non_alphanumeric': 0.11602657591962405, 'fraction_numerical': 0.0, 'mean_word_length': 1.4222919937205651, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1983954', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1383, 'n_words': 383}