Trying to make EditStudents works

This commit is contained in:
Clayzxr
2025-01-24 21:38:53 +01:00
parent fcc0a4413c
commit 16b7579e10
+25 -15
View File
@@ -62,16 +62,13 @@ export const handler: Handlers = {
if (!Array.isArray(data)) {
throw new Error("Invalid request body");
}
console.log("Connecting to mobility database...");
const connection = new Database("databases/data/mobility.db", { create: false });
console.log("Attaching students database...");
connection.run("ATTACH DATABASE 'databases/data/students.db' AS students");
console.log("Students database attached successfully.");
const testStudents = connection.prepare("SELECT COUNT(*) AS count FROM students.students").get();
console.log(`Students table accessible, total records: ${testStudents.count}`);
const insertQuery = connection.prepare(
`INSERT INTO mobility (
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
@@ -88,7 +85,7 @@ export const handler: Handlers = {
for (const mobility of data) {
const {
id = null,
id,
studentId,
startDate,
endDate,
@@ -98,6 +95,19 @@ export const handler: Handlers = {
mobilityStatus = "N/A",
} = mobility;
console.log("Processing mobility data:", mobility);
const studentExists = connection
.prepare(`SELECT COUNT(*) AS count FROM students.students WHERE userId = ?`)
.get(studentId);
console.log(`Student ${studentId} exists:`, studentExists.count > 0);
if (studentExists.count === 0) {
console.warn(`Skipping mobility for unknown studentId: ${studentId}`);
continue;
}
let calculatedWeeksCount = weeksCount;
if (startDate && endDate) {
const start = new Date(startDate);
@@ -109,16 +119,16 @@ export const handler: Handlers = {
}
}
console.log(`Inserting/Updating mobility for studentId: ${studentId}`);
const studentExists = connection
.prepare(`SELECT COUNT(*) AS count FROM students.students WHERE userId = ?`)
.get(studentId);
if (studentExists.count === 0) {
console.warn(`Skipping mobility for unknown studentId: ${studentId}`);
continue;
}
console.log("Executing SQL insert/update query for:", {
id,
studentId,
startDate,
endDate,
calculatedWeeksCount,
destinationCountry,
destinationName,
mobilityStatus,
});
insertQuery.run(
id,