const mysql = require('mysql2/promise') module.exports = async function (user_id, user_name) { // 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 }) // Check if the user already exists in the rewards table await connection.query('SELECT * FROM rewards WHERE user_id = ?', [user_id]) .then(async ([rows, fields]) => { if (rows.length === 0) { // User doesn't exist, insert a new row await connection.query('INSERT INTO rewards SET ?', { user_id, user_name, count: 1, current_count: 1 }) .catch(error => { console.error(error) }) } else { // User exists, update the count const newRow = { count: rows[0].count + 1, current_count: rows[0].current_count + 1 } await connection.query('UPDATE rewards SET ? WHERE user_id = ?', [newRow, user_id]) .catch(error => { console.error(error) }) } }).catch(error => { console.error(error) }) // Terminate the connection to the database await connection.end() }