Traitement commandes et ajout J/arrow + Fix login

This commit is contained in:
Zachary Guénot
2023-05-13 23:10:13 +02:00
parent 05d0b36cc8
commit 34b262a153
13 changed files with 319 additions and 144 deletions

View File

@@ -1,6 +1,7 @@
const mysql = require('mysql2/promise')
require('dotenv').config()
module.exports = rewardRedemption = (async (user_id, user_name) => {
module.exports = rewardRedemption = (async (viewer_id, viewer_name) => {
// Create a connection to the MySQL database
const connection = await mysql.createConnection({
host: process.env.MYSQL_HOST,
@@ -10,21 +11,33 @@ module.exports = rewardRedemption = (async (user_id, user_name) => {
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])
const result = {}
// 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) {
// 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) })
// Viewer doesn't exist, insert a new row
let sql_data = { viewer_id, viewer_name, count: 1, current_count: 1 }
await connection.query('INSERT INTO rewards SET ?', sql_data).catch(error => { console.error(error) })
return result.status = 'insert_entry'
} 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) })
// Viewer exists, update the count
result.count = rows[0].count + 1
result.current_count = rows[0].current_count + 1
let sql_data = { count: result.count, current_count: result.current_count }
await connection.query('UPDATE rewards SET ? WHERE viewer_id = ?', [sql_data, viewer_id]).catch(error => { console.error(error) })
return result.status = 'update_entry'
}
}).catch(error => { console.error(error) })
// Terminate the connection to the database
await connection.end()
//return [newRow.current_count, newRow.count]
return result
})