- Transactions work against the account balance - Needs helper functions for users - Needs more testing
69 lines
2.3 KiB
JavaScript
69 lines
2.3 KiB
JavaScript
|
|
const { DebugBuilder } = require("../utilities/debugBuilder");
|
|
const log = new DebugBuilder("server", "accountController");
|
|
|
|
const { UserStorage } = require("../libStorage");
|
|
const userStorage = new UserStorage();
|
|
|
|
/**
|
|
* Check to see if the discord ID has an account associated with it
|
|
* @param {*} _discordAccountId The discord account to look for
|
|
* @param {*} callback The callback function to be called
|
|
* @callback false|*
|
|
*/
|
|
exports.checkForAccount = (_discordAccountId, callback) => {
|
|
userStorage.getRecordBy("discord_account_id", _discordAccountId, (err, results) => {
|
|
if (err) return callback(err, undefined);
|
|
|
|
if (!results) return callback(undefined, false);
|
|
return callback(undefined, results);
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Create an account from a discord ID
|
|
* @param {*} _discordAccountId
|
|
* @param {*} callback
|
|
*/
|
|
exports.createAccount = (_discordAccountId, callback) => {
|
|
if (!_discordAccountId) return callback(new Error("No discord account specified before creation"));
|
|
userStorage.saveAccount(_discordAccountId, (err, results) => {
|
|
if (err) return callback(err, undefined);
|
|
|
|
if (!results) return callback(new Error("No results from creating account"), undefined);
|
|
|
|
return callback(undefined, results);
|
|
})
|
|
}
|
|
|
|
exports.withdrawBalance = async (_withdrawAmount, _accountId, callback) => {
|
|
userStorage.updateBalance('withdraw', _withdrawAmount, _accountId, async (err, result) => {
|
|
if (err) return callback(err, undefined);
|
|
|
|
if(result) return callback(undefined, result);
|
|
|
|
return callback(undefined, undefined);
|
|
})
|
|
}
|
|
|
|
exports.verifyBalance = (_tokensToBeUsed, _accountId, callback) => {
|
|
userStorage.checkBalance(_tokensToBeUsed, _accountId, (err, results) => {
|
|
if (err) return callback(err, undefined);
|
|
|
|
if(!results) return callback(undefined, false);
|
|
|
|
return callback(undefined, true);
|
|
})
|
|
}
|
|
|
|
exports.insufficientTokensResponse = (interaction) => {
|
|
log.DEBUG("INSUFFICIENT TOKENS RESPONSE")
|
|
|
|
return interaction.reply("Sorry, not enough tokens for this request.");
|
|
}
|
|
|
|
exports.welcomeResponse = (interaction) => {
|
|
log.DEBUG("WELCOME RESPONSE")
|
|
|
|
return interaction.reply("Hey there, you have an account now.");
|
|
} |