Trying to make EditStudents works
This commit is contained in:
@@ -62,16 +62,13 @@ export const handler: Handlers = {
|
|||||||
if (!Array.isArray(data)) {
|
if (!Array.isArray(data)) {
|
||||||
throw new Error("Invalid request body");
|
throw new Error("Invalid request body");
|
||||||
}
|
}
|
||||||
|
console.log("Connecting to mobility database...");
|
||||||
const connection = new Database("databases/data/mobility.db", { create: false });
|
const connection = new Database("databases/data/mobility.db", { create: false });
|
||||||
|
|
||||||
console.log("Attaching students database...");
|
console.log("Attaching students database...");
|
||||||
connection.run("ATTACH DATABASE 'databases/data/students.db' AS students");
|
connection.run("ATTACH DATABASE 'databases/data/students.db' AS students");
|
||||||
console.log("Students database attached successfully.");
|
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(
|
const insertQuery = connection.prepare(
|
||||||
`INSERT INTO mobility (
|
`INSERT INTO mobility (
|
||||||
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
||||||
@@ -85,10 +82,10 @@ export const handler: Handlers = {
|
|||||||
destinationName = excluded.destinationName,
|
destinationName = excluded.destinationName,
|
||||||
mobilityStatus = excluded.mobilityStatus`
|
mobilityStatus = excluded.mobilityStatus`
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const mobility of data) {
|
for (const mobility of data) {
|
||||||
const {
|
const {
|
||||||
id = null,
|
id,
|
||||||
studentId,
|
studentId,
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
@@ -97,7 +94,20 @@ export const handler: Handlers = {
|
|||||||
destinationName,
|
destinationName,
|
||||||
mobilityStatus = "N/A",
|
mobilityStatus = "N/A",
|
||||||
} = mobility;
|
} = 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;
|
let calculatedWeeksCount = weeksCount;
|
||||||
if (startDate && endDate) {
|
if (startDate && endDate) {
|
||||||
const start = new Date(startDate);
|
const start = new Date(startDate);
|
||||||
@@ -108,18 +118,18 @@ export const handler: Handlers = {
|
|||||||
calculatedWeeksCount = null;
|
calculatedWeeksCount = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Inserting/Updating mobility for studentId: ${studentId}`);
|
console.log("Executing SQL insert/update query for:", {
|
||||||
|
id,
|
||||||
const studentExists = connection
|
studentId,
|
||||||
.prepare(`SELECT COUNT(*) AS count FROM students.students WHERE userId = ?`)
|
startDate,
|
||||||
.get(studentId);
|
endDate,
|
||||||
|
calculatedWeeksCount,
|
||||||
if (studentExists.count === 0) {
|
destinationCountry,
|
||||||
console.warn(`Skipping mobility for unknown studentId: ${studentId}`);
|
destinationName,
|
||||||
continue;
|
mobilityStatus,
|
||||||
}
|
});
|
||||||
|
|
||||||
insertQuery.run(
|
insertQuery.run(
|
||||||
id,
|
id,
|
||||||
studentId,
|
studentId,
|
||||||
|
|||||||
Reference in New Issue
Block a user