- Transactions work against the account balance - Needs helper functions for users - Needs more testing
35 lines
1.8 KiB
JavaScript
35 lines
1.8 KiB
JavaScript
// 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);
|
|
}
|
|
});
|
|
} |