Add functionality to create transactions
- Transactions work against the account balance - Needs helper functions for users - Needs more testing
This commit is contained in:
35
controllers/transactionController.js
Normal file
35
controllers/transactionController.js
Normal 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user