Bug fix
This commit is contained in:
@@ -6,6 +6,5 @@ CREATE TABLE mobility (
|
|||||||
weeksCount integer,
|
weeksCount integer,
|
||||||
destinationCountry text,
|
destinationCountry text,
|
||||||
destinationName text,
|
destinationName text,
|
||||||
mobilityStatus text default 'N/A',
|
mobilityStatus text default 'N/A'
|
||||||
foreign key (studentId) references students(userId)
|
|
||||||
);
|
);
|
||||||
@@ -62,10 +62,31 @@ 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...");
|
console.log("Connecting to mobility database...");
|
||||||
using connection = connect("mobility");
|
using connection = connect("mobility");
|
||||||
console.log("Connected to databases.");
|
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(
|
const insertQuery = connection.database.prepare(
|
||||||
`INSERT INTO mobility (
|
`INSERT INTO mobility (
|
||||||
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
||||||
@@ -82,7 +103,7 @@ export const handler: Handlers = {
|
|||||||
|
|
||||||
for (const mobility of data) {
|
for (const mobility of data) {
|
||||||
const {
|
const {
|
||||||
id,
|
id = null,
|
||||||
studentId,
|
studentId,
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
@@ -92,6 +113,16 @@ export const handler: Handlers = {
|
|||||||
mobilityStatus = "N/A",
|
mobilityStatus = "N/A",
|
||||||
} = mobility;
|
} = 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;
|
let calculatedWeeksCount = weeksCount;
|
||||||
if (startDate && endDate) {
|
if (startDate && endDate) {
|
||||||
const start = new Date(startDate);
|
const start = new Date(startDate);
|
||||||
|
|||||||
Reference in New Issue
Block a user