You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

231 lines
5.8 KiB

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 * FROM parcours ORDER BY 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 universitaire non trouvé.'
}
}
return {
success: true,
message: 'Année universitaire 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 universitaire non trouvé ou aucune modification effectuée.'
}
}
return {
success: true,
message: 'Année universitaire 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 }
}
}
module.exports = {
insertParcour,
getParcours,
getSingleParcours,
deletes,
updateparcour,
parcourMatiere,
extractFiche,
getParcourMatiere
}