40 changed files with 2155 additions and 1203 deletions
@ -0,0 +1,305 @@ |
|||
const { database } = require('../database.backup') |
|||
|
|||
/** |
|||
* function to insert etudiant into databases |
|||
*/ |
|||
async function insertEtudiant( |
|||
nom, |
|||
prenom, |
|||
photos, |
|||
date_de_naissances, |
|||
niveau, |
|||
annee_scolaire, |
|||
status, |
|||
num_inscription, |
|||
mention_id, |
|||
sexe, |
|||
nationaliter, |
|||
cin, |
|||
date_delivrence, |
|||
annee_bacc, |
|||
serie, |
|||
boursier, |
|||
domaine, |
|||
contact, |
|||
parcours |
|||
) { |
|||
const query = database.prepare( |
|||
'INSERT INTO etudiants (nom, prenom, photos, date_de_naissances, niveau, annee_scolaire, status, mention_id, num_inscription, sexe, cin, date_delivrance, nationalite, annee_bacc, serie, boursier, domaine, contact, parcours) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' |
|||
) |
|||
|
|||
try { |
|||
let response = await query.run( |
|||
nom, |
|||
prenom, |
|||
photos, |
|||
date_de_naissances, |
|||
niveau, |
|||
annee_scolaire, |
|||
status, |
|||
mention_id, |
|||
num_inscription, |
|||
sexe, |
|||
cin, |
|||
date_delivrence, |
|||
nationaliter, |
|||
annee_bacc, |
|||
serie, |
|||
boursier, |
|||
domaine, |
|||
contact, |
|||
parcours |
|||
) |
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* function to get all etudiants |
|||
* |
|||
* @returns JSON |
|||
*/ |
|||
async function getAllEtudiants() { |
|||
const query = database.prepare('SELECT * FROM etudiants ORDER BY annee_scolaire DESC') |
|||
try { |
|||
let response = await query.all() |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* function to return a single etudiant |
|||
* and display it on the screen |
|||
* |
|||
* @param {int} id |
|||
* @returns Promise |
|||
*/ |
|||
async function getSingleEtudiant(id) { |
|||
const query = database.prepare('SELECT * FROM etudiants WHERE id = ?') |
|||
|
|||
try { |
|||
const etudiants = await query.get(id) |
|||
|
|||
if (etudiants) { |
|||
return etudiants |
|||
} else { |
|||
return { message: 'etudiants pas trouver' } |
|||
} |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* function to get all etudiants M2 |
|||
* |
|||
* @returns JSON |
|||
*/ |
|||
async function FilterDataByNiveau(niveau) { |
|||
const query = database.prepare( |
|||
'SELECT * FROM etudiants WHERE niveau = ? ORDER BY annee_scolaire DESC' |
|||
) |
|||
try { |
|||
let response = await query.all(niveau) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* function to update etudiants |
|||
* |
|||
* @param {*} nom |
|||
* @param {*} prenom |
|||
* @param {*} photos |
|||
* @param {*} date_de_naissances |
|||
* @param {*} niveau |
|||
* @param {*} annee_scolaire |
|||
* @param {*} status |
|||
* @param {*} num_inscription |
|||
* @param {*} id |
|||
* @returns promise |
|||
*/ |
|||
async function updateEtudiant( |
|||
nom, |
|||
prenom, |
|||
photos, |
|||
date_de_naissances, |
|||
niveau, |
|||
annee_scolaire, |
|||
status, |
|||
mention_id, |
|||
num_inscription, |
|||
id, |
|||
sexe, |
|||
nationalite, |
|||
cin, |
|||
date_delivrence, |
|||
annee_bacc, |
|||
serie, |
|||
boursier, |
|||
domaine, |
|||
contact, |
|||
parcours |
|||
) { |
|||
const query = database.prepare( |
|||
'UPDATE etudiants SET nom = ?, prenom = ?, photos = ?, date_de_naissances = ?, niveau = ?, annee_scolaire = ?, status = ?, mention_id = ?, num_inscription = ?, sexe = ?, cin = ?, date_delivrance = ?, nationalite = ?, annee_bacc = ?, serie = ?, boursier = ?, domaine = ?, contact = ?, parcours = ? WHERE id = ?' |
|||
) |
|||
|
|||
try { |
|||
let response = await query.run( |
|||
nom, |
|||
prenom, |
|||
photos, |
|||
date_de_naissances, |
|||
niveau, |
|||
annee_scolaire, |
|||
status, |
|||
mention_id, |
|||
num_inscription, |
|||
sexe, |
|||
cin, |
|||
date_delivrence, |
|||
nationalite, |
|||
annee_bacc, |
|||
serie, |
|||
boursier, |
|||
domaine, |
|||
contact, |
|||
parcours, |
|||
id |
|||
) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* function to return the needed data in dashboard |
|||
* |
|||
* @returns promise |
|||
*/ |
|||
async function getDataToDashboard() { |
|||
const query = database.prepare('SELECT * FROM niveaus') |
|||
const query2 = database.prepare('SELECT * FROM etudiants') |
|||
const query3 = database.prepare('SELECT DISTINCT annee_scolaire FROM etudiants') // get all année scolaire sans doublan
|
|||
|
|||
try { |
|||
let niveau = query.all() |
|||
let etudiants = query2.all() |
|||
let anne_scolaire = query3.all() |
|||
|
|||
return { niveau, etudiants, anne_scolaire } |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function changePDP(photos, id) { |
|||
const query = database.prepare('UPDATE etudiants SET photos = ? WHERE id = ?') |
|||
|
|||
try { |
|||
let response = await query.run(photos, id) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function updateParcours(parcours, id) { |
|||
const query = database.prepare('UPDATE etudiants SET parcours = ? WHERE id = ?') |
|||
|
|||
try { |
|||
let response = await query.run(parcours, id) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function createTranche(etudiant_id, tranchename, montant) { |
|||
const query = database.prepare( |
|||
'INSERT INTO trancheecolage (etudiant_id, tranchename, montant) VALUES (?, ?, ?)' |
|||
) |
|||
|
|||
try { |
|||
let response = query.run(etudiant_id, tranchename, montant) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function getTranche(id) { |
|||
const query = database.prepare('SELECT * FROM trancheecolage WHERE etudiant_id = ?') |
|||
|
|||
try { |
|||
let response = query.all(id) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function updateTranche(id, tranchename, montant) { |
|||
const query = database.prepare( |
|||
'UPDATE trancheecolage SET tranchename = ?, montant = ? WHERE id = ?' |
|||
) |
|||
|
|||
try { |
|||
let response = query.run(tranchename, montant, id) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function deleteTranche(id) { |
|||
const query = database.prepare('DELETE FROM trancheecolage WHERE id = ?') |
|||
|
|||
try { |
|||
let response = query.run(id) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function getSingleTranche(id) { |
|||
try { |
|||
return await database.prepare('SELECT * FROM trancheecolage WHERE id = ?').get(id) |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
module.exports = { |
|||
insertEtudiant, |
|||
getAllEtudiants, |
|||
FilterDataByNiveau, |
|||
getSingleEtudiant, |
|||
updateEtudiant, |
|||
getDataToDashboard, |
|||
changePDP, |
|||
updateParcours, |
|||
createTranche, |
|||
getTranche, |
|||
updateTranche, |
|||
deleteTranche, |
|||
getSingleTranche |
|||
} |
|||
@ -0,0 +1,354 @@ |
|||
const { database } = require('../database.backup') |
|||
const { matiereSysteme } = require('../function/System') |
|||
const { convertArrayAndString } = require('../function/StringArrayConvertion') |
|||
|
|||
/** |
|||
* function uset to create matiere |
|||
* @param {*} nom |
|||
* @returns Promise |
|||
*/ |
|||
async function createMatiere(nom, credit, uniter, ue) { |
|||
const query = database.prepare( |
|||
'INSERT INTO matieres (nom, unite_enseignement, credit, heure, ue) VALUES (?, ?, ?, ?, ?)' |
|||
) |
|||
|
|||
const uniterHeure = database.prepare('SELECT uniter_heure FROM nessesaryTable').get() |
|||
const heure = credit * uniterHeure.uniter_heure |
|||
|
|||
try { |
|||
response = await query.run(nom, uniter, credit, heure, ue) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* function to get all matieres |
|||
* @returns Promise |
|||
*/ |
|||
async function getMatiere() { |
|||
const query = database.prepare('SELECT * FROM matieres ORDER BY id DESC') |
|||
|
|||
try { |
|||
let response = await query.all() |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function displayMatiereFromForm(niveau, mention_id, parcours) { |
|||
// Fetch the semestre array
|
|||
let semestre = await matiereSysteme(niveau) // Ensure this returns an array with at least 2 items
|
|||
|
|||
if (niveau !== 'L1') { |
|||
if (semestre.length < 2) { |
|||
console.error('Error: Semestre array does not contain enough elements.') |
|||
return |
|||
} |
|||
|
|||
// Prepare the query
|
|||
let matiereQuery = database.prepare(` |
|||
SELECT DISTINCT m.* |
|||
FROM matieres m |
|||
JOIN matiere_semestre ms ON m.id = ms.matiere_id |
|||
JOIN semestres s ON ms.semestre_id = s.id |
|||
JOIN parcoursmatiere pm ON m.id = pm.matiere_id |
|||
JOIN parcours p ON pm.parcour_id = p.id |
|||
WHERE (s.nom LIKE ? OR s.nom LIKE ?) |
|||
AND ms.mention_id = ? |
|||
AND p.nom = ? |
|||
`)
|
|||
|
|||
try { |
|||
// Execute the query with parameters
|
|||
let response = matiereQuery.all(`%${semestre[0]}%`, `%${semestre[1]}%`, mention_id, parcours) |
|||
|
|||
console.log(response) |
|||
// Log the response
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} else { |
|||
if (semestre.length < 2) { |
|||
console.error('Error: Semestre array does not contain enough elements.') |
|||
return |
|||
} |
|||
|
|||
// Prepare the query
|
|||
let matiereQuery = database.prepare(` |
|||
SELECT DISTINCT m.* |
|||
FROM matieres m |
|||
JOIN matiere_semestre ms ON m.id = ms.matiere_id |
|||
JOIN semestres s ON ms.semestre_id = s.id |
|||
WHERE (s.nom LIKE ? OR s.nom LIKE ?) |
|||
AND ms.mention_id = ? |
|||
`)
|
|||
|
|||
try { |
|||
// Execute the query with parameters
|
|||
let response = matiereQuery.all(`%${semestre[0]}%`, `%${semestre[1]}%`, mention_id) |
|||
console.log(response) |
|||
// Log the response
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* function to get single matiere |
|||
* @param {*} id |
|||
* @returns promise |
|||
*/ |
|||
async function getSingleMatiere(id) { |
|||
const query = await database.prepare('SELECT * FROM matieres WHERE id = ?') |
|||
|
|||
try { |
|||
let response = query.get(id) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* function used when updating matiere |
|||
* @param {*} nom |
|||
* @param {*} id |
|||
* @returns promise |
|||
*/ |
|||
async function updateMatiere(nom, id, credit, uniter, ue) { |
|||
const query = database.prepare( |
|||
'UPDATE matieres SET nom = ?, credit = ?, unite_enseignement = ?, heure = ?, ue = ? WHERE id = ?' |
|||
) |
|||
|
|||
const uniterHeure = await database.prepare('SELECT uniter_heure FROM nessesaryTable').get() |
|||
const heure = credit * uniterHeure.uniter_heure |
|||
|
|||
try { |
|||
response = await query.run(nom, credit, uniter, heure, ue, id) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function deleteMatiere(id) { |
|||
const query = database.prepare('DELETE FROM matieres WHERE id = ?') |
|||
|
|||
try { |
|||
let response = query.run(id) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function asygnationToMention(formData, id) { |
|||
const clearQuery = database.prepare('DELETE FROM matiere_mention WHERE matiere_id = ?') |
|||
const query = database.prepare( |
|||
'INSERT INTO matiere_mention (matiere_id, mention_id) VALUES (?,?)' |
|||
) |
|||
const selectedKeys = Object.keys(formData).filter((key) => formData[key]) |
|||
const placeholders = selectedKeys.map(() => '?').join(',') |
|||
// Prepare the query with placeholders
|
|||
const clearSemestreMentionQuery = database.prepare( |
|||
`DELETE FROM matiere_semestre WHERE matiere_id = ? AND mention_id NOT IN (${placeholders})` |
|||
) |
|||
|
|||
const clearNoreQuery = database.prepare( |
|||
`DELETE FROM notes WHERE matiere_id = ? AND mention_id NOT IN (${placeholders})` |
|||
) |
|||
|
|||
try { |
|||
let response |
|||
await clearQuery.run(id) |
|||
await clearNoreQuery.run(id, ...selectedKeys) |
|||
clearSemestreMentionQuery.run(id, ...selectedKeys) |
|||
// use transaction for speed execution
|
|||
database.transaction(() => { |
|||
for (let index = 0; index < selectedKeys.length; index++) { |
|||
response = query.run(id, selectedKeys[index]) |
|||
} |
|||
})() |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function getMentionMatiere(id) { |
|||
const query = database.prepare('SELECT * FROM matiere_mention WHERE matiere_id = ?') |
|||
|
|||
try { |
|||
let response = await query.all(id) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function getMentionMatiereChecked(matiere_id) { |
|||
const getMentionMatiere = database.prepare('SELECT * FROM matiere_mention WHERE matiere_id = ?') |
|||
let MentionArray = await getMentionMatiere.all(matiere_id) |
|||
let arrayID = [] |
|||
|
|||
for (let index = 0; index < MentionArray.length; index++) { |
|||
arrayID.push(MentionArray[index].mention_id) |
|||
} |
|||
|
|||
const mentionQuery = database.prepare( |
|||
`SELECT * FROM mentions WHERE id IN (${arrayID.map(() => '?').join(', ')})` |
|||
) |
|||
|
|||
try { |
|||
const results = await mentionQuery.all(...arrayID) |
|||
|
|||
return results |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function getSemestreMatiere(id) { |
|||
const query = database.prepare('SELECT * FROM matiere_semestre WHERE matiere_id = ?') |
|||
|
|||
try { |
|||
let response = await query.all(id) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function getSemestre() { |
|||
const query = database.prepare('SELECT * FROM semestres') |
|||
|
|||
try { |
|||
let response = await query.all() |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function insertUpdateMentionSemestre(matiere_id, formData) { |
|||
const clearQuery = database.prepare('DELETE FROM matiere_semestre WHERE matiere_id = ?') |
|||
clearQuery.run(matiere_id) |
|||
|
|||
const query = database.prepare( |
|||
'INSERT INTO matiere_semestre (matiere_id, semestre_id, mention_id) VALUES (?, ?, ?)' |
|||
) |
|||
let response |
|||
database.transaction(() => { |
|||
for (const key in formData) { |
|||
if (formData.hasOwnProperty(key)) { |
|||
for (let jindex = 0; jindex < formData[key].length; jindex++) { |
|||
response = query.run(matiere_id, formData[key][jindex], key) |
|||
} |
|||
} |
|||
} |
|||
})() |
|||
return response |
|||
} |
|||
|
|||
async function getEnseignants() { |
|||
const getIdQuery = database.prepare( |
|||
'SELECT id FROM matiereEnseignants GROUP BY matiere_id ORDER BY MAX(date) DESC' |
|||
) |
|||
|
|||
const query = database.prepare( |
|||
'SELECT * FROM matiereEnseignants WHERE id IN (' + |
|||
new Array(getIdQuery.all().length).fill('?').join(',') + |
|||
')' |
|||
) |
|||
|
|||
try { |
|||
// Get the latest `id` for each `matiere_id`
|
|||
const latestIds = getIdQuery.all().map((row) => row.id) |
|||
|
|||
// If no ids exist, return an empty array
|
|||
if (latestIds.length === 0) { |
|||
return [] |
|||
} |
|||
|
|||
// Fetch the full details using the filtered IDs
|
|||
let response = query.all(...latestIds) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function insertNewProf(matiere_id, nom, prenom, contact, date) { |
|||
const query = database.prepare( |
|||
'INSERT INTO matiereEnseignants (matiere_id, nom_enseignant, prenom_enseignant, contact, date) VALUES (?, ?, ?, ?, ?)' |
|||
) |
|||
|
|||
try { |
|||
let response = query.run(matiere_id, nom, prenom, contact, date) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function getSIngleProf(id) { |
|||
try { |
|||
const prof = await database |
|||
.prepare('SELECT * FROM matiereEnseignants WHERE matiere_id = ?') |
|||
.get(id) |
|||
|
|||
return prof |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
async function updateProf(matiere_id, nom, prenom, contact, date) { |
|||
const query = database.prepare( |
|||
'UPDATE matiereEnseignants SET nom_enseignant = ?, prenom_enseignant = ?, contact = ?, date = ? WHERE matiere_id = ?' |
|||
) |
|||
|
|||
try { |
|||
let response = query.run(nom, prenom, contact, date, matiere_id) |
|||
|
|||
return response |
|||
} catch (error) { |
|||
return error |
|||
} |
|||
} |
|||
|
|||
module.exports = { |
|||
createMatiere, |
|||
getSIngleProf, |
|||
getMatiere, |
|||
getSingleMatiere, |
|||
updateMatiere, |
|||
displayMatiereFromForm, |
|||
deleteMatiere, |
|||
asygnationToMention, |
|||
getMentionMatiere, |
|||
getMentionMatiereChecked, |
|||
getSemestreMatiere, |
|||
getSemestre, |
|||
insertUpdateMentionSemestre, |
|||
getEnseignants, |
|||
insertNewProf, |
|||
updateProf |
|||
} |
|||
File diff suppressed because it is too large
Loading…
Reference in new issue