Init file manager for Mobility
This commit is contained in:
@@ -20,7 +20,7 @@ export default function EditMobility() {
|
|||||||
(mobility: any) => mobility.studentId === student.id
|
(mobility: any) => mobility.studentId === student.id
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
id: existingMobility ? existingMobility.id : null,
|
id: existingMobility ? existingMobility.id : null,
|
||||||
studentId: student.id,
|
studentId: student.id,
|
||||||
firstName: student.firstName,
|
firstName: student.firstName,
|
||||||
lastName: student.lastName,
|
lastName: student.lastName,
|
||||||
@@ -32,6 +32,7 @@ export default function EditMobility() {
|
|||||||
mobilityStatus: existingMobility?.mobilityStatus || "N/A",
|
mobilityStatus: existingMobility?.mobilityStatus || "N/A",
|
||||||
promotionId: student.promotionId,
|
promotionId: student.promotionId,
|
||||||
promotionName: student.promotionName,
|
promotionName: student.promotionName,
|
||||||
|
attestationFile: null,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
setMobilityData(initializedData);
|
setMobilityData(initializedData);
|
||||||
@@ -40,6 +41,20 @@ export default function EditMobility() {
|
|||||||
fetchMobilityData();
|
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 = (
|
const handleChange = (
|
||||||
studentId: string,
|
studentId: string,
|
||||||
field: keyof MobilityData,
|
field: keyof MobilityData,
|
||||||
@@ -51,16 +66,38 @@ export default function EditMobility() {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
setIsSaving(true);
|
setIsSaving(true);
|
||||||
|
|
||||||
try {
|
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", {
|
const response = await fetch("/mobility/api/insert-mobility", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
body: formData,
|
||||||
body: JSON.stringify({ data: mobilityData }),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
@@ -129,6 +166,7 @@ export default function EditMobility() {
|
|||||||
<th>Destination Country</th>
|
<th>Destination Country</th>
|
||||||
<th>Destination Name</th>
|
<th>Destination Name</th>
|
||||||
<th>Status</th>
|
<th>Status</th>
|
||||||
|
<th>Attestation File</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -161,11 +199,7 @@ export default function EditMobility() {
|
|||||||
type="text"
|
type="text"
|
||||||
value={entry.destinationCountry || ""}
|
value={entry.destinationCountry || ""}
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
handleChange(
|
handleChange(entry.studentId, "destinationCountry", e.target.value)
|
||||||
entry.studentId,
|
|
||||||
"destinationCountry",
|
|
||||||
e.target.value
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
@@ -192,6 +226,15 @@ export default function EditMobility() {
|
|||||||
<option value="Validated">Validated</option>
|
<option value="Validated">Validated</option>
|
||||||
</select>
|
</select>
|
||||||
</td>
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
type="file"
|
||||||
|
accept=".pdf"
|
||||||
|
onChange={(e) =>
|
||||||
|
handleFileChange(entry.studentId, e.target.files?.[0] || null)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
))}
|
))}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
@@ -49,53 +49,64 @@ export const handler: Handlers = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async POST(request) {
|
async POST(request) {
|
||||||
try {
|
console.log("API /mobility/api/insert-mobility POST called");
|
||||||
const body = await request.json();
|
|
||||||
const { data } = body;
|
|
||||||
|
|
||||||
if (!Array.isArray(data)) {
|
try {
|
||||||
throw new Error("Invalid request body");
|
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");
|
using connection = connect("mobility");
|
||||||
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, attestationFile
|
||||||
)
|
)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
ON CONFLICT(id) DO UPDATE SET
|
ON CONFLICT(id) DO UPDATE SET
|
||||||
startDate = excluded.startDate,
|
startDate = excluded.startDate,
|
||||||
endDate = excluded.endDate,
|
endDate = excluded.endDate,
|
||||||
weeksCount = excluded.weeksCount,
|
weeksCount = excluded.weeksCount,
|
||||||
destinationCountry = excluded.destinationCountry,
|
destinationCountry = excluded.destinationCountry,
|
||||||
destinationName = excluded.destinationName,
|
destinationName = excluded.destinationName,
|
||||||
mobilityStatus = excluded.mobilityStatus`
|
mobilityStatus = excluded.mobilityStatus,
|
||||||
|
attestationFile = excluded.attestationFile`
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const mobility of data) {
|
for (const mobility of dataEntries) {
|
||||||
const {
|
const {
|
||||||
id = null,
|
id = null,
|
||||||
studentId,
|
studentId,
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
weeksCount,
|
|
||||||
destinationCountry,
|
destinationCountry,
|
||||||
destinationName,
|
destinationName,
|
||||||
mobilityStatus = "N/A",
|
mobilityStatus = "N/A",
|
||||||
} = mobility;
|
} = mobility;
|
||||||
|
|
||||||
let calculatedWeeksCount = weeksCount;
|
let calculatedWeeksCount = null;
|
||||||
if (startDate && endDate) {
|
if (startDate && endDate) {
|
||||||
const start = new Date(startDate);
|
const start = new Date(startDate);
|
||||||
const end = new Date(endDate);
|
const end = new Date(endDate);
|
||||||
if (start <= end) {
|
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);
|
calculatedWeeksCount = Math.floor(differenceInDays / 7);
|
||||||
} else {
|
|
||||||
calculatedWeeksCount = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const attestationFile = fileMap[studentId] || null;
|
||||||
|
|
||||||
|
console.log(`Inserting/Updating mobility for studentId: ${studentId}`);
|
||||||
insertQuery.run(
|
insertQuery.run(
|
||||||
id,
|
id,
|
||||||
studentId,
|
studentId,
|
||||||
@@ -104,7 +115,8 @@ export const handler: Handlers = {
|
|||||||
calculatedWeeksCount,
|
calculatedWeeksCount,
|
||||||
destinationCountry,
|
destinationCountry,
|
||||||
destinationName,
|
destinationName,
|
||||||
mobilityStatus
|
mobilityStatus,
|
||||||
|
attestationFile
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user