<commit_before>'use strict';

let Project = require('../../common/models/Project');

/**
 * A helper class for managing projects
 */
class ProjectHelper {
    /**
     * Sets the current project and environment
     *
     * @param {String} projectName
     * @param {String} environmentName
     *
     * @returns {Promise} Promise
     */
    static setCurrent(projectName, environmentName) {
        return new Promise((resolve, reject) => {
            // First check if the project exists
            this.getProject(projectName)
            .then((project) => {
                this.currentProject = project.name;

                // Then check if the environment is enabled
                if(project.settings.environments.names.indexOf(environmentName) > -1) {
                    this.currentEnvironment = environmentName;

                    resolve();

                } else {
                    reject(new Error('Environment "' + environment + '" is does not exist in project "' + project.name + '"'));

                }
            })
            .catch(reject);
        });
    }

    /**
     * Gets a list of all available projects
     *
     * @returns {Promise} Array of Project objects
     */
    static getAllProjects() {
        return new Promise((resolve, reject) => {
            MongoHelper.listDatabases()
            .then(resolve)
            .catch(reject); 
        });
    }

    /**
     * Gets a Project object
     *
     * @param {String} name
     *
     * @returns {Promise} Project object
     */
    static getProject(name) {
        return new Promise((resolve, reject) => {
            MongoHelper.find(name, 'settings', {})
            .then((settings) => {
                /*if(!Array.isArray(settings) || settings.length < 1) {
                    reject(new Error('Project "' + name + '" does not exist'));

                } else {*/
                    UserHelper.getAllUsers(name)
                    .then((users) => {
                        let project = new Project();

                        project.name = name;

                        for(let section of (settings || [])) {
                            project.settings[section.section] = section;
                        }

                        // Sanity check
                        if(!project.settings.environments) {
                            project.settings.environments = {
                                section: 'environments',
                                names: [ 'live' ]
                            };
                        }
                    
                        project.users = users;

                        resolve(project.getObject());
                    });
               /* }*/
            })
            .catch(reject);
        });
    }

    /**
     * Gets a list of all available environments
     *
     * @param {String} project
     *
     * @returns {Promise(Array)} environments
     */
    static getAllEnvironments(project) {
        return new Promise((resolve, reject) => {
            SettingsHelper.getSettings('environments')
            .then((environments) => {
                // There should always be at least one environment available
                if(!environments) {
                    environments = [ 'live' ];
                }

                resolve(environments);
            });
        });
    }
}

module.exports = ProjectHelper;
<commit_msg>dashboard<commit_after>'use strict';

let Project = require('../../common/models/Project');

/**
 * A helper class for managing projects
 */
class ProjectHelper {
    /**
     * Sets the current project and environment
     *
     * @param {String} projectName
     * @param {String} environmentName
     *
     * @returns {Promise} Promise
     */
    static setCurrent(projectName, environmentName) {
        return new Promise((resolve, reject) => {
            // First check if the project exists
            this.getProject(projectName)
            .then((project) => {
                this.currentProject = project.name;

                // Then check if the environment is enabled
                if(project.settings.environments.names.indexOf(environmentName) > -1) {
                    this.currentEnvironment = environmentName;

                    resolve();

                } else {
                    reject(new Error('Environment "' + environment + '" is does not exist in project "' + project.name + '"'));

                }
            })
            .catch(reject);
        });
    }

    /**
     * Gets a list of all available projects
     *
     * @returns {Promise} Array of Project objects
     */
    static getAllProjects() {
        return new Promise((resolve, reject) => {
            MongoHelper.listDatabases()
            .then(resolve)
            .catch(reject); 
        });
    }

    /**
     * Gets a Project object
     *
     * @param {String} name
     *
     * @returns {Promise} Project object
     */
    static getProject(name) {
        return new Promise((resolve, reject) => {
            MongoHelper.find(name, 'settings', {})
            .then((settings) => {
                /*if(!Array.isArray(settings) || settings.length < 1) {
                    reject(new Error('Project "' + name + '" does not exist'));

                } else {*/
                    UserHelper.getAllUsers(name)
                    .then((users) => {
                        let project = new Project();

                        project.name = name;

                        for(let section of (settings || [])) {
                            project.settings[section.section] = section;
                        }

                        // Sanity check
                        if(!project.settings.language) {
                            project.settings.language = {
                                section: 'language',
                                selected: [ 'en' ]
                            };
                        }
                        
                        if(!project.settings.environments) {
                            project.settings.environments = {
                                section: 'environments',
                                names: [ 'live' ]
                            };
                        }
                    
                        project.users = users;

                        resolve(project.getObject());
                    });
               /* }*/
            })
            .catch(reject);
        });
    }

    /**
     * Gets a list of all available environments
     *
     * @param {String} project
     *
     * @returns {Promise(Array)} environments
     */
    static getAllEnvironments(project) {
        return new Promise((resolve, reject) => {
            SettingsHelper.getSettings('environments')
            .then((environments) => {
                // There should always be at least one environment available
                if(!environments) {
                    environments = [ 'live' ];
                }

                resolve(environments);
            });
        });
    }
}

module.exports = ProjectHelper;

{'fraction_non_alphanumeric': 0.11025819958129798, 'fraction_numerical': 0.0005582693649685974, 'mean_word_length': 1.1961385228317498, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '4993787', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1597, 'n_words': 372}