<commit_before>const BaseData = require('./base/base.data');
const User = require('../models/user.model');

class UsersData extends BaseData {
    constructor(db) {
        /* Second User param passed as validator, see BaseData*/
        super(db, User, User);
    }

    // REVIEW: This is a demo2 method, modify as needed
    checkPassword(username, password) {
        return this.collection
            .findOne({ username })
            .then((user) => {
                // findOne returns null if username not found
                if (!user) {
                    throw new Error('Invalid user');
                }
                if (user.password !== password) {
                    throw new Error('Invalid password');
                }

                return true;
            })
            .catch((err) => {
                console.log('Error in users.data.js -> checkPassword! Error message:');
                console.log(err);
                console.log('--- End error message ---');
            });
    }

    // REVIEW: This is a demo method, test, modify as needed    
    findById(id) {
        return this.collection
            .findOne({ id })
            .then((user) => {
                if (!user) {
                    throw new Error('Invalid id');
                }
                return user;
            })
            .catch((err) => {
                console.log('Error in users.data.js -> findById! Error message:');
                console.log(err);
                console.log('--- End error message ---');
            });
    }

    // REVIEW: This is a demo method, test, modify as needed        
    findByUsername(username) {
        return this.collection
            .findOne({ username: username })
            .then((user) => {
                if (!user) {
                    return null;
                }
                return user;
            })
            .catch();
    }

    // Finds user by username and updates it's projects. Projects must be an array!
    addFavourites(username, projects) {
        return this.collection.update(
            { 'username': username },
            {
                $addToSet: {
                    'favourites': {
                        $each: projects,
                    },
                },
            }
        );
    }

    removeFavourites(username, projects) {
        return this.collection.update(
            { 'username': username },
            {
                $pull: {
                    'favourites': {
                        $in: projects,
                    },
                },
            }
        );
    }

    updateProfile(username, options) {
        return this.collection.findOneAndUpdate(
            { 'username': username.trim() },
            {
                $set: options,
            }
        );
    }

    getFavouriteProjects(username) {
        return this.collection.find(
            { 'username': username },
            {
                'favourites': 1,
                '_id': 0,
            }
        ).toArray();
    }

    _isModelValid(model) {
        // TODO: add more validation, specific to user
        // then call base method
        return super._isModelValid(model);
    }
}

module.exports = UsersData;
<commit_msg>Added method to check if user has enough money<commit_after>const BaseData = require('./base/base.data');
const User = require('../models/user.model');

class UsersData extends BaseData {
    constructor(db) {
        /* Second User param passed as validator, see BaseData*/
        super(db, User, User);
    }

    // REVIEW: This is a demo2 method, modify as needed
    checkPassword(username, password) {
        return this.collection
            .findOne({ username })
            .then((user) => {
                // findOne returns null if username not found
                if (!user) {
                    throw new Error('Invalid user');
                }
                if (user.password !== password) {
                    throw new Error('Invalid password');
                }

                return true;
            })
            .catch((err) => {
                console.log('Error in users.data.js -> checkPassword! Error message:');
                console.log(err);
                console.log('--- End error message ---');
            });
    }

    // REVIEW: This is a demo method, test, modify as needed    
    findById(id) {
        return this.collection
            .findOne({ id })
            .then((user) => {
                if (!user) {
                    throw new Error('Invalid id');
                }
                return user;
            })
            .catch((err) => {
                console.log('Error in users.data.js -> findById! Error message:');
                console.log(err);
                console.log('--- End error message ---');
            });
    }

    // REVIEW: This is a demo method, test, modify as needed        
    findByUsername(username) {
        return this.collection
            .findOne({ username: username })
            .then((user) => {
                if (!user) {
                    return null;
                }
                return user;
            })
            .catch();
    }

    // Finds user by username and updates it's projects. Projects must be an array!
    addFavourites(username, projects) {
        return this.collection.update(
            { 'username': username },
            {
                $addToSet: {
                    'favourites': {
                        $each: projects,
                    },
                },
            }
        );
    }

    removeFavourites(username, projects) {
        return this.collection.update(
            { 'username': username },
            {
                $pull: {
                    'favourites': {
                        $in: projects,
                    },
                },
            }
        );
    }

    updateProfile(username, options) {
        return this.collection.findOneAndUpdate(
            { 'username': username.trim() },
            {
                $set: options,
            }
        );
    }

    getFavouriteProjects(username) {
        return this.collection.find(
            { 'username': username },
            {
                'favourites': 1,
                '_id': 0,
            }
        ).toArray();
    }

    hasEnoughMoney(username, amountToCheck) {
        return this.collection.findOne(
            { 'username': username },
            {
                'amount': 1,
                '_id': 0,
            }
        ).then((currentAmount) => {
            return amountToCheck <= currentAmount.amount;
        });
    }

    _isModelValid(model) {
        // TODO: add more validation, specific to user
        // then call base method
        return super._isModelValid(model);
    }
}

module.exports = UsersData;

{'fraction_non_alphanumeric': 0.11429813124728379, 'fraction_numerical': 0.0011589164131537012, 'mean_word_length': 1.1615529117094552, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 18, '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': '7442180', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1654, 'n_words': 420}