Moved account related functions to controller
This commit is contained in:
59
controllers/accountController.js
Normal file
59
controllers/accountController.js
Normal 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.");
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user