Add functionality to create transactions

- Transactions work against the account balance
- Needs helper functions for users
- Needs more testing
This commit is contained in:
Logan Cusano
2023-02-26 03:26:24 -05:00
parent fb99d6ab01
commit 14fac69ab7
8 changed files with 194 additions and 44 deletions

View File

@@ -187,30 +187,60 @@ exports.UserStorage = class UserStorage extends Storage {
}
})
}
/**
* Update a user's account Balance
*
* @param {string} _updateType The type of update to make to the account [ withdraw | deposit ]
* @param {number} _updateAmount The amount to update the account
* @param {number} _account_id The ID of the account to update
* @param {function} callback The callback function to call with the results
* @returns Result from the SQL query or false
*/
updateBalance(_updateType, _updateAmount, _account_id, callback){
var sqlQuery = "";
switch(_updateType){
case "withdraw":
// Code here to withdraw funds
sqlQuery = `UPDATE ${this.dbTable} SET balance=balance-${_updateAmount};`;
break;
case "deposit":
// Code here to withdraw funds
sqlQuery = `UPDATE ${this.dbTable} SET balance=balance+${_updateAmount};`;
break;
default:
log.ERROR('Update type not valid: ', _updateType);
return callback(new Error("Update type not valid"));
}
if(!sqlQuery) return callback(new Error("SQL Query empty"), undefined);
updateBalance(){
runSQL(sqlQuery, this.connection, (err, rows) => {
if (err) return callback(err, undefined);
if (rows?.affectedRows > 0) return callback(undefined, rows);
return callback(undefined, undefined);
})
}
}
/*
exports.TransactionStorage = class TransactionStorage extends Storage {
constructor() {
super(transactionsTable);
}
createTransaction(transaction, callback){
const sqlQuery = `INSERT INTO ${this.dbTable} () VALUES ('${}');`;
var sqlQuery = `INSERT INTO ${this.dbTable} (transaction_id, account_id, discord_tokens_used, provider_tokens_used, provider_id, order_date) VALUES ('${transaction.transaction_id}', '${transaction.account_id}', '${transaction.discord_tokens_used}', '${transaction.provider_tokens_used}', '${transaction.provider_id}', '${returnMysqlTime()}');`;
log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`)
runSQL(sqlQuery, this.connection, (err, rows) => {
if (err) return callback(err, undefined);
if (rows[0]?.id) return callback(undefined, rows);
if (rows?.affectedRows > 0) return callback(undefined, rows);
return callback(undefined, undefined);
})
}
}
*/
exports.FeedStorage = class FeedStorage extends Storage {
constructor() {
super(rssFeedsTable);