refactor(web): share allocation workbook export helper
This commit is contained in:
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user