This commit is contained in:
Louis Mazin 2025-12-09 19:45:47 +01:00
parent c0d5d9848b
commit b1962da1ec

View File

@ -43,24 +43,26 @@ const initDatabase = async () => {
player_id VARCHAR(50), player_id VARCHAR(50),
palworld_username VARCHAR(100) NOT NULL, palworld_username VARCHAR(100) NOT NULL,
linked_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, linked_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
lastConnection TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_discord_id (discord_id), INDEX idx_discord_id (discord_id),
INDEX idx_steam_id (steam_id), INDEX idx_steam_id (steam_id),
INDEX idx_player_id (player_id) INDEX idx_player_id (player_id)
) )
`); `);
// Migration: ajouter la colonne lastConnection si elle n'existe pas // Migration: ajouter la colonne lastConnection si elle n'existe pas (syntaxe MySQL)
const [columns] = await connection.execute(`
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = ?
AND TABLE_NAME = 'user_links'
AND COLUMN_NAME = 'lastConnection'
`, [process.env.DB_NAME]);
if (columns.length === 0) {
await connection.execute(` await connection.execute(`
PRAGMA table_info(user_links) ALTER TABLE user_links
ADD COLUMN lastConnection TIMESTAMP NULL DEFAULT NULL
`); `);
const [columns] = await connection.execute(`PRAGMA table_info(user_links)`);
const hasLastConnection = columns.some(col => col.name === 'lastConnection');
if (!hasLastConnection) {
await connection.execute(`ALTER TABLE user_links ADD COLUMN lastConnection TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP`);
console.log('✅ Colonne lastConnection ajoutée'); console.log('✅ Colonne lastConnection ajoutée');
} }
@ -105,7 +107,7 @@ const createTables = async () => {
player_id VARCHAR(50), player_id VARCHAR(50),
palworld_username VARCHAR(100) NOT NULL, palworld_username VARCHAR(100) NOT NULL,
linked_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, linked_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
lastConnection TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, lastConnection TIMESTAMP NULL DEFAULT NULL,
INDEX idx_discord_id (discord_id), INDEX idx_discord_id (discord_id),
INDEX idx_steam_id (steam_id), INDEX idx_steam_id (steam_id),
INDEX idx_player_id (player_id) INDEX idx_player_id (player_id)
@ -214,12 +216,19 @@ const cleanExpiredCodes = async () => {
); );
}; };
const updateLastConnection = async (steamId) => { const updateLastConnection = async (userId) => {
const connection = getConnection(); const connection = getConnection();
await connection.execute(
// Le userId est au format gdk_STEAMID, on extrait le Steam ID
const steamIdMatch = userId.match(/gdk_(\d+)/);
const steamId = steamIdMatch ? steamIdMatch[1] : userId;
const [result] = await connection.execute(
'UPDATE user_links SET lastConnection = NOW() WHERE steam_id = ?', 'UPDATE user_links SET lastConnection = NOW() WHERE steam_id = ?',
[steamId] [steamId]
); );
return { success: true, changes: result.affectedRows };
}; };
module.exports = { module.exports = {