|
|
|
@ -3,12 +3,13 @@ const path = require('path') |
|
|
|
const XLSX = require('xlsx') |
|
|
|
const { getCompressedDefaultImage } = require('../function/GetImageDefaault') |
|
|
|
const { parse } = require('csv-parse/sync') |
|
|
|
const { insertEtudiant, updateEtudiant } = require('../Models/Etudiants') |
|
|
|
const { insertEtudiant } = require('../Models/Etudiants') |
|
|
|
const { database } = require('../database') |
|
|
|
const { getMentions } = require('../Models/Mentions') |
|
|
|
const dayjs = require('dayjs') |
|
|
|
const { getStatusMention } = require('../function/Helper') |
|
|
|
const customParseFormat = require('dayjs/plugin/customParseFormat') |
|
|
|
// const customParseFormatt = require('dayjs/plu')
|
|
|
|
dayjs.extend(customParseFormat) |
|
|
|
|
|
|
|
// Function to convert any date format to 'YYYY-MM-DD'
|
|
|
|
@ -21,7 +22,8 @@ function convertToISODate(input) { |
|
|
|
'DD-MM-YYYY', |
|
|
|
'MM-DD-YYYY', |
|
|
|
'DD/MM/YY', |
|
|
|
'MM/DD/YY' |
|
|
|
'MM/DD/YY', |
|
|
|
'DD-MMM-YY' |
|
|
|
] |
|
|
|
const parsedDate = dayjs(input, formats, true) // Strict parsing to ensure formats are matched correctly
|
|
|
|
|
|
|
|
@ -30,6 +32,12 @@ function convertToISODate(input) { |
|
|
|
return parsedDate.format('YYYY-MM-DD') |
|
|
|
} |
|
|
|
|
|
|
|
// Handle cases like "Vers 2000"
|
|
|
|
const versMatch = typeof input === 'string' && input.match(/vers\s*(\d{4})/i); |
|
|
|
if (versMatch) { |
|
|
|
return `${versMatch[1]}-01-01`; // Return in ISO format
|
|
|
|
} |
|
|
|
|
|
|
|
function excelDateToJSDate(serial) { |
|
|
|
const utc_days = Math.floor(serial - 25569); // days from Jan 1, 1970
|
|
|
|
const utc_value = utc_days * 86400; // seconds in a day
|
|
|
|
@ -75,11 +83,17 @@ async function importFileToDatabase(filePath) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// ✅ Count number of data rows
|
|
|
|
const numberOfLines = records.length; |
|
|
|
console.log(`Number of data rows: ${numberOfLines}`); |
|
|
|
|
|
|
|
try { |
|
|
|
let error = true |
|
|
|
let message = '' |
|
|
|
|
|
|
|
// Vérifier les données en une seule boucle
|
|
|
|
let ar = []; |
|
|
|
let oldNum = ''; |
|
|
|
for (const row of records) { |
|
|
|
if ( |
|
|
|
!row.nom || |
|
|
|
@ -97,8 +111,9 @@ async function importFileToDatabase(filePath) { |
|
|
|
!row.serie || |
|
|
|
!row.code_redoublement || |
|
|
|
!row.boursier || |
|
|
|
!row.domaine || |
|
|
|
!row.contact |
|
|
|
!row.domaine |
|
|
|
// ||
|
|
|
|
// !row.contact
|
|
|
|
) { |
|
|
|
if (!row.nom) { |
|
|
|
message = "Le champ 'nom' est inconnu" |
|
|
|
@ -136,9 +151,10 @@ async function importFileToDatabase(filePath) { |
|
|
|
message = "Le champ 'boursier' est inconnu" |
|
|
|
} else if (!row.domaine) { |
|
|
|
message = "Le champ 'domaine' est inconnu" |
|
|
|
} else if (!row.contact) { |
|
|
|
message = "Le champ 'contact' est inconnu" |
|
|
|
} |
|
|
|
// else if (!row.contact) {
|
|
|
|
// message = "Le champ 'contact' est inconnu"
|
|
|
|
// }
|
|
|
|
error = false |
|
|
|
break |
|
|
|
} |
|
|
|
@ -153,7 +169,7 @@ async function importFileToDatabase(filePath) { |
|
|
|
// Si aucune erreur, insérer les données en batch
|
|
|
|
if (error !== false) { |
|
|
|
// Utiliser transaction pour éviter une latence si l'insertion dépasse 100
|
|
|
|
database.transaction(async () => { |
|
|
|
database.transaction(() => { |
|
|
|
for (const row of records) { |
|
|
|
// Convert row.mention to uppercase and compare with ListMention.nom and ListMention.uniter (also converted to uppercase)
|
|
|
|
const matchedMention = ListMention.find( |
|
|
|
@ -166,62 +182,43 @@ async function importFileToDatabase(filePath) { |
|
|
|
if (matchedMention) { |
|
|
|
row.mention = matchedMention.id |
|
|
|
} |
|
|
|
|
|
|
|
const inscription = database.prepare( |
|
|
|
'SELECT id FROM etudiants WHERE num_inscription = ?' |
|
|
|
).get(row.num_inscription) |
|
|
|
|
|
|
|
// Check if the student already exists in the database
|
|
|
|
if (inscription) { |
|
|
|
|
|
|
|
updateEtudiant( |
|
|
|
row.nom, |
|
|
|
row.prenom, |
|
|
|
getCompressedDefaultImage(), |
|
|
|
convertToISODate(row.date_naissance), |
|
|
|
row.niveau, |
|
|
|
row.annee_scolaire, |
|
|
|
getStatusMention(row.code_redoublement), |
|
|
|
row.mention, |
|
|
|
row.num_inscription, |
|
|
|
inscription.id, // Assuming 'id' is the primary key of the student
|
|
|
|
row.sexe, |
|
|
|
row.nationaliter, |
|
|
|
row.cin, |
|
|
|
row.date_de_delivrance ? convertToISODate(row.date_de_delivrance) : null, |
|
|
|
row.annee_baccalaureat, |
|
|
|
row.serie, |
|
|
|
row.boursier, |
|
|
|
row.domaine, |
|
|
|
row.contact, |
|
|
|
null |
|
|
|
row.num_inscription = row.num_inscription.toString() |
|
|
|
|
|
|
|
ar.push(row) |
|
|
|
console.log(ar.length, 'create'); |
|
|
|
try { |
|
|
|
let compare = row.num_inscription; |
|
|
|
if (compare == oldNum) { |
|
|
|
row.num_inscription = Number(row.num_inscription + 1); |
|
|
|
} |
|
|
|
console.log( |
|
|
|
insertEtudiant( |
|
|
|
row.nom, |
|
|
|
row.prenom, |
|
|
|
getCompressedDefaultImage(), |
|
|
|
convertToISODate(row.date_naissance), |
|
|
|
row.niveau, |
|
|
|
row.annee_scolaire, |
|
|
|
getStatusMention(row.code_redoublement), |
|
|
|
row.num_inscription, |
|
|
|
row.mention, |
|
|
|
row.sexe, |
|
|
|
row.nationaliter, |
|
|
|
row.cin, |
|
|
|
row.date_de_delivrance, |
|
|
|
row.annee_baccalaureat, |
|
|
|
row.serie, |
|
|
|
row.boursier, |
|
|
|
row.domaine, |
|
|
|
row.contact, |
|
|
|
null |
|
|
|
) |
|
|
|
); |
|
|
|
oldNum = compare |
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
console.log(error); |
|
|
|
|
|
|
|
console.log(inscription) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// Insert the student data with the updated mention ID
|
|
|
|
insertEtudiant( |
|
|
|
row.nom, |
|
|
|
row.prenom, |
|
|
|
getCompressedDefaultImage(), |
|
|
|
convertToISODate(row.date_naissance), |
|
|
|
row.niveau, |
|
|
|
row.annee_scolaire, |
|
|
|
getStatusMention(row.code_redoublement), |
|
|
|
row.num_inscription, |
|
|
|
row.mention, |
|
|
|
row.sexe, |
|
|
|
row.nationaliter, |
|
|
|
row.cin, |
|
|
|
row.date_de_delivrance, |
|
|
|
row.annee_baccalaureat, |
|
|
|
row.serie, |
|
|
|
row.boursier, |
|
|
|
row.domaine, |
|
|
|
row.contact |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
})() |
|
|
|
|