const { database } = require('../database') const dayjs = require('dayjs') async function updateCurrentYears() { const fullDate = dayjs().format('YYYY-MM-DD') // Clear current year flag const clearCurrent = database.prepare('UPDATE anneescolaire SET is_Current = 0 WHERE id > 0') clearCurrent.run() // Set the new current year const updateCurrent = database.prepare(` UPDATE anneescolaire SET is_Current = 1 WHERE ? >= debut AND ? <= fin `) // console.log(); updateCurrent.run(fullDate, fullDate) // Check if the update was successful const check = database .prepare( ` SELECT * FROM anneescolaire WHERE ? >= debut AND ? <= fin ` ) .get(fullDate, fullDate) // Insert into traitmentsystem if a current year exists if (check) { let search = database.prepare('SELECT * FROM traitmentsystem WHERE code = ?').get(check.code) console.log(search) if (!search) { const insertQuery = database.prepare(` INSERT INTO traitmentsystem (code, debut, fin) VALUES (?, ?, ?) `) insertQuery.run(check.code, check.debut, check.fin) } } else { console.log('No active school year found for the current date.') } } async function updateStudents() { const getInfinishedYears = database .prepare('SELECT * FROM traitmentsystem WHERE is_finished = 0 ORDER BY id ASC') .get() const allEtudiants = database .prepare('SELECT * FROM etudiants WHERE annee_scolaire = ?') .all(getInfinishedYears.code) function checkNull(params) { if (params == null || params == undefined) { return null } return params } function compareSessionNotes(session1, session2) { let notes if (session2) { if (session1 < session2.note) { notes = session2.note } else { notes = session1 } } else { notes = session1 } return notes } database.transaction(() => { // get all note of student const queryNotes = database.prepare( `SELECT DISTINCT etudiant_id FROM notes WHERE etudiant_niveau = ? AND annee_scolaire = ?` ) let allEtudiantWithNotes = [] let etudiantWithNotes = [] let dataToMap = [] let allEtudiantWithNotesRepech = [] let etudiantWithNotesRepech = [] for (const etudiant of allEtudiants) { const results = queryNotes.all(etudiant.niveau, etudiant.annee_scolaire) etudiantWithNotes.push(...results) // Avoid nested arrays } const uniqueId = etudiantWithNotes.filter( (item, index, self) => index === self.findIndex((t) => t.etudiant_id === item.etudiant_id) ) const query2 = database.prepare( 'SELECT notes.*, etudiants.*, matieres.id, matieres.nom AS nomMat, matieres.credit FROM notes LEFT JOIN etudiants ON (notes.etudiant_id = etudiants.id) LEFT JOIN matieres ON (notes.matiere_id = matieres.id) WHERE notes.etudiant_id = ?' ) for (let j = 0; j < uniqueId.length; j++) { allEtudiantWithNotes.push(query2.all(uniqueId[j].etudiant_id)) } const query = database.prepare( `SELECT DISTINCT etudiant_id FROM notesrepech WHERE etudiant_niveau = ? AND annee_scolaire = ?` ) for (const etudiant of allEtudiants) { const results = query.all(etudiant.niveau, etudiant.annee_scolaire) etudiantWithNotesRepech.push(...results) // Avoid nested arrays } const uniqueIdRepech = etudiantWithNotes.filter( (item, index, self) => index === self.findIndex((t) => t.etudiant_id === item.etudiant_id) ) const query2Repech = database.prepare( 'SELECT notesrepech.*, etudiants.*, matieres.id, matieres.nom AS nomMat, matieres.credit FROM notesrepech INNER JOIN etudiants ON (notesrepech.etudiant_id = etudiants.id) INNER JOIN matieres ON (notesrepech.matiere_id = matieres.id) WHERE notesrepech.etudiant_id = ?' ) for (let j = 0; j < uniqueIdRepech.length; j++) { allEtudiantWithNotesRepech.push(query2Repech.all(uniqueIdRepech[j].etudiant_id)) } for (let index = 0; index < allEtudiantWithNotes.length; index++) { let total = 0 let note = 0 let totalCredit = 0 // Create a new object for each student let modelJson = { id: '', nom: '', prenom: '', photos: '', moyenne: '', mention: '', niveau: '', annee_scolaire: '' } for (let j = 0; j < allEtudiantWithNotes[index].length; j++) { modelJson.id = allEtudiantWithNotes[index][j].etudiant_id modelJson.nom = allEtudiantWithNotes[index][j].nom modelJson.prenom = allEtudiantWithNotes[index][j].prenom modelJson.photos = allEtudiantWithNotes[index][j].photos modelJson.mention = allEtudiantWithNotes[index][j].mention_id modelJson.niveau = allEtudiantWithNotes[index][j].niveau modelJson.annee_scolaire = allEtudiantWithNotes[index][j].annee_scolaire // console.log(checkNull(session[index][j])); if (allEtudiantWithNotesRepech[index]) { note += compareSessionNotes( allEtudiantWithNotes[index][j].note, checkNull(allEtudiantWithNotesRepech[index][j]) ) * allEtudiantWithNotes[index][j].credit } else { note += allEtudiantWithNotes[index][j].note * allEtudiantWithNotes[index][j].credit } totalCredit += allEtudiantWithNotes[index][j].credit } total = note / totalCredit modelJson.moyenne = total.toFixed(2) // Add the new object to the array dataToMap.push(modelJson) } // update all etudiant let updated = false if (dataToMap.length != 0) { let noteSystem = database.prepare('SELECT * FROM notesystems').get() for (let index = 0; index < dataToMap.length; index++) { if (dataToMap[index].moyenne >= noteSystem.admis) { let updateQuery = database.prepare( 'UPDATE etudiants SET niveau = ?, annee_scolaire = ?, status = ? WHERE id = ?' ) updateQuery.run( nextLevel(dataToMap[index].niveau), updateSchoolYear(dataToMap[index].annee_scolaire), 2, dataToMap[index].id ) updated = true } else if ( dataToMap[index].moyenne < noteSystem.admis && dataToMap[index].moyenne >= noteSystem.redouble ) { let updateQuery = database.prepare( 'UPDATE etudiants SET niveau = ?, annee_scolaire = ? status = ? WHERE id = ?' ) updateQuery.run( dataToMap[index].niveau, updateSchoolYear(dataToMap[index].annee_scolaire), 3, dataToMap[index].id ) updated = true } else { let updateQuery = database.prepare( 'UPDATE etudiants SET niveau = ?, annee_scolaire = ? status = ? WHERE id = ?' ) updateQuery.run( dataToMap[index].niveau, dataToMap[index].annee_scolaire, 4, dataToMap[index].id ) updated = true } } } if (updated) { const updateInfinishedYears = database.prepare( 'UPDATE traitmentsystem SET is_finished = 1 WHERE id = ?' ) updateInfinishedYears.run(getInfinishedYears.id) } })() } function nextLevel(niveau) { if (niveau == 'L1') { return 'L2' } else if (niveau == 'L2') { return 'L3' } else if (niveau == 'L3') { return 'M1' } else if (niveau == 'M1') { return 'M2' } else if (niveau == 'M2') { return 'D1' } else if (niveau == 'D1') { return 'D2' } else if (niveau == 'D2') { return 'D3' } else if (niveau == 'D3') { return 'PHD' } } function updateSchoolYear(year) { // Split the year into two parts const [startYear, endYear] = year.split('-').map(Number) // Increment both the start and end year by 1 const newStartYear = startYear + 1 const newEndYear = endYear + 1 // Join the new years with a hyphen const newYear = `${newStartYear}-${newEndYear}` return newYear } async function matiereSysteme(etudiant_niveau) { let systeme if (etudiant_niveau == 'L1') { systeme = ['S1', 'S2'] } else if (etudiant_niveau == 'L2') { systeme = ['S3', 'S4'] } else if (etudiant_niveau == 'L3') { systeme = ['S5', 'S6'] } else if (etudiant_niveau == 'M1') { systeme = ['S7', 'S8'] } else if (etudiant_niveau == 'M2') { systeme = ['S9', 'S10'] } else if (etudiant_niveau == 'D1') { systeme = ['S11', 'S12'] } else if (etudiant_niveau == 'D2') { systeme = ['S13', 'S14'] } else if (etudiant_niveau == 'D3') { systeme = ['S15', 'S16'] } return systeme } async function matiereSystemReverse(semestre) { if (semestre == 'S1' || semestre == 'S2') { return 'L1' } else if (semestre == 'S3' || semestre == 'S4') { return 'L2' } else if (semestre == 'S5' || semestre == 'S6') { return 'L3' } else if (semestre == 'S7' || semestre == 'S8') { return 'M1' } else if (semestre == 'S9' || semestre == 'S10') { return 'M2' } else if (semestre == 'S11' || semestre == 'S12') { return 'D1' } else if (semestre == 'S13' || semestre == 'S14') { return 'D2' } else if (semestre == 'S15' || semestre == 'S16') { return 'D3' } } async function getNessesarytable() { try { const query = await database.prepare('SELECT * FROM nessesaryTable').get() return query } catch (error) { return error } } async function updateNessesaryTable(id, multiplicateur) { const query = database.prepare('UPDATE nessesaryTable SET uniter_heure = ? WHERE id = ?') try { let update = query.run(multiplicateur, id) return update } catch (error) { return error } } module.exports = { matiereSysteme, updateCurrentYears, updateStudents, getNessesarytable, updateNessesaryTable, matiereSystemReverse }