const { pool } = require('../database') const { matiereSystemReverse } = require('../function/System') const dayjs = require('dayjs') async function insertParcour(nom, uniter, mention_id) { const sql = 'INSERT INTO parcours (nom, uniter, mention_id) VALUES (?, ?, ?)' try { let [result] = await pool.query(sql, [nom, uniter, mention_id == null ? 0 : mention_id]) return { success: true, id: result.insertId } } catch (error) { return error } } async function getParcourMatiere(id) { const sql = `SELECT * FROM parcoursmatiere WHERE matiere_id = ?` try { const [rows] = await pool.query(sql, [id]) return rows } catch (error) { console.error(error) return error } } async function getParcours() { const sql = 'SELECT parcours.*, mentions.nom AS mention_nom FROM parcours LEFT JOIN mentions ON parcours.mention_id = mentions.id ORDER BY parcours.id DESC' try { let [rows] = await pool.query(sql) return rows } catch (error) { return error } } async function getSingleParcours(id) { const sql = 'SELECT * FROM parcours WHERE id = ?' try { let [rows] = await pool.query(sql, [id]) return rows[0] } catch (error) { return error } } async function deletes(id) { const sql = 'DELETE FROM parcours WHERE id = ?' try { let [result] = await pool.query(sql, [id]) if (result.affectedRows === 0) { return { success: false, message: 'Année Scolaire non trouvé.' } } return { success: true, message: 'Année Scolaire supprimé avec succès.' } } catch (error) { return error } } async function updateparcour(id, nom, uniter, mention_id) { const sql = 'UPDATE parcours SET nom = ?, uniter = ?, mention_id = ? WHERE id = ?' try { let [result] = await pool.query(sql, [nom, uniter, mention_id, id]) if (result.affectedRows === 0) { return { success: false, message: 'Année Scolaire non trouvé ou aucune modification effectuée.' } } return { success: true, message: 'Année Scolaire mis à jour avec succès.' } } catch (error) { return error } } async function parcourMatiere(matiere_id, parcour_ids) { try { // 1. Delete all existing relations for this matiere const deleteSql = `DELETE FROM parcoursmatiere WHERE matiere_id = ?` await pool.query(deleteSql, [matiere_id]) // 2. Insert new relations if (parcour_ids.length === 0) { return { success: true, message: 'No parcours to insert' } } const insertSql = ` INSERT INTO parcoursmatiere (matiere_id, parcour_id) VALUES ${parcour_ids.map(() => '(?, ?)').join(',')} ` // Flatten values like: [matiere_id, parcour_id1, matiere_id, parcour_id2, ...] const values = parcour_ids.flatMap((pid) => [matiere_id, pid]) const [result] = await pool.query(insertSql, values) return { success: true, affectedRows: result.affectedRows } } catch (error) { console.error(error) return { success: false, error: error.message } } } async function extractFiche(matiere_id) { const now = dayjs().format('YYYY') const connection = await pool.getConnection() try { await connection.beginTransaction() // Get semestre and mention const [matiereSemestre] = await connection.query( ` SELECT matiere_semestre.semestre_id, matiere_semestre.mention_id, semestres.* FROM matiere_semestre INNER JOIN semestres ON matiere_semestre.semestre_id = semestres.id WHERE matiere_semestre.matiere_id = ? `, [matiere_id] ) // Get parcours const [parcours] = await connection.query( ` SELECT * FROM parcoursmatiere INNER JOIN parcours ON parcoursmatiere.parcour_id = parcours.id WHERE parcoursmatiere.matiere_id = ? `, [matiere_id] ) let response = [] let allMention_id = [] for (let index = 0; index < matiereSemestre.length; index++) { response.push(await matiereSystemReverse(matiereSemestre[index].nom)) allMention_id.push(matiereSemestre[index].mention_id) } // Remove duplicates let newResponse = [] for (let index = 0; index < response.length; index++) { if (response[index] !== response[index + 1]) { newResponse.push(response[index]) } } // Get all students for the matched niveau and school year let allStudents = [] for (let index = 0; index < newResponse.length; index++) { const [students] = await connection.query( ` SELECT * FROM etudiants WHERE niveau LIKE ? AND annee_scolaire LIKE ? `, [`%${newResponse[index]}%`, `%${now}%`] ) allStudents.push(...students) } // Filter students by mention const studentFiltredMention = allStudents.filter((etudiant) => allMention_id.includes(etudiant.mention_id) ) // Filter by parcours let allData = [] for (let j = 0; j < parcours.length; j++) { for (let index = 0; index < studentFiltredMention.length; index++) { const currentStudent = studentFiltredMention[index] const parcoursName = parcours[j].nom const parcour_id = parcours[j].parcour_id if (parcour_id == 1) { if (currentStudent.parcours == null || currentStudent.parcours == parcoursName) { allData.push(currentStudent) } } else { if (currentStudent.parcours == parcoursName) { allData.push(currentStudent) } } } } await connection.commit() connection.release() return allData } catch (error) { await connection.rollback() connection.release() console.error(error) return { success: false, error: error.message } } } async function deleteParcours(id) { console.log("id: ", id); const sql = 'DELETE FROM parcours WHERE id = ?'; try { let [result] = await pool.query(sql, [id]); console.log("Résultat DELETE:", result); if (result.affectedRows === 0) { return { success: false, message: 'Parcours non trouvée.' }; } return { success: true, message: 'Matière supprimée avec succès.' }; } catch (error) { console.log("err: ",+ error) return { success: false, error: 'Erreur, veuillez réessayer: ' + error }; } } module.exports = { insertParcour, getParcours, getSingleParcours, deletes, updateparcour, parcourMatiere, extractFiche, deleteParcours, getParcourMatiere }