refactor(web): share allocation workbook export helper
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
import { renderToBuffer } from "@react-pdf/renderer";
|
import { renderToBuffer } from "@react-pdf/renderer";
|
||||||
import { createElement } from "react";
|
import { createElement } from "react";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import * as XLSX from "xlsx";
|
|
||||||
import { buildSplitAllocationReadModel } from "@capakraken/application";
|
import { buildSplitAllocationReadModel } from "@capakraken/application";
|
||||||
import { anonymizeResource, getAnonymizationDirectory } from "@capakraken/api";
|
import { anonymizeResource, getAnonymizationDirectory } from "@capakraken/api";
|
||||||
import { prisma } from "@capakraken/db";
|
import { prisma } from "@capakraken/db";
|
||||||
import type { AllocationLike } from "@capakraken/shared";
|
import type { AllocationLike } from "@capakraken/shared";
|
||||||
import { auth } from "~/server/auth.js";
|
import { auth } from "~/server/auth.js";
|
||||||
import { AllocationReport } from "~/components/reports/AllocationReport.js";
|
import { AllocationReport } from "~/components/reports/AllocationReport.js";
|
||||||
|
import { createWorkbookArrayBuffer } from "~/lib/workbook-export.js";
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
@@ -74,19 +74,19 @@ export async function GET(request: Request) {
|
|||||||
const ts = Date.now();
|
const ts = Date.now();
|
||||||
|
|
||||||
if (format === "xlsx") {
|
if (format === "xlsx") {
|
||||||
const sheetData = rows.map((r: typeof rows[number]) => ({
|
const workbookRows = [
|
||||||
Resource: r.resourceName,
|
["Resource", "Project", "Role", "Start Date", "End Date", "Hours/Day", "Daily Cost (ct)"],
|
||||||
Project: r.projectName,
|
...rows.map((row) => [
|
||||||
Role: r.role,
|
row.resourceName,
|
||||||
"Start Date": r.startDate,
|
row.projectName,
|
||||||
"End Date": r.endDate,
|
row.role,
|
||||||
"Hours/Day": r.hoursPerDay,
|
row.startDate,
|
||||||
"Daily Cost (ct)": r.dailyCostCents,
|
row.endDate,
|
||||||
}));
|
row.hoursPerDay,
|
||||||
const ws = XLSX.utils.json_to_sheet(sheetData);
|
row.dailyCostCents,
|
||||||
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 buffer = Buffer.from(await createWorkbookArrayBuffer("Allocations", workbookRows));
|
||||||
return new NextResponse(buffer as unknown as BodyInit, {
|
return new NextResponse(buffer as unknown as BodyInit, {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
"Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
|
|||||||
Reference in New Issue
Block a user