Update student to have an ID
This commit is contained in:
@@ -13,7 +13,7 @@ interface Promotion {
|
||||
}
|
||||
|
||||
interface Mobility {
|
||||
id: number;
|
||||
id: number | null;
|
||||
studentId: string;
|
||||
startDate: string | null;
|
||||
endDate: string | null;
|
||||
@@ -63,7 +63,20 @@ export default function EditMobility() {
|
||||
|
||||
const updatedMobilities = prevData.mobilities?.map((mobility) => {
|
||||
if (mobility.studentId === studentId) {
|
||||
return { ...mobility, [field]: value };
|
||||
const updatedMobility = { ...mobility, [field]: value };
|
||||
|
||||
if (field === "startDate" || field === "endDate") {
|
||||
const startDate = new Date(updatedMobility.startDate || "");
|
||||
const endDate = new Date(updatedMobility.endDate || "");
|
||||
if (startDate && endDate && startDate <= endDate) {
|
||||
const weeks = Math.ceil((endDate.getTime() - startDate.getTime()) / (7 * 24 * 60 * 60 * 1000));
|
||||
updatedMobility.weeksCount = weeks;
|
||||
} else {
|
||||
updatedMobility.weeksCount = null;
|
||||
}
|
||||
}
|
||||
|
||||
return updatedMobility;
|
||||
}
|
||||
return mobility;
|
||||
}) || [];
|
||||
@@ -86,7 +99,7 @@ export default function EditMobility() {
|
||||
|
||||
if (response.ok) {
|
||||
alert("Data saved successfully!");
|
||||
window.location.reload(); // Refresh the page to load updated data
|
||||
window.location.reload();
|
||||
} else {
|
||||
throw new Error(`Failed to save data: ${response.statusText}`);
|
||||
}
|
||||
@@ -131,6 +144,7 @@ export default function EditMobility() {
|
||||
?.filter((student) => student.promotionId === promo.id)
|
||||
.map((student) => {
|
||||
const mobility = data.mobilities?.find((mob) => mob.studentId === student.id) || {
|
||||
id: null,
|
||||
studentId: student.id,
|
||||
startDate: null,
|
||||
endDate: null,
|
||||
@@ -159,15 +173,7 @@ export default function EditMobility() {
|
||||
onChange={(e) => handleChange(student.id, "endDate", e.target.value)}
|
||||
/>
|
||||
</td>
|
||||
<td>
|
||||
<input
|
||||
type="number"
|
||||
value={mobility.weeksCount || ""}
|
||||
onChange={(e) =>
|
||||
handleChange(student.id, "weeksCount", Number(e.target.value) || null)
|
||||
}
|
||||
/>
|
||||
</td>
|
||||
<td>{mobility.weeksCount ?? "N/A"}</td>
|
||||
<td>
|
||||
<input
|
||||
type="text"
|
||||
|
||||
@@ -65,15 +65,30 @@ export const handler: Handlers = {
|
||||
|
||||
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 (
|
||||
studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
||||
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
startDate = excluded.startDate,
|
||||
endDate = excluded.endDate,
|
||||
weeksCount = excluded.weeksCount,
|
||||
destinationCountry = excluded.destinationCountry,
|
||||
destinationName = excluded.destinationName,
|
||||
mobilityStatus = excluded.mobilityStatus`
|
||||
);
|
||||
|
||||
|
||||
for (const mobility of data) {
|
||||
const {
|
||||
id = null,
|
||||
studentId,
|
||||
startDate,
|
||||
endDate,
|
||||
@@ -82,12 +97,35 @@ export const handler: Handlers = {
|
||||
destinationName,
|
||||
mobilityStatus = "N/A",
|
||||
} = mobility;
|
||||
|
||||
|
||||
let calculatedWeeksCount = weeksCount;
|
||||
if (startDate && endDate) {
|
||||
const start = new Date(startDate);
|
||||
const end = new Date(endDate);
|
||||
if (start <= end) {
|
||||
calculatedWeeksCount = Math.ceil((end.getTime() - start.getTime()) / (7 * 24 * 60 * 60 * 1000));
|
||||
} else {
|
||||
calculatedWeeksCount = null;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
insertQuery.run(
|
||||
id,
|
||||
studentId,
|
||||
startDate,
|
||||
endDate,
|
||||
weeksCount,
|
||||
calculatedWeeksCount,
|
||||
destinationCountry,
|
||||
destinationName,
|
||||
mobilityStatus
|
||||
@@ -95,12 +133,11 @@ export const handler: Handlers = {
|
||||
}
|
||||
|
||||
connection.close();
|
||||
|
||||
console.log("Mobility data inserted successfully");
|
||||
return new Response("Data inserted successfully", { status: 201 });
|
||||
console.log("Mobility data inserted/updated successfully.");
|
||||
return new Response("Data inserted/updated successfully", { status: 200 });
|
||||
} catch (error) {
|
||||
console.error("Error inserting mobility data:", error);
|
||||
return new Response("Failed to insert data", { status: 500 });
|
||||
return new Response("Failed to insert/update data", { status: 500 });
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user