<commit_before>'use strict';

var escapeHtml = require('../escape-html');
var uuid = require('node-uuid');
var multiline = require('multiline');

var mongoose = require('mongoose');
var User = mongoose.model('User');
var BankAccount = mongoose.model('BankAccount');


var bank = function (io, user, args) {
    this.getHelpString = function () {
        return 'bank - Perform financial operations.';
    };

    this.execute = function () {
        var operation = args[0];

        if (typeof operation === 'undefined' || operation === null || operation === '') {
            io.emit(user.uuid + '-response', {
                message: escapeHtml(multiline(function () {/*
                    open <pin> - opens an account.
                    list - lists your accounts.
                    transfer <account1> <pin> <account2> <amount> - sends money from account 1 to 2.
                    balance <account#> - shows account balance.
                    close <account#> <pin> - closes account if there is no money.
                    history <account> - Displays the transfer history for an account.
                */}))
            });
        };

        User.findOne({
            username: user.username
        }, function (err, user) {
            if (operation === 'open') {
                var account = new BankAccount();

                if (!/^\d+$/.test(args[1])) {
                    return io.emit(user.uuid + '-error', {
                        message: escapeHtml('Account PIN must be a number.')
                    });
                }

                account.number = uuid.v4();
                account.pin = args[1];
                account.owner = user.username;

                account.save(function (err) {
                    if (err) {
                        return io.emit(user.uuid + '-error', {
                            message: escapeHtml('An error occurred while creating the account.')
                        });
                    }

                    return io.emit(user.uuid + '-response', {
                        message: escapeHtml('Account #' + account.number + ' created successfully.')
                    });
                });
            }

            if (operation === 'balance') {
                var number = args[1];
                var subCommand = args[2];

                BankAccount.findOne({
                    number: number
                }, function (err, account) {
                    if (err || account === null) {
                        return io.emit(user.uuid + '-error', {
                            message: escapeHtml('Account #' + number + ' not found.')
                        });
                    }

                    if (typeof subCommand === 'undefined' || subCommand === null || subCommand === '') {
                        return io.emit(user.uuid + '-response', {
                            message: escapeHtml('Account #' + number + ' balance: $' + account.balance.toFixed(2))
                        });
                    }

                    if (subCommand === 'add' && user.isAdmin) {
                        var amount = parseFloat(args[3]);

                        if (isNaN(amount)) {
                            return io.emit(user.uuid + '-error', {
                                message: escapeHtml('Amount must be a number.')
                            });
                        }

                        console.log(amount)

                        account.balance += amount;

                        account.save(function (err) {
                            if (err) {

                            }

                            return io.emit(user.uuid + '-response', {
                                message: escapeHtml('Added $' + amount.toFixed(2) + ' to #' + number)
                            });
                        });
                    }

                    return io.emit(user.uuid + '-error', {
                        message: escapeHtml(subCommand + ' is not a command.')
                    });
                });
            }

            if (operation === 'list') {
                BankAccount.find({
                    owner: user.username
                }, function (err, accounts) {
                    var message = 'You own the following accounts:\n';

                    for (var i in accounts) {
                        message += '≤' + accounts[i].number + '≥ : $' + accounts[i].balance.toFixed(2) + '\n';
                    }<commit_msg>Close<commit_after>'use strict';

var escapeHtml = require('../escape-html');
var uuid = require('node-uuid');
var multiline = require('multiline');

var mongoose = require('mongoose');
var User = mongoose.model('User');
var BankAccount = mongoose.model('BankAccount');


var bank = function (io, user, args) {
    this.getHelpString = function () {
        return 'bank - Perform financial operations.';
    };

    this.execute = function () {
        var operation = args[0];

        if (typeof operation === 'undefined' || operation === null || operation === '') {
            io.emit(user.uuid + '-response', {
                message: escapeHtml(multiline(function () {/*
                    open <pin> - opens an account.
                    list - lists your accounts.
                    transfer <account1> <pin> <account2> <amount> - sends money from account 1 to 2.
                    balance <account#> - shows account balance.
                    close <account#> <pin> - closes account if there is no money.
                    history <account> - Displays the transfer history for an account.
                */}))
            });
        };

        User.findOne({
            username: user.username
        }, function (err, user) {
            if (operation === 'open') {
                var account = new BankAccount();

                if (!/^\d+$/.test(args[1])) {
                    return io.emit(user.uuid + '-error', {
                        message: escapeHtml('Account PIN must be a number.')
                    });
                }

                account.number = uuid.v4();
                account.pin = args[1];
                account.owner = user.username;

                account.save(function (err) {
                    if (err) {
                        return io.emit(user.uuid + '-error', {
                            message: escapeHtml('An error occurred while creating the account.')
                        });
                    }

                    return io.emit(user.uuid + '-response', {
                        message: escapeHtml('Account #' + account.number + ' created successfully.')
                    });
                });
            }

            if (operation === 'balance') {
                var number = args[1];
                var subCommand = args[2];

                BankAccount.findOne({
                    number: number
                }, function (err, account) {
                    if (err || account === null) {
                        return io.emit(user.uuid + '-error', {
                            message: escapeHtml('Account #' + number + ' not found.')
                        });
                    }

                    if (typeof subCommand === 'undefined' || subCommand === null || subCommand === '') {
                        return io.emit(user.uuid + '-response', {
                            message: escapeHtml('Account #' + number + ' balance: $' + account.balance.toFixed(2))
                        });
                    }

                    if (subCommand === 'add' && user.isAdmin) {
                        var amount = parseFloat(args[3]);

                        if (isNaN(amount)) {
                            return io.emit(user.uuid + '-error', {
                                message: escapeHtml('Amount must be a number.')
                            });
                        }

                        console.log(amount)

                        account.balance += amount;

                        account.save(function (err) {
                            if (err) {

                            }

                            return io.emit(user.uuid + '-response', {
                                message: escapeHtml('Added $' + amount.toFixed(2) + ' to #' + number)
                            });
                        });
                    }

                    return io.emit(user.uuid + '-error', {
                        message: escapeHtml(subCommand + ' is not a command.')
                    });
                });
            }

            if (operation === 'close') {
                var account = args[1];
                var pin = parseInt(args[2]);

                BankAccount.findOne({
                    number: account
                }, function (err, account) {
                    if (err || fromAccount === null) {
                        return io.emit(user.uuid + '-error', {
                            message: escapeHtml('Account #' + fromNumber + ' not found.')
                        });
                    }

                    if (fromAccount.pin !== fromPin) {
                        return io.emit(user.uuid + '-error', {
                            message: escapeHtml('Access denied')
                        });
                    }

                });
            }

            if (operation === 'list') {
                BankAccount.find({
                    owner: user.username
                }, function (err, accounts) {
                    var message = 'You own the following accounts:\n';

                    for (var i in accounts) {
                        message += '≤' + accounts[i].number + '≥ : $' + accounts[i].balance.toFixed(2) + '\n';
                    }
{'fraction_non_alphanumeric': 0.1229935589408036, 'fraction_numerical': 0.0030671710459053265, 'mean_word_length': 1.0554738390418155, 'pattern_counts': {'":': 0, '<': 21, '<?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': '1029764', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2155, 'n_words': 554}