Init file manager for Mobility
This commit is contained in:
@@ -20,7 +20,7 @@ export default function EditMobility() {
|
||||
(mobility: any) => mobility.studentId === student.id
|
||||
);
|
||||
return {
|
||||
id: existingMobility ? existingMobility.id : null,
|
||||
id: existingMobility ? existingMobility.id : null,
|
||||
studentId: student.id,
|
||||
firstName: student.firstName,
|
||||
lastName: student.lastName,
|
||||
@@ -32,6 +32,7 @@ export default function EditMobility() {
|
||||
mobilityStatus: existingMobility?.mobilityStatus || "N/A",
|
||||
promotionId: student.promotionId,
|
||||
promotionName: student.promotionName,
|
||||
attestationFile: null,
|
||||
};
|
||||
});
|
||||
setMobilityData(initializedData);
|
||||
@@ -40,6 +41,20 @@ export default function EditMobility() {
|
||||
fetchMobilityData();
|
||||
}, []);
|
||||
|
||||
const handleFileChange = (studentId: string, file: File | null) => {
|
||||
if (file && file.type !== "application/pdf") {
|
||||
alert("Only PDF files are allowed.");
|
||||
return;
|
||||
}
|
||||
|
||||
setMobilityData((prev) =>
|
||||
prev.map((entry) =>
|
||||
entry.studentId === studentId
|
||||
? { ...entry, attestationFile: file }
|
||||
: entry
|
||||
)
|
||||
);
|
||||
};
|
||||
const handleChange = (
|
||||
studentId: string,
|
||||
field: keyof MobilityData,
|
||||
@@ -51,16 +66,38 @@ export default function EditMobility() {
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
const handleSave = async () => {
|
||||
setIsSaving(true);
|
||||
|
||||
try {
|
||||
console.log("EditMobility: Sending data to API...");
|
||||
console.log("EditMobility: Preparing data for API...");
|
||||
const formData = new FormData();
|
||||
|
||||
mobilityData.forEach((entry) => {
|
||||
const jsonEntry = {
|
||||
id: entry.id,
|
||||
studentId: entry.studentId,
|
||||
startDate: entry.startDate,
|
||||
endDate: entry.endDate,
|
||||
destinationCountry: entry.destinationCountry,
|
||||
destinationName: entry.destinationName,
|
||||
mobilityStatus: entry.mobilityStatus,
|
||||
};
|
||||
|
||||
formData.append("data", JSON.stringify(jsonEntry));
|
||||
|
||||
if (entry.attestationFile) {
|
||||
formData.append(`file_${entry.studentId}`, entry.attestationFile);
|
||||
}
|
||||
});
|
||||
|
||||
console.log("EditMobility: FormData prepared:", [...formData.entries()]);
|
||||
|
||||
const response = await fetch("/mobility/api/insert-mobility", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ data: mobilityData }),
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
@@ -129,6 +166,7 @@ export default function EditMobility() {
|
||||
<th>Destination Country</th>
|
||||
<th>Destination Name</th>
|
||||
<th>Status</th>
|
||||
<th>Attestation File</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -161,11 +199,7 @@ export default function EditMobility() {
|
||||
type="text"
|
||||
value={entry.destinationCountry || ""}
|
||||
onChange={(e) =>
|
||||
handleChange(
|
||||
entry.studentId,
|
||||
"destinationCountry",
|
||||
e.target.value
|
||||
)
|
||||
handleChange(entry.studentId, "destinationCountry", e.target.value)
|
||||
}
|
||||
/>
|
||||
</td>
|
||||
@@ -192,6 +226,15 @@ export default function EditMobility() {
|
||||
<option value="Validated">Validated</option>
|
||||
</select>
|
||||
</td>
|
||||
<td>
|
||||
<input
|
||||
type="file"
|
||||
accept=".pdf"
|
||||
onChange={(e) =>
|
||||
handleFileChange(entry.studentId, e.target.files?.[0] || null)
|
||||
}
|
||||
/>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
|
||||
@@ -49,53 +49,64 @@ export const handler: Handlers = {
|
||||
},
|
||||
|
||||
async POST(request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { data } = body;
|
||||
console.log("API /mobility/api/insert-mobility POST called");
|
||||
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error("Invalid request body");
|
||||
try {
|
||||
const formData = await request.formData();
|
||||
const dataEntries = formData.getAll("data").map((item) => JSON.parse(item as string));
|
||||
console.log("Parsed data entries:", dataEntries);
|
||||
|
||||
const fileMap: Record<string, Uint8Array> = {};
|
||||
for (const [key, value] of formData.entries()) {
|
||||
if (key.startsWith("file_") && value instanceof File) {
|
||||
const studentId = key.split("_")[1];
|
||||
const file = value as File;
|
||||
fileMap[studentId] = new Uint8Array(await file.arrayBuffer());
|
||||
console.log(`File processed for studentId ${studentId}`);
|
||||
}
|
||||
}
|
||||
|
||||
using connection = connect("mobility");
|
||||
const insertQuery = connection.database.prepare(
|
||||
`INSERT INTO mobility (
|
||||
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
||||
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus, attestationFile
|
||||
)
|
||||
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`
|
||||
mobilityStatus = excluded.mobilityStatus,
|
||||
attestationFile = excluded.attestationFile`
|
||||
);
|
||||
|
||||
for (const mobility of data) {
|
||||
for (const mobility of dataEntries) {
|
||||
const {
|
||||
id = null,
|
||||
studentId,
|
||||
startDate,
|
||||
endDate,
|
||||
weeksCount,
|
||||
destinationCountry,
|
||||
destinationName,
|
||||
mobilityStatus = "N/A",
|
||||
} = mobility;
|
||||
|
||||
let calculatedWeeksCount = weeksCount;
|
||||
let calculatedWeeksCount = null;
|
||||
if (startDate && endDate) {
|
||||
const start = new Date(startDate);
|
||||
const end = new Date(endDate);
|
||||
if (start <= end) {
|
||||
const differenceInDays = Math.ceil((end.getTime() - start.getTime()) / (24 * 60 * 60 * 1000));
|
||||
const differenceInDays = Math.ceil(
|
||||
(end.getTime() - start.getTime()) / (24 * 60 * 60 * 1000)
|
||||
);
|
||||
calculatedWeeksCount = Math.floor(differenceInDays / 7);
|
||||
} else {
|
||||
calculatedWeeksCount = null;
|
||||
}
|
||||
}
|
||||
const attestationFile = fileMap[studentId] || null;
|
||||
|
||||
console.log(`Inserting/Updating mobility for studentId: ${studentId}`);
|
||||
insertQuery.run(
|
||||
id,
|
||||
studentId,
|
||||
@@ -104,7 +115,8 @@ export const handler: Handlers = {
|
||||
calculatedWeeksCount,
|
||||
destinationCountry,
|
||||
destinationName,
|
||||
mobilityStatus
|
||||
mobilityStatus,
|
||||
attestationFile
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user