From 6c602cb10a47babd1e5465bea6e1acbaa479c43f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Oudelet?= Date: Sun, 26 Apr 2026 20:47:41 +0200 Subject: [PATCH 1/3] PMPR-44 : POST /notes/import-xlsx - importer des notes via Excel --- routes/(apps)/notes/api/notes/import-xlsx.ts | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 routes/(apps)/notes/api/notes/import-xlsx.ts diff --git a/routes/(apps)/notes/api/notes/import-xlsx.ts b/routes/(apps)/notes/api/notes/import-xlsx.ts new file mode 100644 index 0000000..134f3d2 --- /dev/null +++ b/routes/(apps)/notes/api/notes/import-xlsx.ts @@ -0,0 +1,49 @@ +// @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 }[]; + + for (const row of rows) { + const { numEtud, note } = row; + + if (!numEtud || note === undefined) { + continue; + } + + await db.insert(notes) + .values({ numEtud, idModule, note }) + .onConflictDoUpdate({ + target: [notes.numEtud, notes.idModule], + set: { note }, + }); + } + + return new Response(null, { status: 204 }); + } catch (error) { + console.error("Error importing notes:", error); + return new Response("Failed to import notes", { status: 500 }); + } + }, +}; \ No newline at end of file -- 2.52.0 From 720a380be816331b28dae5a2fecc3455d6ffce2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Oudelet?= Date: Sun, 26 Apr 2026 23:28:50 +0200 Subject: [PATCH 2/3] PMPR-44 : fix formatting --- routes/(apps)/notes/api/notes/import-xlsx.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/routes/(apps)/notes/api/notes/import-xlsx.ts b/routes/(apps)/notes/api/notes/import-xlsx.ts index 134f3d2..3e4ce09 100644 --- a/routes/(apps)/notes/api/notes/import-xlsx.ts +++ b/routes/(apps)/notes/api/notes/import-xlsx.ts @@ -5,7 +5,7 @@ import { db } from "../../../../../databases/db.ts"; import { notes } from "../../../../../databases/schema.ts"; export const handler: Handlers = { - // # 44 POST /notes/import-xlsx + //# 44 POST /notes/import-xlsx async POST(request) { try { const formData = await request.formData(); @@ -23,7 +23,10 @@ export const handler: Handlers = { 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 }[]; + const rows = XLSX.utils.sheet_to_json(sheet) as { + numEtud: number; + note: number; + }[]; for (const row of rows) { const { numEtud, note } = row; -- 2.52.0 From f71128a7f3158f9aa6cfeb3110b90adf173d79dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Oudelet?= Date: Sun, 26 Apr 2026 23:33:45 +0200 Subject: [PATCH 3/3] PMPR-44 : fix missing newline --- routes/(apps)/notes/api/notes/import-xlsx.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/(apps)/notes/api/notes/import-xlsx.ts b/routes/(apps)/notes/api/notes/import-xlsx.ts index 3e4ce09..b31079b 100644 --- a/routes/(apps)/notes/api/notes/import-xlsx.ts +++ b/routes/(apps)/notes/api/notes/import-xlsx.ts @@ -49,4 +49,4 @@ export const handler: Handlers = { return new Response("Failed to import notes", { status: 500 }); } }, -}; \ No newline at end of file +}; -- 2.52.0