75 lines
3.3 KiB
JavaScript
75 lines
3.3 KiB
JavaScript
const mysql = require('mysql2/promise')
|
|
require('dotenv').config()
|
|
|
|
module.exports = modifyReward = (async (viewer_id, viewer_name, action, quantity, type) => {
|
|
// Create a connection to the MySQL database
|
|
const connection = await mysql.createConnection({
|
|
host: process.env.MYSQL_HOST,
|
|
port: process.env.MYSQL_PORT,
|
|
user: process.env.MYSQL_USER,
|
|
password: process.env.MYSQL_PASSWORD,
|
|
database: process.env.MYSQL_DATABASE
|
|
})
|
|
|
|
const result = {}
|
|
let sql_data = {}
|
|
|
|
// Check if the viewer already exists in the rewards table
|
|
await connection.query('SELECT * FROM rewards WHERE viewer_id = ?', [viewer_id])
|
|
.then(async ([rows, fields]) => {
|
|
if (rows.length === 0) {
|
|
// Viewer doesn't exist, insert a new row
|
|
sql_data = { viewer_id, viewer_name }
|
|
|
|
if (action === 'add' || action === 'set') { // Add or set the viewer with the quantity
|
|
if (type === 'permanent') sql_data.count = quantity
|
|
else if (type === 'current') sql_data.current_count = quantity
|
|
else { sql_data.count = quantity; sql_data.current_count = quantity }
|
|
|
|
await connection.query('INSERT INTO rewards SET ?', sql_data) .catch(error => { console.error(error) })
|
|
return result.status = 'insert_entry'
|
|
}
|
|
else if (action === 'get') return result.status = 'no_get_entry' // Can't get a viewer that doesn't exist
|
|
else if (action === 'remove') return result.status = 'no_delete_entry' // Can't remove a viewer that doesn't exist
|
|
} else {
|
|
// Viewer exists, update the count
|
|
result.count = rows[0].count
|
|
result.current_count = rows[0].current_count
|
|
|
|
if (action === 'get') return result.status = 'get_entry'
|
|
|
|
else if (action === 'add') { // Add the quantity
|
|
if (type === 'permanent') sql_data.count = rows[0].count + quantity
|
|
else if (type === 'current') sql_data.current_count = rows[0].current_count + quantity
|
|
else { sql_data.count = rows[0].count + quantity; sql_data.current_count = rows[0].current_count + quantity }
|
|
|
|
result.status = 'update_add_entry'
|
|
}
|
|
else if (action === 'remove') { // Remove the quantity
|
|
if (type === 'permanent') sql_data.count = rows[0].count - quantity
|
|
else if (type === 'current') sql_data.current_count = rows[0].current_count - quantity
|
|
else { sql_data.count = rows[0].count - quantity; sql_data.current_count = rows[0].current_count - quantity }
|
|
|
|
if (sql_data.count < 0) return result.status = 'not_enough_count' // Can't delete more than the count
|
|
if (sql_data.current_count < 0) return result.status = 'not_enough_current_count' // Can't delete more than the current_count
|
|
|
|
result.status = 'update_remove_entry'
|
|
}
|
|
else if (action === 'set') { // Set the quantity
|
|
if (type === 'permanent') sql_data.count = quantity
|
|
else if (type === 'current') sql_data.current_count = quantity
|
|
else { sql_data.count = quantity; sql_data.current_count = quantity }
|
|
|
|
result.status = 'update_set_entry'
|
|
}
|
|
|
|
await connection.query('UPDATE rewards SET ? WHERE viewer_id = ?', [sql_data, viewer_id]).catch(error => { console.error(error) })
|
|
}
|
|
}).catch(error => { console.error(error) })
|
|
|
|
// Terminate the connection to the database
|
|
await connection.end()
|
|
|
|
result.sql_data = sql_data
|
|
return result
|
|
}) |