const sqlite = require('better-sqlite3') const bcrypt = require('bcryptjs') // Construct the database path using the detected IP let dbPath = `./base/data.db`; // Connect to SQLite database with the initial path let database = new sqlite(dbPath); // Create the users table if it doesn't exist const createUserTableQuery = ` CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username VARCHAR(200) NOT NULL, email VARCHAR(250) NOT NULL UNIQUE, password TEXT NOT NULL, roles VARCHAR(250) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ` database.prepare(createUserTableQuery).run() // Insert a default admin user if not exists const insertDefaultUserQuery = ` INSERT INTO users (username, email, password, roles) SELECT 'admin', 'admin@example.com', ?, 'admin' WHERE NOT EXISTS (SELECT 1 FROM users WHERE username = 'admin'); ` // Hash the password '1234' before storing const hashedPassword = bcrypt.hashSync('123456789', 10) database.prepare(insertDefaultUserQuery).run(hashedPassword) // create table for note status const createStatusTableQuery = ` CREATE TABLE IF NOT EXISTS status ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, nom VARCHAR(200) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ` database.prepare(createStatusTableQuery).run() // create table for mention const createMentionTableQuery = ` CREATE TABLE IF NOT EXISTS mentions ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, nom VARCHAR(250) NOT NULL, uniter VARCHAR(50) NOT NULL, -- Abréviation du nom created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ` database.prepare(createMentionTableQuery).run() // Create the niveau table if it doesn't exist const createNiveauTableQuery = ` CREATE TABLE IF NOT EXISTS niveaus ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, nom VARCHAR(50) NOT NULL, -- Exemple: L1, L2, L3, etc. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ` database.prepare(createNiveauTableQuery).run() // Create the etudiants table if it doesn't exist const createEtudiantsTableQuery = ` CREATE TABLE IF NOT EXISTS etudiants ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, nom VARCHAR(250) DEFAULT NULL, prenom VARCHAR(250) DEFAULT NULL, photos TEXT NOT NULL, date_de_naissances DATE NOT NULL, niveau VARCHAR(250) NOT NULL, -- Clé étrangère vers niveaus annee_scolaire VARCHAR(20) NOT NULL, status INTEGER NOT NULL, mention_id INTEGER NOT NULL, -- Clé étrangère vers mentions num_inscription TEXT NOT NULL UNIQUE, sexe VARCHAR(20) NOT NULL, cin VARCHAR(250) DEFAULT NULL, date_delivrance DEFAULT NULL, nationalite DATE NOT NULL, annee_bacc DATE NOT NULL, serie VARCHAR(20) NOT NULL, boursier BOOLEAN DEFAULT FALSE, domaine VARCHAR(250) NOT NULL, contact VARCHAR(20) NOT NULL, parcours VARCHAR(250) DEFAULT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (status) REFERENCES status(id), FOREIGN KEY (mention_id) REFERENCES mentions(id) ); ` database.prepare(createEtudiantsTableQuery).run() // Create the notes table if it doesn't exist const createMatiereTableQuery = ` CREATE TABLE IF NOT EXISTS matieres ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, nom VARCHAR(250) UNIQUE NOT NULL, unite_enseignement VARCHAR(250) NOT NULL, credit INTEGER NOT NULL, heure INTEGER NOT NULL, ue VARCHAR(10) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ` database.prepare(createMatiereTableQuery).run() // Create the semestre table if it doesn't exist const createSemestreTableQuery = ` CREATE TABLE IF NOT EXISTS semestres ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, nom VARCHAR(30) NOT NULL, -- Exemple: S1, S2, S3, etc. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ` database.prepare(createSemestreTableQuery).run() // Create the semestre table if it doesn't exist const createMatiere_mentionTableQuery = ` CREATE TABLE IF NOT EXISTS matiere_mention ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, matiere_id INTEGER NOT NULL, -- Clé étrangère vers matieres mention_id INTEGER NOT NULL, -- Clé étrangère vers mentions FOREIGN KEY (matiere_id) REFERENCES matieres(id), FOREIGN KEY (mention_id) REFERENCES mentions(id) ); ` database.prepare(createMatiere_mentionTableQuery).run() const createMatiere_semestreTableQuery = ` CREATE TABLE IF NOT EXISTS matiere_semestre ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, matiere_id INTEGER NOT NULL, -- Clé étrangère vers matieres semestre_id INTEGER NOT NULL, -- Clé étrangère vers semestres mention_id INTEGER NOT NULL, -- Clé étrangère vers niveaus FOREIGN KEY (matiere_id) REFERENCES matieres(id), FOREIGN KEY (semestre_id) REFERENCES semestres(id), FOREIGN KEY (mention_id) REFERENCES mentions(id) ); ` database.prepare(createMatiere_semestreTableQuery).run() // Create the notes table if it doesn't exist const createNoteTableQuery = ` CREATE TABLE IF NOT EXISTS notes ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, etudiant_id INTEGER NOT NULL, -- Clé étrangère vers etudiants matiere_id INTEGER NOT NULL, -- Clé étrangère vers matieres etudiant_niveau VARCHAR(50) NOT NULL, mention_id INTEGER NOT NULL, note FLOAT DEFAULT NULL, annee_scolaire VARCHAR(50) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (etudiant_id) REFERENCES etudiants(id), FOREIGN KEY (matiere_id) REFERENCES matieres(id) FOREIGN KEY (mention_id) REFERENCES mentions(id) ); ` database.prepare(createNoteTableQuery).run() // Create the notes second session table if it doesn't exist const createNoteRepechTableQuery = ` CREATE TABLE IF NOT EXISTS notesrepech ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, etudiant_id INTEGER NOT NULL, -- Clé étrangère vers etudiants matiere_id INTEGER NOT NULL, -- Clé étrangère vers matieres etudiant_niveau VARCHAR(50) NOT NULL, mention_id INTEGER NOT NULL, note FLOAT DEFAULT NULL, annee_scolaire VARCHAR(50) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (etudiant_id) REFERENCES etudiants(id), FOREIGN KEY (matiere_id) REFERENCES matieres(id) FOREIGN KEY (mention_id) REFERENCES mentions(id) ); ` database.prepare(createNoteRepechTableQuery).run() // create table for note système const createNoteSystemeTableQuery = ` CREATE TABLE IF NOT EXISTS notesystems ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, admis FLOAT NOT NULL DEFAULT 10, redouble FLOAT NOT NULL DEFAULT 9.99, renvoyer FLOAT NOT NULL DEFAULT 7.99, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ` database.prepare(createNoteSystemeTableQuery).run() // create table année scolaire const createAnneeScolaireTableQuery = ` CREATE TABLE IF NOT EXISTS anneescolaire ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, code VARCHAR(30) NOT NULL, debut DATE NOT NULL, fin DATE NOT NULL, is_current INTEGER DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ` database.prepare(createAnneeScolaireTableQuery).run() // create traitement systeme const createTraitementSystemQuery = ` CREATE TABLE IF NOT EXISTS traitmentsystem ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, code VARCHAR(30) NOT NULL, debut DATE NOT NULL, fin DATE NOT NULL, is_finished INTEGER DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ` database.prepare(createTraitementSystemQuery).run() const createNecessaryParameterTableQuery = ` CREATE TABLE IF NOT EXISTS nessesaryTable ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, uniter_heure INTEGER NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ` database.prepare(createNecessaryParameterTableQuery).run() const createMatiereEnseignantTableQuery = ` CREATE TABLE IF NOT EXISTS matiereEnseignants ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, matiere_id INTEGER NOT NULL, nom_enseignant VARCHAR(250) NOT NULL, prenom_enseignant VARCHAR(250) NOT NULL, contact VARCHAR(11) NOT NULL, date DATE NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (matiere_id) REFERENCES matieres(id) ); ` database.prepare(createMatiereEnseignantTableQuery).run() const createParcourTableQuery = ` CREATE TABLE IF NOT EXISTS parcours ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, nom VARCHAR(250) NOT NULL, uniter VARCHAR(250) NOT NULL, mention_id INTEGER DEFAULT NULL, -- Clé étrangère vers mentions created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ` database.prepare(createParcourTableQuery).run() const createParcourSemestreTableQuery = ` CREATE TABLE IF NOT EXISTS parcoursmatiere ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, matiere_id INTEGER NOT NULL, parcour_id INTEGER NOT NULL, FOREIGN KEY (matiere_id) REFERENCES matieres(id), FOREIGN KEY (parcour_id) REFERENCES parcours(id) ); ` database.prepare(createParcourSemestreTableQuery).run() const createTableEcolageQuery = ` CREATE TABLE IF NOT EXISTS trancheecolage ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, etudiant_id INTEGER NOT NULL, tranchename VARCHAR(255) NOT NULL, montant DOUBLE NOT NULL ); ` database.prepare(createTableEcolageQuery).run() const createTableStoreIP = ` CREATE TABLE IF NOT EXISTS ipconfig ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, ipname VARCHAR(255) NOT NULL ); `; database.prepare(createTableStoreIP).run() // -------------------------------------- function pre-excuter -------------------------------------------- async function insertStatusesIfNotExist() { // Préparation des requêtes const checkStatusQuery = database.prepare(` SELECT COUNT(*) AS count FROM status WHERE nom = ?; `) const insertStatusQuery = database.prepare(` INSERT INTO status (nom) VALUES (?); `) // Tableau des statuts à vérifier/insérer const arrayStatus = ['Nouveau', 'Passant', 'Redoublant', 'Renvoyé', 'Ancien'] for (let index = 0; index < arrayStatus.length; index++) { const statusName = arrayStatus[index] // Vérification si le statut existe déjà const result = checkStatusQuery.get(statusName) // Si le statut n'existe pas, on l'insère if (result.count === 0) { insertStatusQuery.run(statusName) } } } // execute the function insertStatusesIfNotExist() async function insertDefaultNoteSystemIfNotExist() { // Préparation de la requête pour vérifier si une entrée existe déjà const checkNoteSystemQuery = database.prepare(` SELECT COUNT(*) AS count FROM notesystems; `) // Préparation de la requête pour insérer une entrée par défaut const insertNoteSystemQuery = database.prepare(` INSERT INTO notesystems (admis, redouble, renvoyer) VALUES (?, ?, ?); `) // Valeurs par défaut à insérer const defaultValues = { admis: 10.0, redouble: 9.99, renvoyer: 7.99 } // Vérification si une entrée existe déjà const result = checkNoteSystemQuery.get() if (result.count === 0) { // Insérer les valeurs par défaut si aucune entrée n'existe insertNoteSystemQuery.run(defaultValues.admis, defaultValues.redouble, defaultValues.renvoyer) } } insertDefaultNoteSystemIfNotExist() async function semestreCreate() { const query = database.prepare('INSERT INTO semestres (nom) VALUES (?)') // Préparation de la requête pour vérifier si une entrée existe déjà const checkSemestreQuery = database.prepare(` SELECT COUNT(*) AS count FROM semestres; `) try { let arraySemestre = [ 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10', 'S11', 'S12', 'S13', 'S14', 'S14', 'S16' ] // Vérification si une entrée existe déjà const result = checkSemestreQuery.get() if (result.count === 0) { database.transaction(() => { for (let index = 0; index < arraySemestre.length; index++) { query.run(arraySemestre[index]) } })() } } catch (error) { console.log(error) } } const createNecessaryParameterTable = () => { // Check if the table is empty const rowCount = database.prepare(`SELECT COUNT(*) AS count FROM nessesaryTable`).get().count // If the table is empty, insert the default value if (rowCount === 0) { const insertDefaultQuery = ` INSERT INTO nessesaryTable (uniter_heure) VALUES (15); ` database.prepare(insertDefaultQuery).run() } } // Call the function when the app runs createNecessaryParameterTable() semestreCreate() // Function to get the IP from the database function getIP() { const data = database.prepare("SELECT * FROM ipconfig WHERE id = 1").get(); if (data) { return data.ipname; } else { return null; // Explicitly return `null` if no data is found } } // Get the new IP from the database let newIP = getIP(); if (newIP) { // Construct the database path using the new IP from the database dbPath = `\\\\${newIP}\\base\\data.db`; // Reconnect to SQLite database with the updated path database = new sqlite(dbPath); // Re-initialize database connection with new path console.log("now COnnect to the ", dbPath); } module.exports = { database }