71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
// @deno-types="https://cdn.sheetjs.com/xlsx-0.20.3/package/types/index.d.ts"
|
|
import * as XLSX from "https://cdn.sheetjs.com/xlsx-0.20.3/package/xlsx.mjs";
|
|
import { Handlers } from "$fresh/server.ts";
|
|
import { db } from "../../../../../databases/db.ts";
|
|
import { notes } from "../../../../../databases/schema.ts";
|
|
|
|
export const handler: Handlers = {
|
|
//# 44 POST /notes/import-xlsx
|
|
async POST(request) {
|
|
try {
|
|
const formData = await request.formData();
|
|
const file = formData.get("file");
|
|
const idModule = formData.get("idModule");
|
|
|
|
if (!file || !(file instanceof File)) {
|
|
return new Response("Champ 'file' manquant", { status: 400 });
|
|
}
|
|
|
|
if (!idModule || typeof idModule !== "string") {
|
|
return new Response("Champ 'idModule' manquant", { status: 400 });
|
|
}
|
|
|
|
const buffer = await file.arrayBuffer();
|
|
const workbook = XLSX.read(buffer);
|
|
const sheet = workbook.Sheets[workbook.SheetNames[0]];
|
|
const rows = XLSX.utils.sheet_to_json(sheet) as {
|
|
numEtud: number;
|
|
note: number;
|
|
noteSession2?: number;
|
|
}[];
|
|
|
|
for (const row of rows) {
|
|
const { numEtud, note, noteSession2 } = row;
|
|
|
|
if (!numEtud || note === undefined) {
|
|
continue;
|
|
}
|
|
|
|
const values: {
|
|
numEtud: number;
|
|
idModule: string;
|
|
note: number;
|
|
noteSession2?: number | null;
|
|
} = {
|
|
numEtud,
|
|
idModule,
|
|
note,
|
|
};
|
|
const set: { note: number; noteSession2?: number | null } = { note };
|
|
|
|
if (noteSession2 !== undefined) {
|
|
values.noteSession2 = noteSession2;
|
|
set.noteSession2 = noteSession2;
|
|
}
|
|
|
|
await db.insert(notes)
|
|
.values(values)
|
|
.onConflictDoUpdate({
|
|
target: [notes.numEtud, notes.idModule],
|
|
set,
|
|
});
|
|
}
|
|
|
|
return new Response(null, { status: 204 });
|
|
} catch (error) {
|
|
console.error("Error importing notes:", error);
|
|
return new Response("Failed to import notes", { status: 500 });
|
|
}
|
|
},
|
|
};
|