import { Handlers } from "$fresh/server.ts"; import { db } from "$root/databases/db.ts"; import { mobility, promotions, students } from "$root/databases/schema.ts"; import { eq } from "npm:drizzle-orm@0.45.2"; export const handler: Handlers = { async GET() { try { const studentRows = await db .select({ id: students.userId, firstName: students.firstName, lastName: students.lastName, promotionId: students.promotionId, endyear: promotions.endyear, current: promotions.current, }) .from(students) .leftJoin(promotions, eq(students.promotionId, promotions.id)); const mobilityRows = await db.select().from(mobility); const promotionRows = await db .select({ id: promotions.id, endyear: promotions.endyear, current: promotions.current, }) .from(promotions); return new Response( JSON.stringify({ mobilities: mobilityRows, students: studentRows, promotions: promotionRows, }), { status: 200, headers: { "Content-Type": "application/json" } }, ); } catch (error) { console.error("Error fetching mobility data:", error); return new Response("Failed to fetch data", { status: 500 }); } }, async POST(request) { try { const body = await request.json(); const { data } = body; if (!Array.isArray(data)) { throw new Error("Invalid request body"); } for (const entry of data) { const { id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus = "N/A", } = entry; const studentExists = await db .select({ userId: students.userId }) .from(students) .where(eq(students.userId, studentId)) .limit(1) .then((rows) => rows.length > 0); if (!studentExists) { console.warn(`Skipping mobility for unknown studentId: ${studentId}`); continue; } let calculatedWeeksCount = weeksCount; if (startDate && endDate) { const start = new Date(startDate); const end = new Date(endDate); calculatedWeeksCount = start <= end ? Math.ceil( (end.getTime() - start.getTime()) / (7 * 24 * 60 * 60 * 1000), ) : null; } await db .insert(mobility) .values({ id, studentId, startDate, endDate, weeksCount: calculatedWeeksCount, destinationCountry, destinationName, mobilityStatus, }) .onConflictDoUpdate({ target: mobility.id, set: { startDate, endDate, weeksCount: calculatedWeeksCount, destinationCountry, destinationName, mobilityStatus, }, }); } return new Response("Data inserted/updated successfully", { status: 200, }); } catch (error) { console.error("Error inserting mobility data:", error); return new Response("Failed to insert/update data", { status: 500 }); } }, };