const { database } = require('../database') const { matiereSystemReverse } = require('../function/System') const dayjs = require('dayjs') async function insertParcour(nom, uniter, mention_id) { const query = database.prepare('INSERT INTO parcours (nom, uniter, mention_id) VALUES (?, ?, ?)') try { let response = query.run(nom, uniter, mention_id == null ? 0 : mention_id) return response } catch (error) { return error } } async function getParcourMatiere(id) { const parcourMatiereQuery = database.prepare('SELECT * FROM parcoursmatiere WHERE matiere_id = ?') try { let response return (response = await parcourMatiereQuery.all(id)) } catch (error) { return error } } async function getParcours() { const query = database.prepare('SELECT * FROM parcours ORDER BY id DESC') try { let response = query.all() return response } catch (error) { return error } } async function getSingleParcours(id) { const query = database.prepare('SELECT * FROM parcours WHERE id = ?') try { let response = await query.get(id) return response } catch (error) { return error } } async function deletes(id) { const query = database.prepare('DELETE FROM parcours WHERE id = ?') try { let response = await query.run(id) return response } catch (error) { return error } } async function updateparcour(id, nom, uniter, mention_id) { const query = database.prepare( 'UPDATE parcours SET nom = ?, uniter = ?, mention_id = ? WHERE id = ?' ) try { let response = await query.run(nom, uniter, mention_id, id) return response } catch (error) { return error } } async function parcourMatiere(matiere_id, parcour_id) { const query = database.prepare( 'INSERT INTO parcoursmatiere (matiere_id, parcour_id) VALUES (?, ?)' ) database.prepare('DELETE FROM parcoursmatiere WHERE matiere_id = ?').run(matiere_id) try { let response database.transaction(() => { for (let index = 0; index < parcour_id.length; index++) { response = query.run(matiere_id, parcour_id[index]) } })() return response } catch (error) { return error } } async function extractFiche(matiere_id) { const query = database.prepare( '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 = ?' ) const allStudentQuery = database.prepare( 'SELECT * FROM etudiants WHERE niveau LIKE ? AND annee_scolaire LIKE ?' ) const parcourMatiereQuery = database.prepare( 'SELECT * FROM parcoursmatiere INNER JOIN parcours ON (parcoursmatiere.parcour_id = parcours.id) WHERE parcoursmatiere.matiere_id = ?' ) try { let matiereSemestre = query.all(matiere_id) let response = [] let allSTudent = [] let allMention_id = [] let realSponse = [] let now = dayjs().format('YYYY') realSponse = await database.transaction(async () => { let parcours = parcourMatiereQuery.all(Number(matiere_id)) for (let index = 0; index < matiereSemestre.length; index++) { response.push(await matiereSystemReverse(matiereSemestre[index].nom)) allMention_id.push(matiereSemestre[index].mention_id) } let newResponse = [] for (let index = 0; index < response.length; index++) { if (response[index] != response[index + 1]) { newResponse.push(response[index]) } } for (let index = 0; index < newResponse.length; index++) { allSTudent = allStudentQuery.all(`%${newResponse[index]}%`, `%${now}%`) } let studentFiltredMention = [] for (let index = 0; index < allSTudent.length; index++) { if (allMention_id.includes(allSTudent[index].mention_id)) { studentFiltredMention.push(allSTudent[index]) } } let allData = [] for (let j = 0; j < parcours.length; j++) { for (let index = 0; index < studentFiltredMention.length; index++) { if (parcours[j].parcour_id == 1) { if ( studentFiltredMention[index].parcours == null || studentFiltredMention[index].parcours == parcours[j].nom ) { allData.push(studentFiltredMention[index]) } } else { if (studentFiltredMention[index].parcours == parcours[j].nom) { allData.push(studentFiltredMention[index]) } } } } return allData })() return realSponse } catch (error) { return error } } module.exports = { insertParcour, getParcours, getSingleParcours, deletes, updateparcour, parcourMatiere, extractFiche, getParcourMatiere }