<commit_before>const _ = require('lodash');
const config = require('../../config');
const common = require('../../lib/common');
const models = require('../../models');
const mail = require('../mail');

/**
 * Returns setup status
 *
 * @return {Promise<Boolean>}
 */
async function checkIsSetup() {
    return models.User.isSetup();
}

/**
 * Allows an assertion to be made about setup status.
 *
 * @param  {Boolean} status True: setup must be complete. False: setup must not be complete.
 * @return {Function} returns a "task ready" function
 */
function assertSetupCompleted(status) {
    return async function checkPermission(__) {
        const isSetup = await checkIsSetup();

        if (isSetup === status) {
            return __;
        }

        const completed = common.i18n.t('errors.api.authentication.setupAlreadyCompleted');
        const notCompleted = common.i18n.t('errors.api.authentication.setupMustBeCompleted');

        function throwReason(reason) {
            throw new common.errors.NoPermissionError({message: reason});
        }

        if (isSetup) {
            throwReason(completed);
        } else {
            throwReason(notCompleted);
        }
    };
}

async function setupUser(userData) {
    const context = {context: {internal: true}};

    const owner = await models.User.findOne({role: 'Owner', status: 'all'});

    if (!owner) {
        throw new common.errors.GhostError({
            message: common.i18n.t('errors.api.authentication.setupUnableToRun')
        });
    }

    const user = await models.User.setup(userData, _.extend({id: owner.id}, context));

    return {
        user: user,
        userData: userData
    };
}

async function doSettings(data, settingsAPI) {
    const context = {context: {user: data.user.id}};
    const user = data.user;
    const blogTitle = data.userData.blogTitle;

    let userSettings;

    if (!blogTitle || typeof blogTitle !== 'string') {
        return user;
    }

    userSettings = [
        {key: 'title', value: blogTitle.trim()},
        {key: 'description', value: common.i18n.t('common.api.authentication.sampleBlogDescription')}
    ];

    await settingsAPI.edit({settings: userSettings}, context);

    return user;
}

function sendNotification(setupUser, mailAPI) {
    const data = {
        ownerEmail: setupUser.email
    };

    if (config.get('sendWelcomeEmail')) {
        return mail.utils.generateContent({data: data, template: 'welcome'})
            .then((content) => {
                const message = {
                        to: setupUser.email,
                        subject: common.i18n.t('common.api.authentication.mail.yourNewGhostBlog'),
                        html: content.html,
                        text: content.text
                    },
                    payload = {
                        mail: [{
                            message: message,
                            options: {}
                        }]
                    };

                mailAPI.send(payload, {context: {internal: true}})
                    .catch((err) => {
                        err.context = common.i18n.t('errors.api.authentication.unableToSendWelcomeEmail');
                        common.logging.error(err);
                    });
            })
            .return(setupUser);
    }

    return setupUser;
}

module.exports = {
    checkIsSetup: checkIsSetup,
    assertSetupCompleted: assertSetupCompleted,
    setupUser: setupUser,
    doSettings: doSettings,
    sendNotification: sendNotification
};<commit_msg>Refactored sendNotification method to just use email address as parameter<commit_after>const _ = require('lodash');
const config = require('../../config');
const common = require('../../lib/common');
const models = require('../../models');
const mail = require('../mail');

/**
 * Returns setup status
 *
 * @return {Promise<Boolean>}
 */
async function checkIsSetup() {
    return models.User.isSetup();
}

/**
 * Allows an assertion to be made about setup status.
 *
 * @param  {Boolean} status True: setup must be complete. False: setup must not be complete.
 * @return {Function} returns a "task ready" function
 */
function assertSetupCompleted(status) {
    return async function checkPermission(__) {
        const isSetup = await checkIsSetup();

        if (isSetup === status) {
            return __;
        }

        const completed = common.i18n.t('errors.api.authentication.setupAlreadyCompleted');
        const notCompleted = common.i18n.t('errors.api.authentication.setupMustBeCompleted');

        function throwReason(reason) {
            throw new common.errors.NoPermissionError({message: reason});
        }

        if (isSetup) {
            throwReason(completed);
        } else {
            throwReason(notCompleted);
        }
    };
}

async function setupUser(userData) {
    const context = {context: {internal: true}};

    const owner = await models.User.findOne({role: 'Owner', status: 'all'});

    if (!owner) {
        throw new common.errors.GhostError({
            message: common.i18n.t('errors.api.authentication.setupUnableToRun')
        });
    }

    const user = await models.User.setup(userData, _.extend({id: owner.id}, context));

    return {
        user: user,
        userData: userData
    };
}

async function doSettings(data, settingsAPI) {
    const context = {context: {user: data.user.id}};
    const user = data.user;
    const blogTitle = data.userData.blogTitle;

    let userSettings;

    if (!blogTitle || typeof blogTitle !== 'string') {
        return user;
    }

    userSettings = [
        {key: 'title', value: blogTitle.trim()},
        {key: 'description', value: common.i18n.t('common.api.authentication.sampleBlogDescription')}
    ];

    await settingsAPI.edit({settings: userSettings}, context);

    return user;
}

function sendNotification(email, mailAPI) {
    if (config.get('sendWelcomeEmail')) {
        const data = {
            ownerEmail: email
        };

        return mail.utils.generateContent({data: data, template: 'welcome'})
            .then((content) => {
                const message = {
                        to: email,
                        subject: common.i18n.t('common.api.authentication.mail.yourNewGhostBlog'),
                        html: content.html,
                        text: content.text
                    },
                    payload = {
                        mail: [{
                            message: message,
                            options: {}
                        }]
                    };

                mailAPI.send(payload, {context: {internal: true}})
                    .catch((err) => {
                        err.context = common.i18n.t('errors.api.authentication.unableToSendWelcomeEmail');
                        common.logging.error(err);
                    });
            });
    }
}

module.exports = {
    checkIsSetup: checkIsSetup,
    assertSetupCompleted: assertSetupCompleted,
    setupUser: setupUser,
    doSettings: doSettings,
    sendNotification: sendNotification
};
{'fraction_non_alphanumeric': 0.12475359053787666, 'fraction_numerical': 0.0033793297662630243, 'mean_word_length': 2.0445777968281185, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '7371595', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1845, 'n_words': 426}