const mysql = require('mysql2/promise'); require('dotenv').config(); const pool = mysql.createPool({ host: process.env.DB_HOST, user: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.DB_NAME, }); /** * Initialize the database and create necessary tables * */ async function initDB() { try { const connection = await pool.getConnection(); // Create users table if it doesn't exist await connection.query(` CREATE TABLE IF NOT EXISTS users ( id INT AUTO_INCREMENT PRIMARY KEY NOT NULL, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, role VARCHAR(20) DEFAULT 'user', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); `); // table fournisseurs await connection.query(` CREATE TABLE IF NOT EXISTS fournisseurs ( id BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL, name VARCHAR(255) NOT NULL, category VARCHAR(50) NOT NULL, contact_person VARCHAR(255) DEFAULT NULL, phone VARCHAR(20) DEFAULT NULL, email VARCHAR(255) DEFAULT NULL, status VARCHAR(20) DEFAULT 'active', last_order_date DATE DEFAULT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); `); // Create emplacements table if it doesn't exist await connection.query(` CREATE TABLE IF NOT EXISTS emplacements ( id BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL, name VARCHAR(255) NOT NULL, type VARCHAR(50) NOT NULL, temperature DECIMAL(5, 2) DEFAULT NULL, capacity INT DEFAULT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); `); // table compartiments await connection.query(` CREATE TABLE IF NOT EXISTS compartiments ( id BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL, name VARCHAR(255) NOT NULL, capacity INT DEFAULT NULL, uniter VARCHAR(50) NOT NULL DEFAULT 'kg', id_emplacement BIGINT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (id_emplacement) REFERENCES emplacements(id) ON DELETE CASCADE ); `); // table stock await connection.query(` CREATE TABLE IF NOT EXISTS stocks ( id BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL, articles VARCHAR(255) NOT NULL, quantity INT NOT NULL DEFAULT 0, uniter VARCHAR(50) NOT NULL DEFAULT 'kg', price_unit DECIMAL(10, 2) NOT NULL DEFAULT 0.00, id_emplacement BIGINT NOT NULL, id_compartiment BIGINT NOT NULL, fournisseur_id BIGINT DEFAULT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (id_emplacement) REFERENCES emplacements(id) ON DELETE CASCADE, FOREIGN KEY (fournisseur_id) REFERENCES fournisseurs(id) ON DELETE SET NULL, FOREIGN KEY (id_compartiment) REFERENCES compartiments(id) ON DELETE CASCADE ); `); // ajoute une autre table si necessaire // add a default admin user if none exists const [rows] = await connection.query(`SELECT COUNT(*) as count FROM users`); if (rows[0].count === 0) { const bcrypt = require('bcryptjs'); const hashedPassword = await bcrypt.hash('admin123', 10); await connection.query( 'INSERT INTO users (username, password, role) VALUES (?, ?, ?)', ['admin', hashedPassword, 'admin'] ); console.log('✅ Default admin user created: admin / admin123'); } connection.release(); console.log('✅ Database initialized'); } catch (err) { console.error('❌ Failed to initialize database:', err.message); } } module.exports = { pool, initDB, };