From 82b5e6a68e2d18ff280aa05ba60dddb973be6f18 Mon Sep 17 00:00:00 2001 From: fabriceBJHost Date: Tue, 17 Jun 2025 10:55:27 +0200 Subject: [PATCH] dernier mise a jour university --- database/database.js | 20 +++--- database/import/Etudiants.js | 119 +++++++++++++++++------------------ 2 files changed, 68 insertions(+), 71 deletions(-) diff --git a/database/database.js b/database/database.js index 066120d..5f8be5f 100644 --- a/database/database.js +++ b/database/database.js @@ -77,22 +77,22 @@ const createEtudiantsTableQuery = ` 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, + photos TEXT DEFAULT NULL, + date_de_naissances DATE DEFAULT NULL, niveau VARCHAR(250) NOT NULL, -- Clé étrangère vers niveaus annee_scolaire VARCHAR(20) NOT NULL, - status INTEGER NOT NULL, + status INTEGER DEFAULT NULL, mention_id INTEGER NOT NULL, -- Clé étrangère vers mentions - num_inscription TEXT NOT NULL UNIQUE, - sexe VARCHAR(20) NOT NULL, + num_inscription TEXT NOT NULL, + sexe VARCHAR(20) DEFAULT NULL, cin VARCHAR(250) DEFAULT NULL, date_delivrance DEFAULT NULL, - nationalite DATE NOT NULL, - annee_bacc DATE NOT NULL, - serie VARCHAR(20) NOT NULL, + nationalite DATE DEFAULT NULL, + annee_bacc DATE DEFAULT NULL, + serie VARCHAR(20) DEFAULT NULL, boursier BOOLEAN DEFAULT FALSE, - domaine VARCHAR(250) NOT NULL, - contact VARCHAR(20) NOT NULL, + domaine VARCHAR(250) DEFAULT NULL, + contact VARCHAR(20) DEFAULT NULL, parcours VARCHAR(250) DEFAULT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, diff --git a/database/import/Etudiants.js b/database/import/Etudiants.js index ed9a010..e19317c 100644 --- a/database/import/Etudiants.js +++ b/database/import/Etudiants.js @@ -3,12 +3,13 @@ const path = require('path') const XLSX = require('xlsx') const { getCompressedDefaultImage } = require('../function/GetImageDefaault') const { parse } = require('csv-parse/sync') -const { insertEtudiant, updateEtudiant } = require('../Models/Etudiants') +const { insertEtudiant } = require('../Models/Etudiants') const { database } = require('../database') const { getMentions } = require('../Models/Mentions') const dayjs = require('dayjs') const { getStatusMention } = require('../function/Helper') const customParseFormat = require('dayjs/plugin/customParseFormat') +// const customParseFormatt = require('dayjs/plu') dayjs.extend(customParseFormat) // Function to convert any date format to 'YYYY-MM-DD' @@ -21,7 +22,8 @@ function convertToISODate(input) { 'DD-MM-YYYY', 'MM-DD-YYYY', 'DD/MM/YY', - 'MM/DD/YY' + 'MM/DD/YY', + 'DD-MMM-YY' ] const parsedDate = dayjs(input, formats, true) // Strict parsing to ensure formats are matched correctly @@ -30,6 +32,12 @@ function convertToISODate(input) { return parsedDate.format('YYYY-MM-DD') } + // Handle cases like "Vers 2000" + const versMatch = typeof input === 'string' && input.match(/vers\s*(\d{4})/i); + if (versMatch) { + return `${versMatch[1]}-01-01`; // Return in ISO format + } + function excelDateToJSDate(serial) { const utc_days = Math.floor(serial - 25569); // days from Jan 1, 1970 const utc_value = utc_days * 86400; // seconds in a day @@ -75,11 +83,17 @@ async function importFileToDatabase(filePath) { return } + // ✅ Count number of data rows + const numberOfLines = records.length; + console.log(`Number of data rows: ${numberOfLines}`); + try { let error = true let message = '' // Vérifier les données en une seule boucle + let ar = []; + let oldNum = ''; for (const row of records) { if ( !row.nom || @@ -97,8 +111,9 @@ async function importFileToDatabase(filePath) { !row.serie || !row.code_redoublement || !row.boursier || - !row.domaine || - !row.contact + !row.domaine + // || + // !row.contact ) { if (!row.nom) { message = "Le champ 'nom' est inconnu" @@ -136,9 +151,10 @@ async function importFileToDatabase(filePath) { message = "Le champ 'boursier' est inconnu" } else if (!row.domaine) { message = "Le champ 'domaine' est inconnu" - } else if (!row.contact) { - message = "Le champ 'contact' est inconnu" } + // else if (!row.contact) { + // message = "Le champ 'contact' est inconnu" + // } error = false break } @@ -153,7 +169,7 @@ async function importFileToDatabase(filePath) { // Si aucune erreur, insérer les données en batch if (error !== false) { // Utiliser transaction pour éviter une latence si l'insertion dépasse 100 - database.transaction(async () => { + database.transaction(() => { for (const row of records) { // Convert row.mention to uppercase and compare with ListMention.nom and ListMention.uniter (also converted to uppercase) const matchedMention = ListMention.find( @@ -166,62 +182,43 @@ async function importFileToDatabase(filePath) { if (matchedMention) { row.mention = matchedMention.id } - - const inscription = database.prepare( - 'SELECT id FROM etudiants WHERE num_inscription = ?' - ).get(row.num_inscription) - - // Check if the student already exists in the database - if (inscription) { - - updateEtudiant( - row.nom, - row.prenom, - getCompressedDefaultImage(), - convertToISODate(row.date_naissance), - row.niveau, - row.annee_scolaire, - getStatusMention(row.code_redoublement), - row.mention, - row.num_inscription, - inscription.id, // Assuming 'id' is the primary key of the student - row.sexe, - row.nationaliter, - row.cin, - row.date_de_delivrance ? convertToISODate(row.date_de_delivrance) : null, - row.annee_baccalaureat, - row.serie, - row.boursier, - row.domaine, - row.contact, - null + row.num_inscription = row.num_inscription.toString() + + ar.push(row) + console.log(ar.length, 'create'); + try { + let compare = row.num_inscription; + if (compare == oldNum) { + row.num_inscription = Number(row.num_inscription + 1); + } + console.log( + insertEtudiant( + row.nom, + row.prenom, + getCompressedDefaultImage(), + convertToISODate(row.date_naissance), + row.niveau, + row.annee_scolaire, + getStatusMention(row.code_redoublement), + row.num_inscription, + row.mention, + row.sexe, + row.nationaliter, + row.cin, + row.date_de_delivrance, + row.annee_baccalaureat, + row.serie, + row.boursier, + row.domaine, + row.contact, + null + ) ); + oldNum = compare + + } catch (error) { + console.log(error); - console.log(inscription) - - } else { - - // Insert the student data with the updated mention ID - insertEtudiant( - row.nom, - row.prenom, - getCompressedDefaultImage(), - convertToISODate(row.date_naissance), - row.niveau, - row.annee_scolaire, - getStatusMention(row.code_redoublement), - row.num_inscription, - row.mention, - row.sexe, - row.nationaliter, - row.cin, - row.date_de_delivrance, - row.annee_baccalaureat, - row.serie, - row.boursier, - row.domaine, - row.contact - ) } } })()