From dfa289213cc9f41b5305b4be8ce950fbbae4e9d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hartmut=20N=C3=B6renberg?= Date: Tue, 31 Mar 2026 23:06:21 +0200 Subject: [PATCH] refactor(web): share allocation workbook export helper --- .../src/app/api/reports/allocations/route.ts | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/apps/web/src/app/api/reports/allocations/route.ts b/apps/web/src/app/api/reports/allocations/route.ts index 7a730e5..18332b6 100644 --- a/apps/web/src/app/api/reports/allocations/route.ts +++ b/apps/web/src/app/api/reports/allocations/route.ts @@ -1,13 +1,13 @@ import { renderToBuffer } from "@react-pdf/renderer"; import { createElement } from "react"; import { NextResponse } from "next/server"; -import * as XLSX from "xlsx"; import { buildSplitAllocationReadModel } from "@capakraken/application"; import { anonymizeResource, getAnonymizationDirectory } from "@capakraken/api"; import { prisma } from "@capakraken/db"; import type { AllocationLike } from "@capakraken/shared"; import { auth } from "~/server/auth.js"; import { AllocationReport } from "~/components/reports/AllocationReport.js"; +import { createWorkbookArrayBuffer } from "~/lib/workbook-export.js"; export async function GET(request: Request) { const session = await auth(); @@ -74,19 +74,19 @@ export async function GET(request: Request) { const ts = Date.now(); if (format === "xlsx") { - const sheetData = rows.map((r: typeof rows[number]) => ({ - Resource: r.resourceName, - Project: r.projectName, - Role: r.role, - "Start Date": r.startDate, - "End Date": r.endDate, - "Hours/Day": r.hoursPerDay, - "Daily Cost (ct)": r.dailyCostCents, - })); - const ws = XLSX.utils.json_to_sheet(sheetData); - const wb = XLSX.utils.book_new(); - XLSX.utils.book_append_sheet(wb, ws, "Allocations"); - const buffer = XLSX.write(wb, { type: "buffer", bookType: "xlsx" }) as Buffer; + const workbookRows = [ + ["Resource", "Project", "Role", "Start Date", "End Date", "Hours/Day", "Daily Cost (ct)"], + ...rows.map((row) => [ + row.resourceName, + row.projectName, + row.role, + row.startDate, + row.endDate, + row.hoursPerDay, + row.dailyCostCents, + ]), + ]; + const buffer = Buffer.from(await createWorkbookArrayBuffer("Allocations", workbookRows)); return new NextResponse(buffer as unknown as BodyInit, { headers: { "Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",