Added Payment Authorization Middleware
- Default options for commands - Verify user has enough balance to create the transaction - Need to create methods to create transactions
This commit is contained in:
@@ -12,6 +12,9 @@ const mysql = require("mysql");
|
||||
|
||||
const rssFeedsTable = process.env.DB_RSS_FEEDS_TABLE;
|
||||
const rssPostsTable = process.env.DB_RSS_POSTS_TABLE;
|
||||
const accountsTable = process.env.DB_ACCOUNTS_TABLE;
|
||||
const transactionsTable = process.env.DB_TRANSACTIONS_TABLE;
|
||||
const pricingTable = process.env.DB_PRICING_TABLE;
|
||||
|
||||
// Helper Functions
|
||||
// Function to run and handle SQL errors
|
||||
@@ -30,6 +33,15 @@ function runSQL(sqlQuery, connection, callback = (err, rows) => {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a formatted date time string from now for MySQL
|
||||
*
|
||||
* @returns Date string for now formatted for MySQL
|
||||
*/
|
||||
function returnMysqlTime(){
|
||||
return new Date().toISOString().slice(0, 19).replace('T', ' ');
|
||||
}
|
||||
|
||||
class Storage {
|
||||
constructor(_dbTable) {
|
||||
this.connection = mysql.createPool({
|
||||
@@ -142,6 +154,63 @@ class Storage {
|
||||
}
|
||||
}
|
||||
|
||||
exports.UserStorage = class UserStorage extends Storage {
|
||||
constructor() {
|
||||
super(accountsTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a new account to the database
|
||||
* @param {*} _discordAccountId The Discord ID the the user
|
||||
* @param {*} callback The callback to be sent
|
||||
* @callback Error|Array|*
|
||||
*/
|
||||
saveAccount(_discordAccountId, callback){
|
||||
const sqlQuery = `INSERT INTO ${this.dbTable} (discord_account_id, balance) VALUES ('${_discordAccountId}', ${0});`;
|
||||
|
||||
log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`)
|
||||
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) return callback(err, undefined);
|
||||
if (rows?.affectedRows > 0) return callback(undefined, rows);
|
||||
return callback(undefined, undefined);
|
||||
})
|
||||
}
|
||||
|
||||
checkBalance(_tokensToBeUsed, _account_id, callback) {
|
||||
this.getRecordBy('account_id', _account_id, (err, record) => {
|
||||
if (err) return callback(err, undefined);
|
||||
|
||||
if (record?.balance && record.balance > _tokensToBeUsed) return callback(undefined, true);
|
||||
else{
|
||||
return callback(undefined, false);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
updateBalance(){
|
||||
|
||||
}
|
||||
}
|
||||
/*
|
||||
exports.TransactionStorage = class TransactionStorage extends Storage {
|
||||
constructor() {
|
||||
super(transactionsTable);
|
||||
}
|
||||
|
||||
createTransaction(transaction, callback){
|
||||
const sqlQuery = `INSERT INTO ${this.dbTable} () VALUES ('${}');`;
|
||||
|
||||
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);
|
||||
return callback(undefined, undefined);
|
||||
})
|
||||
}
|
||||
}
|
||||
*/
|
||||
exports.FeedStorage = class FeedStorage extends Storage {
|
||||
constructor() {
|
||||
super(rssFeedsTable);
|
||||
@@ -329,7 +398,7 @@ exports.PostStorage = class PostStorage extends Storage {
|
||||
}
|
||||
|
||||
savePost(_postObject, callback){
|
||||
const tempCreationDate = new Date().toISOString().slice(0, 19).replace('T', ' ');
|
||||
const tempCreationDate = returnMysqlTime();
|
||||
log.DEBUG("Saving Post Object:", _postObject);
|
||||
if (!_postObject?.guid || !_postObject?.link) {
|
||||
return callback(new Error("Post object malformed, check the object before saving it"), undefined)
|
||||
|
||||
Reference in New Issue
Block a user