<commit_before>'use strict';
const _ = require('lodash');
const BaseApp = require('./app');

/**
 * User Class
 */
class User {

    /**
     * Constructor
     * @param {*} platform
     * @param {*=} config
     */
    constructor(platform, config) {
        this.platform = platform;
        this.config = {};
        this.config.userMetaData = config.userMetaData;
        this.config.userContext = config.userContext;
        this.metaData = {};
        this.context = {};
        this.data = {};
        this.isNew = false;
    }

    /**
     * Loads user data from database
     * @return {Promise<any>}
     */
    loadDataFromDb() {
        return new Promise((resolve, reject) => {
            this.platform.jovo.db().loadObject((error, data) => {
                if (error && (error.code === 'ERR_MAIN_KEY_NOT_FOUND' ||
                        error.code === 'ERR_DATA_KEY_NOT_FOUND')) {
                    this.setIsNewUser(true);
                    this.setData(this.createUserData());
                } else if (!data[this.platform.jovo.app.config.userDataCol]) {
                    this.setData(this.createUserData());
                } else {
                    this.setData(data[this.platform.jovo.app.config.userDataCol]);
                }
                resolve();
            });
        });
    }

    /**
     * Saves user data to database
     * @return {Promise<any>}
     */
    saveDataToDb() {
        return new Promise((resolve, reject) => {
            this.updateMetaData();
            this.updateContext();
            this.platform.jovo.db().saveFullObject(
                this.platform.jovo.app.config.userDataCol,
                this.getData(), function(error, data) {
                    if (error) {
                        reject(error);
                        return;
                    }
                    resolve();
                });
        });
    }

    /**
     * Sets isNew flag for user
     * @param {boolean} isNew
     */
    setIsNewUser(isNew) {
        this.isNew = isNew;
    }

    /**
     * Returns isNew flag
     * @return {boolean|*}
     */
    isNewUser() {
        return this.isNew;
    }

    /**
     * Sets data
     * @param {*} data
     */
    setData(data) {
        if (!data) {
            data = {};
        }
        this.data = data.data;
        this.metaData = data.metaData;
        this.context = data.context || {};

        if (_.get(this, 'config.userContext.prev.size') > 0) {<commit_msg>:bug: Fixes: setData() overwrites prev array everytime<commit_after>'use strict';
const _ = require('lodash');
const BaseApp = require('./app');

/**
 * User Class
 */
class User {

    /**
     * Constructor
     * @param {*} platform
     * @param {*=} config
     */
    constructor(platform, config) {
        this.platform = platform;
        this.config = {};
        this.config.userMetaData = config.userMetaData;
        this.config.userContext = config.userContext;
        this.metaData = {};
        this.context = {};
        this.data = {};
        this.isNew = false;
    }

    /**
     * Loads user data from database
     * @return {Promise<any>}
     */
    loadDataFromDb() {
        return new Promise((resolve, reject) => {
            this.platform.jovo.db().loadObject((error, data) => {
                if (error && (error.code === 'ERR_MAIN_KEY_NOT_FOUND' ||
                        error.code === 'ERR_DATA_KEY_NOT_FOUND')) {
                    this.setIsNewUser(true);
                    this.setData(this.createUserData());
                } else if (!data[this.platform.jovo.app.config.userDataCol]) {
                    this.setData(this.createUserData());
                } else {
                    this.setData(data[this.platform.jovo.app.config.userDataCol]);
                }
                resolve();
            });
        });
    }

    /**
     * Saves user data to database
     * @return {Promise<any>}
     */
    saveDataToDb() {
        return new Promise((resolve, reject) => {
            this.updateMetaData();
            this.updateContext();
            this.platform.jovo.db().saveFullObject(
                this.platform.jovo.app.config.userDataCol,
                this.getData(), function(error, data) {
                    if (error) {
                        reject(error);
                        return;
                    }
                    resolve();
                });
        });
    }

    /**
     * Sets isNew flag for user
     * @param {boolean} isNew
     */
    setIsNewUser(isNew) {
        this.isNew = isNew;
    }

    /**
     * Returns isNew flag
     * @return {boolean|*}
     */
    isNewUser() {
        return this.isNew;
    }

    /**
     * Sets data
     * @param {*} data
     */
    setData(data) {
        if (!data) {
            data = {};
        }
        this.data = data.data;
        this.metaData = data.metaData;
        this.context = data.context || {};

        if (_.get(this, 'config.userContext.prev.size') > 0 && _.isEmpty(this.context)) {
{'fraction_non_alphanumeric': 0.14135606661379857, 'fraction_numerical': 0.0003965107057890563, 'mean_word_length': 1.4336710082006754, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '593204', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1421, 'n_words': 247}