test
This commit is contained in:
parent
c0d5d9848b
commit
b1962da1ec
37
database.js
37
database.js
@ -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)
|
||||||
await connection.execute(`
|
const [columns] = await connection.execute(`
|
||||||
PRAGMA table_info(user_links)
|
SELECT COLUMN_NAME
|
||||||
`);
|
FROM INFORMATION_SCHEMA.COLUMNS
|
||||||
|
WHERE TABLE_SCHEMA = ?
|
||||||
|
AND TABLE_NAME = 'user_links'
|
||||||
|
AND COLUMN_NAME = 'lastConnection'
|
||||||
|
`, [process.env.DB_NAME]);
|
||||||
|
|
||||||
const [columns] = await connection.execute(`PRAGMA table_info(user_links)`);
|
if (columns.length === 0) {
|
||||||
|
await connection.execute(`
|
||||||
const hasLastConnection = columns.some(col => col.name === 'lastConnection');
|
ALTER TABLE user_links
|
||||||
|
ADD COLUMN lastConnection TIMESTAMP NULL DEFAULT NULL
|
||||||
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 = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user