Bug fix
This commit is contained in:
@@ -6,6 +6,5 @@ CREATE TABLE mobility (
|
||||
weeksCount integer,
|
||||
destinationCountry text,
|
||||
destinationName text,
|
||||
mobilityStatus text default 'N/A',
|
||||
foreign key (studentId) references students(userId)
|
||||
mobilityStatus text default 'N/A'
|
||||
);
|
||||
@@ -62,10 +62,31 @@ export const handler: Handlers = {
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error("Invalid request body");
|
||||
}
|
||||
|
||||
console.log("Connecting to mobility database...");
|
||||
using connection = connect("mobility");
|
||||
using connection = connect("mobility");
|
||||
console.log("Connected to databases.");
|
||||
|
||||
const attachedDatabases = connection.database
|
||||
.prepare("PRAGMA database_list")
|
||||
.all();
|
||||
console.log("Attached databases:", attachedDatabases);
|
||||
|
||||
const tablesInMain = connection.database
|
||||
.prepare("SELECT name FROM sqlite_master WHERE type='table'")
|
||||
.all();
|
||||
console.log("Tables in main:", tablesInMain);
|
||||
|
||||
const tablesInStudents = connection.database
|
||||
.prepare("SELECT name FROM students.sqlite_master WHERE type='table'")
|
||||
.all();
|
||||
console.log("Tables in students:", tablesInStudents);
|
||||
|
||||
const testQuery = connection.database
|
||||
.prepare("SELECT COUNT(*) AS count FROM students.students")
|
||||
.get();
|
||||
console.log(`Test query result: Students table has ${testQuery.count} rows.`);
|
||||
|
||||
const insertQuery = connection.database.prepare(
|
||||
`INSERT INTO mobility (
|
||||
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
||||
@@ -82,7 +103,7 @@ export const handler: Handlers = {
|
||||
|
||||
for (const mobility of data) {
|
||||
const {
|
||||
id,
|
||||
id = null,
|
||||
studentId,
|
||||
startDate,
|
||||
endDate,
|
||||
@@ -92,6 +113,16 @@ export const handler: Handlers = {
|
||||
mobilityStatus = "N/A",
|
||||
} = mobility;
|
||||
|
||||
console.log(`Checking if studentId ${studentId} exists in students.students`);
|
||||
const studentExists = connection.database
|
||||
.prepare("SELECT COUNT(*) AS count FROM students.students WHERE userId = ?")
|
||||
.get(studentId);
|
||||
|
||||
if (studentExists.count === 0) {
|
||||
console.warn(`Student with ID ${studentId} does not exist. Skipping.`);
|
||||
continue;
|
||||
}
|
||||
|
||||
let calculatedWeeksCount = weeksCount;
|
||||
if (startDate && endDate) {
|
||||
const start = new Date(startDate);
|
||||
|
||||
Reference in New Issue
Block a user