Moved account related functions to controller

This commit is contained in:
Logan Cusano
2023-02-26 01:31:19 -05:00
parent 013e407da8
commit 2dd43c07ae
2 changed files with 66 additions and 55 deletions

View File

@@ -0,0 +1,59 @@
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.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.");
}

View File

@@ -4,61 +4,13 @@
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "balanceAuthorizations");
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|*
*/
function 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
*/
function 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);
})
}
function 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);
})
}
function insufficientTokensResponse(interaction){
log.DEBUG("INSUFFICIENT TOKENS RESPONSE")
return interaction.reply("Sorry, not enough tokens for this request.");
}
function welcomeResponse(interaction){
log.DEBUG("WELCOME RESPONSE")
return interaction.reply("Hey there, you have an account now.");
}
const {
checkForAccount,
createAccount,
verifyBalance,
insufficientTokensResponse,
welcomeResponse
} = require("../controllers/accountController");
exports.authorizeTokenUsage = async (interaction, command, next) => {
log.DEBUG("Command requires tokens? ", command.isPrivileged)