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

@@ -0,0 +1,35 @@
// Controller for managing transactions
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "transactionController");
const { TransactionStorage } = require("../libStorage");
const transactionStorage = new TransactionStorage();
const { BaseTransaction } = require("../utilities/recordHelper");
const { withdrawBalance } = require("./accountController");
exports.createTransaction = async (_provider_transaction_id, _account_id, _discord_tokens_used, _provider_tokens_used, _provider_id, callback) => {
if (!_provider_transaction_id && !_account_id && !_discord_tokens_used && !_provider_id) return callback(new Error("Invalid vars when creating transaction", {vars: [_provider_transaction_id, _account_id, _discord_tokens_used, _provider_id, callback]}))
const newTransaction = new BaseTransaction(_provider_transaction_id, _account_id, _discord_tokens_used, _provider_tokens_used, _provider_id, callback);
log.DEBUG("New Transaction Object: ", newTransaction);
withdrawBalance(newTransaction.discord_tokens_used, newTransaction.account_id, (err, withdrawResult) => {
if (err) return callback(err, undefined);
if (withdrawResult){
log.DEBUG("New withdraw result: ", withdrawResult);
transactionStorage.createTransaction(newTransaction, async (err, transactionResult) =>{
if (err) return callback(err, undefined);
if(transactionResult){
log.DEBUG("New transaction result: ", transactionResult);
return callback(undefined, transactionResult);
}
})
}
else {
return callback(undefined, undefined);
}
});
}